LTE_MQTT.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "LTE_MQTT.h"
  2. #include "lte.h"
  3. // 配置mqtt参数
  4. int MQTT_config()
  5. {
  6. u16 cat1_timeout = 0;
  7. char configMessage[64];
  8. snprintf(configMessage, sizeof(configMessage), "AT+QMTCFG=\"version\",%d,%d \r\n",client_idx, MQTT_4);
  9. while(LTE_SendCmd(configMessage,"OK", 1000)){
  10. OSTimeDly(1);
  11. cat1_timeout ++;
  12. if(cat1_timeout >= 2000){
  13. return TIMEOUT;
  14. }
  15. }
  16. cat1_timeout = 0;
  17. memset(configMessage, 0, sizeof(configMessage));
  18. snprintf(configMessage, sizeof(configMessage), "AT+QMTCFG=\"keepalive\",%d,%d \r\n",client_idx, MQTT_4);
  19. while(LTE_SendCmd(configMessage,"OK", 1000)){
  20. OSTimeDly(1);
  21. cat1_timeout ++;
  22. if(cat1_timeout >= 2000){
  23. return TIMEOUT;
  24. }
  25. }
  26. // 配置需求参数
  27. }
  28. // 连接MQTT- 接收模式
  29. int MQTT_recv_connect()
  30. {
  31. u16 cat1_timeout = 0;
  32. // 选择模式
  33. while(LTE_SendCmd("AT+QMTCFG=\"recv/mode\",1,0,1 \r\n","OK", 1000)){
  34. OSTimeDly(1);
  35. cat1_timeout ++;
  36. if(cat1_timeout >= 2000){
  37. return TIMEOUT;
  38. }
  39. }
  40. cat1_timeout = 0;
  41. while(LTE_SendCmd("AT+QMTOPEN=? \r\n","OK", 1000)){
  42. OSTimeDly(1);
  43. cat1_timeout ++;
  44. if(cat1_timeout >= 2000){
  45. return TIMEOUT;
  46. }
  47. }
  48. // 打开mqtt
  49. cat1_timeout = 0;
  50. char message[64];
  51. snprintf(message, sizeof(message), "AT+QMTOPEN=%d,\"183.162.218.20 \",1883 \r\n",client_idx);
  52. while(LTE_SendCmd(message,"OK", 1000)){
  53. OSTimeDly(1);
  54. cat1_timeout ++;
  55. if(cat1_timeout >= 2000){
  56. return TIMEOUT;
  57. }
  58. }
  59. cat1_timeout = 0;
  60. while(LTE_SendCmd("AT+QMTOPEN=? \r\n","OK", 1000)){
  61. OSTimeDly(1);
  62. cat1_timeout ++;
  63. if(cat1_timeout >= 2000){
  64. return TIMEOUT;
  65. }
  66. }
  67. // 连接服务器
  68. cat1_timeout = 0;
  69. memset(message, 0, sizeof(message));
  70. snprintf(message, sizeof(message), "AT+QMTCONN=%d,%s,%s,%s \r\n",client_idx,
  71. (char*)&clientid,(char*)&username1,(char*)&password);
  72. while(LTE_SendCmd(message,"OK", 1000)){
  73. OSTimeDly(1);
  74. cat1_timeout ++;
  75. if(cat1_timeout >= 2000){
  76. return TIMEOUT;
  77. }
  78. }
  79. int result,index;
  80. sscanf((char*)&LTE_RxBuff, "+QMTOPEN: %d,%d, ", &index, &result);
  81. #if 0
  82. // 拿返回的result做判断
  83. switch(result){
  84. case NETSUCCESS:
  85. break;
  86. case NETERR:
  87. return -1;
  88. case 1:
  89. return -1;
  90. }
  91. #endif
  92. // 订阅主题
  93. cat1_timeout = 0;
  94. memset(message, 0, sizeof(message));
  95. snprintf(message, sizeof(message), "AT+QMTSUB=%d,msgid,topic,qos \r\n",client_idx);
  96. while(LTE_SendCmd(message,"OK", 1000)){
  97. OSTimeDly(1);
  98. cat1_timeout ++;
  99. if(cat1_timeout >= 2000){
  100. return TIMEOUT;
  101. }
  102. }
  103. //发布消息
  104. cat1_timeout = 0;
  105. memset(message, 0, sizeof(message));
  106. snprintf(message, sizeof(message), "AT+QMTPUBEX=%d,msgid,qos,retain,topic,length \r\n",client_idx);
  107. while(LTE_SendCmd(message,"OK", 1000)){
  108. OSTimeDly(1);
  109. cat1_timeout ++;
  110. if(cat1_timeout >= 2000){
  111. return TIMEOUT;
  112. }
  113. }
  114. // 从缓存中读取消息
  115. cat1_timeout = 0;
  116. memset(message, 0, sizeof(message));
  117. snprintf(message, sizeof(message), "AT+QMTRECV=%d,recv_id \r\n",client_idx);
  118. while(LTE_SendCmd(message,"OK", 1000)){
  119. OSTimeDly(1);
  120. cat1_timeout ++;
  121. if(cat1_timeout >= 2000){
  122. return TIMEOUT;
  123. }
  124. }
  125. int client_idx1,msgid,payload_len;
  126. char topic[80],payload[512];
  127. sscanf((char*)&LTE_RxBuff, "+QMTRECV:%d,%d,%s,%d],%s ",
  128. &client_idx1, &msgid, (char*)&topic, &payload_len, (char*)&payload);
  129. }
  130. // 连接MQTT- 发送模式
  131. int MQTT_send_connect()
  132. {
  133. u16 cat1_timeout = 0;
  134. // 选择模式
  135. while(LTE_SendCmd("AT+QMTCFG=\"send/mode\",1,0,1 \r\n","OK", 1000)){
  136. OSTimeDly(1);
  137. cat1_timeout ++;
  138. if(cat1_timeout >= 2000){
  139. return TIMEOUT;
  140. }
  141. }
  142. cat1_timeout = 0;
  143. while(LTE_SendCmd("AT+QMTOPEN=? \r\n","OK", 1000)){
  144. OSTimeDly(1);
  145. cat1_timeout ++;
  146. if(cat1_timeout >= 2000){
  147. return TIMEOUT;
  148. }
  149. }
  150. // 打开mqtt
  151. cat1_timeout = 0;
  152. char message[64];
  153. snprintf(message, sizeof(message), "AT+QMTOPEN=%d,\"183.162.218.20 \",1883 \r\n",client_idx);
  154. while(LTE_SendCmd(message,"OK", 1000)){
  155. OSTimeDly(1);
  156. cat1_timeout ++;
  157. if(cat1_timeout >= 2000){
  158. return TIMEOUT;
  159. }
  160. }
  161. cat1_timeout = 0;
  162. while(LTE_SendCmd("AT+QMTOPEN=? \r\n","OK", 1000)){
  163. OSTimeDly(1);
  164. cat1_timeout ++;
  165. if(cat1_timeout >= 2000){
  166. return TIMEOUT;
  167. }
  168. }
  169. // 连接服务器
  170. cat1_timeout = 0;
  171. memset(message, 0, sizeof(message));
  172. snprintf(message, sizeof(message), "AT+QMTCONN=%d,clientid,username,password \r\n",client_idx);
  173. while(LTE_SendCmd(message,"OK", 1000)){
  174. OSTimeDly(1);
  175. cat1_timeout ++;
  176. if(cat1_timeout >= 2000){
  177. return TIMEOUT;
  178. }
  179. }
  180. // 订阅主题
  181. cat1_timeout = 0;
  182. memset(message, 0, sizeof(message));
  183. snprintf(message, sizeof(message), "AT+QMTSUB=%d,msgid,topic,qos \r\n",client_idx);
  184. while(LTE_SendCmd(message,"OK", 1000)){
  185. OSTimeDly(1);
  186. cat1_timeout ++;
  187. if(cat1_timeout >= 2000){
  188. return TIMEOUT;
  189. }
  190. }
  191. //发布消息
  192. cat1_timeout = 0;
  193. memset(message, 0, sizeof(message));
  194. snprintf(message, sizeof(message), "AT+QMTPUBEX=%d,msgid,qos,retain,topic,length \r\n",client_idx);
  195. while(LTE_SendCmd(message,"OK", 1000)){
  196. OSTimeDly(1);
  197. cat1_timeout ++;
  198. if(cat1_timeout >= 2000){
  199. return TIMEOUT;
  200. }
  201. }
  202. // 从缓存中读取消息
  203. cat1_timeout = 0;
  204. memset(message, 0, sizeof(message));
  205. snprintf(message, sizeof(message), "AT+QMTRECV=%d,recv_id \r\n",client_idx);
  206. while(LTE_SendCmd(message,"OK", 1000)){
  207. OSTimeDly(1);
  208. cat1_timeout ++;
  209. if(cat1_timeout >= 2000){
  210. return TIMEOUT;
  211. }
  212. }
  213. int client_idx1,msgid,payload_len;
  214. char topic[80],payload[5000];
  215. sscanf((char*)&LTE_RxBuff, "+QMTRECV:%d,%d,%s,%d],%s ",
  216. &client_idx1, &msgid, (char*)&topic, &payload_len, (char*)&payload);
  217. }