上个月做的是电压源的项目,再往上个月是GJX监控设备项目,简单记录下该项目的实现,使用的新塘某款单片机+GSM模块+几个传感器,整体很简单,就是定时采集数据然后,上报到服务器,接收服务器控制指令,控制开发。唯一复杂的是使用了MQTT协议,在网上找了一份嵌入式MQTT包,然后重写发送和接收部分,原来使用的是socket,现在没有socket了,就是串口连GSM模块发AT指令,然后配置成透传,基本上就OK了,因为GSM模块就用了透传,单片机直接往串口里丢数据,GSM自动发到服务器是,MQTT协议花了一段时间大概了解了下,我的理解就是一个应用层的协议,所以GSM整个工作流程也就基本确定下来了,首先在服务器上建立一个MQTT server,然后单片机通过向串口下发AT指令建立一个TCP连接,用MQTT协议组包,直接发送到串口,然后串口监听回包就OK了。花了一段时间去调试温度湿度,斜率震动,RTC这些东西,贴个主流程代码
int main(void)
{
int ret = -1;
uint32_t pubtick = 0;
uint32_t ctltick = 0;
uint8_t AlarmFlag = 0;
CheckFlag = 0;
TimerFlag = 0;
/* Init System, IP clock and multi-function I/O */
SYS_Init();
GpioInit();
UART0_Init();
UART1_Init();
UART_ENABLE_INT(UART1, (UART_INTEN_RDAIEN_Msk| UART_INTEN_RXTOIEN_Msk)); /*config uart1 interrupt*/
NVIC_EnableIRQ(UART1_IRQn);
UART_ENABLE_INT(UART0, (UART_INTEN_RDAIEN_Msk| UART_INTEN_RXTOIEN_Msk)); /*config uart1 interrupt*/
NVIC_EnableIRQ(UART0_IRQn);
TMR0_Init();
TMR1_Init();
RestGps();
RestGsm();
I2C0_Init();
I2C1_Init();
Getuid();
// SetSlope();
Pver = 20;
// memset(&CONFIG,0,sizeof(CONFIG));
CheckWakeFlag();
SetAccData();
SetAlarm(0);//rtc 15min
//GetConfig();
GetFsuId(FsuId);
ret = GsmInit();
if(ret)
{
printf("gsminit timeout\n");
goto error;
}
ConnectFlag = 0;
Connect:
ret = MQTT_Connect(ConnectFlag);
if(ret != 0)
goto error;
ret = MQTT_SubAndPub();
if(ret != 0)
goto error;
pubtick = gTick1msCnt;
while(1)
{
if(MQTTMsgReceved_proc() == LOGIN_ACK)
break;
if((YA_GetSysTimeInterval(pubtick) >= 15000))//15 s
goto error;
}
ret = UpdateFsuId();
if(ret)
goto Connect;
SerializePubMsg(ONLINE);
mqtt_publish(PubTopic);
pubtick = gTick1msCnt;
while(1)
{
// FeedDog();
if(SendFlag)
{
SendFlag = 0;
// printf("send all data\n");
GetAllData();
SerializePubMsg(SEND_CONFIG_DATA);
mqtt_publish(PubTopic);
AlarmFlag=GetLockState();
}
switch(WakeUpFlag)
{
case 0:
PowerDownFlag = 1;
break;
case 1:
#if 0
SerializePubMsg(SEND_ALARM);
mqtt_publish(PubTopic);
#endif
PowerDownFlag = 1;
break;
case 2:
if(TimerFlag)
{
ctltick = gTick1msCnt;
TimerFlag = 0;
CheckFlag = 1;
}
if(CheckFlag)
{
if((YA_GetSysTimeInterval(ctltick) >= 10000))
{
SendFlag = 1;
CheckFlag = 0;
CheckHaltFlag = 1;
break;
}
}
if(CheckHaltFlag)
{
AlarmFlag=GetLockState();
if(!AlarmFlag)
PowerDownFlag = 1;
}
break;
default:
break;
}
MQTTMsgReceved_proc();
// SetWakeAndHalt(AlarmFlag);
if(PowerDownFlag||(YA_GetSysTimeInterval(pubtick) >= HaltTime))
{
if(WakeUpFlag != 1)
SetAccWakeup();
SetAlarm(AlarmFlag);
PowerDown();
}
}
error:
SetAccWakeup();
SetAlarm(1);
PowerDown();
return 0;
}