EC800(3722).c 28 KB

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