123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- #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);
-
-
- }
|