WPS (Wi-Fi Protected Setup) in SDK

russus
Posts: 14
Joined: Thu Nov 10, 2016 8:56 pm

WPS (Wi-Fi Protected Setup) in SDK

Postby russus » Wed Nov 23, 2016 7:07 pm

I can't figure out how to implement WPS in SDK. There is an example that comes with SDK, however it does not seem to work for me. It compiles, but nothing happens. From the uart output I can see that wps does not even start.

I really really need help here. The code is given below. Can someone explain me the role of key_init_single and why GPIO 13 is used? Do we need to toggle it somehow for WPS to start working?

Code: Select all

#include "osapi.h"
#include "user_interface.h"
 
#include "driver/key.h"
 
#define WPS_KEY_NUM        1
 
#define WPS_KEY_IO_MUX     PERIPHS_IO_MUX_MTCK_U
#define WPS_KEY_IO_NUM     13
#define WPS_KEY_IO_FUNC    FUNC_GPIO13
 
LOCAL struct keys_param keys;
LOCAL struct single_key_param *single_key;
 
LOCAL void ICACHE_FLASH_ATTR
user_wps_status_cb(int status)
{
    switch (status) {
        case WPS_CB_ST_SUCCESS:
            wifi_wps_disable();
            wifi_station_connect();
            break;
        case WPS_CB_ST_FAILED:
        case WPS_CB_ST_TIMEOUT:
            wifi_wps_start();
            break;
    }
}
 
LOCAL void ICACHE_FLASH_ATTR
user_wps_key_short_press(void)
{
    wifi_wps_disable();
    wifi_wps_enable(WPS_TYPE_PBC);
    wifi_set_wps_cb(user_wps_status_cb);
    wifi_wps_start();
}
 
void ICACHE_FLASH_ATTR
user_rf_pre_init(void)
{
}
 
void ICACHE_FLASH_ATTR
user_init(void)
{
    single_key = key_init_single(WPS_KEY_IO_NUM, WPS_KEY_IO_MUX, WPS_KEY_IO_FUNC,
                                            NULL, user_wps_key_short_press);
 
    keys.key_num = WPS_KEY_NUM;
    keys.single_key = &single_key;
 
    key_init(&keys);
 
    wifi_set_opmode(STATION_MODE);
}

gjump
Posts: 19
Joined: Thu Nov 24, 2016 6:24 am

Re: WPS (Wi-Fi Protected Setup) in SDK

Postby gjump » Thu Nov 24, 2016 6:29 am

Same here, I only get this from UART:
Hello world!
SDK version: 1.5.0
wifi_wps_enable
mode : sta(18:fe:34:d2:df:a2)
add if0


I'm using the ESP12E module.

The callback function is never called. I left the code running for 10min just to be sure.

russus
Posts: 14
Joined: Thu Nov 10, 2016 8:56 pm

Re: WPS (Wi-Fi Protected Setup) in SDK

Postby russus » Fri Nov 25, 2016 1:39 pm

