EC800(3767).c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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 == 12){
  930. stateStep = 1;
  931. }
  932. break;
  933. case 1: // 初始化GNSS
  934. EC800_gnss_init();
  935. if(gnssStep == 8){
  936. stateStep = 2;
  937. }
  938. break;
  939. case 2: // 订阅主题
  940. right = EC800_subscribeToTopic("aabbcc");
  941. if(right != 1){ // 发布消息失败,可能断开链接
  942. // stateStep = 0; // 重新初始化
  943. }else{
  944. stateStep = 3;
  945. }
  946. break;
  947. case 3:
  948. // 获取定位信息
  949. EC800_readGnssDate();
  950. // 没有获取到定位信息 重复获取三次
  951. if((s_messageDate.latitude == 0) && (s_messageDate.longitude == 0)){
  952. tudeErrcnt++;
  953. if(tudeErrcnt == 4){
  954. tudeErrcnt = 0;
  955. stateStep = 4;
  956. }
  957. }else{
  958. tudeErrcnt = 0;
  959. stateStep = 4;
  960. }
  961. break;
  962. case 4: // 与服务器对时
  963. right = EC800_time_calibration();
  964. stateStep = 6; // 去步骤6等待对时响应 收到对时响应后,发布实时消息
  965. break;
  966. case 5: // 发布消息
  967. right = EC800_uploadRealDate();
  968. stateStep = 6;
  969. // if(right == 0){ // 发布消息失败,可能断开链接
  970. // stateStep = 0; // 重新初始化
  971. // linkStep = 0;
  972. // stateStep = 6;
  973. // }else if(right == 1){
  974. // stateStep = 6;
  975. // }
  976. break;
  977. case 6: // 接收消息
  978. HAL_UART_Receive_DMA(&huart3, (uint8_t*)g_usart3_rx_buf, USART3_REC_LEN); //设置接收缓冲区
  979. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  980. if(g_usart3_rx_sta!=0){
  981. HAL_UART_Transmit(&huart1, (uint8_t*)g_usart3_rx_buf, strlen(g_usart3_rx_buf), HAL_MAX_DELAY); // 打印数据除去URC
  982. right = EC800_respondParse();
  983. if(right == CTR_CODE_JUDETIME){ // 对时响应
  984. stateStep = 5;
  985. right = 0;
  986. timesCnt = 0;
  987. }
  988. memset(g_usart3_rx_buf, 0, USART3_REC_LEN); // 清除数据buff,接收新的数据
  989. g_usart3_rx_sta = 0; // 清除接收状态
  990. }
  991. // 转弯行驶中
  992. if(turnFlag == 1){
  993. if(timesCnt < TURN_INTERVAL_TIME_MS){
  994. timesCnt++;
  995. }else{
  996. timesCnt= 0;
  997. stateStep = 3;
  998. HAL_UART_DMAStop(&huart3);
  999. __HAL_UART_DISABLE_IT(&huart3, UART_IT_IDLE);
  1000. }
  1001. }else{ // 正常行驶中
  1002. if(timesCnt < PUBLISH_TIME_MS){
  1003. timesCnt++;
  1004. }else{
  1005. timesCnt= 0;
  1006. stateStep = 3;
  1007. HAL_UART_DMAStop(&huart3);
  1008. __HAL_UART_DISABLE_IT(&huart3, UART_IT_IDLE);
  1009. }
  1010. }
  1011. break;
  1012. default:
  1013. break;
  1014. }
  1015. }