ESP32_WROOM_32D.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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[128] = {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 [128];
  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. memset(wifi_tx_buf, 0, sizeof(wifi_tx_buf));
  390. wifi_tx_len = 0;
  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. memset(wifi_tx_buf, 0, sizeof(wifi_tx_buf));
  461. wifi_tx_len = 0;
  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. /* 过滤其他设备的UDP广播: 命令为PC_CMD_SETIP且数据以"Mini7"开头(本机send_Devinf_pc广播的设备信息),
  490. 说明是同类设备发出的广播而非上位机下发的SETIP,丢弃不做后续处理 */
  491. if(iProcLen > 0 && PC_CMD_SETIP == uart0_massage.cmd &&
  492. strncmp((const char *)uart0_data, "Mini7", 5) == 0)
  493. {
  494. Van_Device_Printf(VAN_LOG_WIFI, "\n[WiFi] filter peer device broadcast");
  495. wifi_receive_clear();
  496. iProcLen = 0; /* 标记为已过滤,跳过后续PC命令处理 */
  497. }
  498. if (iProcLen > 0) {
  499. sysinf.pc_channel_recv = PC_CONNECT_TYPE_WIFI;
  500. sysinf.pc_link_timeout = 0;
  501. uart0_massage.data = (char *)uart0_data;
  502. query_pc_cmd(&uart0_massage);
  503. wifi_receive_clear();
  504. }
  505. // wifi_receive_clear();
  506. // set_pc_chanel_recv(1);
  507. // b2b_recv_data(p, udp_data_len);
  508. }
  509. break;
  510. case WIFI_CLOSE:{
  511. if(NULL != find_str_loop(g_wifi_rx_buf, WIFI_RX_BUF_SIZE ,&g_wifi_rx_head, g_wifi_rx_last, "CLOSED\r\n"))
  512. {
  513. wifi_state = WIFI_LISTEN;
  514. wifi_ack_delay = 0;
  515. }
  516. }
  517. break;
  518. default:
  519. break;
  520. }
  521. if(WIFI_TRANS != wifi_state){
  522. wifi_receive_clear();
  523. }
  524. }
  525. void WIFI_send_deal(void)
  526. {
  527. static uint8_t try_count = 0;
  528. static uint8_t try_con = 0;
  529. static uint32_t wifi_start_delay = 0;
  530. static uint8_t res_count = 0;
  531. uint8_t wifi_result = 0;
  532. //启动超时
  533. if(wifi_state < WIFI_LISTEN && wifi_start_delay++ > 120*10)
  534. {
  535. wifi_start_delay = 0;
  536. wifi_state = WIFI_POWEROFF;
  537. wifi_ack_delay = 0;
  538. }
  539. //等待模块应答
  540. if(wifi_ack_delay > 0)
  541. {
  542. wifi_ack_delay -= 100;
  543. return;
  544. }
  545. if(resendtime >= MAX_RESEND_TIME){
  546. wifi_state = WIFI_POWEROFF;
  547. }
  548. if((WIFI_TRANS != wifi_state )|| ((WIFI_TRANS == wifi_state)&& wifi_tx_len )){
  549. resendtime++;
  550. }
  551. switch (wifi_state)
  552. {
  553. case WIFI_POWEROFF:
  554. case WIFI_ERROR:
  555. ESP32_AT_RST();
  556. wifi_start_delay = 0;
  557. resendtime = 0;
  558. try_count = 0;
  559. if(sysinf.connectTcpFlag){
  560. /*持续5次均不能链接到tcp,状态回退,重启链接udp*/
  561. res_count++;
  562. if(res_count > 5){
  563. sysinf.connectTcpFlag = 0;
  564. }
  565. }
  566. break;
  567. case WIFI_POWERON:
  568. ESP32_AT();
  569. break;
  570. case WIFI_INIT:
  571. ESP32_EchoOff();
  572. break;
  573. case WIFI_STATION:
  574. ESP32_Net_Mode_Choose(STA);
  575. break;
  576. case WIFI_DISAUTO:
  577. ESP32_DisAutoLink();
  578. try_count = 0;
  579. resendtime = 0;
  580. break;
  581. case WIFI_CWLAP:
  582. ESP32_CWLAP(sysinf.wifiname);
  583. try_count++;
  584. if(try_count == MAX_RESEND_TIME)
  585. {
  586. wifi_state = WIFI_POWEROFF;
  587. if(wifi_set_ssid == 1)
  588. {
  589. wifi_set_ssid = 0;
  590. wifi_result = 0xff;
  591. data_send_to_pc(PC_CMD_SETWIFI, (char *)&wifi_result, 1);
  592. }
  593. Van_Device_Printf(VAN_LOG_ERROR, "no wifi AP:%s\n\r",sysinf.wifiname);
  594. }
  595. try_con = 0;
  596. resendtime = 0;
  597. break;
  598. case WIFI_LINK_AP:
  599. ESP32_JoinAP(sysinf.wifiname, sysinf.password,MAC_ADDR);
  600. try_con++ ;
  601. if(try_con == MAX_RESEND_TIME)
  602. {
  603. wifi_state = WIFI_POWEROFF;
  604. if(wifi_set_ssid == 1)
  605. {
  606. wifi_set_ssid = 0;
  607. wifi_result = 0xfe;
  608. data_send_to_pc(PC_CMD_SETWIFI, (char *)&wifi_result, 1);
  609. }
  610. Van_Device_Printf(VAN_LOG_ERROR, "can't jonn AP:%s %s\n\r",sysinf.wifiname,sysinf.password);
  611. }
  612. break;
  613. case WIFI_GET_IP:
  614. ESP32_GetIp();
  615. if(wifi_set_ssid == 1)
  616. {
  617. wifi_set_ssid = 0;
  618. wifi_result = 1;
  619. data_send_to_pc(PC_CMD_SETWIFI, (char *)&wifi_result, 1);
  620. }
  621. break;
  622. case WIFI_SETMUX:
  623. ESP32_Enable_MultipleId(1);
  624. break;
  625. case WIFI_LISTEN:
  626. if(sysinf.connectTcpFlag){
  627. if(resendtime < MAX_RESEND_TIME)
  628. {
  629. ESP32_Link_Server(enumTCP,sysinf.TCP_IP_Addr, sysinf.TCP_IP_PORT, Multiple_ID_0);
  630. }else{
  631. /*多次TCP未链接,链接UDP*/
  632. // connectTcpFlag = 0;
  633. // resendtime = 0;
  634. memset(wifi_tx_buf, 0, sizeof(wifi_tx_buf));
  635. wifi_tx_len = 0;
  636. }
  637. }else{
  638. if(resendtime < MAX_RESEND_TIME)
  639. {
  640. ESP32_Link_Server(enumUDP,User_ESP32_UDPServer_IP, User_ESP32_UDPServer_PORT, Multiple_ID_0);
  641. }
  642. }
  643. break;
  644. case WIFI_TRANS:
  645. if(resendtime < MAX_RESEND_TIME)
  646. {
  647. if(wifi_tx_len){
  648. char cStr [40] = {0};
  649. Van_Device_Printf(VAN_LOG_WIFI,"\nbuflen:%d %d %s",g_wifi_rx_head,g_wifi_rx_last, g_wifi_rx_buf);
  650. my_sprintf ( cStr,sizeof(cStr),"AT+CIPSEND=0,%d", wifi_tx_len );
  651. ESP32_Send_AT_Cmd ( cStr, 3000 );
  652. }
  653. }
  654. if(sysinf.connectTcpFlag){
  655. /*tcp链接上,复位计数清零*/
  656. res_count = 0;
  657. }
  658. break;
  659. case WIFI_CLOSE:
  660. ESP32_STOPTCP(Single_ID_0);
  661. break;
  662. default:
  663. wifi_state = WIFI_ERROR;
  664. break;
  665. }
  666. }
  667. //wifi任务
  668. void WIFI_process()
  669. {
  670. if(sysinf.pc_channel_recv == 2 && wifi_state > WIFI_SETMUX){
  671. /*串口状态下,不走wifi处理*/
  672. return;
  673. }
  674. WIFI_recv_deal();
  675. WIFI_send_deal();
  676. }
  677. static uint32_t m_pow_n(uint32_t m, int32_t n)
  678. {
  679. uint32_t i = 0, ret = 1;
  680. if (n < 0) return 0;
  681. for (i = 0; i < n; i++)
  682. {
  683. ret *= m;
  684. }
  685. return ret;
  686. }
  687. // 返回值为打印字符的个数
  688. // 支持%d,%o, %x,%s,%c,%f(只打印6位数字)
  689. int my_sprintf(char *pdes,unsigned int len, const char* str, ...)
  690. {
  691. if (str == NULL) return -1;
  692. uint32_t ret_num = 0;// 返回打印字符的个数
  693. char* pStr = (char*)str;// 指向str
  694. int ArgIntVal = 0; // 接收整型
  695. uint32_t ArgHexVal = 0;// 接十六进制
  696. char* ArgStrVal = NULL; // 接收字符型
  697. double ArgFloVal = 0.0; // 接受浮点型
  698. uint32_t val_seg = 0; // 数据切分
  699. uint32_t val_temp = 0; // 临时保存数据
  700. int cnt = 0; // 数据长度计数
  701. int cnt_float = 0; // 数据长度计数
  702. va_list pArgs; // 定义va_list类型指针,用于存储参数的地址
  703. va_start(pArgs, str); // 初始化pArgs
  704. while (*pStr != '\0')
  705. {
  706. switch (*pStr)
  707. {
  708. case ' ':
  709. case '\r':
  710. case '\n':
  711. *pdes++ = (*pStr); ret_num++; break;
  712. case '\t':
  713. *pdes++ =(*pStr); ret_num += 4; break;
  714. case '%':
  715. pStr++;
  716. // % 格式解析
  717. if('.' == *pStr)
  718. {
  719. pStr++;
  720. cnt_float = *pStr - '0';// %.Nf,输出指定位数小数
  721. pStr++;
  722. }
  723. switch (*pStr)
  724. {
  725. case '%':
  726. *pdes++ =('%');// %%,输出%
  727. ret_num++;
  728. pStr++;
  729. continue;
  730. case 'c':
  731. ArgIntVal = va_arg(pArgs, int);// %c,输出char
  732. *pdes++ =((char)ArgIntVal);
  733. ret_num++;
  734. pStr++;
  735. continue;
  736. case 'd':
  737. // 接收整型
  738. ArgIntVal = va_arg(pArgs, int);
  739. if (ArgIntVal < 0)// 如果为负数打印,负号
  740. {
  741. ArgIntVal = -ArgIntVal;// 取相反数
  742. *pdes++ =('-');
  743. ret_num++;
  744. }
  745. val_seg = ArgIntVal;// 赋值给 val_seg处理数据
  746. // 计算ArgIntVal长度
  747. if (ArgIntVal)
  748. {
  749. while (val_seg) {
  750. cnt++;
  751. val_seg /= 10;
  752. }
  753. }
  754. else cnt = 1;// 数字0的长度为1
  755. ret_num += cnt;// 字符个数加上整数的长度
  756. // 将整数转为单个字符打印
  757. while (cnt)
  758. {
  759. val_seg = ArgIntVal / m_pow_n(10, cnt - 1);
  760. ArgIntVal %= m_pow_n(10, cnt - 1);
  761. *pdes++ =((char)val_seg + '0');
  762. cnt--;
  763. }
  764. pStr++;
  765. continue;
  766. case 'o':
  767. // 接收整型
  768. ArgIntVal = va_arg(pArgs, int);
  769. if (ArgIntVal < 0)// 如果为负数打印,负号
  770. {
  771. ArgIntVal = -ArgIntVal;// 取相反数
  772. *pdes++ =('-');
  773. ret_num++;
  774. }
  775. val_seg = ArgIntVal;// 赋值给 val_seg处理数据
  776. // 计算ArgIntVal长度
  777. if (ArgIntVal)
  778. {
  779. while (val_seg) {
  780. cnt++;
  781. val_seg /= 8;
  782. }
  783. }
  784. else cnt = 1;// 数字0的长度为1
  785. ret_num += cnt;// 字符个数加上整数的长度
  786. // 将整数转为单个字符打印
  787. while (cnt)
  788. {
  789. val_seg = ArgIntVal / m_pow_n(8, cnt - 1);
  790. ArgIntVal %= m_pow_n(8, cnt - 1);
  791. *pdes++ =((char)val_seg + '0');
  792. cnt--;
  793. }
  794. pStr++;
  795. continue;
  796. case 'x':
  797. // 接收16进制
  798. ArgHexVal = va_arg(pArgs, uint32_t);
  799. val_seg = ArgHexVal;
  800. // 计算ArgIntVal长度
  801. if (ArgHexVal)
  802. {
  803. while (val_seg) {
  804. cnt++;
  805. val_seg /= 16;
  806. }
  807. }
  808. else cnt = 1;// 数字0的长度为1
  809. ret_num += cnt;// 字符个数加上整数的长度
  810. // 将整数转为单个字符打印
  811. while (cnt)
  812. {
  813. val_seg = ArgHexVal / m_pow_n(16, cnt - 1);
  814. ArgHexVal %= m_pow_n(16, cnt - 1);
  815. if (val_seg <= 9)
  816. *pdes++ =((char)val_seg + '0');
  817. else
  818. {
  819. //*pdes++ =((char)val_seg - 10 + 'a'); //小写字母
  820. *pdes++ =((char)val_seg - 10 + 'A');
  821. }
  822. cnt--;
  823. }
  824. pStr++;
  825. continue;
  826. case 'b':
  827. // 接收整型
  828. ArgIntVal = va_arg(pArgs, int);
  829. val_seg = ArgIntVal;
  830. // 计算ArgIntVal长度
  831. if (ArgIntVal)
  832. {
  833. while (val_seg) {
  834. cnt++;
  835. val_seg /= 2;
  836. }
  837. }
  838. else cnt = 1;// 数字0的长度为1
  839. ret_num += cnt;// 字符个数加上整数的长度
  840. // 将整数转为单个字符打印
  841. while (cnt)
  842. {
  843. val_seg = ArgIntVal / m_pow_n(2, cnt - 1);
  844. ArgIntVal %= m_pow_n(2, cnt - 1);
  845. *pdes++ =((char)val_seg + '0');
  846. cnt--;
  847. }
  848. pStr++;
  849. continue;
  850. case 's':
  851. // 接收字符
  852. ArgStrVal = va_arg(pArgs, char*);
  853. ret_num += (uint32_t)strlen(ArgStrVal);
  854. while (*ArgStrVal)
  855. {
  856. *pdes++ =(*ArgStrVal);
  857. ArgStrVal++;
  858. }
  859. pStr++;
  860. continue;
  861. case 'f':
  862. // 接收浮点型 保留6为小数,不采取四舍五入
  863. ArgFloVal = va_arg(pArgs, double);
  864. val_seg = (uint32_t)ArgFloVal;// 取整数部分
  865. val_temp = val_seg; // 临时保存整数部分数据
  866. ArgFloVal = ArgFloVal - val_seg;// 得出余下的小数部分
  867. // 计算整数部分长度
  868. if (val_seg)
  869. {
  870. while (val_seg) {
  871. cnt++;
  872. val_seg /= 10;
  873. }
  874. }
  875. else cnt = 1;// 数字0的长度为1
  876. ret_num += cnt;// 字符个数加上整数的长度
  877. // 将整数转为单个字符打印
  878. while (cnt)
  879. {
  880. val_seg = val_temp / m_pow_n(10, cnt - 1);
  881. val_temp %= m_pow_n(10, cnt - 1);
  882. *pdes++ =((char)val_seg + '0');
  883. cnt--;
  884. }
  885. // 打印小数点
  886. *pdes++ =('.');
  887. ret_num++;
  888. // printf("\r\n %f\r\n", ArgFloVal);
  889. if(cnt_float == 0)
  890. cnt_float = 6;
  891. // 开始输出小数部分
  892. //ArgFloVal *= 1000000;
  893. ArgFloVal *= m_pow_n(10, cnt_float);
  894. val_temp = (int)ArgFloVal;// 取整数部分
  895. while (cnt_float)
  896. {
  897. val_seg = val_temp / m_pow_n(10, cnt_float - 1);
  898. val_temp %= m_pow_n(10, cnt_float - 1);
  899. *pdes++ =((char)val_seg + '0');
  900. cnt_float--;
  901. }
  902. ret_num += 6;
  903. pStr++;
  904. continue;
  905. default:// % 匹配错误,暂输出空格
  906. *pdes++ =(' '); ret_num++;
  907. break;
  908. }
  909. break;
  910. default:
  911. *pdes++ =(*pStr); ret_num++;
  912. break;
  913. }
  914. pStr++;
  915. if(ret_num > len){
  916. break;
  917. }
  918. }
  919. va_end(pArgs);// 结束取参数
  920. *pdes++ = 0;
  921. return ret_num;
  922. }