ml307a.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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[500]={0};
  16. uint8_t g_ML307A_Loc[1024]={0};
  17. uint8_t g_ML307A_8buf_Down[500]={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 TcpConn(char *value, uint8_t cid, char *IP, uint16_t port, uint8_t tcpConnmode)
  73. {
  74. char *ptr = value;
  75. ptr += sprintf(ptr, "AT+MIPOPEN=%d,\"TCP\",\"%s\",%d,60,%d\r\n",cid,IP,port,tcpConnmode);
  76. }
  77. void ML307A_Init(void)
  78. {
  79. printf("\t###02\t4G_init...\r\n");
  80. sendCmd_4G("AT+MGNSS=0\r\n", "OK", 1, 1);
  81. //sendCmd_4G("+++", "OK", 1, 1);//TCP连接 0普通模式 2缓存模式 1透传模式
  82. switch(step4g)
  83. {
  84. case 0:
  85. {
  86. if (sendCmd_4G("AT\r\n", "OK", 1, 5))
  87. {
  88. printf("#4G模组正常\r\n");
  89. if (sendCmd_4G("AT+ICCID\r\n", "OK", 1, 5))
  90. {
  91. memcpy(ICCID,&U4recvBuff[10],20);
  92. ICCID[20]=0x00;
  93. ICCID[21]=0x01;
  94. }
  95. step4g++;
  96. }
  97. else
  98. {
  99. ML307AReset();
  100. }
  101. }
  102. case 1:
  103. {
  104. if (sendCmd_4G("AT+CPIN?\r\n", "+CPIN: READY", 1, 5))
  105. {
  106. printf("#4G模组状态正常\r\n");
  107. step4g++;
  108. }
  109. else
  110. {
  111. ML307AReset();
  112. }
  113. }
  114. case 2:
  115. {
  116. if (sendCmd_4G("AT+CEREG?\r\n", "+CEREG: 0,1", 1, 5))
  117. {
  118. printf("#4G模组附着正常\r\n");
  119. printf("#4G模组初始化完成\r\n");
  120. step4g++;
  121. }
  122. else
  123. {
  124. ML307AReset();
  125. }
  126. }
  127. case 3:
  128. {
  129. if(sendCmd_4G("AT+MIPCLOSE=1\r\n", "+MIPSEND:", 1, 1))//发送11BYTE MAX1460
  130. {
  131. printf("#断开连接\r\n");
  132. }
  133. char TCPconnvalue[100];
  134. TcpConn(TCPconnvalue, 1, JLPTIP, JLPTPORT,0); //cid=1 tcpConnmode=0 普通模式
  135. if(sendCmd_4G(TCPconnvalue, "+MIPOPEN: 1,0", 1, 3)) //TCP连接 0普通模式 2缓存模式 1透传模式
  136. {
  137. step4g++;
  138. printf("#TCP连接成功\r\n");
  139. }
  140. else
  141. {
  142. tcpconn=false;
  143. ML307AReset();
  144. }
  145. }
  146. case 4:
  147. {
  148. if(sendCmd_4G("AT+MIPCFG=\"encoding\",1,1,1\r\n", "OK", 1, 1))//默认输入输出为ASCII,0 发16,1 接16,1
  149. {
  150. sendCmd_4G("AT+MLBSCFG=\"method\",40\r\n", "OK", 1, 1);//配置LBS接口为ONEos定位服务
  151. sendCmd_4G("AT+MLBSCFG=\"nearbtsen\",1\r\n", "OK", 1, 1);//启动邻区信息参与定位
  152. printf("#TCP设置完成\r\n");
  153. break;
  154. }
  155. else
  156. {
  157. ML307AReset();
  158. }
  159. }
  160. }
  161. menu.current=0;//回到初始
  162. refresh=true;
  163. module4G_F=true;
  164. }
  165. /**
  166. * @breaf 4G发送串口命令
  167. */
  168. uint8_t sendCmd_4G(char *pCmd, char *pRes, uint32_t timeOut, uint8_t sendNum)
  169. {
  170. uint8_t i = 0;
  171. uint32_t time;
  172. for(i = 0; i < sendNum; i++)
  173. {
  174. time = timeOut * 10;
  175. memset(g_ML307A_8buf_Down,0,sizeof(g_ML307A_8buf_Down));
  176. USART_SendString(&huart4, pCmd);
  177. HAL_Delay(800);
  178. while(time--)
  179. {
  180. if(strstr((const char *)g_ML307A_8buf_Down, pRes) != NULL) // 如果检索到关键词
  181. {
  182. return 1;
  183. }
  184. HAL_Delay(100);
  185. }
  186. }
  187. return 0;
  188. }
  189. /**
  190. @brief 重启模块
  191. @param 无
  192. @return 无
  193. */
  194. void ML307AReset(void)
  195. {
  196. step4g=0;
  197. reset4Gmodule=true;
  198. module4G_F=false;
  199. resetcount_4g+=1;
  200. printf("4G Module_Reset\n");
  201. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
  202. HAL_Delay(5000);
  203. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
  204. }
  205. //IP地址:183.230.40.96
  206. //端口号:1883
  207. //MQTT客户端ID:dtuv1
  208. //MQTT用户名:SwU99jNiWK
  209. //MQTT登录密码: version=2018-10-31&res=products%2FSwU99jNiWK%2Fdevices%2Fdtuv1&et=207998147912&method=md5&sign=WKSoO%2BqDTtQ1LqEklV9NUA%3D%3D
  210. void Work_4Gtest(void)
  211. {
  212. {
  213. //需保存的工作函数
  214. if(sendCmd_4G("AT+MLBSLOC\r\n", "OK", 1, 1))
  215. {
  216. HAL_Delay(5000);
  217. memcpy(JD_str, &g_ML307A_8buf_Down[14], 12);
  218. memcpy(WD_str, &g_ML307A_8buf_Down[27], 11);
  219. //printf("%s\r\n",g_ML307A_8buf_Down);
  220. //调试TCP报文
  221. double num;
  222. double num1;
  223. num = atof(JD_str);
  224. num1 = atof(WD_str);
  225. num = num* 1000000;
  226. num1= num1* 1000000;
  227. JD_dw = (uint32_t)num;
  228. WD_dw = (uint32_t)num1;
  229. }
  230. }
  231. }
  232. extern TT808 tt808;