EC800(8157).c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. //----------------------------说明
  2. // 程序使用hal库,数据传输使用的串口,串口设置为发送+dma,接收+dma+空闲中断的模式
  3. #include "EC800.h"
  4. //-----------------------------变量定义
  5. //---------
  6. ec800Date s_ec800Date = {
  7. .ip = "39.98.37.180",
  8. .port = 32449,
  9. .clientid = "NULL",
  10. .username = "hechun",
  11. .password = "admin123",
  12. };
  13. messageDate s_messageDate = {0};
  14. typedef struct{
  15. uint8_t maxSpeed; // 服务器下发的允许最大车速
  16. uint16_t year; // 上传时间 年
  17. uint8_t month; // 上传时间 月
  18. uint8_t day; // 上传时间 日
  19. uint8_t hour; // 上传时间 时
  20. uint8_t minute; // 上传时间 分
  21. uint8_t sec; // 上传时间 秒
  22. }recDate;
  23. recDate s_recDate = {0};
  24. nmea_msg s_nmea_msg = {0};
  25. nmea_utc_time s_nmea_utc_time = {0};
  26. // AT指令响应超时时间定义
  27. #define REC_TIMEOUT (10000) // 1ms
  28. /**
  29. * @brief 计算字符串的长度
  30. * @param str: 所需计算字符串的指针
  31. * @note
  32. * @retval 无
  33. */
  34. int EC800_calculateStringLength(const char* str) {
  35. int length = 0;
  36. while (str[length] != '\0') {
  37. length++;
  38. }
  39. return length;
  40. }
  41. /**
  42. * @brief 发送指令函数
  43. * @param command: 指令
  44. * @param enterNum: 进入函数次数,只有第一次的时候清除buff
  45. * @note
  46. * @retval 无
  47. */
  48. void EC800M_SendCommand(const char* command)
  49. {
  50. uint32_t stringLen = 0;
  51. stringLen = EC800_calculateStringLength(command);
  52. //等待发送状态OK
  53. while(HAL_DMA_GetState(&hdma_usart3_tx) == HAL_DMA_STATE_BUSY) osDelay(1);
  54. //发送数据
  55. HAL_UART_Transmit_DMA(&huart3, (uint8_t*)command, stringLen);
  56. }
  57. /**
  58. * @brief 接收指令回复函数
  59. * @param haystack: 接收的字符串数据
  60. * @param needle: 正确回复的数据
  61. * @note
  62. * @retval 无
  63. */
  64. char* EC800M_RecRespond(char *haystack, const char *needle ){
  65. char* p = NULL;
  66. HAL_UART_Receive_DMA(&huart3, (uint8_t*)g_usart3_rx_buf, USART3_REC_LEN); //设置接收缓冲区
  67. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  68. while(g_usart3_rx_sta == 0) osDelay(1);
  69. __HAL_UART_DISABLE_IT(&huart3, UART_IT_IDLE); // 关闭空闲中断
  70. g_usart3_rx_sta= 0;
  71. p = strstr(haystack, needle);
  72. memset(haystack, 0, USART3_REC_LEN); // 清除数据buff
  73. return p;
  74. }
  75. // 自定义的搜索函数
  76. void* search_sequence(const void* haystack, size_t haystack_len, const void* needle, size_t needle_len) {
  77. const unsigned char* h = (const unsigned char*)haystack;
  78. const unsigned char* n = (const unsigned char*)needle;
  79. // 如果needle为空或haystack长度小于needle长度,则直接返回NULL
  80. if (needle_len == 0 || haystack_len < needle_len) {
  81. return NULL;
  82. }
  83. for (size_t i = 0; i <= haystack_len - needle_len; ++i) {
  84. size_t j;
  85. for (j = 0; j < needle_len; ++j) {
  86. if (h[i + j] != n[j]) {
  87. break; // 如果当前字符不匹配,则跳出内层循环
  88. }
  89. }
  90. if (j == needle_len) {
  91. return (void*)(h + i); // 找到匹配的序列,返回其在haystack中的位置
  92. }
  93. }
  94. // 如果遍历了整个haystack都没有找到匹配的序列,则返回NULL
  95. return NULL;
  96. }
  97. // 接收并比较响应字符串
  98. uint8_t Accept_and_Compare_Str(const char* needle){
  99. uint8_t temp = 0;
  100. static uint16_t timeOutCnt = 0; // 超时计数
  101. HAL_UART_Receive_DMA(&huart3, (uint8_t*)g_usart3_rx_buf, USART3_REC_LEN); //设置接收缓冲区
  102. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  103. while(!temp) {
  104. osDelay(1);
  105. if(search_sequence(g_usart3_rx_buf, USART3_REC_LEN, needle, strlen(needle))){
  106. temp = 1;
  107. }
  108. if(search_sequence(g_usart3_rx_buf, USART3_REC_LEN, "ERROR", strlen("ERROR"))){
  109. temp = 2;
  110. }
  111. if(timeOutCnt < REC_TIMEOUT){
  112. timeOutCnt++;
  113. }else{
  114. timeOutCnt = 0;
  115. temp = 3;
  116. }
  117. }
  118. g_usart3_rx_sta = 0;
  119. __HAL_UART_DISABLE_IT(&huart3, UART_IT_IDLE); // 关闭空闲中断
  120. return temp;
  121. }
  122. /**
  123. * @brief 接收使能函数
  124. * @note
  125. * @retval 无
  126. */
  127. void EC800_recEnable(void){
  128. HAL_UART_Receive_DMA(&huart3, (uint8_t*)g_usart3_rx_buf, USART3_REC_LEN); //设置接收缓冲区
  129. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  130. while(g_usart3_rx_sta == 0) osDelay(1);
  131. }
  132. /**
  133. * @brief 提取检查信号响应字符串字段中的信号量并进行信号判断
  134. * @param data: 响应的信号量指针
  135. * @note
  136. * @retval 无
  137. */
  138. static uint8_t EC800_extractSignal(char *data){
  139. char *token;
  140. int field_1;
  141. // 使用strtok函数提取字段
  142. token = strtok(data, ":,");
  143. while (token != NULL) {
  144. field_1 = atoi(token); // 转换为整数类型
  145. token = strtok(NULL, ":,");
  146. memset(data, 0, USART3_REC_LEN); // 清除数据buff
  147. g_usart3_rx_sta = 0; // 清除接收状态
  148. if (field_1 < 31) {
  149. return 1;
  150. } else {
  151. return 0;
  152. }
  153. }
  154. return 0;
  155. }
  156. /**
  157. * @brief 获取IMSI号
  158. * @param 无
  159. * @note
  160. * @retval 1: imsi没有获取到 0;imsi获取到
  161. */
  162. uint8_t EC800_getIMSI(void){
  163. char* found = NULL;
  164. char number[16]; // 15 digits + 1 for null terminator
  165. int numberIndex = 0;
  166. int numberFound = 0;
  167. EC800M_SendCommand(AT_CIMI);
  168. EC800_recEnable();
  169. found = strstr(g_usart3_rx_buf, AT_RESP_OK);
  170. if (found != NULL) {
  171. printf("IMSI is get\r\n");
  172. }else{
  173. printf("IMSI is not get\r\n");
  174. return 1;
  175. }
  176. for (int i = 0; i < strlen(g_usart3_rx_buf); i++) {
  177. if (isdigit(g_usart3_rx_buf[i])) {
  178. number[numberIndex] = g_usart3_rx_buf[i];
  179. numberIndex++;
  180. if (numberIndex >= 15) {
  181. numberFound = 1;
  182. break;
  183. }
  184. }
  185. }
  186. number[numberIndex] = '\0';
  187. if (numberFound) {
  188. memcpy(s_messageDate.imsi, number, strlen(number));
  189. printf("IMSI is: %s\n", number);
  190. } else {
  191. printf("Error: Unable to extract the number\n");
  192. }
  193. return 0;
  194. }
  195. /**
  196. * @brief EC800M确认网络并链接MQTT服务器
  197. * @param NONE
  198. * @note
  199. * @retval 无
  200. */
  201. uint8_t linkStep = 0;
  202. void EC800M_link(void){
  203. uint8_t temp = 0;
  204. char command[100] = {0};
  205. char errrCnt = 0; // 错误计数
  206. char* found = NULL;
  207. switch(linkStep){
  208. case 0: // 基础配置
  209. EC800M_SendCommand(AT_CMD_TEST);
  210. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  211. if (found != NULL) {
  212. printf("Module status normal\r\n");
  213. }else{
  214. errrCnt++;
  215. printf("Module status abnormal\r\n");
  216. }
  217. if(errrCnt == 0){
  218. linkStep = 1;
  219. }else{
  220. errrCnt = 0;
  221. }
  222. break;
  223. case 1: // 获取ISMI号
  224. errrCnt = EC800_getIMSI();
  225. if(errrCnt == 0){
  226. linkStep = 2;
  227. }else{
  228. errrCnt = 0;
  229. }
  230. break;
  231. case 2:
  232. EC800M_SendCommand(AT_CMD_ATE0);
  233. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  234. if (found != NULL) {
  235. printf("Cancel module echo\r\n");
  236. }else{
  237. errrCnt++;
  238. printf("Failed to cancel module echo\r\n");
  239. }
  240. if(errrCnt == 0){
  241. linkStep = 3;
  242. }else{
  243. errrCnt = 0;
  244. }
  245. break;
  246. case 3:
  247. EC800M_SendCommand(AT_CMD_CPIN);
  248. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_CPIN_READY);
  249. if (found != NULL) {
  250. printf("SIM card normal\r\n");
  251. }else{
  252. errrCnt++;
  253. printf("SIM card abnormal\r\n");
  254. }
  255. if(errrCnt == 0){
  256. linkStep = 4;
  257. }else{
  258. errrCnt = 0;
  259. }
  260. break;
  261. case 4:
  262. EC800M_SendCommand(AT_CMD_CSQ);
  263. EC800_recEnable();
  264. temp = EC800_extractSignal(g_usart3_rx_buf);
  265. if (temp) {
  266. printf("signal normal\r\n");
  267. }else{
  268. errrCnt++;
  269. printf("signal abnormal\r\n");
  270. }
  271. if(errrCnt == 0){
  272. linkStep = 5;
  273. }else{
  274. errrCnt = 0;
  275. }
  276. break;
  277. case 5:
  278. EC800M_SendCommand(AT_CMD_CREG);
  279. EC800_recEnable();
  280. // 使用字符串处理函数判断是否为正常状态
  281. if (strstr(g_usart3_rx_buf, "1") != NULL || strstr(g_usart3_rx_buf, "5") != NULL) {
  282. printf("The module successfully registered on the GSM network\r\n");
  283. } else {
  284. errrCnt++;
  285. printf("The module failed to register on the GSM network\r\n");
  286. }
  287. memset(g_usart3_rx_buf, 0, USART3_REC_LEN); // 清除数据buff
  288. g_usart3_rx_sta = 0; // 清除接收状态
  289. if(errrCnt == 0){
  290. linkStep = 6;
  291. }else{
  292. errrCnt = 0;
  293. }
  294. break;
  295. case 6:
  296. EC800M_SendCommand(AT_CMD_QIDEACT);
  297. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  298. if (found != NULL) {
  299. printf("Successfully disabled mobile scene\r\n");
  300. }else{
  301. errrCnt++;
  302. printf("Failed to disable mobile scene\r\n");
  303. }
  304. if(errrCnt == 0){
  305. linkStep = 7;
  306. }else{
  307. errrCnt = 0;
  308. }
  309. break;
  310. case 7:
  311. EC800M_SendCommand(AT_CMD_QIACT);
  312. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  313. if (found != NULL) {
  314. printf("Successfully enabled mobile scene\r\n");
  315. }else{
  316. errrCnt++;
  317. printf("Failed to enable mobile scene\r\n");
  318. }
  319. if(errrCnt == 0){
  320. linkStep = 8;
  321. }else{
  322. errrCnt = 0;
  323. }
  324. break;
  325. case 8: // 连接mqtt服务器
  326. sprintf(command, "AT+QMTCFG=\"qmtping\",0,%d\r\n", 30);
  327. EC800M_SendCommand(command);
  328. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  329. if (found != NULL) {
  330. printf("MQTT heartbeat set successfully\r\n");
  331. }else{
  332. errrCnt++;
  333. printf("Failed to set MQTT heartbeat\r\n");
  334. }
  335. if(errrCnt == 0){
  336. linkStep = 9;
  337. }else{
  338. errrCnt = 0;
  339. }
  340. break;
  341. case 9:
  342. EC800M_SendCommand(AT_CMD_QMTCFG_SET_DATA_MODE);
  343. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  344. if (found != NULL) {
  345. printf("Data receiving mode set successfully.\r\n");
  346. }else{
  347. errrCnt++;
  348. printf("Failed to set data receiving mode\r\n");
  349. }
  350. if(errrCnt == 0){
  351. linkStep = 10;
  352. }else{
  353. errrCnt = 0;
  354. }
  355. break;
  356. case 10:
  357. sprintf(command,"AT+QMTOPEN=0,\"%s\",%d\r\n", s_ec800Date.ip, s_ec800Date.port);
  358. EC800M_SendCommand(command);
  359. // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  360. // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_QMTOPEN);
  361. //
  362. // if (found != NULL) {
  363. // printf("Successfully opened a network for the module\r\n");
  364. // }else{
  365. // errrCnt++;
  366. // printf("Failed to open a network for the module\r\n");
  367. // }
  368. temp = Accept_and_Compare_Str(AT_RESP_QMTOPEN);
  369. if (temp == 1) {
  370. printf("Successfully opened a network for the module\r\n");
  371. }else{
  372. errrCnt++;
  373. printf("Failed to open a network for the module\r\n");
  374. }
  375. if(errrCnt == 0){
  376. linkStep = 11;
  377. }else{
  378. errrCnt = 0;
  379. }
  380. break;
  381. case 11:
  382. sprintf(command,"AT+QMTCONN=0,%s,%s,%s\r\n",s_ec800Date.clientid, s_ec800Date.username, s_ec800Date.password);
  383. EC800M_SendCommand(command);
  384. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  385. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_QMTCONN);
  386. if (found != NULL) {
  387. printf("Successfully connected to the MQTT server\r\n");
  388. }else{
  389. errrCnt++;
  390. printf("Failed to connect to the MQTT server\r\n");
  391. }
  392. if(errrCnt == 0){
  393. linkStep = 12;
  394. }else{
  395. errrCnt = 0;
  396. }
  397. break;
  398. default: break;
  399. }
  400. }
  401. /**
  402. * @brief EC800M初始化GNSS
  403. * @param NONE
  404. * @note
  405. * @retval 无
  406. */
  407. uint8_t gnssStep = 0;
  408. void EC800_gnss_init(void){
  409. char errrCnt = 0; // 错误计数
  410. char* found = NULL;
  411. switch(gnssStep){
  412. case 6:
  413. EC800M_SendCommand(AT_QGPS_0);
  414. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  415. if (found != NULL) {
  416. printf("Turn off the GNSS module\r\n");
  417. }else{
  418. errrCnt++;
  419. printf("Failed to turn off the GNSS module\r\n");
  420. }
  421. if(errrCnt == 0){
  422. gnssStep = 7;
  423. }else{
  424. gnssStep = 7;
  425. }
  426. break;
  427. case 7:
  428. EC800M_SendCommand(AT_QGPS_1);
  429. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  430. if (found != NULL) {
  431. printf("Turn on the GNSS module\r\n");
  432. }else{
  433. errrCnt++;
  434. printf("Failed to turn on the GNSS module\r\n");
  435. }
  436. if(errrCnt == 0){
  437. gnssStep = 8;
  438. }else{
  439. errrCnt = 0;
  440. }
  441. break;
  442. case 0:
  443. EC800M_SendCommand(AT_QGPSCFG_out_port);
  444. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  445. if (found != NULL) {
  446. printf("Outputting NMEA Sentences via Serial Debugging\r\n");
  447. }else{
  448. errrCnt++;
  449. printf("The NMEA sentence output is set incorrectly\r\n");
  450. }
  451. if(errrCnt == 0){
  452. gnssStep = 1;
  453. }else{
  454. errrCnt = 0;
  455. }
  456. break;
  457. case 1:
  458. EC800M_SendCommand(AT_QGPSCFG_nmeasrc);
  459. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  460. if (found != NULL) {
  461. printf("Enabling NMEA sentence retrieval via AT+QGPSGNMEA\r\n");
  462. }else{
  463. errrCnt++;
  464. printf("Failed to enable retrieving NMEA sentences via AT+QGPSGNMEA\r\n");
  465. }
  466. if(errrCnt == 0){
  467. gnssStep = 2;
  468. }else{
  469. errrCnt = 0;
  470. }
  471. break;
  472. case 2:
  473. EC800M_SendCommand(AT_QGPSCFG_gpsnmeatype);
  474. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  475. if (found != NULL) {
  476. printf("Configuring the output type of NMEA sentences to all formats\r\n");
  477. }else{
  478. errrCnt++;
  479. printf("Failed to configure the output type of NMEA sentences to all formats\r\n");
  480. }
  481. if(errrCnt == 0){
  482. gnssStep = 3;
  483. }else{
  484. errrCnt = 0;
  485. }
  486. break;
  487. case 3:
  488. EC800M_SendCommand(AT_QGPSCFG_gnssconfig);
  489. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  490. if (found != NULL) {
  491. printf("Configuring the supported GNSS satellite navigation systems to GPS+BeiDou\r\n");
  492. }else{
  493. errrCnt++;
  494. printf("Failed to configure the supported GNSS satellite navigation systems to GPS+BeiDou\r\n");
  495. }
  496. if(errrCnt == 0){
  497. gnssStep = 4;
  498. }else{
  499. errrCnt = 0;
  500. }
  501. break;
  502. case 4:
  503. EC800M_SendCommand(AT_QGPSCFG_autogps);
  504. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  505. if (found != NULL) {
  506. printf("Disable GNSS auto-start\r\n");
  507. }else{
  508. errrCnt++;
  509. printf("Failed to disable GNSS auto-start\r\n");
  510. }
  511. if(errrCnt == 0){
  512. gnssStep = 5;
  513. }else{
  514. errrCnt = 0;
  515. }
  516. break;
  517. case 5:
  518. EC800M_SendCommand(AT_QGPSCFG_apflash);
  519. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  520. if (found != NULL) {
  521. printf("Disable AP-Flash quick start function\r\n");
  522. }else{
  523. errrCnt++;
  524. printf("Failed to disable AP-Flash quick start function\r\n");
  525. }
  526. if(errrCnt == 0){
  527. gnssStep = 6;
  528. }else{
  529. errrCnt = 0;
  530. }
  531. break;
  532. default:
  533. break;
  534. }
  535. }
  536. //UTC时间转换为任意时区时间,如果是转换为北京时间,timezone传8即可
  537. void utc_to_local_time(nmea_utc_time* utc_time, int8_t timezone, nmea_utc_time* local_time)
  538. {
  539. int year,month,day,hour;
  540. int lastday = 0; //last day of this month 本月天数
  541. int lastlastday = 0; //last day of last month 上个月天数
  542. year = utc_time->year; //utc time
  543. month = utc_time->month;
  544. day = utc_time->date;
  545. hour = utc_time->hour + timezone;
  546. //1月大,2月小,3月大,4月小,5月大,6月小,7月大,8月大,9月小,10月大,11月小,12月大
  547. if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
  548. {
  549. lastday = 31;//本月天数
  550. lastlastday = 30;//这里应该补上上个月的天数
  551. if(month == 3)
  552. {
  553. if((year%400 == 0)||(year%4 == 0 && year%100 != 0))//if this is lunar year
  554. lastlastday = 29;
  555. else
  556. lastlastday = 28;
  557. }
  558. if(month == 8 || month == 1)//这里应该是8月和1月,因为8月和1月的上一个月(7月和12月)的天数是31天的
  559. lastlastday = 31;
  560. }
  561. else if(month == 4 || month == 6 || month == 9 || month == 11)
  562. {
  563. lastday = 30;
  564. lastlastday = 31;
  565. }
  566. else
  567. {
  568. lastlastday = 31;
  569. if((year%400 == 0)||(year%4 == 0 && year%100 != 0))
  570. lastday = 29;
  571. else
  572. lastday = 28;
  573. }
  574. if(hour >= 24)// if >24, day+1
  575. {
  576. hour -= 24;
  577. day += 1;
  578. if(day > lastday)// next month, day-lastday of this month
  579. {
  580. day -= lastday;
  581. month += 1;
  582. if(month > 12)// next year, month-12
  583. {
  584. month -= 12;
  585. year += 1;
  586. }
  587. }
  588. }
  589. if(hour < 0)// if <0, day-1
  590. {
  591. hour += 24;
  592. day -= 1;
  593. if(day < 1)// month-1, day=last day of last month
  594. {
  595. day = lastlastday;
  596. month -= 1;
  597. if(month < 1)// last year, month=12
  598. {
  599. month = 12;
  600. year -= 1;
  601. }
  602. }
  603. }
  604. // transfer value to local_time
  605. local_time->year = year;
  606. local_time->month = month;
  607. local_time->date = day;
  608. local_time->hour = hour;
  609. local_time->min = utc_time->min;
  610. local_time->sec = utc_time->sec;
  611. }
  612. /**
  613. * @brief 常用时间格式转时间戳
  614. * @param NONE
  615. * @note
  616. * @retval 无
  617. */
  618. uint32_t EC800_mktime (unsigned int year, unsigned int mon,
  619. unsigned int day, unsigned int hour,
  620. unsigned int min, unsigned int sec)
  621. {
  622. if (0 >= (int) (mon -= 2)){ /**//* 1..12 -> 11,12,1..10 */
  623. mon += 12; /**//* Puts Feb last since it has leap day */
  624. year -= 1;
  625. }
  626. return (((
  627. (unsigned long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +
  628. year*365 - 719499
  629. )*24 + hour /**//* now have hours */
  630. )*60 + min /**//* now have minutes */
  631. )*60 + sec; /**//* finally seconds */
  632. }
  633. /**
  634. * @brief 读取GNSS的数据
  635. * @param NONE
  636. * @note
  637. * @retval 无
  638. */
  639. void EC800_readGnssDate(void){
  640. char* found = NULL;
  641. EC800M_SendCommand(AT_QGPSGNMEA_RMC);
  642. EC800_recEnable();
  643. found = strstr(g_usart3_rx_buf, AT_RESP_OK);
  644. if (found != NULL) {
  645. printf("Location information received successfully\r\n");
  646. }else{
  647. printf("Location information reception failed\r\n");
  648. return ;
  649. }
  650. printf("Location information:%s\r\n", g_usart3_rx_buf);
  651. NMEA_GNRMC_Analysis(&s_nmea_msg, (uint8_t*)g_usart3_rx_buf);
  652. utc_to_local_time(&(s_nmea_msg.utc), 8, &s_nmea_utc_time);
  653. printf("timer-%d/%d/%d %d:%d:%d\r\n",s_nmea_utc_time.year,s_nmea_utc_time.month,s_nmea_utc_time.date,s_nmea_utc_time.hour,s_nmea_utc_time.min,s_nmea_utc_time.sec);
  654. s_messageDate.Timestamp = EC800_mktime(s_nmea_utc_time.year,s_nmea_utc_time.month,s_nmea_utc_time.date,s_nmea_utc_time.hour,s_nmea_utc_time.min,s_nmea_utc_time.sec);
  655. g_usart3_rx_sta= 0;
  656. memset(g_usart3_rx_buf, 0, USART3_REC_LEN); // 清除数据buff
  657. printf("latitude:%d%d--longitude:%d%d\r\n", s_nmea_msg.latitude, s_nmea_msg.nshemi, s_nmea_msg.longitude, s_nmea_msg.ewhemi);
  658. s_messageDate.latitude = s_nmea_msg.latitude;
  659. s_messageDate.nshemi = s_nmea_msg.nshemi;
  660. s_messageDate.longitude = s_nmea_msg.longitude;
  661. s_messageDate.ewhemi = s_nmea_msg.ewhemi;
  662. }
  663. /**
  664. * @brief EC800M订阅主题
  665. * @param topic1: 订阅的主题名称1字符串形式
  666. * @param topic2: 订阅的主题名称2字符串形式
  667. * @note
  668. * @retval 无
  669. */
  670. uint8_t EC800_subscribeToTopic(const char* topic1){
  671. char command[100] = {0};
  672. uint8_t recTemp = 0;
  673. sprintf(command, "AT+QMTSUB=0,1,\"%s\",0\r\n", topic1);
  674. EC800M_SendCommand(command);
  675. recTemp = Accept_and_Compare_Str(AT_RESP_QMTSUB);
  676. if (recTemp == 1) {
  677. printf("Subscription to the topic successful\r\n");
  678. return 1;
  679. }else if (recTemp == 2){
  680. printf("Subscription to the topic failed\r\n");
  681. return 0;
  682. }
  683. }
  684. /**
  685. * @brief EC800M发布消息
  686. * @param topic: 消息发布的主题 字符串形式
  687. * @param message: 发布的消息 字符串形式
  688. * @param len: 发布消息的字节长度
  689. * @note
  690. * @retval 无
  691. */
  692. uint8_t EC800_publishMessage(const char* topic, const char* message, uint16_t len){
  693. char command[100] = {0};
  694. char* found = NULL;
  695. char* responseCmd = ">";
  696. static uint8_t publishStep = 0;
  697. switch(publishStep){
  698. case 0:
  699. sprintf(command,"AT+QMTPUBEX=0,0,0,0,\"%s\",%d\r\n",topic,len);
  700. EC800M_SendCommand(command);
  701. found = EC800M_RecRespond(g_usart3_rx_buf, responseCmd);
  702. if (found != NULL) {
  703. found = NULL; // 将found指针指向NULL处
  704. printf("The topic has been linked\r\n");
  705. publishStep = 1;
  706. }else{
  707. printf("The topic link failed\r\n");
  708. publishStep = 0;
  709. }
  710. // break;
  711. case 1:
  712. EC800M_SendCommand(message);
  713. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  714. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_QMTPUBEX);
  715. if (found != NULL) {
  716. printf("The message has been successfully posted\r\n");
  717. publishStep = 0;
  718. }else{
  719. printf("Message publication failed\r\n");
  720. publishStep = 0;
  721. }
  722. break;
  723. default:
  724. break;
  725. }
  726. return 2;
  727. }
  728. /**
  729. * @brief EC800M与服务器进行时间对时
  730. * @note
  731. * @retval 无
  732. */
  733. uint8_t EC800_time_calibration(void){
  734. cJSON *root = NULL;
  735. char *jsonString = NULL;
  736. uint8_t temp = 0;
  737. // 定义对象 { }
  738. root = cJSON_CreateObject();
  739. // 插入元素,对应 键值对
  740. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  741. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  742. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  743. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  744. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(DEV_ID));
  745. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  746. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  747. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  748. // 将 JSON 对象转换为字符串
  749. jsonString = cJSON_Print(root);
  750. cJSON_Delete(root);
  751. temp = EC800_publishMessage("ABCDEFG", jsonString, strlen(jsonString));
  752. free(jsonString);
  753. return temp;
  754. }
  755. /**
  756. * @brief 将服务器响应的字符串中的年月日进行分离
  757. * @note
  758. * @retval 无
  759. */
  760. void parseServerTime(char* str){
  761. char year[5];
  762. char month[3];
  763. char day[3];
  764. char hour[3];
  765. char minute[3];
  766. char second[3];
  767. strncpy(year, str, 4);
  768. year[4] = '\0';
  769. strncpy(month, str + 4, 2);
  770. month[2] = '\0';
  771. strncpy(day, str + 6, 2);
  772. day[2] = '\0';
  773. strncpy(hour, str + 8, 2);
  774. hour[2] = '\0';
  775. strncpy(minute, str + 10, 2);
  776. minute[2] = '\0';
  777. strncpy(second, str + 12, 2);
  778. second[2] = '\0';
  779. s_recDate.year = atoi(year);
  780. s_recDate.month = atoi(month);
  781. s_recDate.day = atoi(day);
  782. s_recDate.hour = atoi(hour);
  783. s_recDate.minute = atoi(minute);
  784. s_recDate.sec = atoi(second);
  785. s_messageDate.Timestamp = EC800_mktime(s_recDate.year,s_recDate.month,s_recDate.day,s_recDate.hour,s_recDate.minute,s_recDate.sec);
  786. printf("Year (int): %d\n", s_recDate.year);
  787. printf("Month (int): %d\n", s_recDate.month);
  788. printf("Day (int): %d\n", s_recDate.day);
  789. printf("Hour (int): %d\n", s_recDate.hour);
  790. printf("Minute (int): %d\n", s_recDate.minute);
  791. printf("Second (int): %d\n", s_recDate.sec);
  792. }
  793. /**
  794. * @brief 解析对时数据帧的响应
  795. * @note
  796. * @retval 无
  797. */
  798. void EC800_parseRespondTime(cJSON *root, cJSON *item){
  799. uint8_t temp = 0;
  800. // 获取"resultCode"字段的值
  801. item = cJSON_GetObjectItem(root, "resultCode");
  802. if (item == NULL) {
  803. printf("Field \"resultCode\" not found\n");
  804. }else{
  805. // 打印"resultCode"字段的值
  806. printf("resultCode: %d\n", item->valueint);
  807. if((item->valueint) == 1){
  808. printf("The upload of synchronized data was successful.\n");
  809. temp = 1; // 上传成功
  810. }else{
  811. printf("The upload of synchronized data has failed.\n");
  812. }
  813. }
  814. if(temp == 1){
  815. item = cJSON_GetObjectItem(root, "serverTime");
  816. if (item == NULL) {
  817. printf("Field \"serverTime\" not found\n");
  818. }else{
  819. // 打印"serverTime"字段的值
  820. printf("serverTime: %d\n", item->valuestring);
  821. parseServerTime(item->valuestring);
  822. }
  823. }
  824. }
  825. /**
  826. * @brief EC800M上传实时数据
  827. * @note
  828. * @retval 无
  829. */
  830. uint8_t EC800_uploadRealDate(void){
  831. cJSON *root = NULL;
  832. char *jsonString = NULL;
  833. cJSON *rt000 = NULL;
  834. cJSON *rt000Object1 = NULL;
  835. uint8_t temp = 0;
  836. // 定义对象 { }
  837. root = cJSON_CreateObject();
  838. //
  839. // 插入元素,对应 键值对
  840. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  841. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  842. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  843. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  844. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(DEV_ID));
  845. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  846. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  847. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  848. // 定义 { } 对象
  849. rt000Object1 = cJSON_CreateObject();
  850. cJSON_AddItemToObject(rt000Object1, "rt003", cJSON_CreateNumber(s_messageDate.vehicleStatus));
  851. cJSON_AddItemToObject(rt000Object1, "rt025", cJSON_CreateNumber(s_messageDate.demandVol));
  852. cJSON_AddItemToObject(rt000Object1, "rt026", cJSON_CreateNumber(s_messageDate.demandCur));
  853. cJSON_AddItemToObject(rt000Object1, "rt027", cJSON_CreateString((char *)s_messageDate.VIN));
  854. cJSON_AddItemToObject(rt000Object1, "rt028", cJSON_CreateString(s_messageDate.lat_long_data));
  855. cJSON_AddItemToObject(rt000Object1, "rt029", cJSON_CreateNumber(s_messageDate.drivDirection));
  856. cJSON_AddItemToObject(rt000Object1, "rt030", cJSON_CreateNumber(s_messageDate.vehicleSpeed));
  857. cJSON_AddItemToObject(rt000Object1, "rt031", cJSON_CreateNumber(s_messageDate.dailyDrivTime));
  858. cJSON_AddItemToObject(rt000Object1, "rt032", cJSON_CreateNumber(s_messageDate.dailyDrivMileage));
  859. cJSON_AddItemToObject(rt000Object1, "rt033", cJSON_CreateNumber(s_messageDate.accTotalDrivTime_h_min));
  860. cJSON_AddItemToObject(rt000Object1, "rt034", cJSON_CreateNumber((s_messageDate.accTotalMileage_h << 16) + s_messageDate.accTotalMileage_l));
  861. cJSON_AddItemToObject(rt000Object1, "rt035", cJSON_CreateNumber(s_messageDate.runTime));
  862. // 定义 [ ] 数组
  863. rt000 = cJSON_CreateArray();
  864. // 往数组中添加元素
  865. cJSON_AddItemToArray(rt000, rt000Object1);
  866. // 将子项插入根项中
  867. cJSON_AddItemToObject(root, "rt000", rt000);
  868. //
  869. // 将 JSON 对象转换为字符串
  870. jsonString = cJSON_Print(root);
  871. cJSON_Delete(root);
  872. temp = EC800_publishMessage("ABCDEFG", jsonString, strlen(jsonString));
  873. free(jsonString);
  874. return temp;
  875. }
  876. /**
  877. * @brief 解析实时数据响应
  878. * @note
  879. * @retval 无
  880. */
  881. void EC800_respondRealDate(cJSON *root, cJSON *item){
  882. // 获取"name"字段的值
  883. item = cJSON_GetObjectItem(root, "resultCode");
  884. if (item == NULL) {
  885. printf("Field \"resultCode\" not found\n");
  886. }else{
  887. // 打印"name"字段的值
  888. printf("resultCode: %d\n", item->valueint);
  889. if((item->valueint) == 1){
  890. printf("Uploaded real-time data successfully\n");
  891. }else{
  892. printf("The upload of real-time data has failed\n");
  893. }
  894. }
  895. }
  896. /**
  897. * @brief 通信响应解析
  898. * @note
  899. * @return 当前的控制命令
  900. */
  901. short EC800_respondParse(void){
  902. cJSON *root = NULL;
  903. cJSON *item = NULL;
  904. short cmd = 0;
  905. // 找到JSON数据的起始位置
  906. const char* start = strchr(g_usart3_rx_buf, '{');
  907. if (start == NULL) {
  908. printf("JSON data not found\n");
  909. return 0;
  910. }
  911. // 解析JSON数据
  912. root = cJSON_Parse(start);
  913. if (root == NULL) {
  914. printf("Failed to parse JSON data\n");
  915. cJSON_Delete(root);
  916. return 0;
  917. }
  918. // 获取"controlCode"字段的值
  919. item = cJSON_GetObjectItem(root, "controlCode");
  920. if (item == NULL) {
  921. printf("Field \"controlCode\" not found\n");
  922. }else{
  923. // 打印"controlCode"字段的值
  924. printf("controlCode: %d\n", item->valueint);
  925. cmd = item->valueint;
  926. }
  927. // 解析对应命令的对应数据
  928. switch(cmd){
  929. case 106:
  930. EC800_parseRespondTime(root, item);
  931. break;
  932. case 202:
  933. EC800_respondRealDate(root, item);
  934. break;
  935. default :
  936. break;
  937. }
  938. cJSON_Delete(root);
  939. return (cmd);
  940. }
  941. /**
  942. * @brief EC800M状态转换与使用
  943. * @note
  944. * @retval 无
  945. */
  946. uint8_t stateStep = 0;
  947. void EC800_stateTransition_use(void){
  948. uint8_t right = 1; // 返回是否为正确
  949. static uint16_t timesCnt = 0;
  950. static uint8_t tudeErrcnt = 0; // 定位信息错误计数
  951. ec800Date *p_ec800Date = &s_ec800Date;
  952. switch(stateStep){
  953. case 0: // 初始化模块
  954. EC800M_link();
  955. if(linkStep == 12){
  956. stateStep = 1;
  957. p_ec800Date->ec800InitFlag = 1; // 初始化完成
  958. }else{
  959. p_ec800Date->ec800InitFlag = 0; // 初始化未完成
  960. }
  961. break;
  962. case 1: // 初始化GNSS
  963. EC800_gnss_init();
  964. if(gnssStep == 8){
  965. stateStep = 2;
  966. }
  967. break;
  968. case 2: // 订阅主题
  969. right = EC800_subscribeToTopic("aabbcc");
  970. if(right != 1){ // 发布消息失败,可能断开链接
  971. // stateStep = 0; // 重新初始化
  972. }else{
  973. stateStep = 3;
  974. }
  975. break;
  976. case 3:
  977. // 获取定位信息
  978. EC800_readGnssDate();
  979. // 没有获取到定位信息 重复获取三次
  980. if((s_messageDate.latitude == 0) && (s_messageDate.longitude == 0)){
  981. tudeErrcnt++;
  982. if(tudeErrcnt == 4){
  983. tudeErrcnt = 0;
  984. stateStep = 4;
  985. }
  986. }else{
  987. tudeErrcnt = 0;
  988. stateStep = 4;
  989. }
  990. break;
  991. case 4: // 与服务器对时
  992. right = EC800_time_calibration();
  993. stateStep = 6; // 去步骤6等待对时响应 收到对时响应后,发布实时消息
  994. break;
  995. case 5: // 发布消息
  996. right = EC800_uploadRealDate();
  997. stateStep = 6;
  998. // if(right == 0){ // 发布消息失败,可能断开链接
  999. // stateStep = 0; // 重新初始化
  1000. // linkStep = 0;
  1001. // stateStep = 6;
  1002. // }else if(right == 1){
  1003. // stateStep = 6;
  1004. // }
  1005. break;
  1006. case 6: // 接收消息
  1007. HAL_UART_Receive_DMA(&huart3, (uint8_t*)g_usart3_rx_buf, USART3_REC_LEN); //设置接收缓冲区
  1008. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  1009. if(g_usart3_rx_sta!=0){
  1010. HAL_UART_Transmit(&huart1, (uint8_t*)g_usart3_rx_buf, strlen(g_usart3_rx_buf), HAL_MAX_DELAY); // 打印数据除去URC
  1011. right = EC800_respondParse();
  1012. if(right == CTR_CODE_JUDETIME){ // 对时响应
  1013. stateStep = 5;
  1014. right = 0;
  1015. timesCnt = 0;
  1016. }
  1017. memset(g_usart3_rx_buf, 0, USART3_REC_LEN); // 清除数据buff,接收新的数据
  1018. g_usart3_rx_sta = 0; // 清除接收状态
  1019. }
  1020. // 转弯行驶中
  1021. if(turnFlag == 1){
  1022. if(timesCnt < TURN_INTERVAL_TIME_MS){
  1023. timesCnt++;
  1024. }else{
  1025. timesCnt= 0;
  1026. stateStep = 3;
  1027. HAL_UART_DMAStop(&huart3);
  1028. __HAL_UART_DISABLE_IT(&huart3, UART_IT_IDLE);
  1029. }
  1030. }else{ // 正常行驶中
  1031. if(timesCnt < PUBLISH_TIME_MS){
  1032. timesCnt++;
  1033. }else{
  1034. timesCnt= 0;
  1035. stateStep = 3;
  1036. HAL_UART_DMAStop(&huart3);
  1037. __HAL_UART_DISABLE_IT(&huart3, UART_IT_IDLE);
  1038. }
  1039. }
  1040. break;
  1041. default:
  1042. break;
  1043. }
  1044. }