EC800(4157).c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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. uint8_t recBack = 0;
  698. switch(publishStep){
  699. case 0:
  700. sprintf(command,"AT+QMTPUBEX=0,0,0,0,\"%s\",%d\r\n",topic,len);
  701. EC800M_SendCommand(command);
  702. found = EC800M_RecRespond(g_usart3_rx_buf, responseCmd);
  703. if (found != NULL) {
  704. found = NULL; // 将found指针指向NULL处
  705. printf("The topic has been linked\r\n");
  706. publishStep = 1;
  707. }else{
  708. printf("The topic link failed\r\n");
  709. publishStep = 0;
  710. }
  711. // break;
  712. case 1:
  713. EC800M_SendCommand(message);
  714. // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  715. // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_QMTPUBEX);
  716. // if (found != NULL) {
  717. // printf("The message has been successfully posted\r\n");
  718. // publishStep = 0;
  719. // }else{
  720. // printf("Message publication failed\r\n");
  721. // publishStep = 0;
  722. // }
  723. recBack = Accept_and_Compare_Str(AT_RESP_QMTPUBEX);
  724. if (recBack == 1) {
  725. publishStep = 0;
  726. printf("The message has been successfully posted\r\n");
  727. }else{
  728. printf("Message publication failed\r\n");
  729. publishStep = 0;
  730. }
  731. break;
  732. default:
  733. break;
  734. }
  735. return recBack;
  736. }
  737. /**
  738. * @brief EC800M与服务器进行时间对时
  739. * @note
  740. * @retval 无
  741. */
  742. uint8_t EC800_time_calibration(void){
  743. cJSON *root = NULL;
  744. char *jsonString = NULL;
  745. uint8_t temp = 0;
  746. // 定义对象 { }
  747. root = cJSON_CreateObject();
  748. // 插入元素,对应 键值对
  749. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  750. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  751. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  752. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  753. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(DEV_ID));
  754. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  755. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  756. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  757. // 将 JSON 对象转换为字符串
  758. jsonString = cJSON_Print(root);
  759. cJSON_Delete(root);
  760. temp = EC800_publishMessage("ABCDEFG", jsonString, strlen(jsonString));
  761. free(jsonString);
  762. return temp;
  763. }
  764. /**
  765. * @brief 将服务器响应的字符串中的年月日进行分离
  766. * @note
  767. * @retval 无
  768. */
  769. void parseServerTime(char* str){
  770. char year[5];
  771. char month[3];
  772. char day[3];
  773. char hour[3];
  774. char minute[3];
  775. char second[3];
  776. strncpy(year, str, 4);
  777. year[4] = '\0';
  778. strncpy(month, str + 4, 2);
  779. month[2] = '\0';
  780. strncpy(day, str + 6, 2);
  781. day[2] = '\0';
  782. strncpy(hour, str + 8, 2);
  783. hour[2] = '\0';
  784. strncpy(minute, str + 10, 2);
  785. minute[2] = '\0';
  786. strncpy(second, str + 12, 2);
  787. second[2] = '\0';
  788. s_recDate.year = atoi(year);
  789. s_recDate.month = atoi(month);
  790. s_recDate.day = atoi(day);
  791. s_recDate.hour = atoi(hour);
  792. s_recDate.minute = atoi(minute);
  793. s_recDate.sec = atoi(second);
  794. s_messageDate.Timestamp = EC800_mktime(s_recDate.year,s_recDate.month,s_recDate.day,s_recDate.hour,s_recDate.minute,s_recDate.sec);
  795. printf("Year (int): %d\n", s_recDate.year);
  796. printf("Month (int): %d\n", s_recDate.month);
  797. printf("Day (int): %d\n", s_recDate.day);
  798. printf("Hour (int): %d\n", s_recDate.hour);
  799. printf("Minute (int): %d\n", s_recDate.minute);
  800. printf("Second (int): %d\n", s_recDate.sec);
  801. }
  802. /**
  803. * @brief 解析对时数据帧的响应
  804. * @note
  805. * @retval 无
  806. */
  807. void EC800_parseRespondTime(cJSON *root, cJSON *item){
  808. uint8_t temp = 0;
  809. // 获取"resultCode"字段的值
  810. item = cJSON_GetObjectItem(root, "resultCode");
  811. if (item == NULL) {
  812. printf("Field \"resultCode\" not found\n");
  813. }else{
  814. // 打印"resultCode"字段的值
  815. printf("resultCode: %d\n", item->valueint);
  816. if((item->valueint) == 1){
  817. printf("The upload of synchronized data was successful.\n");
  818. temp = 1; // 上传成功
  819. }else{
  820. printf("The upload of synchronized data has failed.\n");
  821. }
  822. }
  823. if(temp == 1){
  824. item = cJSON_GetObjectItem(root, "serverTime");
  825. if (item == NULL) {
  826. printf("Field \"serverTime\" not found\n");
  827. }else{
  828. // 打印"serverTime"字段的值
  829. printf("serverTime: %d\n", item->valuestring);
  830. parseServerTime(item->valuestring);
  831. }
  832. }
  833. }
  834. /**
  835. * @brief EC800M上传实时数据
  836. * @note
  837. * @retval 无
  838. */
  839. uint8_t EC800_uploadRealDate(void){
  840. cJSON *root = NULL;
  841. char *jsonString = NULL;
  842. cJSON *rt000 = NULL;
  843. cJSON *rt000Object1 = NULL;
  844. uint8_t temp = 0;
  845. // 定义对象 { }
  846. root = cJSON_CreateObject();
  847. //
  848. // 插入元素,对应 键值对
  849. cJSON_AddItemToObject(root, "msgType", cJSON_CreateString("json"));
  850. cJSON_AddItemToObject(root, "imsi", cJSON_CreateString(s_messageDate.imsi));
  851. cJSON_AddItemToObject(root, "hardVersion", cJSON_CreateString(HARD_VERSION));
  852. cJSON_AddItemToObject(root, "softVersion", cJSON_CreateString(SOFT_VERSION));
  853. cJSON_AddItemToObject(root, "devId", cJSON_CreateString(DEV_ID));
  854. cJSON_AddItemToObject(root, "protocolVersion", cJSON_CreateString(PROTOCOL_VERSION));
  855. cJSON_AddItemToObject(root, "txnNo", cJSON_CreateNumber(s_messageDate.Timestamp));
  856. cJSON_AddItemToObject(root, "devType", cJSON_CreateNumber(DEV_TYPE));
  857. // 定义 { } 对象
  858. rt000Object1 = cJSON_CreateObject();
  859. cJSON_AddItemToObject(rt000Object1, "rt003", cJSON_CreateNumber(s_messageDate.vehicleStatus));
  860. cJSON_AddItemToObject(rt000Object1, "rt025", cJSON_CreateNumber(s_messageDate.demandVol));
  861. cJSON_AddItemToObject(rt000Object1, "rt026", cJSON_CreateNumber(s_messageDate.demandCur));
  862. cJSON_AddItemToObject(rt000Object1, "rt027", cJSON_CreateString((char *)s_messageDate.VIN));
  863. cJSON_AddItemToObject(rt000Object1, "rt028", cJSON_CreateString(s_messageDate.lat_long_data));
  864. cJSON_AddItemToObject(rt000Object1, "rt029", cJSON_CreateNumber(s_messageDate.drivDirection));
  865. cJSON_AddItemToObject(rt000Object1, "rt030", cJSON_CreateNumber(s_messageDate.vehicleSpeed));
  866. cJSON_AddItemToObject(rt000Object1, "rt031", cJSON_CreateNumber(s_messageDate.dailyDrivTime));
  867. cJSON_AddItemToObject(rt000Object1, "rt032", cJSON_CreateNumber(s_messageDate.dailyDrivMileage));
  868. cJSON_AddItemToObject(rt000Object1, "rt033", cJSON_CreateNumber(s_messageDate.accTotalDrivTime_h_min));
  869. cJSON_AddItemToObject(rt000Object1, "rt034", cJSON_CreateNumber((s_messageDate.accTotalMileage_h << 16) + s_messageDate.accTotalMileage_l));
  870. cJSON_AddItemToObject(rt000Object1, "rt035", cJSON_CreateNumber(s_messageDate.runTime));
  871. // 定义 [ ] 数组
  872. rt000 = cJSON_CreateArray();
  873. // 往数组中添加元素
  874. cJSON_AddItemToArray(rt000, rt000Object1);
  875. // 将子项插入根项中
  876. cJSON_AddItemToObject(root, "rt000", rt000);
  877. //
  878. // 将 JSON 对象转换为字符串
  879. jsonString = cJSON_Print(root);
  880. cJSON_Delete(root);
  881. temp = EC800_publishMessage("ABCDEFG", jsonString, strlen(jsonString));
  882. free(jsonString);
  883. return temp;
  884. }
  885. /**
  886. * @brief 解析实时数据响应
  887. * @note
  888. * @retval 无
  889. */
  890. void EC800_respondRealDate(cJSON *root, cJSON *item){
  891. // 获取"name"字段的值
  892. item = cJSON_GetObjectItem(root, "resultCode");
  893. if (item == NULL) {
  894. printf("Field \"resultCode\" not found\n");
  895. }else{
  896. // 打印"name"字段的值
  897. printf("resultCode: %d\n", item->valueint);
  898. if((item->valueint) == 1){
  899. printf("Uploaded real-time data successfully\n");
  900. }else{
  901. printf("The upload of real-time data has failed\n");
  902. }
  903. }
  904. }
  905. /**
  906. * @brief 通信响应解析
  907. * @note
  908. * @return 当前的控制命令
  909. */
  910. short EC800_respondParse(void){
  911. cJSON *root = NULL;
  912. cJSON *item = NULL;
  913. short cmd = 0;
  914. // 找到JSON数据的起始位置
  915. const char* start = strchr(g_usart3_rx_buf, '{');
  916. if (start == NULL) {
  917. printf("JSON data not found\n");
  918. return 0;
  919. }
  920. // 解析JSON数据
  921. root = cJSON_Parse(start);
  922. if (root == NULL) {
  923. printf("Failed to parse JSON data\n");
  924. cJSON_Delete(root);
  925. return 0;
  926. }
  927. // 获取"controlCode"字段的值
  928. item = cJSON_GetObjectItem(root, "controlCode");
  929. if (item == NULL) {
  930. printf("Field \"controlCode\" not found\n");
  931. }else{
  932. // 打印"controlCode"字段的值
  933. printf("controlCode: %d\n", item->valueint);
  934. cmd = item->valueint;
  935. }
  936. // 解析对应命令的对应数据
  937. switch(cmd){
  938. case 106:
  939. EC800_parseRespondTime(root, item);
  940. break;
  941. case 202:
  942. EC800_respondRealDate(root, item);
  943. break;
  944. default :
  945. break;
  946. }
  947. cJSON_Delete(root);
  948. return (cmd);
  949. }
  950. /**
  951. * @brief EC800M状态转换与使用
  952. * @note
  953. * @retval 无
  954. */
  955. uint8_t stateStep = 0;
  956. void EC800_stateTransition_use(void){
  957. uint8_t right = 1; // 返回是否为正确
  958. static uint16_t timesCnt = 0;
  959. static uint8_t tudeErrcnt = 0; // 定位信息错误计数
  960. ec800Date *p_ec800Date = &s_ec800Date;
  961. switch(stateStep){
  962. case 0: // 初始化模块
  963. EC800M_link();
  964. if(linkStep == 12){
  965. stateStep = 1;
  966. p_ec800Date->ec800InitFlag = 1; // 初始化完成
  967. }else{
  968. p_ec800Date->ec800InitFlag = 0; // 初始化未完成
  969. }
  970. break;
  971. case 1: // 初始化GNSS
  972. EC800_gnss_init();
  973. if(gnssStep == 8){
  974. stateStep = 2;
  975. }
  976. break;
  977. case 2: // 订阅主题
  978. right = EC800_subscribeToTopic("aabbcc");
  979. if(right != 1){ // 发布消息失败,可能断开链接
  980. // stateStep = 0; // 重新初始化
  981. }else{
  982. stateStep = 3;
  983. }
  984. break;
  985. case 3:
  986. // 获取定位信息
  987. EC800_readGnssDate();
  988. // 没有获取到定位信息 重复获取三次
  989. if((s_messageDate.latitude == 0) && (s_messageDate.longitude == 0)){
  990. tudeErrcnt++;
  991. if(tudeErrcnt == 4){
  992. tudeErrcnt = 0;
  993. stateStep = 4;
  994. }
  995. }else{
  996. tudeErrcnt = 0;
  997. stateStep = 4;
  998. }
  999. break;
  1000. case 4: // 与服务器对时
  1001. right = EC800_time_calibration();
  1002. if(right == 1){
  1003. stateStep = 6; // 去步骤6等待对时响应 收到对时响应后,发布实时消息
  1004. }
  1005. break;
  1006. case 5: // 发布消息
  1007. right = EC800_uploadRealDate();
  1008. stateStep = 6;
  1009. // if(right == 0){ // 发布消息失败,可能断开链接
  1010. // stateStep = 0; // 重新初始化
  1011. // linkStep = 0;
  1012. // stateStep = 6;
  1013. // }else if(right == 1){
  1014. // stateStep = 6;
  1015. // }
  1016. break;
  1017. case 6: // 接收消息
  1018. HAL_UART_Receive_DMA(&huart3, (uint8_t*)g_usart3_rx_buf, USART3_REC_LEN); //设置接收缓冲区
  1019. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  1020. if(g_usart3_rx_sta!=0){
  1021. HAL_UART_Transmit(&huart1, (uint8_t*)g_usart3_rx_buf, strlen(g_usart3_rx_buf), HAL_MAX_DELAY); // 打印数据除去URC
  1022. right = EC800_respondParse();
  1023. if(right == CTR_CODE_JUDETIME){ // 对时响应
  1024. stateStep = 5;
  1025. right = 0;
  1026. timesCnt = 0;
  1027. }
  1028. memset(g_usart3_rx_buf, 0, USART3_REC_LEN); // 清除数据buff,接收新的数据
  1029. g_usart3_rx_sta = 0; // 清除接收状态
  1030. }
  1031. // 转弯行驶中
  1032. if(turnFlag == 1){
  1033. if(timesCnt < TURN_INTERVAL_TIME_MS){
  1034. timesCnt++;
  1035. }else{
  1036. timesCnt= 0;
  1037. stateStep = 3;
  1038. HAL_UART_DMAStop(&huart3);
  1039. __HAL_UART_DISABLE_IT(&huart3, UART_IT_IDLE);
  1040. }
  1041. }else{ // 正常行驶中
  1042. if(timesCnt < PUBLISH_TIME_MS){
  1043. timesCnt++;
  1044. }else{
  1045. timesCnt= 0;
  1046. stateStep = 3;
  1047. HAL_UART_DMAStop(&huart3);
  1048. __HAL_UART_DISABLE_IT(&huart3, UART_IT_IDLE);
  1049. }
  1050. }
  1051. break;
  1052. default:
  1053. break;
  1054. }
  1055. }