#include "LTE_MQTT.h" #include "lte.h" // 配置mqtt参数 int MQTT_config() { u16 cat1_timeout = 0; char configMessage[64]; snprintf(configMessage, sizeof(configMessage), "AT+QMTCFG=\"version\",%d,%d \r\n",client_idx, MQTT_4); while(LTE_SendCmd(configMessage,"OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; memset(configMessage, 0, sizeof(configMessage)); snprintf(configMessage, sizeof(configMessage), "AT+QMTCFG=\"keepalive\",%d,%d \r\n",client_idx, MQTT_4); while(LTE_SendCmd(configMessage,"OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } // 配置需求参数 } // 连接MQTT- 接收模式 int MQTT_recv_connect() { u16 cat1_timeout = 0; // 选择模式 while(LTE_SendCmd("AT+QMTCFG=\"recv/mode\",1,0,1 \r\n","OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; while(LTE_SendCmd("AT+QMTOPEN=? \r\n","OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } // 打开mqtt cat1_timeout = 0; char message[64]; snprintf(message, sizeof(message), "AT+QMTOPEN=%d,\"183.162.218.20 \",1883 \r\n",client_idx); while(LTE_SendCmd(message,"OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; while(LTE_SendCmd("AT+QMTOPEN=? \r\n","OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } // 连接服务器 cat1_timeout = 0; memset(message, 0, sizeof(message)); snprintf(message, sizeof(message), "AT+QMTCONN=%d,%s,%s,%s \r\n",client_idx, (char*)&clientid,(char*)&username1,(char*)&password); while(LTE_SendCmd(message,"OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } int result,index; sscanf((char*)<E_RxBuff, "+QMTOPEN: %d,%d, ", &index, &result); #if 0 // 拿返回的result做判断 switch(result){ case NETSUCCESS: break; case NETERR: return -1; case 1: return -1; } #endif // 订阅主题 cat1_timeout = 0; memset(message, 0, sizeof(message)); snprintf(message, sizeof(message), "AT+QMTSUB=%d,msgid,topic,qos \r\n",client_idx); while(LTE_SendCmd(message,"OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } //发布消息 cat1_timeout = 0; memset(message, 0, sizeof(message)); snprintf(message, sizeof(message), "AT+QMTPUBEX=%d,msgid,qos,retain,topic,length \r\n",client_idx); while(LTE_SendCmd(message,"OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } // 从缓存中读取消息 cat1_timeout = 0; memset(message, 0, sizeof(message)); snprintf(message, sizeof(message), "AT+QMTRECV=%d,recv_id \r\n",client_idx); while(LTE_SendCmd(message,"OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } int client_idx1,msgid,payload_len; char topic[80],payload[512]; sscanf((char*)<E_RxBuff, "+QMTRECV:%d,%d,%s,%d],%s ", &client_idx1, &msgid, (char*)&topic, &payload_len, (char*)&payload); } // 连接MQTT- 发送模式 int MQTT_send_connect() { u16 cat1_timeout = 0; // 选择模式 while(LTE_SendCmd("AT+QMTCFG=\"send/mode\",1,0,1 \r\n","OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; while(LTE_SendCmd("AT+QMTOPEN=? \r\n","OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } // 打开mqtt cat1_timeout = 0; char message[64]; snprintf(message, sizeof(message), "AT+QMTOPEN=%d,\"183.162.218.20 \",1883 \r\n",client_idx); while(LTE_SendCmd(message,"OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } cat1_timeout = 0; while(LTE_SendCmd("AT+QMTOPEN=? \r\n","OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } // 连接服务器 cat1_timeout = 0; memset(message, 0, sizeof(message)); snprintf(message, sizeof(message), "AT+QMTCONN=%d,clientid,username,password \r\n",client_idx); while(LTE_SendCmd(message,"OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } // 订阅主题 cat1_timeout = 0; memset(message, 0, sizeof(message)); snprintf(message, sizeof(message), "AT+QMTSUB=%d,msgid,topic,qos \r\n",client_idx); while(LTE_SendCmd(message,"OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } //发布消息 cat1_timeout = 0; memset(message, 0, sizeof(message)); snprintf(message, sizeof(message), "AT+QMTPUBEX=%d,msgid,qos,retain,topic,length \r\n",client_idx); while(LTE_SendCmd(message,"OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } // 从缓存中读取消息 cat1_timeout = 0; memset(message, 0, sizeof(message)); snprintf(message, sizeof(message), "AT+QMTRECV=%d,recv_id \r\n",client_idx); while(LTE_SendCmd(message,"OK", 1000)){ OSTimeDly(1); cat1_timeout ++; if(cat1_timeout >= 2000){ return TIMEOUT; } } int client_idx1,msgid,payload_len; char topic[80],payload[5000]; sscanf((char*)<E_RxBuff, "+QMTRECV:%d,%d,%s,%d],%s ", &client_idx1, &msgid, (char*)&topic, &payload_len, (char*)&payload); }