-->
Page 1 of 3

Problems with wifi_set_event_handler_cb

PostPosted: Sat Nov 07, 2015 2:17 am
by is0lated
I've been trying to get the SDK working, so far I've managed to get a program to compile and flash it to my board (I think it's an 02). The problem I'm having is that I always seem to get the output of the default wifi_set_event_handler_cb() that's mentioned in the SDK guide. I'm using the currently using the latest pre-release from the Espressif forums and I've tried the latest official from the forums, both give me the same thing. I've attached the code I'm trying to get working (it's just the "basic_example" from the examples.) Is there something I'm doing wrong?

Code: Select all#include "ets_sys.h"
#include "osapi.h"
#include "gpio.h"
#include "os_type.h"
#include "ip_addr.h"
#include "espconn.h"
#include "user_config.h"
#include "user_interface.h"

//We need to include the uart driver
#include "driver/uart.h"

#define user_procTaskPrio        0
#define user_procTaskQueueLen    1
os_event_t    user_procTaskQueue[user_procTaskQueueLen];
static void loop(os_event_t *events);

//Main code function
static void ICACHE_FLASH_ATTR
loop(os_event_t *events)
{
    //os_printf("Hello\n\r");
    os_delay_us(10000);
    system_os_post(user_procTaskPrio, 0, 0 );
}

void wifi_cb(System_Event_t *evt)
{
        os_printf("An event occurred: %x\n", evt->event);
}

//Init function
void ICACHE_FLASH_ATTR
user_init()
{
   //Edited out my details
    char ssid[32] = "MySSID";
    char password[64] = "MyPassword";
    struct station_config stationConf;

    //Set station mode
    wifi_set_opmode( 0x1 );
   
    //Set up the UART, maaaybe?
    uart_init(BIT_RATE_9600, BIT_RATE_9600);
   
    //Set ap settings
    os_memcpy(&stationConf.ssid, ssid, 32);
    os_memcpy(&stationConf.password, password, 64);
    wifi_station_set_config(&stationConf);
   
    //Hook up the call back for wifi stuff
    wifi_set_event_handler_cb(wifi_cb);

    //Start os task
    system_os_task(loop, user_procTaskPrio,user_procTaskQueue, user_procTaskQueueLen);

    system_os_post(user_procTaskPrio, 0, 0 );
}

Re: Problems with wifi_set_event_handler_cb

PostPosted: Sat Nov 07, 2015 7:42 pm
by kolban
If I am reading your code correctly, you are registering a wifi event callback and setting the configuration of the WiFi station ... however what I am not seeing is any attempt by your code to have your ESP8266 connect to an access point. For example, I am not seeing a call to wifi_station_connect().

Until and unless you instruct the WiFi environment in your ESP8266 to perform some task, I wouldn't expect to see a callback to the WiFi event handler as no new WiFi events will have happened.

Might I suggest that in future ... when you post code, also document the intent and algorithms used. Not everyone will make the time to read through code line by line to see what might be going wrong.

Re: Problems with wifi_set_event_handler_cb

PostPosted: Sat Nov 07, 2015 9:18 pm
by is0lated
Thanks for looking over my code quickly, I'll make sure I comment it better in future. The problem isn't that the callback isn't being called or that it's not connecting, rather that it IS connecting and it IS running a callback rountine, just not the one I'm trying to run.

When I run that EXACT code (with my network details) the output is

Code: Select allscandone
no shhh... found, reconnect after 1s
reconnect

scandone
no shhh... found, reconnect after 1s
reconnect

scandone
no shhh... found, reconnect after 1s
reconnect

scandone
no shhh... found, reconnect after 1s
reconnect

scandone
no shhh... found, reconnect after 1s
reconnect

scandone

state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
a
dd 0
aid 2
cnt

connected with shhh..., channel 6
dhcp client start...

ip:192.168.1.7,mask:255.255.255.0,gw:192.168.1.1

pm open,type:2 0


Which appears to be very close to the output of the example wifi event handler given in the SDK documentation (see pages 73-74 of the version 1.4 SDK API guide).

Re: Problems with wifi_set_event_handler_cb

PostPosted: Sat Nov 07, 2015 11:55 pm
by kolban
Could you try performing an explicit WiFi connect in your code and see if it makes any differences?