EC800(8045).c 29 KB

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