ec800m.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. #include "ec800m.h"
  2. #include "usart.h"
  3. #include "systick.h"
  4. #include "cJson.h"
  5. #include "string.h"
  6. #include "log.h"
  7. #include "systick.h"
  8. void EC800MPwoerOn(void)
  9. {
  10. rcu_periph_clock_enable(RCU_GPIOD);
  11. gpio_init(GPIOD, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
  12. gpio_bit_set(GPIOD,GPIO_PIN_2);
  13. delay_1ms(5000);
  14. gd_pull_EC800M_pwr_up();
  15. gd_pull_EC800M_rst_down();
  16. delay_1ms(50);
  17. gd_pull_EC800M_pwr_down();
  18. delay_1ms(320);
  19. gd_pull_EC800M_rst_up();
  20. delay_1ms(800);
  21. gd_pull_EC800M_pwr_up();
  22. }
  23. void EC800MSendCmd(char *buf, uint16_t len)
  24. {
  25. uint16_t i;
  26. uint16_t data;
  27. for (i = 0; i < len; i++)
  28. {
  29. data = buf[i];
  30. usart_data_transmit(COM_EC800, data);
  31. while (RESET == usart_flag_get(COM_EC800, USART_FLAG_TBE))
  32. ;
  33. }
  34. }
  35. void EC800MWaitReady()
  36. {
  37. WaitResponse(RSP_READY, 0);
  38. }
  39. /*
  40. * 函数名:bool WaitResponse(char *expectStr, int timeout)
  41. * 输入参数:expectStr 需要匹配的关键字 timeout超时时间
  42. * 输出参数:true flase
  43. * 返回值:无
  44. * 函数作用:从UART0_RX_BUF缓冲区中匹配有无对应关键字有关键字则返回true无返回false
  45. */
  46. bool WaitResponse(char *expectStr, int timeout)
  47. {
  48. bool timeoutFlag = false;
  49. if (timeout >= 0)
  50. {
  51. timeoutFlag = true;
  52. }
  53. // gd_485_send((char *)&UART0_RX_BUF, strlen(UART0_RX_BUF)); 不清楚此处调用485意义
  54. while (1)
  55. {
  56. delay_1ms(50);
  57. if (UART0_RX_STAT > 0)
  58. {
  59. UART0_RX_STAT = 0;
  60. char *p = strstr((char *)&UART0_RX_BUF, expectStr);
  61. if (p)
  62. {
  63. Clear_DMA_Buffer();
  64. return true;
  65. }
  66. }
  67. timeout -= 50;
  68. if (timeoutFlag == true && timeout <= 0)
  69. {
  70. Clear_DMA_Buffer();
  71. return false;
  72. }
  73. };
  74. }
  75. //bool WaitResponseserver(CONFIG_PARAMS *gateway,int timeout)
  76. //{
  77. // bool timeoutFlag = false;
  78. // if (timeout >= 0)
  79. // {
  80. // timeoutFlag = true;
  81. // }
  82. // // gd_485_send((char *)&UART0_RX_BUF, strlen(UART0_RX_BUF)); 不清楚此处调用485意义
  83. // while (1)
  84. // {
  85. // char *p=NULL;
  86. // char *Q=NULL;
  87. // uint8_t seg1;
  88. // uint8_t seg2;
  89. // char seg3[20];
  90. // char seg6[10];
  91. // uint8_t seg4;
  92. // char seg5[25];
  93. // char buf[20];
  94. // char *token;
  95. // delay_1ms(50);
  96. // if (UART0_RX_STAT > 0)
  97. // {
  98. // UART0_RX_STAT = 0;
  99. //// char *p = strstr((char *)&UART0_RX_BUF, expectStr);
  100. //
  101. //
  102. //
  103. // if(strstr((char *)&UART0_RX_BUF, "+COPS:"))
  104. // {
  105. // Q=strstr((char *)&UART0_RX_BUF, "+COPS:");
  106. //
  107. // sscanf(Q, "+COPS:%d,%d,%s", &seg1,&seg2,seg3);
  108. // sscanf(Q, "+COPS:%d,%d,%*s%s ", &seg1,&seg2,seg6);
  109. //
  110. // if(strstr((char *)&seg3, "CHN-CT"))
  111. // {
  112. // strcpy((char *)gateway->Operatortyp,"CHN-CT");
  113. // p=strchr((char *)&seg3,',')+1;
  114. // strcpy((char *)gateway->Networktyp,p);
  115. // strcpy(gateway->Networkstatu,"1");//注册到网络
  116. //
  117. //
  118. // }else if(strstr((char *)&seg3, "CHN-UNICOM"))
  119. // {
  120. // strcpy((char *)gateway->Operatortyp,"CHN-UNICOM");
  121. // p=strchr((char *)&seg3,',')+1;
  122. // strcpy((char *)gateway->Networktyp,p);
  123. // strcpy(gateway->Networkstatu,"1");//注册到网络
  124. //
  125. // }else if(strstr((char *)&seg3, "CHINA"))
  126. // {
  127. // strcpy((char *)gateway->Operatortyp,"CHINA MOBILE");
  128. // p=strchr((char *)&seg6,',')+1;
  129. // strcpy((char *)gateway->Networktyp,p);
  130. // strcpy(gateway->Networkstatu,"1");//注册到网络
  131. //
  132. // }else
  133. // {
  134. // strcpy((char *)gateway->Operatortyp,"");
  135. // strcpy((char *)gateway->Networktyp,"");
  136. // strcpy(gateway->Networkstatu,"0");//注册到网络
  137. // }
  138. //
  139. // }else if(strstr((char *)&UART0_RX_BUF, "+CLCK:"))
  140. // {
  141. // Q=strstr((char *)&UART0_RX_BUF, "+CLCK:");
  142. // sscanf(Q, "+CLCK:%d", &seg1);
  143. // sprintf(buf,"%d",seg1);
  144. // memcpy(gateway->SIMcardstatus,buf,sizeof(buf));
  145. // memset(buf,0,sizeof(buf));
  146. //
  147. // }else if(strstr((char *)&UART0_RX_BUF, "+QCCID:"))
  148. // {
  149. // p = strchr((char *)&UART0_RX_BUF,':')+2;
  150. // sscanf(p, "%20s", seg5);
  151. // strcpy((char *)gateway->SIMcardICCID,seg5);
  152. // }else if(strstr((char *)&UART0_RX_BUF, "+GSN"))
  153. // {
  154. // p = strchr((char *)&UART0_RX_BUF,'N')+4;
  155. // sscanf(p, "%15s", seg5);
  156. // strcpy((char *)gateway->IMEInumber,seg5);
  157. //
  158. // }else if(strstr((char *)&UART0_RX_BUF, "+CSQ:"))
  159. // {
  160. // Q=strstr((char *)&UART0_RX_BUF, "+CSQ:");
  161. // sscanf(Q, "+CSQ:%d,%d", &seg1,&seg2);
  162. // sprintf(buf,"%d",(int)(((float)seg1/31)*100));
  163. // memcpy(gateway->Signalstrength,buf,sizeof(buf));
  164. // memset(buf,0,sizeof(buf));
  165. // }
  166. // }else if(strstr((char *)&UART0_RX_BUF, "+CME ERROR:"))//QCCID没有的情况出现+CME ERROR:
  167. // {
  168. // strcpy((char *)gateway->SIMcardICCID,"");
  169. //
  170. // }
  171. // timeout -= 50;
  172. // if (timeoutFlag == true && timeout <= 0)
  173. // {
  174. // Clear_DMA_Buffer();
  175. // return false;
  176. // }
  177. // };
  178. //}
  179. /*
  180. * 函数名:void EC800MSetPDP()
  181. * 输入参数:无
  182. * 输出参数:无
  183. * 返回值:无
  184. * 函数作用:设置对应运营商和激活ip
  185. */
  186. void EC800MSetPDP()
  187. {
  188. delay_1ms(200);
  189. EC800MSendCmd(CMD_SET_PDP, strlen(CMD_SET_PDP));
  190. WaitResponse(RSP_OK, 1000);
  191. delay_1ms(200);
  192. EC800MSendCmd(CMD_SET_ACTIVE, strlen(CMD_SET_ACTIVE));
  193. WaitResponse(RSP_OK, 1000);
  194. delay_1ms(200);
  195. // //测试代码查看ip
  196. // char *test="AT+QIACT?\r\n";
  197. // EC800MSendCmd(test,strlen(test));
  198. // WaitResponse(RSP_OK, 1000);
  199. }
  200. //void ec800m_information_service(CONFIG_PARAMS *gateway)
  201. //{
  202. ////delay_1ms(200);
  203. // EC800MSendCmd(CMD_SERVICE_Operatortyp, strlen(CMD_SERVICE_Operatortyp)); //
  204. // WaitResponseserver(gateway, 200);
  205. //// delay_1ms(200);
  206. // EC800MSendCmd(CMD_SERVICE_SIMcardstatus, strlen(CMD_SERVICE_SIMcardstatus)); // 输入URL
  207. // WaitResponseserver(gateway, 200);
  208. //// delay_1ms(200);
  209. //
  210. // EC800MSendCmd(CMD_SERVICE_SIMcardICCID, strlen(CMD_SERVICE_SIMcardICCID)); // 输入URL
  211. // WaitResponseserver(gateway, 200);
  212. ////delay_1ms(200);
  213. // EC800MSendCmd(CMD_SERVICE_IMEInumber, strlen(CMD_SERVICE_IMEInumber)); // 输入URL
  214. // WaitResponseserver(gateway, 200);
  215. ////delay_1ms(200);
  216. // EC800MSendCmd(CMD_SERVICE_Signalstrength, strlen(CMD_SERVICE_Signalstrength)); // 输入URL
  217. // WaitResponseserver(gateway, 200);
  218. //}
  219. /*
  220. * 函数名:bool EC800MGetUrl(char *url)
  221. * 输入参数:url 网址信息
  222. * 输出参数:无
  223. * 返回值:无
  224. * 函数作用:从对应的http获取信息
  225. */
  226. bool EC800MGetUrl(char *url,char *dmaBuffer,uint32_t bufferSize)
  227. {
  228. // Set url
  229. char command[100];
  230. sprintf(command, CMD_SET_URL, strlen(url)); // CMD_SET_URL "%d\r\n"
  231. EC800MSendCmd(command, strlen(command)); // 设置服务器URL 响应若参数格式正确,且不发送 HTTP(S) GET/POST 请求:CONNECT
  232. WaitResponse(RSP_CONNECT, 100);
  233. delay_1ms(200);
  234. EC800MSendCmd(url, strlen(url)); // 输入URL
  235. if (WaitResponse(RSP_OK, 5000) == false)
  236. return false;
  237. // Send get request
  238. delay_1ms(200);
  239. EC800MSendCmd(CMD_GET, strlen(CMD_GET)); // 向服务器发送get请求
  240. if (WaitResponse("QHTTPGET:", 5000) == false)
  241. return false;
  242. // read to UFS
  243. delay_1ms(200);
  244. EC800MSendCmd(CMD_GET_TO_FILE, strlen(CMD_GET_TO_FILE)); //"AT+QHTTPREADFILE=\"UFS:http.txt\",80\r\n"
  245. if (WaitResponse("QHTTPREADFILE:", 5000) == false)
  246. return false;
  247. // get data from UFS
  248. delay_1ms(1000);
  249. dma_config_change(dmaBuffer,bufferSize);
  250. EC800MSendCmd(CMD_READ_FILE, strlen(CMD_READ_FILE)); //"AT+QFDWL=http.txt\r\n"
  251. delay_1ms(6000);
  252. return true;
  253. }
  254. /*
  255. * 函数名:void ConnectMQTTSever(uint8_t *host, uint16_t port, uint8_t keepalive, uint8_t *clientid)
  256. * 输入参数:host:mqtt服务器IP port:mqtt端口,keepalive mqtt心跳时间,clientid设备id
  257. * 输出参数:无
  258. * 返回值:无
  259. * 函数作用:与服务器建立连接
  260. */
  261. void ConnectMQTTSever(uint8_t *host, uint16_t port, uint8_t keepalive, uint8_t *clientid)
  262. {
  263. char cmd[150];
  264. bool success = false;
  265. int retry_count = 0;
  266. // 主动断开连接
  267. EC800MSendCmd(CMD_MQTT_DISCONNECT, 0);
  268. // 设置mqtt版本
  269. EC800MSendCmd(CMD_MQTT_VERSION, strlen(CMD_MQTT_VERSION));
  270. WaitResponse(RSP_OK, 100);
  271. sprintf(cmd, "AT+QMTCFG=\"qmtping\",0,%d\r\n", keepalive);
  272. EC800MSendCmd(cmd, strlen(cmd));
  273. WaitResponse(RSP_OK, 0);
  274. // EC800MSendCmd(CMD_MQTT_DISCONNECT, 0);
  275. sprintf(cmd, "AT+QMTOPEN=0,\"%s\",%d\r\n", host, port);
  276. do
  277. {
  278. EC800MSendCmd(cmd, strlen(cmd));
  279. success = WaitResponse("QMTOPEN: 0,0", 1000);
  280. retry_count++;
  281. } while (!success && retry_count < 3);
  282. if (success)
  283. {
  284. retry_count = 0;
  285. sprintf(cmd, "AT+QMTCONN=0,\"%s\",\"%s\",\"%s\"\r\n", clientid, "", ""); // 后两个为username password
  286. do
  287. {
  288. EC800MSendCmd(cmd, strlen(cmd));
  289. success = WaitResponse("QMTCONN: 0,0,0", 1000);
  290. retry_count++;
  291. } while (!success && retry_count < 3);
  292. }
  293. }
  294. ///*
  295. // * 函数名:void MQTTPublish(CONFIG_PARAMS *gateway)
  296. // * 输入参数:CONFIG_PARAMS *gateway 网关内存储信息
  297. // * 输出参数:无
  298. // * 返回值:无
  299. // * 函数作用:读出采集到的数据进行上报,若未采集到数据则不进行任何处理 23/4/4添加testId测试丢包情况
  300. // */
  301. //void MQTTPublish(CONFIG_PARAMS *gateway)
  302. //{
  303. // char payload_out[200];
  304. // char mqtt_publish[200];
  305. // if (gateway->dataSource == 1) // 645协议查询到的值进行上报
  306. // {
  307. // for (int i = 0; i < gateway->device_read_data_num; i++)
  308. // {
  309. // if (gateway->device_read_data[i].rxLen!= 0)
  310. // {
  311. // float value;
  312. //
  313. // uint8_t minute,hour,day,month,year;
  314. // switch (gateway->device_read_data[i].rxLen)
  315. // {
  316. // case 0:
  317. // case 1:
  318. // case 2:
  319. // case 3:
  320. // case 4:
  321. // memcpy(&value, gateway->device_read_data[i].data, 4); // 取出相应的值给value
  322. // sprintf((char *)payload_out,
  323. // "{\"deviceId\":\"%s\",\"data\":[{\"deviceId\":\"%s\",\"%s\": %.2f}]}",
  324. // gateway->deviceId,
  325. // gateway->device_read_data[i].deviceId,
  326. // gateway->device_read_data[i].keyword,
  327. // value);
  328. // break;
  329. // case 5:
  330. // memcpy(&value, gateway->device_read_data[i].data, 4);
  331. // year =gateway->device_read_data[i].data[4];
  332. // month = gateway->device_read_data[i].data[3];
  333. // day = gateway->device_read_data[i].data[2];
  334. // hour = gateway->device_read_data[i].data[1];
  335. // minute = gateway->device_read_data[i].data[0];
  336. // sprintf((char *)payload_out,
  337. // "{\"deviceId\":\"%s\",\"data\":[{\"deviceId\":\"%s\",\"%s\": \"%02X%02X%02X%02X%02X\"}]}",
  338. // gateway->deviceId,gateway->device_read_data[i].deviceId,gateway->device_read_data[i].keyword,year,month,day,hour,minute);
  339. // break;
  340. // case 6:
  341. // case 7:
  342. // case 8:
  343. // case 9:
  344. // memcpy(&value, gateway->device_read_data[i].data, 4);
  345. // year =gateway->device_read_data[i].data[8];
  346. // month = gateway->device_read_data[i].data[7];
  347. // day = gateway->device_read_data[i].data[6];
  348. // hour = gateway->device_read_data[i].data[5];
  349. // minute = gateway->device_read_data[i].data[4];
  350. // sprintf((char *)payload_out,
  351. // "{\"deviceId\":\"%s\",\"data\":[{\"deviceId\":\"%s\",\"%s\":\"%02X%02X%02X%02X%02X%.2f\"}]}",
  352. // gateway->deviceId,gateway->device_read_data[i].deviceId,gateway->device_read_data[i].keyword,year,month,day,hour,minute,value);
  353. // break;
  354. // default:
  355. // //不在此区域要清空上一次sprintf
  356. // break;
  357. // }
  358. // sprintf((char *)mqtt_publish, "AT+QMTPUBEX=0,0,0,0,\"%s\",%d\r\n", gateway->messageTopic, strlen(payload_out));
  359. // EC800MSendCmd(mqtt_publish, strlen(mqtt_publish));
  360. // WaitResponse("QMTPUBEX", 1000);
  361. // delay_1ms(50);
  362. // EC800MSendCmd(payload_out, strlen(payload_out));
  363. // //WaitResponse(RSP_OK, 1000);
  364. // //特殊处理2023、6、28日添加publish失败重新订阅MQTT平台
  365. // if(WaitResponse("QMTPUBEX", 1000)==false)
  366. // {
  367. // ConnectMQTTSever(gateway->host, gateway->port, 60, gateway->deviceId);
  368. // MQTTSubTopic(gateway->commandTopic);
  369. // }
  370. //
  371. // }
  372. // }
  373. // }
  374. // else if (gateway->dataSource == 2) // modbus协议处理
  375. // {
  376. // for (int i = 0; i < gateway->device_read_data_num; i++)
  377. // {
  378. // if (gateway->device_read_data[i].rxLen!= 0)
  379. // {
  380. // // 功能码03、01需要分开,因功能码01查询到的值为开关转台
  381. // if (gateway->device_read_data[i].mdbFunctionCode == 0x03)
  382. // {
  383. // if(gateway->device_read_data[i].decimalPoint!=0)
  384. // {
  385. // float value;
  386. // memcpy(&value, gateway->device_read_data[i].data, 4); // 取出相应的值给value
  387. // sprintf((char *)payload_out,
  388. // "{\"deviceId\":\"%s\",\"data\":[{\"deviceId\":\"%s\",\"%s\": %.2f\r\n}]}",
  389. // gateway->deviceId,
  390. // gateway->device_read_data[i].deviceId,
  391. // gateway->device_read_data[i].keyword,
  392. // value);
  393. // }
  394. // else if(gateway->device_read_data[i].decimalPoint==0)
  395. // {
  396. // uint32_t value;
  397. // value=gateway->device_read_data[i].data[0]+(gateway->device_read_data[i].data[1]>>8)+(gateway->device_read_data[i].data[2]>>16)+(gateway->device_read_data[i].data[3]>>24);
  398. // sprintf((char *)payload_out,
  399. // "{\"deviceId\":\"%s\",\"data\":[{\"deviceId\":\"%s\",\"%s\": %d\r\n}]}",
  400. // gateway->deviceId,
  401. // gateway->device_read_data[i].deviceId,
  402. // gateway->device_read_data[i].keyword,
  403. // value);
  404. // }
  405. //
  406. // }
  407. // else if (gateway->device_read_data[i].mdbFunctionCode == 0x01)
  408. // {
  409. // uint8_t value[4];
  410. // uint8_t state;
  411. // memcpy(&value, gateway->device_read_data[i].data, 4);
  412. // if (value[2] == 0xFF)
  413. // {
  414. // state = 1;
  415. // }
  416. // else
  417. // {
  418. // state = 0;
  419. // }
  420. // sprintf((char *)payload_out,
  421. // "{\"deviceId\":\"%s\",\"data\":[{\"deviceId\":\"%s\",\"%s\": %d}]}",
  422. // gateway->deviceId,
  423. // gateway->device_read_data[i].deviceId,
  424. // gateway->device_read_data[i].keyword,
  425. // state);
  426. // }
  427. // sprintf((char *)mqtt_publish, "AT+QMTPUBEX=0,0,0,0,\"%s\",%d\r\n", gateway->messageTopic, strlen(payload_out));
  428. // EC800MSendCmd(mqtt_publish, strlen(mqtt_publish));
  429. // WaitResponse("QMTPUBEX", 1000);
  430. // delay_1ms(50);
  431. // EC800MSendCmd(payload_out, strlen(payload_out));
  432. // //WaitResponse(RSP_OK, 1000);
  433. // //特殊处理2023、6、28日添加publish失败重新订阅MQTT平台
  434. // if(WaitResponse("QMTPUBEX", 1000)==false)
  435. // {
  436. // NVIC_SystemReset();
  437. // }
  438. //
  439. // }
  440. // }
  441. //
  442. // }
  443. //}
  444. /*
  445. * 函数名:void MQTTPublish(CONFIG_PARAMS *gateway)
  446. * 输入参数:CONFIG_PARAMS *gateway 网关内存储信息
  447. * 输出参数:无
  448. * 返回值:无
  449. * 函数作用:读出采集到的数据进行上报,若未采集到数据则不进行任何处理 23/4/4添加testId测试丢包情况7/18日重写此方法
  450. */
  451. void MQTTPublish(CONFIG_PARAMS *gateway)
  452. {
  453. char *payload_out=malloc(25*1024); //要发送的字符串
  454. char mqtt_publish[200]; //发送字符串的指令
  455. sprintf((char *)payload_out,"{\"deviceId\":\"%s\",\"data\":[",gateway->deviceId);//组成json头部
  456. for (int i = 0; i < gateway->device_read_data_num; i++)
  457. {
  458. if (gateway->dataSource == 1)
  459. {
  460. if (gateway->device_read_data[i].rxLen!= 0) //dlt645
  461. {
  462. float value;
  463. uint8_t minute,hour,day,month,year;
  464. switch (gateway->device_read_data[i].rxLen)
  465. {
  466. case 0:
  467. case 1:
  468. case 2:
  469. case 3:
  470. case 4:
  471. memcpy(&value, gateway->device_read_data[i].data, 4); // 取出相应的值给value
  472. sprintf(payload_out+strlen(payload_out),"{\"deviceId\":\"%s\",\"%s\": %.2f},",gateway->device_read_data[i].deviceId,gateway->device_read_data[i].keyword,value);
  473. break;
  474. case 5:
  475. memcpy(&value, gateway->device_read_data[i].data, 4);
  476. year =gateway->device_read_data[i].data[4];
  477. month = gateway->device_read_data[i].data[3];
  478. day = gateway->device_read_data[i].data[2];
  479. hour = gateway->device_read_data[i].data[1];
  480. minute = gateway->device_read_data[i].data[0];
  481. sprintf(payload_out+strlen(payload_out),"{\"deviceId\":\"%s\",\"%s\": \"%02X%02X%02X%02X%02X\"},",gateway->device_read_data[i].deviceId,gateway->device_read_data[i].keyword,year,month,day,hour,minute);
  482. break;
  483. case 6:
  484. case 7:
  485. case 8:
  486. case 9:
  487. memcpy(&value, gateway->device_read_data[i].data, 4);
  488. year =gateway->device_read_data[i].data[8];
  489. month = gateway->device_read_data[i].data[7];
  490. day = gateway->device_read_data[i].data[6];
  491. hour = gateway->device_read_data[i].data[5];
  492. minute = gateway->device_read_data[i].data[4];
  493. sprintf(payload_out+strlen(payload_out),"{\"deviceId\":\"%s\",\"%s\":\"%02X%02X%02X%02X%02X%.2f\"},",gateway->device_read_data[i].deviceId,gateway->device_read_data[i].keyword,year,month,day,hour,minute,value);
  494. break;
  495. default:
  496. //不在此区域要清空上一次sprintf
  497. break;
  498. }
  499. }
  500. }
  501. else if(gateway->dataSource == 2) //modbus
  502. {
  503. if (gateway->device_read_data[i].rxLen!= 0)
  504. {
  505. // 功能码03、01需要分开,因功能码01查询到的值为开关转台
  506. if (gateway->device_read_data[i].mdbFunctionCode == 0x03)
  507. {
  508. if(gateway->device_read_data[i].decimalPoint!=0)
  509. {
  510. float value;
  511. memcpy(&value, gateway->device_read_data[i].data, 4); // 取出相应的值给value
  512. sprintf(payload_out+strlen(payload_out),"{\"deviceId\":\"%s\",\"%s\": %.2f},",gateway->device_read_data[i].deviceId,gateway->device_read_data[i].keyword,value);
  513. }
  514. else if(gateway->device_read_data[i].decimalPoint==0)
  515. {
  516. uint32_t value;
  517. value=gateway->device_read_data[i].data[0]+(gateway->device_read_data[i].data[1]>>8)+(gateway->device_read_data[i].data[2]>>16)+(gateway->device_read_data[i].data[3]>>24);
  518. sprintf(payload_out+strlen(payload_out),"{\"deviceId\":\"%s\",\"%s\": %d},",gateway->device_read_data[i].deviceId,gateway->device_read_data[i].keyword,value);
  519. }
  520. }
  521. else if (gateway->device_read_data[i].mdbFunctionCode == 0x01)
  522. {
  523. uint8_t value[4];
  524. uint8_t state;
  525. memcpy(&value, gateway->device_read_data[i].data, 4);
  526. if (value[2] == 0xFF)
  527. {
  528. state = 1;
  529. }
  530. else
  531. {
  532. state = 0;
  533. }
  534. sprintf(payload_out+strlen(payload_out),"{\"deviceId\":\"%s\",\"%s\": %d},",gateway->device_read_data[i].deviceId,gateway->device_read_data[i].keyword,state);
  535. }
  536. }
  537. }
  538. }
  539. if(strlen(payload_out)>30)//小于这个值组成的字符串证明没有数据采集到、就不进行发布
  540. {
  541. sprintf(payload_out+strlen(payload_out)-1,"]}");
  542. sprintf((char *)mqtt_publish, "AT+QMTPUBEX=0,0,0,0,\"%s\",%d\r\n", gateway->messageTopic, strlen(payload_out));
  543. EC800MSendCmd(mqtt_publish, strlen(mqtt_publish));
  544. WaitResponse("QMTPUBEX", 1000);
  545. delay_1ms(50);
  546. EC800MSendCmd(payload_out, strlen(payload_out));
  547. }
  548. free(payload_out);
  549. payload_out=NULL;
  550. }
  551. /*
  552. * 函数名:void MQTTSubTopic(uint8_t *commandTopic)
  553. * 输入参数:uint8_t *commandTopic 订阅的主题
  554. * 输出参数:无
  555. * 返回值:无
  556. * 函数作用:读出采集到的数据进行上报,若未采集到数据则不进行任何处理
  557. */
  558. void MQTTSubTopic(uint8_t *commandTopic)
  559. {
  560. char AT_SUB[100];
  561. sprintf(AT_SUB, "AT+QMTSUB=0,1,\"%s\",0\r\n", commandTopic);
  562. bool success = false;
  563. uint8_t retry_count = 0;
  564. do
  565. {
  566. EC800MSendCmd(AT_SUB, strlen(AT_SUB));
  567. success = WaitResponse(RSP_OK, 1000);
  568. retry_count++;
  569. } while (!success && retry_count < 3);
  570. }