[SOLVED] How can i login to a WiFi network?

Stational
Posts: 42
Joined: Sat Jul 02, 2016 10:54 pm

[SOLVED] How can i login to a WiFi network?

Postby Stational » Sat Jul 02, 2016 10:56 pm

Hello,
i am wondering how i can login into a wifi network, this is my code:

Code: Select all

#include <ets_sys.h>
#include <osapi.h>
#include <gpio.h>
#include <os_type.h>

void ICACHE_FLASH_ATTR user_set_station_config(void){
   char ssid[32] = "";
   char password[64] = "";
   struct station_config stationConf;

   stationConf.bssid_set = 0;

   os_memcpy(&stationConf.ssid,ssid,32);
   os_memcpy(&stationConf.password,password,64);
   wifi_station_set_config(&stationConf);
}

void user_init(void)
{
   wifi_set_opmode(1);
   user_set_station_config();
}


But i get the error "storage size of 'stationConf' is not known".

Any ideas how i can fix that error?

pratik

Re: How can i login to a WiFi network?

Postby pratik » Sun Jul 03, 2016 1:32 pm

Hello Stational,

Did you leave the SSID and PASSWORD strings empty?
For which line of code are you getting this error?
In the following code, you need to define SSID and PASSWORD strings, for example, SSID can be "home_router" and PASSWORD is "admin".

Code: Select all

char ssid[32] = SSID;
   char password[64] = PASSWORD;
   struct station_config stationConf;
   
   os_memset(stationConf.ssid, 0, 32);
   os_memset(stationConf.password, 0, 64);
   //need not mac address
   stationConf.bssid_set = 0;
   
   //Set ap settings
   os_memcpy(&stationConf.ssid, ssid, 32);
   os_memcpy(&stationConf.password, password, 64);
   wifi_station_set_config(&stationConf);

Stational
Posts: 42
Joined: Sat Jul 02, 2016 10:54 pm

Re: How can i login to a WiFi network?

Postby Stational » Sun Jul 03, 2016 3:20 pm

Hi pratik,

thanks a lot for your answer.
Of course i filled the variables ''ssid'' and ''password'' with the appropriate data from my home wifi network. I think it is reasonable that i don't show my home network data on the internet.

The error appears in this line:

Code: Select all

struct station_config stationConf;


Maybe i am missing some kind if include header file?

Edit: Maybe i missunderstood something,

does this code create a new access point or does this code make the esp8266 connect to a wifi network?

Edit: It looks like another person has had that problem a while ago:

http://41j.com/blog/2015/01/esp8266-wif ... t-connect/

But it didn't get fixed.

I googled and i found out that in normal C this error appears when a struct is not known so maybe the struct 'station_config' is also not known? Maybe because i missed some header file?

It looks like the struct 'station_config' is defined in this file:

https://github.com/espressif/ESP8266_RT ... /esp_sta.h

Okay, i just copy paste the struct and now it compiles:

Code: Select all

#include <ets_sys.h>
#include <osapi.h>
#include <gpio.h>
#include <os_type.h>

struct station_config {
    uint8 ssid[32];         /**< SSID of target AP*/
    uint8 password[64];     /**< password of target AP*/
    uint8 bssid_set;        /**< whether set MAC address of target AP or not. Generally, station_config.bssid_set needs to be 0; and it needs to be 1 only when users need to check the MAC address of the AP.*/
    uint8 bssid[6];         /**< MAC address of target AP*/
};

void ICACHE_FLASH_ATTR user_set_station_config(void){
   char ssid[32] = "";
   char password[64] = "";
   struct station_config stationConf;

   os_memset(stationConf.ssid, 0, 32);
   os_memset(stationConf.password, 0, 64);
   //need not mac address
   stationConf.bssid_set = 0;

   //Set ap settings
   os_memcpy(&stationConf.ssid, ssid, 32);
   os_memcpy(&stationConf.password, password, 64);
   wifi_station_set_config(&stationConf);
}

void user_init(void)
{
   wifi_set_opmode(0x01);
   user_set_station_config();
}

Stational
Posts: 42
Joined: Sat Jul 02, 2016 10:54 pm

Re: How can i login to a WiFi network?

Postby Stational » Sun Jul 03, 2016 8:52 pm

Code: Select all

#include <ets_sys.h>
#include <osapi.h>
#include <gpio.h>
#include <os_type.h>
#include "driver/uart.h"
#include "driver/esp_sta.h"

LOCAL os_timer_t hello_timer;
extern int ets_uart_printf(const char *fmt, ...);

void ICACHE_FLASH_ATTR user_set_station_config(void){
   char ssid[32] = "my_home_ssid";
   char password[64] = "my_home_password";
   struct station_config stationConf;

   os_memset(stationConf.ssid, 0, 32);
   os_memset(stationConf.password, 0, 64);
   //need not mac address
   stationConf.bssid_set = 0;

   //Set ap settings
   os_memcpy(&stationConf.ssid, ssid, 32);
   os_memcpy(&stationConf.password, password, 64);
   wifi_station_set_config(&stationConf);
   wifi_station_connect();
}

LOCAL void ICACHE_FLASH_ATTR hello_cb(STATION_STATUS connState)
{
   char str[20];
   os_sprintf(str,"%d", connState);
   ets_uart_printf(str);
}

void user_init(void)
{
   wifi_set_opmode(STATIONAP_MODE);
   user_set_station_config();

   STATION_STATUS connState = wifi_station_get_connect_status();

   uart_init(BIT_RATE_115200, BIT_RATE_115200);

   os_timer_disarm(&hello_timer);
   os_timer_setfn(&hello_timer, (os_timer_func_t *)hello_cb, connState);
   os_timer_arm(&hello_timer, 1000, 1);
}


I still don't understand why i get these errors.

1. Error:
'STATIONAP_MODE' undeclared

2. Error:
'Unknown type "AUTH_MODE" in file esp_sta.h'

One question:
should the above code (the wifi connection part) normally work without any additional includes (such as 'esp_sta.h')? I see the wif connection code alot over the internet but no one posts their include-files.

pratik

Re: How can i login to a WiFi network?

Postby pratik » Sun Jul 03, 2016 11:11 pm

Hello Stational,

Use the same code that you posted in your original question, just add this one header file
#include "user_interface.h"

And I would also suggest that you use " " to write the names of the header files. I'm not sure what platform you are using and how your toolchain is configured so that might cause problems as well.

NOTE: Adding definition of structure in same file will compile in some cases but the code will surely not work. So you must make it compile by including the right things. :)
Let me know if it works!

Stational
Posts: 42
Joined: Sat Jul 02, 2016 10:54 pm

Re: How can i login to a WiFi network?

Postby Stational » Sun Jul 03, 2016 11:32 pm

Thanks alot pratik,

i knew something was wrong with the include files ^^

Now i can finally connect to a wifi-network :)

Who is online

Users browsing this forum: No registered users and 15 guests