ESP32_WROOM_32D.c 28 KB

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