ec800m.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. /*
  76. * 函数名:void EC800MSetPDP()
  77. * 输入参数:无
  78. * 输出参数:无
  79. * 返回值:无
  80. * 函数作用:设置对应运营商和激活ip
  81. */
  82. void EC800MSetPDP()
  83. {
  84. delay_1ms(200);
  85. EC800MSendCmd(CMD_SET_PDP, strlen(CMD_SET_PDP));
  86. WaitResponse(RSP_OK, 1000);
  87. delay_1ms(200);
  88. EC800MSendCmd(CMD_SET_ACTIVE, strlen(CMD_SET_ACTIVE));
  89. WaitResponse(RSP_OK, 1000);
  90. delay_1ms(200);
  91. }
  92. /*
  93. * 函数名:bool EC800MGetUrl(char *url)
  94. * 输入参数:url 网址信息
  95. * 输出参数:无
  96. * 返回值:无
  97. * 函数作用:从对应的http获取信息
  98. */
  99. bool EC800MGetUrl(char *url,char *dmaBuffer,uint32_t bufferSize)
  100. {
  101. // Set url
  102. char command[100];
  103. sprintf(command, CMD_SET_URL, strlen(url)); // CMD_SET_URL "%d\r\n"
  104. EC800MSendCmd(command, strlen(command)); // 设置服务器URL 响应若参数格式正确,且不发送 HTTP(S) GET/POST 请求:CONNECT
  105. WaitResponse(RSP_CONNECT, 100);
  106. delay_1ms(200);
  107. EC800MSendCmd(url, strlen(url)); // 输入URL
  108. if (WaitResponse(RSP_OK, 5000) == false)
  109. return false;
  110. // Send get request
  111. delay_1ms(200);
  112. EC800MSendCmd(CMD_GET, strlen(CMD_GET)); // 向服务器发送get请求
  113. if (WaitResponse("QHTTPGET:", 5000) == false)
  114. return false;
  115. // read to UFS
  116. delay_1ms(200);
  117. EC800MSendCmd(CMD_GET_TO_FILE, strlen(CMD_GET_TO_FILE)); //"AT+QHTTPREADFILE=\"UFS:http.txt\",80\r\n"
  118. if (WaitResponse("QHTTPREADFILE:", 5000) == false)
  119. return false;
  120. // get data from UFS
  121. delay_1ms(1000);
  122. dma_config_change(dmaBuffer,bufferSize);
  123. delay_1ms(1000);
  124. EC800MSendCmd(CMD_READ_FILE, strlen(CMD_READ_FILE)); //"AT+QFDWL=http.txt\r\n"
  125. delay_1ms(6000);
  126. return true;
  127. }
  128. /*
  129. * 函数名:void ConnectMQTTSever(uint8_t *host, uint16_t port, uint8_t keepalive, uint8_t *clientid)
  130. * 输入参数:host:mqtt服务器IP port:mqtt端口,keepalive mqtt心跳时间,clientid设备id
  131. * 输出参数:无
  132. * 返回值:无
  133. * 函数作用:与服务器建立连接
  134. */
  135. void ConnectMQTTSever(uint8_t *host, uint16_t port, uint8_t keepalive, uint8_t *clientid)
  136. {
  137. char cmd[150];
  138. bool success = false;
  139. int retry_count = 0;
  140. // 主动断开连接
  141. EC800MSendCmd(CMD_MQTT_DISCONNECT, 0);
  142. // 设置mqtt版本
  143. EC800MSendCmd(CMD_MQTT_VERSION, strlen(CMD_MQTT_VERSION));
  144. WaitResponse(RSP_OK, 100);
  145. sprintf(cmd, "AT+QMTCFG=\"qmtping\",0,%d\r\n", keepalive);
  146. EC800MSendCmd(cmd, strlen(cmd));
  147. WaitResponse(RSP_OK, 0);
  148. // EC800MSendCmd(CMD_MQTT_DISCONNECT, 0);
  149. sprintf(cmd, "AT+QMTOPEN=0,\"%s\",%d\r\n", host, port);
  150. do
  151. {
  152. EC800MSendCmd(cmd, strlen(cmd));
  153. success = WaitResponse("QMTOPEN: 0,0", 1000);
  154. retry_count++;
  155. } while (!success && retry_count < 3);
  156. if (success)
  157. {
  158. retry_count = 0;
  159. sprintf(cmd, "AT+QMTCONN=0,\"%s\",\"%s\",\"%s\"\r\n", clientid, "", ""); // 后两个为username password
  160. do
  161. {
  162. EC800MSendCmd(cmd, strlen(cmd));
  163. success = WaitResponse("QMTCONN: 0,0,0", 1000);
  164. retry_count++;
  165. } while (!success && retry_count < 3);
  166. }
  167. }
  168. /*
  169. * 函数名:void MQTTPublish(CONFIG_PARAMS *gateway)
  170. * 输入参数:CONFIG_PARAMS *gateway 网关内存储信息
  171. * 输出参数:无
  172. * 返回值:无
  173. * 函数作用:读出采集到的数据进行上报,若未采集到数据则不进行任何处理 23/4/4添加testId测试丢包情况7/18日重写此方法
  174. */
  175. void MQTTPublish(void)
  176. {
  177. GATEWAY_PARAMS *gateway;
  178. gateway=get_gateway_config_params();
  179. char *payload_out=malloc(25*1024); //要发送的字符串
  180. char mqtt_publish[200]; //发送字符串的指令
  181. sprintf((char *)payload_out,"{\"deviceId\":\"%s\",\"data\":[",gateway->deviceId);//组成json头部
  182. DEVICE_PARAMS *currentDevice=gateway->device_params;
  183. READ_MODBUS_COMMAND *read_modbus_command=NULL;
  184. READ_DLT645_COMMAND *read_dlt645_command=NULL;
  185. switch(currentDevice->protocol)
  186. {
  187. case DLT645_07:
  188. case DLT645_97:
  189. read_dlt645_command=currentDevice->params->node_read_dlt645_command;
  190. break;
  191. case MODBUS:
  192. read_modbus_command=currentDevice->params->node_read_modbus_command;
  193. break;
  194. }
  195. while(1)
  196. {
  197. __START__WHILE:
  198. switch(currentDevice->protocol)
  199. {
  200. case DLT645_07:
  201. case DLT645_97:
  202. if(read_dlt645_command->rxLen!=0)
  203. {
  204. float value;
  205. switch(read_dlt645_command->rxLen)
  206. {
  207. case 4:
  208. memcpy(&value, read_dlt645_command->data, 4);
  209. sprintf(payload_out+strlen(payload_out),"{\"deviceId\":\"%s\",\"%s\": %.2f},",currentDevice->deviceID,read_dlt645_command->keyword,value);
  210. break;
  211. case 5:
  212. sprintf(payload_out+strlen(payload_out),"{\"deviceId\":\"%s\",\"%s\": \"%02X%02X%02X%02X%02X\"},",currentDevice->deviceID,read_dlt645_command->keyword,read_dlt645_command->data[4],
  213. read_dlt645_command->data[3],read_dlt645_command->data[2],read_dlt645_command->data[1],read_dlt645_command->data[0]);
  214. break;
  215. break;
  216. case 9:
  217. memcpy(&value, read_dlt645_command->data, 4);
  218. sprintf(payload_out+strlen(payload_out),"{\"deviceId\":\"%s\",\"%s\":\"%02X%02X%02X%02X%02X%.2f\"},",currentDevice->deviceID,read_dlt645_command->keyword,read_dlt645_command->data[8],
  219. read_dlt645_command->data[7],read_dlt645_command->data[6],read_dlt645_command->data[5],read_dlt645_command->data[4],value);
  220. break;
  221. }
  222. }
  223. break;
  224. case MODBUS:
  225. {
  226. if(read_modbus_command->rxLen!=0)
  227. {
  228. if(read_modbus_command->functionCode==0x03)
  229. {
  230. if(read_modbus_command->decimalPoint!=0)
  231. {
  232. float value;
  233. memcpy(&value, read_modbus_command->value, 4); // 取出相应的值给value
  234. sprintf(payload_out+strlen(payload_out),"{\"deviceId\":\"%s\",\"%s\": %.2f},",currentDevice->deviceID,read_modbus_command->keyword,value);
  235. }
  236. else
  237. {
  238. uint32_t value;
  239. memcpy(&value, read_modbus_command->value, 4);
  240. sprintf(payload_out+strlen(payload_out),"{\"deviceId\":\"%s\",\"%s\": %d},",currentDevice->deviceID,read_modbus_command->keyword,value);
  241. }
  242. }
  243. else if(read_modbus_command->functionCode==0x01)
  244. {
  245. uint8_t value[4];
  246. uint8_t state;
  247. memcpy(&value, read_modbus_command->value, 4);
  248. if (value[2] == 0xFF)
  249. {
  250. state = 1;
  251. }
  252. else
  253. {
  254. state = 0;
  255. }
  256. sprintf(payload_out+strlen(payload_out),"{\"deviceId\":\"%s\",\"%s\": %d},",currentDevice->deviceID,read_modbus_command->keyword,state);
  257. }
  258. }
  259. }
  260. break;
  261. }
  262. switch(currentDevice->protocol)
  263. {
  264. case DLT645_07:
  265. case DLT645_97:
  266. read_dlt645_command=read_dlt645_command->nextParams;
  267. break;
  268. case MODBUS:
  269. read_modbus_command=read_modbus_command->nextParams;
  270. break;
  271. }
  272. if(read_dlt645_command==NULL&&read_modbus_command==NULL)
  273. {
  274. currentDevice=currentDevice->nextDevice;
  275. if(currentDevice==NULL)
  276. {
  277. goto __END__WHILE;
  278. }
  279. else
  280. {
  281. read_dlt645_command=currentDevice->params->node_read_dlt645_command;
  282. read_modbus_command=currentDevice->params->node_read_modbus_command;
  283. }
  284. }
  285. goto __START__WHILE;
  286. }
  287. __END__WHILE:
  288. if(strlen(payload_out)>40)//小于这个值组成的字符串证明没有数据采集到、就不进行发布
  289. {
  290. sprintf(payload_out+strlen(payload_out)-1,"]}");
  291. sprintf((char *)mqtt_publish, "AT+QMTPUBEX=0,0,0,0,\"%s\",%d\r\n", gateway->messageTopic, strlen(payload_out));
  292. EC800MSendCmd(mqtt_publish, strlen(mqtt_publish));
  293. WaitResponse("QMTPUBEX", 1000);
  294. delay_1ms(50);
  295. EC800MSendCmd(payload_out, strlen(payload_out));
  296. }
  297. free(payload_out);
  298. payload_out=NULL;
  299. }
  300. /*
  301. * 函数名:void MQTTSubTopic(uint8_t *commandTopic)
  302. * 输入参数:uint8_t *commandTopic 订阅的主题
  303. * 输出参数:无
  304. * 返回值:无
  305. * 函数作用:读出采集到的数据进行上报,若未采集到数据则不进行任何处理
  306. */
  307. void MQTTSubTopic(uint8_t *commandTopic)
  308. {
  309. char AT_SUB[100];
  310. sprintf(AT_SUB, "AT+QMTSUB=0,1,\"%s\",0\r\n", commandTopic);
  311. bool success = false;
  312. uint8_t retry_count = 0;
  313. do
  314. {
  315. EC800MSendCmd(AT_SUB, strlen(AT_SUB));
  316. success = WaitResponse(RSP_OK, 1000);
  317. retry_count++;
  318. } while (!success && retry_count < 3);
  319. }