尝试将ESP8266连接到 WiFi AP。源代码如下:
user_init:
void user_init(void)
{
int i = 0;
uart_div_modify(0,UART_CLK_FREQ/115200);
for (i = 0; i < 5; i++) {
os_printf("Wait %d...n", 5-i);
sys_msleep(1000);
}
if (wifi_get_opmode() != STA
tiON_MODE) {
os_printf("Setting ESP to STATION moden");
wifi_set_opmode(STATION_MODE);
}
if (wifi_get_opmode() == STATION_MODE) {
os_printf("ESP 处于 STATION 模式n");
wifi_station_get_config(&config);
memset(config.ssid, 0, sizeof(config.ssid));
memset(config.password, 0, sizeof(config.password));
sprintf(config.ssid, "%s", DEMO_AP_SSID);
sprintf(config.password, "%s", DEMO_AP_PASSWORD);
wifi_station_set_config(&config);
sys_msleep(100);
wifi_station_get_config(&config);
os_printf("OPMODE: %u, SSID: %s, PASS: %sn", wifi_get_opmode(), config.ssid, config.password);
}
os_printf("创建新线程以检查连接n");
xTaskCreate(checkWiFiConnection, "myTask", 256, NULL, 2, NULL);
os_printf("已创建任务n");
while(1) {
os_printf("在user_initn");
sys_msleep(1000);
}
}
checkwifi连接:
void checkWiFiConnection(void *pvParameters) {
os_printf("输入了用于检查WIFI连接的新线程n");
os_timer_disarm(&wifiConnectionFuncTimer);
os_timer_setfn(&wifiConnectionFuncTimer, (os_timer_func_t *)checkwifi连接func, 0);
os_timer_arm(&wifiConnectionFuncTimer, 1000, 0);
while(endTaskWifiConnection != 100) {
os_printf("In checking wifi connection %dn", endTaskWifiConnection);
sys_msleep(1000);
}
os_printf("Delete thread for checking WIFI connectionn");
vTaskDelete(NULL);
}
checkwifi连接func
无效 checkwifi连接func() {
os_printf("Entered checking WiFi connection Function - %dn", endTaskWifiConnection);
struct ip_info ipConfig;
endTaskWifiConnection = endTaskWifiConnection + 1;
os_timer_disarm(&wifiConnectionFuncTimer);
wifi_get_ip_info(STATION_IF, &ipConfig);
if (wifi_station_get_connect_status() == STATION_GOT_IP && ipConfig.ip.addr != 0)
{
os_printf("WiFi connectedrn");
os_printf("Start TCP connecting...n");
// Отправляем данные на ПК
//senddata();
}
else
{
os_printf("WiFi 已断开连接n");
wifi_station_get_config(&config);
os_printf("OPMODE: %u, SSID: %s, PASS: %sn", wifi_get_opmode(), config.ssid, config.password);
os_printf("尝试连接到 WiFi 网络n");
uint8 state = wifi_station_connect();
if (state) {
os_printf("Connected OK. WiFi is now activen");
}
else
os_printf("连接失败n");
if(wifi_station_get_connect_status() == STATION_WRONG_PASSWORD)
{
os_printf("WiFi connecting error, wrong passwordrn");
}
else if(wifi_station_get_connect_status() == STATION_NO_AP_FOUND)
{
os_printf("WiFi connecting error, ap not foundrn");
}
else if(wifi_station_get_connect_status() == STATION_CONNECT_FAIL)
{
os_printf("WiFi connecting failrn");
}
else
{
os_printf("WiFi连接...rn");
}
}
os_timer_setfn(&wifiConnectionFuncTimer, (os_timer_func_t *)checkwifi连接func, 0);
os_timer_arm(&wifiConnectionFuncTimer, 1000, 0);
}
很遗憾,wifi没有连接。以下是日志:
"
等 4...
等待 3...
等待 2...
等待 1...
ESP 处于 STATION 模式
操作模式:1,SSID:ESPTEST,通过:12345678
创建新线程以检查连接
已创建任务
在user_init
输入了用于检查WIFI连接的新线程
在检查 wifi 连接时 0
在user_init
在检查 wifi 连接时 0
已输入检查WiFi连接功能 - 0
WiFi 已断开连接
操作模式:1,SSID:ESPTEST,通过:12345678
尝试连接到 WiFi 网络
连接失败
WiFi连接...
在user_init
检查 wifi 连接 1
输入检查WiFi连接功能 - 1
WiFi 已断开连接
操作模式:1,SSID:ESPTEST,通过:12345678
尝试连接到 WiFi 网络
连接失败
WiFi连接...
在user_init
"
请帮我知道连接失败的原因是什么?
谢谢。