ml307a.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include "ml307a.h"
  2. #include "mba32a.h"
  3. #include "usart.h"
  4. #include "tt808.h"
  5. #include <stdlib.h>
  6. bool u4RecvFlag = false;
  7. bool ml307aFail = false;
  8. bool reset4Gmodule = false;
  9. bool module4G_F = false;
  10. uint8_t u4length=0;
  11. uint8_t step4g;
  12. Time time;
  13. bool conn4g_loc=false;
  14. uint8_t resetcount_4g=0;
  15. uint8_t g_ML307A_8buf[2048]={0};
  16. uint8_t g_ML307A_Loc[1024]={0};
  17. uint8_t g_ML307A_8buf_Down[2048]={0};
  18. uint8_t MLBSLOCcount = 0;
  19. extern UART_HandleTypeDef huart3;
  20. extern UART_HandleTypeDef huart4;
  21. char JD_str[13];
  22. char WD_str[12];
  23. char JWD_str[25];
  24. uint32_t JD_dw=0;
  25. uint32_t WD_dw=0;
  26. extern Menu_table menu;
  27. void ML307AReset(void);
  28. uint8_t sendCmd_4G(char *pCmd, char *pRes, uint32_t timeOut, uint8_t sendNum);
  29. uint8_t MQTT_PUB(uint8_t connect_id, char *topic, char *mesg, char* pRes);
  30. uint8_t MQTT_CONN(char *IpAddr, uint16_t port, char *ID, char *User, char *Passwd, char* pRes)
  31. {
  32. char MQTT_Sendbuf[128];
  33. sprintf((char *)MQTT_Sendbuf,"AT+MQTTCONN=0,\"%s\",%d,\"%s\",\"%s\",\"%s\"\r\n",IpAddr, port, ID,User, Passwd);
  34. if(sendCmd_4G(MQTT_Sendbuf,pRes,1,1))
  35. {
  36. return 1;
  37. }
  38. return 0;
  39. }
  40. //"AT+MQTTPUB=0,\"world\",1,0,0,26,\"abcdefghijklmnopqrstuvwxyz\"\r\n","+MQTTURC: \"publish\",",1,1)) //QOS01 =<connect_id>,<topic>,<qos>,<retain>,<dup>,<msg_len>[,<message>]
  41. uint8_t MQTT_PUB(uint8_t connect_id, char *topic, char *mesg, char* pRes)
  42. {
  43. char MQTT_Sendbuf[128];
  44. uint8_t length = strlen(mesg);
  45. sprintf((char *)MQTT_Sendbuf,"AT+MQTTPUB=%d,\"%s\",1,0,0,%d,\"%s\"\r\n",connect_id, topic, length, mesg);
  46. if(sendCmd_4G(MQTT_Sendbuf,pRes,1,1))
  47. {
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. /**
  53. * @breaf 4G初始化
  54. */
  55. uint8_t MQTTCONNPUB(void)
  56. {
  57. if(MQTT_CONN((char*)PCMQTTADDR,PCMQTTPORT,"",(char*)PCMQTTUSER,(char*)PCMQTTPSWD,"+MQTTURC: \"conn\",0,0"))
  58. {
  59. printf("#连接MQTT成功\r\n");
  60. if(sendCmd_4G("AT+MQTTSUB=0,\"world\",1\r\n","+MQTTURC: \"suback\",",1,1))
  61. {
  62. printf("#订阅成功");
  63. conn4g_loc=true;
  64. return 1;
  65. }
  66. }
  67. return 0;
  68. }
  69. void CONNTCP(void);
  70. bool tcpconn =true;
  71. uint8_t ICCID[60];
  72. void ML307A_Init(void)
  73. {
  74. printf("\t###02\t4G_init...\r\n");
  75. sendCmd_4G("+++", "OK", 1, 1);//TCP连接 0普通模式 2缓存模式 1透传模式
  76. switch(step4g)
  77. {
  78. case 0:
  79. {
  80. if (sendCmd_4G("AT\r\n", "OK", 1, 5))
  81. {
  82. printf("#4G模组正常\r\n");
  83. if (sendCmd_4G("AT+ICCID\r\n", "OK", 1, 5))
  84. {
  85. memcpy(ICCID,&U4recvBuff[10],20);
  86. ICCID[20]=0x00;
  87. ICCID[21]=0x01;
  88. }
  89. step4g++;
  90. }
  91. }
  92. case 1:
  93. {
  94. if (sendCmd_4G("AT+CPIN?\r\n", "+CPIN: READY", 1, 5))
  95. {
  96. printf("#4G模组状态正常\r\n");
  97. step4g++;
  98. }
  99. }
  100. case 2:
  101. {
  102. if (sendCmd_4G("AT+CEREG?\r\n", "+CEREG: 0,1", 1, 5))
  103. {
  104. printf("#4G模组附着正常\r\n");
  105. printf("#4G模组初始化完成\r\n");
  106. step4g++;
  107. }
  108. else
  109. {
  110. break;
  111. }
  112. }
  113. case 3:
  114. {
  115. if(sendCmd_4G("AT+MIPCLOSE=1\r\n", "+MIPSEND:", 1, 1))//发送11BYTE MAX1460
  116. {
  117. printf("#断开连接\r\n");
  118. }
  119. sendCmd_4G("AT+MLBSCFG=\"method\",40\r\n", "OK", 1, 1);//配置LBS接口为ONEos定位服务
  120. sendCmd_4G("AT+MLBSCFG=\"nearbtsen\",1\r\n", "OK", 1, 1);//启动邻区信息参与定位
  121. printf("#TCP连接成功\r\n");
  122. if(sendCmd_4G("AT+MIPCFG=\"encoding\",1,1,1\r\n", "OK", 1, 3))//默认输入输出为ASCII,0 发16,1 接16,1
  123. {
  124. printf("#TCP设置完成\r\n");
  125. }
  126. if(sendCmd_4G("AT+MIPOPEN=1,\"TCP\",\"121.199.29.128\",7342,60,0\r\n", "+MIPOPEN: 1,0", 1, 3))//TCP连接 0普通模式 2缓存模式 1透传模式
  127. //if(sendCmd_4G("AT+MIPOPEN=1,\"TCP\",\"118.31.22.26\",8901,60,0\r\n", "+MIPOPEN: 1,0", 1, 3))//TCP连接 0普通模式 2缓存模式 1透传模式
  128. //if(sendCmd_4G("AT+MIPOPEN=1,\"TCP\",\"118.31.22.26\",8901,60,2\r\n", "+MIPOPEN: 1,0", 1, 3))//TCP连接 0普通模式 2缓存模式 1透传模式
  129. {
  130. break;
  131. }
  132. else
  133. {
  134. tcpconn=false;
  135. }
  136. }
  137. }
  138. menu.current=0;//回到初始
  139. refresh=true;
  140. module4G_F=true;
  141. }
  142. /**
  143. * @breaf 4G发送串口命令
  144. */
  145. uint8_t sendCmd_4G(char *pCmd, char *pRes, uint32_t timeOut, uint8_t sendNum)
  146. {
  147. uint8_t i = 0;
  148. uint32_t time;
  149. for(i = 0; i < sendNum; i++)
  150. {
  151. time = timeOut * 10;
  152. memset(g_ML307A_8buf_Down,0,sizeof(g_ML307A_8buf_Down));
  153. USART_SendString(&huart4, pCmd);
  154. HAL_Delay(800);
  155. while(time--)
  156. {
  157. if(strstr((const char *)g_ML307A_8buf_Down, pRes) != NULL) // 如果检索到关键词
  158. {
  159. return 1;
  160. }
  161. HAL_Delay(100);
  162. }
  163. }
  164. return 0;
  165. }
  166. /**
  167. @brief 重启模块
  168. @param 无
  169. @return 无
  170. */
  171. void ML307AReset(void)
  172. {
  173. step4g=0;
  174. reset4Gmodule=true;
  175. module4G_F=false;
  176. resetcount_4g+=1;
  177. printf("4G Module_Reset\n");
  178. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
  179. HAL_Delay(5000);
  180. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
  181. }
  182. //IP地址:183.230.40.96
  183. //端口号:1883
  184. //MQTT客户端ID:dtuv1
  185. //MQTT用户名:SwU99jNiWK
  186. //MQTT登录密码: version=2018-10-31&res=products%2FSwU99jNiWK%2Fdevices%2Fdtuv1&et=207998147912&method=md5&sign=WKSoO%2BqDTtQ1LqEklV9NUA%3D%3D
  187. void Work_4Gtest(void)
  188. {
  189. {
  190. //需保存的工作函数
  191. if(sendCmd_4G("AT+MLBSLOC\r\n", "OK", 1, 1))
  192. {
  193. HAL_Delay(5000);
  194. memcpy(JD_str, &g_ML307A_8buf_Down[14], 12);
  195. memcpy(WD_str, &g_ML307A_8buf_Down[27], 11);
  196. //printf("%s\r\n",g_ML307A_8buf_Down);
  197. //调试TCP报文
  198. double num;
  199. double num1;
  200. num = atof(JD_str);
  201. num1 = atof(WD_str);
  202. num = num* 1000000;
  203. num1= num1* 1000000;
  204. JD_dw = (uint32_t)num;
  205. WD_dw = (uint32_t)num1;
  206. }
  207. }
  208. }
  209. extern TT808 tt808;