when I use AT firmware and apply the AT command "AT+WPS=1" everything works fine (Router's Push button needs to be pushed before sending the command). Maybe somebody knows how to find a source code for this command ???

gjump
Posts: 19
Joined: Thu Nov 24, 2016 6:24 am

Re: WPS (Wi-Fi Protected Setup) in SDK

Postby gjump » Sat Nov 26, 2016 1:05 am

Ohh, nice. I'll take a look at this firmware source code if available.

gjump
Posts: 19
Joined: Thu Nov 24, 2016 6:24 am

Re: WPS (Wi-Fi Protected Setup) in SDK

Postby gjump » Sat Nov 26, 2016 2:00 am

Well, the AT firmware is closed source :/

gjump
Posts: 19
Joined: Thu Nov 24, 2016 6:24 am

Re: WPS (Wi-Fi Protected Setup) in SDK

Postby gjump » Sun Nov 27, 2016 9:13 am

Well, I think I've found a "solution", I'm now using a different router and the same code works like a charm, pressed the WPS button and 10 seconds later it connected. Tested both routers with a PC and the WPS function worked, but only one worked with ESP8266, tried different encryption (WPA/WPA2, TKIP and AES) everything works for only one router. So... Maybe is the WPS functions fault.
It would be very nice to hear something from Espressif staff now.

Sheers!

russus
Posts: 14
Joined: Thu Nov 10, 2016 8:56 pm

Re: WPS (Wi-Fi Protected Setup) in SDK

Postby russus » Mon Nov 28, 2016 2:28 pm

I am glad to hear it worked for you. I guess, it means that the source code is fine.

I understood that the router could be an obstacle. This was the reason behind trying the AT firmware in my case. Since it worked fine, I assumed that the router is not an issue here. Was I correct to make this conclusion?

The output from running the SDK's code is below. No sign of WPS whatsoever

Code: Select all

load 0x40100000, len 29052, room 16
tail 12
chksum 0xcb
ho 0 tail 12 room 4
load 0x3ffe8000, len 1932, room 12
tail 0
chksum 0x64
load 0x3ffe8790, len 1816, room 8
tail 0
chksum 0x91
csum 0x91
rf cal sector: 123
rf[112] : 00
rf[113] : 00
rf[114] : 01

SDK ver: 2.0.0(656edbf) compiled @ Jul 19 2016 17:58:40
phy ver: 1055, pp ver: 10.2

mode : sta(18:fe:34:a3:25:31)
add if0


Am I understanding correctly that the code that comes with SDK works out of the box for you?

gjump
Posts: 19
Joined: Thu Nov 24, 2016 6:24 am

Re: WPS (Wi-Fi Protected Setup) in SDK

Postby gjump » Mon Nov 28, 2016 9:12 pm

Yes, the code sample code works. I'm just calling the function via timer(~3s after boot), cause I don't have any buttons attached to my ESP. The output is like this:
11:04:12.384> wifi_wps_enable
11:04:12.384> wps scan
11:04:12.384> build public key start
11:04:16.440> build public key finish
11:04:17.937> wps discover [Teste]
11:04:17.937> scandone
11:04:17.937> WPS: neg start
11:04:18.065> scandone
11:04:18.936> state: 0 -> 2 (b0)
11:04:18.936> state: 2 -> 3 (0)
11:04:19.027> state: 3 -> 5 (10)
11:04:19.027> add 0
11:04:19.027> aid 1
11:04:19.027> cnt
11:04:20.496> process pubkey start
11:04:25.366> process pubkey finish
11:04:25.426> WPS: key[senhateste]
11:04:25.426> wps finished
11:04:25.426> state: 5 -> 2 (1a0)
11:04:25.426> rm 0
11:04:26.425> >>WPS OK!!<<wifi_wps_disable
11:04:26.425> state: 2 -> 0 (0)
11:04:26.524> scandone
11:04:27.548> state: 0 -> 2 (b0)
11:04:27.548> state: 2 -> 3 (0)
11:04:27.548> state: 3 -> 5 (10)
11:04:27.548> add 0
11:04:27.548> aid 1
11:04:27.548> cnt
11:04:27.781>
11:04:27.781> connected with Teste, channel 11


Maybe you can give a shot using another router. I know that the AT firmware works, but who knows. I'm going to check if WPS works with the "not working" router using AT firmware.
But as I said, the code was supposed to work with any router.
And I'm still waiting for someone from Espressif to post something.

-sheers

russus
Posts: 14
Joined: Thu Nov 10, 2016 8:56 pm

Re: WPS (Wi-Fi Protected Setup) in SDK

Postby russus » Mon Nov 28, 2016 10:01 pm

Thanks for giving more information.

Maybe you can give a shot using another router

I'll definitely try another router when I get a chance. Honestly, I doubt that is the problem.

I added some print statements to the related functions.

Code: Select all

LOCAL void ICACHE_FLASH_ATTR
user_wps_status_cb(int status)
{
   os_printf("\n\rwps_status_cb\n\r");
   switch (status) {
      case WPS_CB_ST_SUCCESS:
         wifi_wps_disable();
         wifi_station_connect();
         break;
      case WPS_CB_ST_FAILED:
      case WPS_CB_ST_TIMEOUT:
         wifi_wps_start();
         break;
   }
}


Code: Select all

LOCAL void ICACHE_FLASH_ATTR
user_wps_key_short_press(void)
{
   os_printf("\n\rkey_short_press\n\r");
   wifi_wps_disable();
   wifi_wps_enable(WPS_TYPE_PBC);
   wifi_set_wps_cb(user_wps_status_cb);
   wifi_wps_start();
}


The output is similar to the one without. It makes me think that nothing is being called from user_main() during execution.

Can you please post your user_main if possible ? How do you introduce the delay?

I'm just calling the function via timer(~3s after boot), cause I don't have any buttons attached to my ESP

Can you elaborate on this a little bit. I feel I am missing something cricual because I dont understand the role of gpio13. It is a software WPS, why do you need buttons attached to your ESP ?

gjump
Posts: 19
Joined: Thu Nov 24, 2016 6:24 am

Re: WPS (Wi-Fi Protected Setup) in SDK

Postby gjump » Tue Nov 29, 2016 12:58 am

Russus, if you are using the example WPS code, GPIO13 triggers the WPS function on ESP, this is initialized by

Code: Select all

single_key = key_init_single(WPS_KEY_IO_NUM, WPS_KEY_IO_MUX, WPS_KEY_IO_FUNC,
                                          NULL, user_wps_key_short_press);

This functions set the "user_wps_key_short_press" to be called when you press the GPIO13 (can be any GPIO, just need to be redefined).
So, the logic is, you press the WPS key in your router, then press the WPS key(GPIO13 in that case) on ESP, this triggers the WPS functions inside the user_wps_key_short_press, as you can see:

Code: Select all

LOCAL void ICACHE_FLASH_ATTR
user_wps_key_short_press(void)
{
   wifi_wps_disable();
   wifi_wps_enable(WPS_TYPE_PBC);
   wifi_set_wps_cb(user_wps_status_cb);
   wifi_wps_start();
}


In my case, that I don't have any button attached to my ESP, I'm calling the function "user_wps_key_short_press" via a timer, 3 seconds after the board init.

This is done using this code(i've removed the lines that doesn't belong to WPS functionality)

Code: Select all



static void ICACHE_FLASH_ATTR ledtimer_func(void *arg)
{
  os_timer_disarm(&ledtimer);
  console_printf("LEDtimer %d\n\r",ledout++);
  if(ledout<2) // If seconds passed < 2, reload the timer
    {
      os_timer_setfn(&ledtimer, (os_timer_func_t *)ledtimer_func, NULL);
      os_timer_arm(&ledtimer, 1000, 0);
    }
  else user_wps_key_short_press(); // Else, call the WPS functions
}


void ICACHE_FLASH_ATTR
user_init(void)
{
   UARTInit(BIT_RATE_115200);
   console_printf("Hello world!\r\n");
   wifi_set_opmode(STATION_MODE);
   wifi_station_set_reconnect_policy(0); // To avoid using the previous saved credentials
   wifi_station_set_auto_connect(0); // To avoid using the previous saved credentials
   
   
   os_timer_disarm(&ledtimer);
   os_timer_setfn(&ledtimer, (os_timer_func_t *)ledtimer_func, NULL);
   os_timer_arm(&ledtimer, 1000, 0);
}


Explanation
I've a timer (ledtimer) attached to a callback function(ledtimer_func) that is called every second, once this function is called it checks the counter (ledout), if its <2 the timer is rearmed to be called again in 1 second. When ledout is >2 the rearming procedure isn't executed and "user_wps_key_short_press" is called, then triggering the WPS functions inside it.
But again, this code exists just because I don't have a button attached to my ESP, and as far as I've tested, you can't call the WPS functions right after you setup the module in STATION mode, you need to wait a little (not sure how long, I randomly picked 3s).

-Sheers

Who is online

Users browsing this forum: No registered users and 314 guests