ESP32_WROOM_32D.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. #include "ESP32_WROOM_32D.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <stdarg.h>
  6. extern int Flag3S;
  7. #define g_uMsgCmdFrameLen (6)
  8. extern uint8_t Devicenum;
  9. extern uint8_t g_wifi_rx_buf[WIFI_RX_BUF_SIZE];
  10. extern uint32_t g_wifi_rx_last;
  11. extern uint32_t g_wifi_rx_head;
  12. uint8_t wifi_tx_buf[WIFI_TX_BUF_SIZE];
  13. uint16_t wifi_tx_len;
  14. static int wifi_state = 0; //WiFi模块当前状态
  15. static int wifi_ack_delay = 0; //WiFi应答等待
  16. static int wifi_set_ssid = 0; //WiFi应答SSID设置
  17. uint32_t unlink_time = 0;
  18. extern uint32_t pc_link_timeout;
  19. char TCP_IP_PORT[8] = {"8001"};
  20. char MAC_ADDR[20] = {0};
  21. uint32_t broadDevFlag = 0;
  22. extern uint8_t pc_channel_recv;
  23. static int resendtime = 0;
  24. uint16_t WriteComm2(char *data, uint16_t len)
  25. {
  26. //return UART_Write(UART2, data, len);
  27. HAL_UART_Transmit((UART_HandleTypeDef *)sysinf.ESPUart,(const uint8_t *)data, len,1000);
  28. // HAL_UART_Transmit_DMA((UART_HandleTypeDef *)sysinf.ESPUart,(const uint8_t *)data, len);
  29. return 0;
  30. }
  31. /*WIFI单次发送指令*/
  32. void ESP32_Send_AT_Cmd(char *cmd,uint32_t time)
  33. {
  34. char buff[100] = {0};
  35. strcpy(buff, cmd);
  36. strcat(buff, "\r\n");
  37. Van_Device_Printf(VAN_LOG_WIFI,"\nW--> %s", buff);
  38. WriteComm2(buff, strlen(buff));
  39. wifi_ack_delay = time;
  40. }
  41. void ESP32_AT(void)
  42. {
  43. ESP32_Send_AT_Cmd("AT",5000);
  44. }
  45. /*恢复出厂设置*/
  46. void ESP32_AT_Restore(void)
  47. {
  48. ESP32_Send_AT_Cmd("AT+RESTORE",500);
  49. }
  50. /*复位*/
  51. void ESP32_AT_RST(void)
  52. {
  53. ESP32_Send_AT_Cmd("AT+RST",8000);
  54. }
  55. /*上电自动连接AP*/
  56. void ESP32_AT_AUTOCONN(void)
  57. {
  58. ESP32_Send_AT_Cmd("AT+CWAUTOCONN=0",5000);
  59. }
  60. /*WIFI AP/STA/AP+STA模式选择*/
  61. void ESP32_Net_Mode_Choose(ENUM_Net_ModeTypeDef enumMode)
  62. {
  63. switch ( enumMode )
  64. {
  65. case STA:
  66. ESP32_Send_AT_Cmd ( "AT+CWMODE=1", 5000 );
  67. break;
  68. case AP:
  69. ESP32_Send_AT_Cmd ( "AT+CWMODE=2", 5000 );
  70. break;
  71. case STA_AP:
  72. ESP32_Send_AT_Cmd ( "AT+CWMODE=3", 5000 );
  73. break;
  74. default:
  75. break;
  76. }
  77. }
  78. /*WIFI加入AP*/
  79. void ESP32_JoinAP( char * pSSID, char * pPassWord, char * pMacAddr)
  80. {
  81. char cCmd [80];
  82. my_sprintf ( cCmd, sizeof(cCmd), "AT+CWJAP=\"%s\",\"%s\",\"%s\"", pSSID, pPassWord,pMacAddr);
  83. ESP32_Send_AT_Cmd( cCmd,5000 );
  84. }
  85. /*WIFI建立AP*/
  86. void ESP32_SetupAP( char * pSSID, char * pPassWord)
  87. {
  88. char cCmd [40];
  89. my_sprintf ( cCmd,sizeof(cCmd), "AT+CWSAP=\"%s\",\"%s\"", pSSID, pPassWord );
  90. ESP32_Send_AT_Cmd( cCmd, 5000 );
  91. }
  92. /*WIFI查询IP*/
  93. void ESP32_GetIp(void)
  94. {
  95. ESP32_Send_AT_Cmd( "AT+CIPSTA?", 5000 );
  96. }
  97. /*WIFI禁止自动连接*/
  98. void ESP32_DisAutoLink(void)
  99. {
  100. ESP32_Send_AT_Cmd( "AT+CWAUTOCONN=0", 5000 );
  101. }
  102. void ESP32_CWLAP(char * pSSID)
  103. {
  104. char cCmd [80];
  105. my_sprintf ( cCmd, sizeof(cCmd), "AT+CWLAP=\"%s\"", pSSID);
  106. ESP32_Send_AT_Cmd( cCmd, 5000 );
  107. }
  108. /*WIFI关闭回显*/
  109. void ESP32_EchoOff(void)
  110. {
  111. ESP32_Send_AT_Cmd( "ATE0", 5000 );
  112. }
  113. /*WIFI初始化*/
  114. void ESP32_Init(void)
  115. {
  116. ESP32_Send_AT_Cmd( "+++", 5000 );
  117. }
  118. /*WIFI 单/多连接*/
  119. void ESP32_Enable_MultipleId (uint8_t enumEnUnvarnishTx )
  120. {
  121. char cCmd [20];
  122. my_sprintf ( cCmd, sizeof(cCmd), "AT+CIPMUX=%d", ( enumEnUnvarnishTx ? 1 : 0 ) );
  123. ESP32_Send_AT_Cmd ( cCmd, 5000 );
  124. }
  125. /*WIFI AP作为服务端开启或关闭,1---开启服务:0---关闭服务*/
  126. void ESP32_AP_Server (uint8_t open)
  127. {
  128. char cCmd [40];
  129. my_sprintf ( cCmd,sizeof(cCmd), "AT+CIPSERVER=%d", open);
  130. ESP32_Send_AT_Cmd( cCmd, 5000 );
  131. }
  132. /*WIFI STA选择TCP或UDP协议*/
  133. void ESP32_Link_Server(ENUM_NetPro_TypeDef enumE, char * ip, char * ComNum, ENUM_ID_NO_TypeDef id)
  134. {
  135. char cStr [100] = { 0 }, cCmd [60];
  136. switch ( enumE )
  137. {
  138. case enumTCP:
  139. my_sprintf ( cStr, sizeof(cCmd), "\"%s\",\"%s\",%s,6", "TCP", ip, ComNum );
  140. break;
  141. case enumUDP:
  142. my_sprintf ( cStr,sizeof(cCmd), "\"%s\",\"%s\",%s,9090,0", "UDP", ip, ComNum );
  143. break;
  144. default:
  145. break;
  146. }
  147. if ( id < 5 )
  148. my_sprintf ( cCmd,sizeof(cCmd), "AT+CIPSTART=%d,%s", id, cStr);
  149. else
  150. my_sprintf ( cCmd, sizeof(cCmd), "AT+CIPSTART=%s", cStr );
  151. ESP32_Send_AT_Cmd ( cCmd,5000 );
  152. }
  153. /*WIFI透传*/
  154. void ESP32_UnvarnishSend ( void )
  155. {
  156. ESP32_Send_AT_Cmd( "AT+CIPMODE=1", 5000 );//设置透传模式
  157. }
  158. /*WIFI停止TCP*/
  159. void ESP32_STOPTCP ( ENUM_ID_NO_TypeDef id )
  160. {
  161. char cCmd [60];
  162. my_sprintf ( cCmd,sizeof(cCmd), "AT+CIPCLOSE=%d", id);
  163. ESP32_Send_AT_Cmd ( cCmd, 20000 );
  164. }
  165. /*退出透传*/
  166. void ESP32_ExitUnvarnishSend( void )
  167. {
  168. ESP32_Send_AT_Cmd("+++", 5000);
  169. }
  170. ///*设置默认波特率为460800*/
  171. //void ESP32_SET_BUAR(void)
  172. //{
  173. // ESP32_Send_AT_Cmd("AT+UART_DEF=460800,8,1,0,0","OK",NULL,500);
  174. //}
  175. /********************************************************************
  176. * int WIFI_Send_Data(uint8_t *data, uint8_t len, uint8_t flag)
  177. *
  178. *描述:通过WIFI发送数据,异步
  179. *参数:data:数据
  180. *参数:len:数据长度
  181. *参数:flag:1:立即发送,0:暂存
  182. *返回:无
  183. *其他:无
  184. *--------------------------------------------------------------------
  185. *记录:
  186. ********************************************************************/
  187. int WIFI_Send_Data(uint8_t *data, uint16_t len, uint8_t flag)
  188. {
  189. char cStr [40] = {0};
  190. if (wifi_state < WIFI_TRANS)
  191. return -1;
  192. if(wifi_ack_delay > 0)
  193. return -2;
  194. if(wifi_tx_len)
  195. return -3;
  196. if(len > WIFI_TX_BUF_SIZE){
  197. Van_Device_Printf(VAN_LOG_WIFI,"WIFI_Send len:%d error ", len);
  198. return -1;
  199. }
  200. memcpy(&wifi_tx_buf[wifi_tx_len], data, len);
  201. wifi_tx_len += len;
  202. if(flag)
  203. {
  204. wifi_ack_delay = 3000;
  205. my_sprintf ( cStr, sizeof(cStr),"AT+CIPSEND=0,%d", wifi_tx_len );
  206. ESP32_Send_AT_Cmd ( cStr, 3000 );
  207. return wifi_tx_len;
  208. }
  209. return 0;
  210. }
  211. int WIFI_Send_MSG(uint8_t*msgHead,uint8_t *data, uint8_t * crc, uint16_t len)
  212. {
  213. char cStr [40] = {0};
  214. if (wifi_state != WIFI_TRANS)
  215. return -1;
  216. if(wifi_ack_delay > 0)
  217. return -2;
  218. if(wifi_tx_len > 0)
  219. return -3;
  220. memcpy(&wifi_tx_buf[0], msgHead, g_uMsgCmdFrameLen);
  221. memcpy(&wifi_tx_buf[g_uMsgCmdFrameLen], data, len);
  222. memcpy(&wifi_tx_buf[g_uMsgCmdFrameLen+len], crc, 2);
  223. wifi_tx_len = g_uMsgCmdFrameLen+len+2;
  224. wifi_ack_delay = 3000;
  225. my_sprintf ( cStr, sizeof(cStr),"AT+CIPSEND=0,%d", wifi_tx_len );
  226. ESP32_Send_AT_Cmd ( cStr, 3000 );
  227. return wifi_tx_len;
  228. }
  229. int WIFI_SendIlde(void){
  230. if(wifi_tx_len == 0)
  231. return 0;
  232. if (wifi_state < WIFI_TRANS)
  233. return 0;
  234. if(wifi_ack_delay > 0)
  235. return 0;
  236. return 1;
  237. }
  238. int WIFI_ReSend(void)
  239. {
  240. char cStr [40] = {0};
  241. if(wifi_tx_len == 0)
  242. return 0;
  243. if (wifi_state < WIFI_TRANS)
  244. return -1;
  245. if(wifi_ack_delay > 0)
  246. return -2;
  247. wifi_ack_delay = 3000;
  248. my_sprintf ( cStr, sizeof(cStr),"AT+CIPSEND=0,%d", wifi_tx_len );
  249. ESP32_Send_AT_Cmd ( cStr, 3000 );
  250. return 0;
  251. }
  252. //wifi修改SSID
  253. void WIFI_set_ssid(void)
  254. {
  255. wifi_ack_delay = 0;
  256. wifi_state = WIFI_POWEROFF;
  257. wifi_set_ssid = 1;
  258. if(wifi_tx_len > 0)
  259. {
  260. WriteComm2((char *)wifi_tx_buf, wifi_tx_len);
  261. wifi_tx_len = 0;
  262. }
  263. }
  264. //wifi修改SSID
  265. void WIFI_set_CloseConnect(void)
  266. {
  267. wifi_state = WIFI_CLOSE;
  268. }
  269. // //在内存中查找字符串
  270. // char* find_str(void *src, int size, char *str)
  271. // {
  272. // char *p = (char*)src;
  273. // int len = strlen((const char *)str);
  274. // if(len < 1)
  275. // return p;
  276. // for(int i = 0; i < size; i++)
  277. // {
  278. // if(p[i] == *str)
  279. // {
  280. // if( 0 == memcmp(&p[i], str, len))
  281. // {
  282. // return &p[i];
  283. // }
  284. // }
  285. // }
  286. // return NULL;
  287. // }
  288. //在内存中查找字符串
  289. char* find_str_loop(void *src, int size, uint32_t *start, uint32_t end, char *str) {
  290. if (src == NULL || start == NULL || str == NULL || size <= 0)
  291. return NULL;
  292. unsigned char *buf = (unsigned char*)src;
  293. int head = *start;
  294. int len = strlen(str);
  295. // 空串直接返回当前位置(不移动头指针)
  296. if (len == 0) {
  297. return (char*)(buf + head);
  298. }
  299. // 计算有效数据长度
  300. int valid_len;
  301. if (end >= head)
  302. valid_len = end - head;
  303. else
  304. valid_len = (size - head) + end;
  305. // 若有效数据长度小于目标串长度,不可能匹配
  306. if (valid_len < len)
  307. return NULL;
  308. // 从 head 开始遍历所有可能的起始位置(不包括 end)
  309. int pos = head;
  310. while (pos != end) {
  311. // 检查从 pos 开始的 len 个字节是否与 str 匹配
  312. int i;
  313. for (i = 0; i < len; i++) {
  314. int idx = (pos + i) % size;
  315. if (buf[idx] != (unsigned char)str[i])
  316. break;
  317. }
  318. if (i == len) { // 完全匹配
  319. // 更新头指针为匹配串之后的位置
  320. *start = (pos + len) % size;
  321. return (char*)(buf + pos);
  322. }
  323. // 移动到下一个起始位置
  324. pos = (pos + 1) % size;
  325. // 如果移动后到达 end,提前终止(有效数据遍历完毕)
  326. // 但 while 条件会处理,可省略
  327. }
  328. // 未找到
  329. return NULL;
  330. }
  331. /********************************************************************
  332. * WIFI_recv_deal()
  333. *
  334. *描述:接收帧解析
  335. *参数:无
  336. *返回:无
  337. *其他:无
  338. *--------------------------------------------------------------------
  339. *记录:
  340. ********************************************************************/
  341. void WIFI_recv_deal(void)
  342. {
  343. char *p;
  344. int data_len = g_wifi_rx_last>g_wifi_rx_head?(g_wifi_rx_last-g_wifi_rx_head):(WIFI_RX_BUF_SIZE+g_wifi_rx_last-g_wifi_rx_head);
  345. // int udp_data_len = 0;
  346. char* data = (char*)g_wifi_rx_buf;
  347. if((data_len < 5)&&(wifi_state != WIFI_TRANS))
  348. return;
  349. //判断是否接收完成
  350. if(wifi_state > WIFI_LISTEN)
  351. {
  352. if(data[g_wifi_rx_last-1] != '\n' && data[g_wifi_rx_last-2] != '\n')
  353. {
  354. return;
  355. }
  356. }
  357. switch (wifi_state)
  358. {
  359. //指令模式
  360. case WIFI_POWEROFF:
  361. case WIFI_ERROR:
  362. if(NULL != find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last,"ready") ||
  363. NULL != find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last,"boot"))
  364. {
  365. wifi_state = WIFI_POWERON;
  366. wifi_ack_delay = 0;
  367. memset(wifi_tx_buf,0,sizeof(wifi_tx_buf));
  368. wifi_tx_len = 0;
  369. // wifi_receive_clear();
  370. }
  371. break;
  372. case WIFI_POWERON:
  373. case WIFI_INIT:
  374. case WIFI_STATION:
  375. case WIFI_DISAUTO:
  376. case WIFI_LINK_AP:
  377. case WIFI_GET_IP:
  378. case WIFI_SETMUX:
  379. case WIFI_LISTEN:
  380. if(NULL != find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last, "OK\r\n"))
  381. {
  382. Van_Device_Printf(VAN_LOG_WIFI,"\nW<-- %s", data);
  383. wifi_ack_delay = 0;
  384. if(WIFI_LISTEN == wifi_state){
  385. /*上电需要广播地址*/
  386. broadDevFlag = 1;
  387. sysinf.s10_flag = 1;
  388. resendtime = 0;
  389. wifi_tx_len = 0;
  390. memset(wifi_tx_buf, 0, sizeof(wifi_tx_buf));
  391. // if(sysinf.connectTcpFlag == 1){
  392. // if(wifi_tx_len){
  393. // char cStr [40] = {0};
  394. // my_sprintf ( cStr,sizeof(cStr), "AT+CIPSEND=0,%d", wifi_tx_len );
  395. // ESP32_Send_AT_Cmd ( cStr, 3000 );
  396. // }
  397. // // sysinf.pc_link_timeout = 0;
  398. // // sysinf.pc_channel_recv = PC_CONNECT_TYPE_WIFI;
  399. // }
  400. // wifi_receive_clear();
  401. }
  402. wifi_state++;
  403. resendtime = 0;
  404. HAL_Delay(1000);
  405. //
  406. }
  407. break;
  408. case WIFI_CWLAP:{
  409. char* strStart = find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last,sysinf.wifiname);
  410. uint32_t apHeader = g_wifi_rx_head;
  411. if(NULL != strStart)
  412. {
  413. if(NULL != find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last, "OK\r\n")){
  414. /*循环始终需要OK,找到OK后,恢复APhead位置,重新搜寻mac*/
  415. g_wifi_rx_head = apHeader;
  416. Van_Device_Printf(VAN_LOG_WIFI,"\nW<-- %s", data);
  417. wifi_ack_delay = 0;
  418. strStart+=strlen(sysinf.wifiname);
  419. strStart =find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last,",\"");
  420. unsigned int startPos = ((unsigned int)strStart-(unsigned int)g_wifi_rx_buf + 2)%WIFI_RX_BUF_SIZE;/*跳过,\*/
  421. for(unsigned i=0;i<17;i++){
  422. MAC_ADDR[i] = g_wifi_rx_buf[(i+startPos)%WIFI_RX_BUF_SIZE];
  423. }
  424. MAC_ADDR[18] = 0;
  425. wifi_state++;
  426. }
  427. // wifi_receive_clear();
  428. }
  429. }
  430. break;
  431. //数据模式
  432. case WIFI_TRANS:
  433. //收到AT+CIPSEND回应,可发送数据
  434. if(NULL != find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last, "CLOSED"))
  435. {
  436. Van_Device_Printf(VAN_LOG_WIFI,"\nW<-- %d %s", data_len,data);
  437. wifi_state = WIFI_LISTEN;
  438. wifi_ack_delay = 0;//从发送写指令,到此才算发送完成
  439. broadDevFlag = 0;
  440. }
  441. else if ((NULL != find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last, "ERROR"))||
  442. (NULL != find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last,"SEND FAIL")))
  443. {
  444. if(resendtime < MAX_RESEND_TIME){
  445. char cStr [40] = {0};
  446. my_sprintf ( cStr,sizeof(cStr), "AT+CIPSEND=0,%d", wifi_tx_len );
  447. ESP32_Send_AT_Cmd ( cStr, 3000 );
  448. resendtime ++;
  449. }else{
  450. wifi_state = WIFI_POWEROFF;
  451. wifi_ack_delay = 0;
  452. }
  453. // wifi_receive_clear();
  454. } else if(NULL != find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last, "SEND OK"))
  455. {
  456. //收到AT+CIPSEND回应,可发送数据
  457. Van_Device_Printf(VAN_LOG_WIFI,"\n--> SEND OK", wifi_tx_len);
  458. wifi_ack_delay = 0;//从发送写指令,到此才算发送完成
  459. resendtime = 0;
  460. wifi_tx_len = 0;
  461. memset(wifi_tx_buf, 0, sizeof(wifi_tx_buf));
  462. // wifi_receive_clear();
  463. }
  464. else if(NULL != find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last, "OK\r\n"))
  465. {
  466. wifi_receive_clear();
  467. Van_Device_Printf(VAN_LOG_WIFI,"\n--> OK W %d", wifi_tx_len);
  468. WriteComm2((char *)wifi_tx_buf, wifi_tx_len+1);
  469. wifi_ack_delay = 3000;
  470. }
  471. else if(NULL != find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last, ">"))
  472. {
  473. wifi_receive_clear();
  474. Van_Device_Printf(VAN_LOG_WIFI,"\n--> > W %d", wifi_tx_len);
  475. WriteComm2((char *)wifi_tx_buf, wifi_tx_len+1);
  476. wifi_ack_delay = 3000;
  477. }
  478. if(NULL != find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last, "DISCONNECT"))
  479. {
  480. Van_Device_Printf(VAN_LOG_WIFI,"\nW--> %s", data);
  481. wifi_state = WIFI_POWEROFF;
  482. wifi_ack_delay = 0;
  483. }
  484. //收到PC数据
  485. p = find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last, "+IPD,0,");
  486. if(NULL != p)
  487. {
  488. int iProcLen = DecodeNew(&uart0_massage,uart0_data,g_wifi_rx_buf, &g_wifi_rx_head,g_wifi_rx_last,WIFI_RX_BUF_SIZE);
  489. if (iProcLen > 0) {
  490. sysinf.pc_channel_recv = PC_CONNECT_TYPE_WIFI;
  491. sysinf.pc_link_timeout = 0;
  492. uart0_massage.data = (char *)uart0_data;
  493. query_pc_cmd(&uart0_massage);
  494. wifi_receive_clear();
  495. }
  496. // wifi_receive_clear();
  497. // set_pc_chanel_recv(1);
  498. // b2b_recv_data(p, udp_data_len);
  499. }
  500. break;
  501. case WIFI_CLOSE:{
  502. if(NULL != find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last, "CLOSED\r\n"))
  503. {
  504. wifi_state = WIFI_LISTEN;
  505. wifi_ack_delay = 0;
  506. }
  507. }
  508. break;
  509. default:
  510. break;
  511. }
  512. if(WIFI_TRANS != wifi_state){
  513. wifi_receive_clear();
  514. }
  515. }
  516. void WIFI_send_deal(void)
  517. {
  518. static uint8_t try_count = 0;
  519. static uint8_t try_con = 0;
  520. static uint32_t wifi_start_delay = 0;
  521. static uint8_t res_count = 0;
  522. uint8_t wifi_result = 0;
  523. //启动超时
  524. if(wifi_state < WIFI_LISTEN && wifi_start_delay++ > 120*10)
  525. {
  526. wifi_start_delay = 0;
  527. wifi_state = WIFI_POWEROFF;
  528. wifi_ack_delay = 0;
  529. }
  530. //等待模块应答
  531. if(wifi_ack_delay > 0)
  532. {
  533. wifi_ack_delay -= 100;
  534. return;
  535. }
  536. if(resendtime >= MAX_RESEND_TIME){
  537. wifi_state = WIFI_POWEROFF;
  538. }
  539. if((WIFI_TRANS != wifi_state )|| ((WIFI_TRANS == wifi_state)&& wifi_tx_len )){
  540. resendtime++;
  541. }
  542. switch (wifi_state)
  543. {
  544. case WIFI_POWEROFF:
  545. case WIFI_ERROR:
  546. ESP32_AT_RST();
  547. wifi_start_delay = 0;
  548. resendtime = 0;
  549. try_count = 0;
  550. if(sysinf.connectTcpFlag){
  551. /*持续5次均不能链接到tcp,状态回退,重启链接udp*/
  552. res_count++;
  553. if(res_count > 5){
  554. sysinf.connectTcpFlag = 0;
  555. }
  556. }
  557. break;
  558. case WIFI_POWERON:
  559. ESP32_AT();
  560. break;
  561. case WIFI_INIT:
  562. ESP32_EchoOff();
  563. break;
  564. case WIFI_STATION:
  565. ESP32_Net_Mode_Choose(STA);
  566. break;
  567. case WIFI_DISAUTO:
  568. ESP32_DisAutoLink();
  569. try_count = 0;
  570. break;
  571. case WIFI_CWLAP:
  572. ESP32_CWLAP(sysinf.wifiname);
  573. if(try_count++ > 7)
  574. {
  575. wifi_state = WIFI_POWEROFF;
  576. if(wifi_set_ssid == 1)
  577. {
  578. wifi_set_ssid = 0;
  579. wifi_result = 0xff;
  580. data_send_to_pc(PC_CMD_SETWIFI, (char *)&wifi_result, 1);
  581. }
  582. }
  583. try_con = 0;
  584. break;
  585. case WIFI_LINK_AP:
  586. ESP32_JoinAP(sysinf.wifiname, sysinf.password,MAC_ADDR);
  587. if(try_con++ >= 5)
  588. {
  589. wifi_state = WIFI_POWEROFF;
  590. if(wifi_set_ssid == 1)
  591. {
  592. wifi_set_ssid = 0;
  593. wifi_result = 0xfe;
  594. data_send_to_pc(PC_CMD_SETWIFI, (char *)&wifi_result, 1);
  595. }
  596. }
  597. break;
  598. case WIFI_GET_IP:
  599. ESP32_GetIp();
  600. if(wifi_set_ssid == 1)
  601. {
  602. wifi_set_ssid = 0;
  603. wifi_result = 1;
  604. data_send_to_pc(PC_CMD_SETWIFI, (char *)&wifi_result, 1);
  605. }
  606. break;
  607. case WIFI_SETMUX:
  608. ESP32_Enable_MultipleId(1);
  609. break;
  610. case WIFI_LISTEN:
  611. if(sysinf.connectTcpFlag){
  612. if(resendtime < MAX_RESEND_TIME)
  613. {
  614. ESP32_Link_Server(enumTCP,sysinf.TCP_IP_Addr, sysinf.TCP_IP_PORT, Multiple_ID_0);
  615. }else{
  616. /*多次TCP未链接,链接UDP*/
  617. // connectTcpFlag = 0;
  618. // resendtime = 0;
  619. wifi_tx_len = 0;
  620. memset(wifi_tx_buf, 0, sizeof(wifi_tx_buf));
  621. }
  622. }else{
  623. if(resendtime < MAX_RESEND_TIME)
  624. {
  625. ESP32_Link_Server(enumUDP,User_ESP32_UDPServer_IP, User_ESP32_UDPServer_PORT, Multiple_ID_0);
  626. }
  627. }
  628. break;
  629. case WIFI_TRANS:
  630. if(resendtime < MAX_RESEND_TIME)
  631. {
  632. if(wifi_tx_len){
  633. char cStr [40] = {0};
  634. Van_Device_Printf(VAN_LOG_WIFI,"\nbuflen:%d %d %s",g_wifi_rx_head,g_wifi_rx_last, g_wifi_rx_buf);
  635. my_sprintf ( cStr,sizeof(cStr),"AT+CIPSEND=0,%d", wifi_tx_len );
  636. ESP32_Send_AT_Cmd ( cStr, 3000 );
  637. }
  638. }
  639. if(sysinf.connectTcpFlag){
  640. /*tcp链接上,复位计数清零*/
  641. res_count = 0;
  642. }
  643. break;
  644. case WIFI_CLOSE:
  645. ESP32_STOPTCP(Single_ID_0);
  646. break;
  647. default:
  648. wifi_state = WIFI_ERROR;
  649. break;
  650. }
  651. }
  652. //wifi任务
  653. void WIFI_process()
  654. {
  655. if(sysinf.pc_channel_recv == 2 && wifi_state > WIFI_SETMUX){
  656. /*串口状态下,不走wifi处理*/
  657. return;
  658. }
  659. WIFI_recv_deal();
  660. WIFI_send_deal();
  661. }
  662. static uint32_t m_pow_n(uint32_t m, int32_t n)
  663. {
  664. uint32_t i = 0, ret = 1;
  665. if (n < 0) return 0;
  666. for (i = 0; i < n; i++)
  667. {
  668. ret *= m;
  669. }
  670. return ret;
  671. }
  672. // 返回值为打印字符的个数
  673. // 支持%d,%o, %x,%s,%c,%f(只打印6位数字)
  674. int my_sprintf(char *pdes,unsigned int len, const char* str, ...)
  675. {
  676. if (str == NULL) return -1;
  677. uint32_t ret_num = 0;// 返回打印字符的个数
  678. char* pStr = (char*)str;// 指向str
  679. int ArgIntVal = 0; // 接收整型
  680. uint32_t ArgHexVal = 0;// 接十六进制
  681. char* ArgStrVal = NULL; // 接收字符型
  682. double ArgFloVal = 0.0; // 接受浮点型
  683. uint32_t val_seg = 0; // 数据切分
  684. uint32_t val_temp = 0; // 临时保存数据
  685. int cnt = 0; // 数据长度计数
  686. int cnt_float = 0; // 数据长度计数
  687. va_list pArgs; // 定义va_list类型指针,用于存储参数的地址
  688. va_start(pArgs, str); // 初始化pArgs
  689. while (*pStr != '\0')
  690. {
  691. switch (*pStr)
  692. {
  693. case ' ':
  694. case '\r':
  695. case '\n':
  696. *pdes++ = (*pStr); ret_num++; break;
  697. case '\t':
  698. *pdes++ =(*pStr); ret_num += 4; break;
  699. case '%':
  700. pStr++;
  701. // % 格式解析
  702. if('.' == *pStr)
  703. {
  704. pStr++;
  705. cnt_float = *pStr - '0';// %.Nf,输出指定位数小数
  706. pStr++;
  707. }
  708. switch (*pStr)
  709. {
  710. case '%':
  711. *pdes++ =('%');// %%,输出%
  712. ret_num++;
  713. pStr++;
  714. continue;
  715. case 'c':
  716. ArgIntVal = va_arg(pArgs, int);// %c,输出char
  717. *pdes++ =((char)ArgIntVal);
  718. ret_num++;
  719. pStr++;
  720. continue;
  721. case 'd':
  722. // 接收整型
  723. ArgIntVal = va_arg(pArgs, int);
  724. if (ArgIntVal < 0)// 如果为负数打印,负号
  725. {
  726. ArgIntVal = -ArgIntVal;// 取相反数
  727. *pdes++ =('-');
  728. ret_num++;
  729. }
  730. val_seg = ArgIntVal;// 赋值给 val_seg处理数据
  731. // 计算ArgIntVal长度
  732. if (ArgIntVal)
  733. {
  734. while (val_seg) {
  735. cnt++;
  736. val_seg /= 10;
  737. }
  738. }
  739. else cnt = 1;// 数字0的长度为1
  740. ret_num += cnt;// 字符个数加上整数的长度
  741. // 将整数转为单个字符打印
  742. while (cnt)
  743. {
  744. val_seg = ArgIntVal / m_pow_n(10, cnt - 1);
  745. ArgIntVal %= m_pow_n(10, cnt - 1);
  746. *pdes++ =((char)val_seg + '0');
  747. cnt--;
  748. }
  749. pStr++;
  750. continue;
  751. case 'o':
  752. // 接收整型
  753. ArgIntVal = va_arg(pArgs, int);
  754. if (ArgIntVal < 0)// 如果为负数打印,负号
  755. {
  756. ArgIntVal = -ArgIntVal;// 取相反数
  757. *pdes++ =('-');
  758. ret_num++;
  759. }
  760. val_seg = ArgIntVal;// 赋值给 val_seg处理数据
  761. // 计算ArgIntVal长度
  762. if (ArgIntVal)
  763. {
  764. while (val_seg) {
  765. cnt++;
  766. val_seg /= 8;
  767. }
  768. }
  769. else cnt = 1;// 数字0的长度为1
  770. ret_num += cnt;// 字符个数加上整数的长度
  771. // 将整数转为单个字符打印
  772. while (cnt)
  773. {
  774. val_seg = ArgIntVal / m_pow_n(8, cnt - 1);
  775. ArgIntVal %= m_pow_n(8, cnt - 1);
  776. *pdes++ =((char)val_seg + '0');
  777. cnt--;
  778. }
  779. pStr++;
  780. continue;
  781. case 'x':
  782. // 接收16进制
  783. ArgHexVal = va_arg(pArgs, uint32_t);
  784. val_seg = ArgHexVal;
  785. // 计算ArgIntVal长度
  786. if (ArgHexVal)
  787. {
  788. while (val_seg) {
  789. cnt++;
  790. val_seg /= 16;
  791. }
  792. }
  793. else cnt = 1;// 数字0的长度为1
  794. ret_num += cnt;// 字符个数加上整数的长度
  795. // 将整数转为单个字符打印
  796. while (cnt)
  797. {
  798. val_seg = ArgHexVal / m_pow_n(16, cnt - 1);
  799. ArgHexVal %= m_pow_n(16, cnt - 1);
  800. if (val_seg <= 9)
  801. *pdes++ =((char)val_seg + '0');
  802. else
  803. {
  804. //*pdes++ =((char)val_seg - 10 + 'a'); //小写字母
  805. *pdes++ =((char)val_seg - 10 + 'A');
  806. }
  807. cnt--;
  808. }
  809. pStr++;
  810. continue;
  811. case 'b':
  812. // 接收整型
  813. ArgIntVal = va_arg(pArgs, int);
  814. val_seg = ArgIntVal;
  815. // 计算ArgIntVal长度
  816. if (ArgIntVal)
  817. {
  818. while (val_seg) {
  819. cnt++;
  820. val_seg /= 2;
  821. }
  822. }
  823. else cnt = 1;// 数字0的长度为1
  824. ret_num += cnt;// 字符个数加上整数的长度
  825. // 将整数转为单个字符打印
  826. while (cnt)
  827. {
  828. val_seg = ArgIntVal / m_pow_n(2, cnt - 1);
  829. ArgIntVal %= m_pow_n(2, cnt - 1);
  830. *pdes++ =((char)val_seg + '0');
  831. cnt--;
  832. }
  833. pStr++;
  834. continue;
  835. case 's':
  836. // 接收字符
  837. ArgStrVal = va_arg(pArgs, char*);
  838. ret_num += (uint32_t)strlen(ArgStrVal);
  839. while (*ArgStrVal)
  840. {
  841. *pdes++ =(*ArgStrVal);
  842. ArgStrVal++;
  843. }
  844. pStr++;
  845. continue;
  846. case 'f':
  847. // 接收浮点型 保留6为小数,不采取四舍五入
  848. ArgFloVal = va_arg(pArgs, double);
  849. val_seg = (uint32_t)ArgFloVal;// 取整数部分
  850. val_temp = val_seg; // 临时保存整数部分数据
  851. ArgFloVal = ArgFloVal - val_seg;// 得出余下的小数部分
  852. // 计算整数部分长度
  853. if (val_seg)
  854. {
  855. while (val_seg) {
  856. cnt++;
  857. val_seg /= 10;
  858. }
  859. }
  860. else cnt = 1;// 数字0的长度为1
  861. ret_num += cnt;// 字符个数加上整数的长度
  862. // 将整数转为单个字符打印
  863. while (cnt)
  864. {
  865. val_seg = val_temp / m_pow_n(10, cnt - 1);
  866. val_temp %= m_pow_n(10, cnt - 1);
  867. *pdes++ =((char)val_seg + '0');
  868. cnt--;
  869. }
  870. // 打印小数点
  871. *pdes++ =('.');
  872. ret_num++;
  873. // printf("\r\n %f\r\n", ArgFloVal);
  874. if(cnt_float == 0)
  875. cnt_float = 6;
  876. // 开始输出小数部分
  877. //ArgFloVal *= 1000000;
  878. ArgFloVal *= m_pow_n(10, cnt_float);
  879. val_temp = (int)ArgFloVal;// 取整数部分
  880. while (cnt_float)
  881. {
  882. val_seg = val_temp / m_pow_n(10, cnt_float - 1);
  883. val_temp %= m_pow_n(10, cnt_float - 1);
  884. *pdes++ =((char)val_seg + '0');
  885. cnt_float--;
  886. }
  887. ret_num += 6;
  888. pStr++;
  889. continue;
  890. default:// % 匹配错误,暂输出空格
  891. *pdes++ =(' '); ret_num++;
  892. break;
  893. }
  894. break;
  895. default:
  896. *pdes++ =(*pStr); ret_num++;
  897. break;
  898. }
  899. pStr++;
  900. if(ret_num > len){
  901. break;
  902. }
  903. }
  904. va_end(pArgs);// 结束取参数
  905. *pdes++ = 0;
  906. return ret_num;
  907. }