How to connect to WiFi AP

MaximProg
Posts: 2
Joined: Fri Dec 22, 2017 8:43 pm

How to connect to WiFi AP

Postby MaximProg » Fri Dec 22, 2017 8:50 pm

Good day,

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.

Her Mary
Posts: 537
Joined: Mon Oct 27, 2014 11:09 am

Re: How to connect to WiFi AP

Postby Her Mary » Mon Dec 25, 2017 2:12 pm

Are you using the ESP8266_RTOS_SDK?
There are some examples may help you. https://github.com/espressif/esp8266-rtos-sample-code/tree/master/03Wifi

MaximProg
Posts: 2
Joined: Fri Dec 22, 2017 8:43 pm

Re: How to connect to WiFi AP

Postby MaximProg » Tue Dec 26, 2017 11:05 am

Thank you, Her Majesty. This helped.

User avatar
Uptery
Posts: 1
Joined: Thu Dec 28, 2017 7:25 pm

Re: How to connect to WiFi AP

Postby Uptery » Thu Dec 28, 2017 8:07 pm

Here is the code I use to connect to a network. Not sure this is much help, I have only just started working with ESP myself.

import time
import network
from network import WLAN
wlan = network.WLAN(mode=WLAN.STA)
wlan.connect('station ssid', auth=(WLAN.WPA2, 'password'))
print ('conneting - please wait.')
while not wlan.isconnected():
time.sleep_ms(50)
print ('Conneted')
print(wlan.ifconfig())

The response is:

Soft resetting the ESP
mode : sta(24:0a:c4:00:70:9c)
conneting - please wait.
I (438703) wifi: n:1 0, o:6 0, ap:255 255, sta:1 0, prof:6
I (439678) wifi: state: 0 -> 2 (b0)
I (439680) wifi: state: 2 -> 3 (0)
I (439685) wifi: state: 3 -> 5 (10)
I (439711) wifi: connected with BrightBox-2bqx6h, channel 1
Conneted
('192.168.1.98', '255.255.255.0', '192.168.1.1', '0.0.0.0')
MicroPython v1.8.6-234-g765f2bc on 2016-12-02; WiPy with ESP32
Type "help()" for more information.
"Let us sacrifice our today so that our children can have a better tomorrow."
General project of this past year Bestazy.Start from spring 2018.

Who is online

Users browsing this forum: No registered users and 119 guests