#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; void wifi_set(void) { switch(wifi_set_state) { case 0: { 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; } if(!chaxun_flag) { at_command_send((uint8_t *)"AT+SLEEP=0\r\n"); chaxun_flag = true; wifi_set_state++; } } break; case 1: { at_command_send((uint8_t *)"+++"); wifi_set_state++; } break; case 2: { at_command_send((uint8_t *)"AT+RESTORE\r\n"); wifi_set_state++; } break; case 3: { at_command_send((uint8_t *)"AT+RST\r\n"); wifi_set_state++; } break; case 4: { at_command_send((uint8_t *)"AT+CWMODE=1\r\n");//设置 Wi-Fi 模式为 STA wifi_set_state++; } break; case 5: { at_command_send((uint8_t *)"AT+CWSTAPROTO=31\r\n"); wifi_set_state++; } break; case 6: { 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: { at_command_send((uint8_t *)"AT+CWDHCP=1,1\r\n");//开启DHCP,AT+CWDHCP=1,1 wifi_set_state++; } break; case 9: { at_command_send((uint8_t *)"AT+CIPSTA?\r\n"); wifi_set_state++; } break; case 10: { 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: { at_command_send((uint8_t *)"AT+CWSTATE?\r\n"); wifi_set_state = 0; } break; case 12: { at_command_send((uint8_t *)"+++");//退出透传 wifi_set_state++; } break; case 13: { 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++; // if(array1[11] == 'O' && array1[12] == 'K') // { // wifi_set_state++; // } // else // { // wifi_set_state = 13; // 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和端口 // 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: { at_command_send((uint8_t *)"AT+CWSTAPROTO?\r\n");//进入 UART Wi-Fi 透传模式 并发送数据 wifi_set_state = 0;; } break; } } void at_callback(void) { }