Could you please help. Trying to connect ESP8266 to WiFi AP. Here is the source code :
user_init:
Code: Select all
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() != STATION_MODE) {
os_printf("Setting ESP to STATION mode\n");
wifi_set_opmode(STATION_MODE);
}
if (wifi_get_opmode() == STATION_MODE) {
os_printf("ESP is in STATION mode\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: %s\n", wifi_get_opmode(), config.ssid, config.password);
}
os_printf("Creating new thread to check connection\n");
xTaskCreate(checkWiFiConnection, "myTask", 256, NULL, 2, NULL);
os_printf("Created task\n");
while(1) {
os_printf("In user_init\n");
sys_msleep(1000);
}
}
checkWiFiConnection:
Code: Select all
void checkWiFiConnection(void *pvParameters) {
os_printf("Entered new thread for checking WIFI connection\n");
os_timer_disarm(&wifiConnectionFuncTimer);
os_timer_setfn(&wifiConnectionFuncTimer, (os_timer_func_t *)checkWiFiConnectionFunc, 0);
os_timer_arm(&wifiConnectionFuncTimer, 1000, 0);
while(endTaskWifiConnection != 100) {
os_printf("In checking wifi connection %d\n", endTaskWifiConnection);
sys_msleep(1000);
}
os_printf("Delete thread for checking WIFI connection\n");
vTaskDelete(NULL);
}
checkWiFiConnectionFunc
Code: Select all
void checkWiFiConnectionFunc() {
os_printf("Entered checking WiFi connection Function - %d\n", 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 connected\r\n");
os_printf("Start TCP connecting...\n");
// Отправляем данные на ПК
//senddata();
}
else
{
os_printf("WiFi is disconnected\n");
wifi_station_get_config(&config);
os_printf("OPMODE: %u, SSID: %s, PASS: %s\n", wifi_get_opmode(), config.ssid, config.password);
os_printf("Trying to connect to WiFi net\n");
uint8 state = wifi_station_connect();
if (state) {
os_printf("Connected OK. WiFi is now active\n");
}
else
os_printf("Failed to connect\n");
if(wifi_station_get_connect_status() == STATION_WRONG_PASSWORD)
{
os_printf("WiFi connecting error, wrong password\r\n");
}
else if(wifi_station_get_connect_status() == STATION_NO_AP_FOUND)
{
os_printf("WiFi connecting error, ap not found\r\n");
}
else if(wifi_station_get_connect_status() == STATION_CONNECT_FAIL)
{
os_printf("WiFi connecting fail\r\n");
}
else
{
os_printf("WiFi connecting...\r\n");
}
}
os_timer_setfn(&wifiConnectionFuncTimer, (os_timer_func_t *)checkWiFiConnectionFunc, 0);
os_timer_arm(&wifiConnectionFuncTimer, 1000, 0);
}
Unfortunately, wifi is not connected. Here are the logs:
"
Wait 4...
Wait 3...
Wait 2...
Wait 1...
ESP is in STATION mode
OPMODE: 1, SSID: ESPTEST, PASS: 12345678
Creating new thread to check connection
Created task
In user_init
Entered new thread for checking WIFI connection
In checking wifi connection 0
In user_init
In checking wifi connection 0
Entered checking WiFi connection Function - 0
WiFi is disconnected
OPMODE: 1, SSID: ESPTEST, PASS: 12345678
Trying to connect to WiFi net
Failed to connect
WiFi connecting...
In user_init
In checking wifi connection 1
Entered checking WiFi connection Function - 1
WiFi is disconnected
OPMODE: 1, SSID: ESPTEST, PASS: 12345678
Trying to connect to WiFi net
Failed to connect
WiFi connecting...
In user_init
"
Please, help me know what is the reason for connection fail?
Thank you.