lte.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #include "main.h"
  2. #include "lte.h"
  3. #include "myFIle.h"
  4. #include "malloc.h"
  5. #include "LTE_MQTT.h"
  6. #include "usart.h"
  7. uint8_t config_flag = 0;
  8. uint8_t UART6_RxCounter = 0;
  9. int socket;
  10. //PG6 初始化、拉低
  11. void SIM_CD_config()
  12. {
  13. GPIO_InitTypeDef GPIO_InitStruct = {0};
  14. __HAL_RCC_GPIOG_CLK_ENABLE();
  15. GPIO_InitStruct.Pin = GPIO_PIN_6;
  16. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  17. GPIO_InitStruct.Pull = GPIO_PULLUP;
  18. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  19. HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
  20. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_6,GPIO_PIN_RESET);
  21. }
  22. // 模块上电
  23. // 拉高PA4,给EC800M上电
  24. void VBAT_config()
  25. {
  26. GPIO_InitTypeDef GPIO_InitStructure;
  27. __HAL_RCC_GPIOA_CLK_ENABLE();
  28. GPIO_InitStructure.Pin = GPIO_PIN_4;
  29. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  30. GPIO_InitStructure.Pull = GPIO_PULLUP;
  31. GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
  32. HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
  33. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET);
  34. }
  35. // 拉高PB12脚
  36. // 开机:需要在稳定的高电平时,拉低一段时间,再拉高
  37. // 本机因为初始化前为高电平,只需要拉低后即可开机
  38. //PWRKEY1电平与GPIOB12电平同步
  39. void EC800M_open()
  40. {
  41. VBAT_config(); // 模块上电
  42. GPIO_InitTypeDef GPIO_InitStructure;
  43. __HAL_RCC_GPIOB_CLK_ENABLE();
  44. GPIO_InitStructure.Pin = GPIO_PIN_12 ;
  45. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  46. GPIO_InitStructure.Pull = GPIO_PULLDOWN;
  47. GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
  48. HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
  49. //当GPIO引脚初始化前为低电平时,需要进行下列步骤
  50. // delay_ms(35);
  51. // GPIO_WriteBit(GPIOB,GPIO_Pin_12,1);
  52. // GPIO_WriteBit(GPIOB,GPIO_Pin_13,1);
  53. // delay_ms(500);
  54. //
  55. // GPIO_WriteBit(GPIOB,GPIO_Pin_12,0);
  56. // GPIO_WriteBit(GPIOB,GPIO_Pin_13,0);
  57. delay_ms(1000); // 延时一段时间
  58. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_SET);
  59. }
  60. // 发送AT指令并对RX数据进行匹配
  61. int Iot_SendCmd(char* cmd, char* reply, int wait)
  62. {
  63. uint8_t i=0;
  64. int time = wait * 100;
  65. int rssi,res;
  66. memset(UART6_RX_BUF, 0, BUFF_SIZE);
  67. HAL_UART_Transmit(&USART_InitStruct_DEBUG, (uint8_t*)cmd, strlen(cmd), HAL_MAX_DELAY);
  68. LTE_Delay(time);
  69. while(UART6_RX_STAT != 1) // 判断是否接收数据
  70. {
  71. LTE_Delay(1);
  72. i++;
  73. if(i >= wait){
  74. printf("cat1 check out\r\n");
  75. return -1;
  76. }
  77. }
  78. UART6_RX_STAT = 0;
  79. if (!strcmp(reply,"+CSQ"))
  80. {
  81. char* rss_str = strstr((char*)UART6_RX_BUF, "+CSQ:");
  82. if (!rss_str) return 1;
  83. sscanf(rss_str, "+CSQ:%d,%d", &rssi, &res);
  84. if (rssi != 99) return 0;
  85. }
  86. else if (!strcmp(reply,"+QMTPUBEX"))
  87. {
  88. char MQ_str[64] = {0};
  89. sprintf(MQ_str,"%s",UART6_RX_BUF);
  90. char* MQ_send = strstr(MQ_str + 5, ">");
  91. if (MQ_send){
  92. // HAL_UART_Transmit(&USART_InitStruct_DEBUG, (uint8_t*)pubJsonString, strlen(pubJsonString), HAL_MAX_DELAY);
  93. return 0;
  94. }
  95. }
  96. if (strstr((char*)UART6_RX_BUF, reply))
  97. {
  98. return 0;
  99. }
  100. return 1;
  101. }
  102. // EC800M 的初始化
  103. void ec800_init()
  104. {
  105. uint16_t cat1_timeout = 0;
  106. while(Iot_SendCmd("AT\r\n","OK", 1)){
  107. LTE_Delay(1);
  108. cat1_timeout ++;
  109. if(cat1_timeout >= 2000){
  110. return;
  111. }
  112. }
  113. cat1_timeout = 0;
  114. while(Iot_SendCmd("AT+CFUN?\r\n","OK", 1)){
  115. LTE_Delay(1);
  116. cat1_timeout ++;
  117. if(cat1_timeout >= 2000){
  118. printf(" uart false\r\n");
  119. return;
  120. }
  121. }
  122. cat1_timeout = 0;
  123. Iot_SendCmd("AT+QCFG=\"nat\",0\r\n","OK", 1);
  124. while(Iot_SendCmd("AT+QCFG=\"nat\"\r\n","OK", 1)){
  125. LTE_Delay(1);
  126. cat1_timeout ++;
  127. if(cat1_timeout >= 2000){
  128. printf(" uart false\r\n");
  129. return;
  130. }
  131. }
  132. cat1_timeout = 0;
  133. printf("uart ok\r\n");
  134. while(Iot_SendCmd("AT+CPIN?\r\n","READY", 1)){
  135. LTE_Delay(1);
  136. cat1_timeout ++;
  137. if(cat1_timeout >= 2000){
  138. return;
  139. }
  140. }
  141. cat1_timeout = 0;
  142. printf("simcard ok\r\n");
  143. while(Iot_SendCmd("AT+CSQ\r\n","+CSQ", 1)){
  144. LTE_Delay(1);
  145. cat1_timeout ++;
  146. if(cat1_timeout >= 2000){
  147. return;
  148. }
  149. }
  150. // cat1_timeout = 0;
  151. // while(Iot_SendCmd("AT+COPS?\r\n","0,0", 1)){
  152. // LTE_Delay(1);
  153. // cat1_timeout ++;
  154. // if(cat1_timeout >= 2000){
  155. // return;
  156. // }
  157. // }
  158. cat1_timeout = 0;
  159. while(Iot_SendCmd("AT+COPS=0,0,\"CHN-CT\",7\r\n","0,0", 1)){
  160. LTE_Delay(1);
  161. cat1_timeout ++;
  162. if(cat1_timeout >= 2000){
  163. return;
  164. }
  165. }
  166. cat1_timeout = 0;
  167. while(Iot_SendCmd("AT+CEREG?\r\n","0,1", 1)){
  168. LTE_Delay(1);
  169. cat1_timeout ++;
  170. if(cat1_timeout >= 2000){
  171. return;
  172. }
  173. }
  174. printf("网络注册 ok\r\n");
  175. cat1_timeout = 0;
  176. while(Iot_SendCmd("AT+CGATT?\r\n","+CGATT: 1", 1)){
  177. LTE_Delay(1);
  178. cat1_timeout ++;
  179. if(cat1_timeout >= 2000){
  180. return;
  181. }
  182. }
  183. printf(" 网络附着 ok\r\n");
  184. }
  185. // TCP初始化
  186. void ec800_TCP(){
  187. uint16_t cat1_timeout = 0;
  188. while(Iot_SendCmd("AT+QHTTPCFG=\"contextid\",1\r\n","OK", 1)){
  189. LTE_Delay(1);
  190. cat1_timeout ++;
  191. if(cat1_timeout >= 2000){
  192. return;
  193. }
  194. }
  195. cat1_timeout = 0;
  196. while(Iot_SendCmd("AT+QHTTPCFG=\"contenttype\",1\r\n","OK", 3)){
  197. LTE_Delay(1);
  198. cat1_timeout ++;
  199. if(cat1_timeout >= 2000){
  200. return;
  201. }
  202. }
  203. // APN 联通:UNINET 移动:CMNET 电信:CTNET
  204. cat1_timeout = 0;
  205. while(Iot_SendCmd("AT+QICSGP=1,1,\"CTNET\",\"\",\"\",1\r\n","OK", 3)){
  206. LTE_Delay(1);
  207. cat1_timeout ++;
  208. if(cat1_timeout >= 2000){
  209. return;
  210. }
  211. }
  212. cat1_timeout = 0;
  213. while(Iot_SendCmd("AT+QIACT?\r\n","+QIACT", 3)){
  214. LTE_Delay(1);
  215. cat1_timeout ++;
  216. if(cat1_timeout >= 2000){
  217. return;
  218. }
  219. }
  220. cat1_timeout = 0;
  221. while(Iot_SendCmd("AT+QIACT=1\r\n","OK", 1)){
  222. LTE_Delay(1);
  223. cat1_timeout ++;
  224. if(cat1_timeout >= 2000){
  225. return;
  226. }
  227. }
  228. cat1_timeout = 0;
  229. while(Iot_SendCmd("AT+QIACT?\r\n","OK", 3)){
  230. LTE_Delay(1);
  231. cat1_timeout ++;
  232. if(cat1_timeout >= 2000){
  233. return;
  234. }
  235. }
  236. // 周期性发送心跳包
  237. // cat1_timeout = 0;
  238. // char message[64];
  239. // memset(message, 0, sizeof(message));
  240. // snprintf(message, sizeof(message), "AT+QICFG=\"send/auto\",1,120,\"C000\"\r\n");
  241. // while(Iot_SendCmd(message,"OK",2)){
  242. // LTE_Delay(1);
  243. // cat1_timeout ++;
  244. // if(cat1_timeout >= 2000){
  245. // return;
  246. // }
  247. // }
  248. }
  249. // 连接网址
  250. void http_set_url(char *url)
  251. {
  252. uint16_t cat1_timeout = 0;
  253. char message[32];
  254. snprintf(message, sizeof(message), "AT+QHTTPURL=%d,%d\r\n", strlen(url), 5);
  255. while(Iot_SendCmd(message,"CONNECT", 2)){
  256. LTE_Delay(1);
  257. cat1_timeout ++;
  258. if(cat1_timeout >= 2000){
  259. return;
  260. }
  261. }
  262. cat1_timeout = 0;
  263. printf("ready to send url\r\n");
  264. while(Iot_SendCmd(url,"OK", 5)){
  265. LTE_Delay(1);
  266. cat1_timeout ++;
  267. if(cat1_timeout >= 2000){
  268. return;
  269. }
  270. }
  271. cat1_timeout = 0;
  272. printf("url set OK\r\n");
  273. }
  274. //定义通过串口6发送AT指令命令
  275. int LTE_SendCmd(char *cmd, char* reply)
  276. {
  277. UART6_RxCounter = 0;
  278. int timeout = 10;
  279. memset(UART6_RX_BUF, 0, BUFF_SIZE);
  280. HAL_UART_Transmit(&USART_InitStruct_DEBUG, (uint8_t*)cmd, strlen(cmd), HAL_MAX_DELAY);
  281. while(timeout--) {
  282. LTE_Delay(1);
  283. if(strstr((char*)&UART6_RX_BUF, reply))
  284. break;
  285. printf("time = %d ", timeout);
  286. }
  287. printf("\r\n");
  288. if(timeout <= 0)
  289. return 1;
  290. return 0;
  291. }
  292. // HTTP获取数据
  293. int LTE_HTTP_get()
  294. {
  295. uint16_t cat1_timeout;
  296. int err,httprspcode,content_length,fd;
  297. __start:
  298. //发送 HTTP GET 请求,最大响应时间为 80 秒
  299. cat1_timeout = 0;
  300. while(Iot_SendCmd("AT+QHTTPGET=80\r\n","OK",1)){
  301. LTE_Delay(1);
  302. cat1_timeout ++;
  303. if(cat1_timeout >= 2000){
  304. goto __start;
  305. }
  306. }
  307. LTE_Delay(500);
  308. char* getStr = mymalloc(SRAMEX, 64);
  309. getStr = strstr((char*)&UART6_RX_BUF,"+QHTTPGET:");
  310. if (!getStr) {
  311. myfree(SRAMEX, getStr);
  312. goto __start;
  313. }
  314. sscanf(getStr, "+QHTTPGET: %d,%d,%d ", &err, &httprspcode, &content_length);
  315. if(200 == httprspcode)
  316. {
  317. printf("connect OK\r\n");
  318. myfree(SRAMEX, getStr);
  319. }
  320. else
  321. {
  322. myfree(SRAMEX, getStr);
  323. goto __start;
  324. }
  325. // 读取 HTTP 响应信息并将其储存到 UFS 文件中
  326. cat1_timeout = 0;
  327. while(Iot_SendCmd("AT+QHTTPREADFILE=\"UFS:test.txt\",80 \r\n","OK",20)){
  328. LTE_Delay(1);
  329. cat1_timeout ++;
  330. if(cat1_timeout >= 2000){
  331. return TIMEOUT;
  332. }
  333. }
  334. cat1_timeout = 0;
  335. // 打开文件
  336. while(Iot_SendCmd("AT+QFOPEN=\"test.txt\",2\r\n","OK",5)){
  337. LTE_Delay(1);
  338. cat1_timeout ++;
  339. if(cat1_timeout >= 2000){
  340. return TIMEOUT;
  341. }
  342. }
  343. char* readStr = mymalloc(SRAMEX, 128);
  344. readStr = strstr((char*)&UART6_RX_BUF,"+QFOPEN:");
  345. if(!readStr)
  346. {
  347. myfree(SRAMEX, readStr);
  348. goto __start;
  349. }
  350. sscanf(readStr, "+QFOPEN:%d ", &fd);// 获取文件指针
  351. myfree(SRAMEX, readStr);
  352. // 读取文件
  353. char* readMessage = mymalloc(SRAMEX, 32);
  354. sprintf(readMessage,"AT+QFREAD=%d\r\n", fd);
  355. while(Iot_SendCmd(readMessage,"OK",40)){
  356. printf("%s\n",UART6_RX_BUF);
  357. LTE_Delay(1);
  358. cat1_timeout ++;
  359. if(cat1_timeout >= 2000){
  360. myfree(SRAMEX, readMessage);
  361. goto __start;
  362. }
  363. }
  364. myfree(SRAMEX, readMessage);
  365. DeleteDirFile("device.txt");
  366. write_file("device.txt",(char*)&UART6_RX_BUF,strlen((char*)&UART6_RX_BUF));
  367. config_flag = 1;
  368. cat1_timeout = 0;
  369. // 关闭文件
  370. char closeCMD[32];
  371. sprintf(closeCMD,"AT+QFCLOSE=%d\r\n", fd);
  372. while(Iot_SendCmd(closeCMD,"OK",1)){
  373. LTE_Delay(1);
  374. cat1_timeout ++;
  375. if(cat1_timeout >= 2000){
  376. myfree(SRAMEX, closeCMD);
  377. return TIMEOUT;
  378. }
  379. }
  380. return SUCCESS;
  381. }