#include "atcom.h" int wifi_set_state = 0; int connect_state = 0; char wifiname[100] = "ZTE-JSGSDT\0"; char wifipassword[100] = "Ss1@3456\0"; char myip[100] = "192.168.1.180\0"; char hisip[100] = "192.168.1.83\0"; char port[100] = "12399\0"; void at_command_send(uint8_t *Tx_Buf) { int size = strlen((const char*)Tx_Buf); uart_send(&huart6, Tx_Buf, size); } void at_send(uint8_t *Tx_Buf,int size) { uart_send(&huart6, Tx_Buf, size); } bool chaxun_flag = false; bool first_flag = false; void wifi_set(void) { switch(wifi_set_state) { case 0://待机状态,查询WIFI连接结果,开机自动连接 { if(_memcmp(array1,(const uint8_t*)"AT+CWSTATE?",11) == 0) { if(array1[22] == '0') connect_state = 0; //ESP32-C6 station 尚未进行任何 Wi-Fi 连接 else if(array1[22] == '1') connect_state = 1; //ESP32-C6 station 已经连接上 AP,但尚未获取到 IPv4 地址 else if(array1[22] == '2') connect_state = 2; //ESP32-C6 station 已经连接上 AP,并已经获取到 IPv4 地址 else if(array1[22] == '3') connect_state = 3; //ESP32-C6 station 正在进行 Wi-Fi 连接或 Wi-Fi 重连 else if(array1[22] == '4') connect_state = 4; //ESP32-C6 station 处于 Wi-Fi 断开状态 if(connect_state != 2) wifi_set_state = 11; else { if(!first_flag) wifi_set_state = 14; } } if(!chaxun_flag) { at_command_send((uint8_t *)"AT+SLEEP=0\r\n"); chaxun_flag = true; wifi_set_state++; } } break; case 1://连接WIFI前先退出透传模式 { at_command_send((uint8_t *)"+++"); wifi_set_state++; } break; case 2://等待 { wifi_set_state++; } break; case 3://重置芯片 { at_command_send((uint8_t *)"AT+RST\r\n"); wifi_set_state++; } break; case 4://设置 Wi-Fi 模式 { at_command_send((uint8_t *)"AT+CWMODE=1\r\n");//设置 Wi-Fi 模式为 STA wifi_set_state++; } break; case 5://修改wifi协议 { at_command_send((uint8_t *)"AT+CWSTAPROTO=31\r\n");//修改wifi协议,WIFI6 wifi_set_state++; } break; case 6://连接WIFI { int a = strlen(wifiname); int b = strlen(wifipassword); int c = 10 + a + 3 + b + 3; char wifi_str[100] = "AT+CWJAP=\""; strcat(wifi_str,wifiname); strcat(wifi_str,"\",\""); strcat(wifi_str,wifipassword); strcat(wifi_str,"\"\r\n"); at_send((uint8_t *)wifi_str,c);//设置 Wi-Fi 名称和密码 delay_ms(1000); wifi_set_state++; } break; case 7://判断是否连接成功,失败再次连接 { if(array1[15] == 'O' && array1[16] == 'K') wifi_set_state++; else { int a = strlen(wifiname); int b = strlen(wifipassword); int c = 10 + a + 3 + b + 3; char wifi_str[100] = "AT+CWJAP=\""; strcat(wifi_str,wifiname); strcat(wifi_str,"\",\""); strcat(wifi_str,wifipassword); strcat(wifi_str,"\"\r\n"); at_send((uint8_t *)wifi_str,c);//设置 Wi-Fi 名称和密码 delay_ms(1000); wifi_set_state++; } } break; case 8://DHCP { at_command_send((uint8_t *)"AT+CWDHCP=1,1\r\n");//开启DHCP,AT+CWDHCP=1,1 wifi_set_state++; } break; case 9://查询IP地址 { at_command_send((uint8_t *)"AT+CIPSTA?\r\n"); wifi_set_state++; } break; case 10://判断IP地址是否获取,失败再次查询 { if(array1[23] == '"') { for(int i = 0;i < 99;i++) { if(array1[24+i] == '"') { myip[i] = '\0'; i = 100; } else { myip[i] = array1[24 + i]; } } wifi_set_state++; } else { at_command_send((uint8_t *)"AT+CIPSTA?\r\n"); } } break; case 11://查询WIFI连接状态 { at_command_send((uint8_t *)"AT+CWSTATE?\r\n"); wifi_set_state = 0; } break; case 12://退出透传 { at_command_send((uint8_t *)"+++");//退出透传 wifi_set_state++; first_flag = true; } break; case 13://断开之前的TCP连接 { at_command_send((uint8_t *)"AT+CIPCLOSE\r\n");//断开TCP连接 wifi_set_state++; } break; case 14://连接服务器 { int a = strlen(hisip); int b = strlen(port); int c = 19 + a + 2 + b + 2; char server_str[100] = "AT+CIPSTART=\"TCP\",\""; strcat(server_str,hisip); strcat(server_str,"\","); strcat(server_str,port); strcat(server_str,"\r\n"); at_send((uint8_t *)server_str,c);//设置要连接的服务端IP和端口 delay_ms(1000); wifi_set_state++; } break; case 15://等待 { wifi_set_state++; } break; case 16://传输延迟 { at_command_send((uint8_t *)"AT+TRANSINTVL=0\r\n");//设置透传模式下收到数据后立即发送 wifi_set_state++; } break; case 17://接收透传 { at_command_send((uint8_t *)"AT+CIPMODE=1\r\n");//进入 UART Wi-Fi 透传接收模式 wifi_set_state++; } break; case 18://发送透传 { at_command_send((uint8_t *)"AT+CIPSEND\r\n");//进入 UART Wi-Fi 透传模式 并发送数据 wifi_set_state = 0;; } break; case 19://查询WIFI协议 { at_command_send((uint8_t *)"AT+CWSTAPROTO?\r\n");//查询Station 模式下 802.11 b/g/n 协议标准 wifi_set_state = 0;; } break; } } void at_callback(void) { }