The use of the ESP8266 in the world of IoT

User avatar
By debuti
#27390 Hi everybody.

I've been playing with the SDK lately, i can connect to APs and blink LEDs and so, but when i try to scan for near APs, the status code always says FAIL.

Here is my code i've been 3 days changing up things, but nothing works. Hope anyone could hint something

Thanks!!

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_interface.h"
#include "user_config.h"

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


/** WIFI Functions **/
void ICACHE_FLASH_ATTR wifiInit() {
    os_printf(">>>%s\n", __FUNCTION__);
    //wifi_station_set_hostname("autolocator");
    wifi_set_opmode(STATION_MODE);
    os_printf("<<<%s\n", __FUNCTION__);
}

static void ICACHE_FLASH_ATTR wifiScan_cb(void *arg, STATUS status) {
    os_printf(">>>%s\n", __FUNCTION__);

    uint8 ssid[33];

    switch (status ) {
    case OK:
      os_printf("Status OK\n");
       struct bss_info *bss_link = (struct bss_info *)arg;
      bss_link = bss_link->next.stqe_next;//ignore first

      while (bss_link != NULL) {
        os_memset(ssid, 0, 33);
        if (os_strlen(bss_link->ssid) <= 32)
        {
         os_memcpy(ssid, bss_link->ssid, os_strlen(bss_link->ssid));
        }
        else
        {
         os_memcpy(ssid, bss_link->ssid, 32);
        }
        os_sprintf("+CWLAP:(%d,\"%s\",%d,\""MACSTR"\",%d)\r\n",
                  bss_link->authmode,
                     ssid,
                     bss_link->rssi,
                  MAC2STR(bss_link->bssid),
                     bss_link->channel);
        bss_link = bss_link->next.stqe_next;
      }
      break;
   case FAIL:
       os_printf("Status FAIL\n");
         break;
   case PENDING:
       os_printf("Status PENDING\n");
         break;
   case BUSY:
       os_printf("Status BUSY\n");
         break;
   case CANCEL:
       os_printf("Status CANCEL\n");
         break;
   default:
       os_printf("Status UNKNOWN\n");
         break;
    }
    os_printf("<<<%s\n", __FUNCTION__);
}

/** UART Functions **/
void ICACHE_FLASH_ATTR uartInit() {
    os_printf(">>>%s\n", __FUNCTION__ );
    uart_div_modify( 0, UART_CLK_FREQ / ( 115200 ) );
    os_printf("<<<%s\n", __FUNCTION__);
}

/** GPIO Functions **/
void ICACHE_FLASH_ATTR gpioInit() {
    os_printf(">>>%s\n", __FUNCTION__ );
    gpio_init();
    os_printf("<<<%s\n", __FUNCTION__);
}

/** Entry point **/
//Main code function
void ICACHE_FLASH_ATTR loop(os_event_t *events) {
    os_printf(">>>%s\n", __FUNCTION__ );
    os_delay_us(1*1000000);

    wifi_promiscuous_enable(0);
    os_printf("mode=%u\n",wifi_get_opmode());
    os_printf("set mode=%u\n", wifi_set_opmode(STATION_MODE));
    os_printf("mode_afterset=%u\n",wifi_get_opmode());
    os_printf("scan_result=%u\n",wifi_station_scan(NULL,wifiScan_cb));

    system_os_post(user_procTaskPrio,
                   0,
                   0);
    os_printf("<<<%s\n", __FUNCTION__);
}

void ICACHE_FLASH_ATTR user_rf_pre_init(void) {}

//Init function
void ICACHE_FLASH_ATTR user_init( void ) {
    os_printf(">>>%s\n", __FUNCTION__ );
   uartInit();
    os_delay_us(0.1*1000000);
   gpioInit();
    wifiInit();
   
    //Start os task
    //os_delay_us(5*1000000);
    system_os_task(loop,
                   user_procTaskPrio,
                   user_procTaskQueue,
                   user_procTaskQueueLen);
    system_os_post(user_procTaskPrio,
                   0,
                   0);
    os_printf("<<<%s\n", __FUNCTION__);
}
User avatar
By st0ff3r
#87399 I think there is a bug in the esp's 802.11 code that wrongly sends and reacts on beacons with Channel Switch Announcements while scanning and therefore switching channels.

I wrote a simple cli to send those kind of beacons and even if the esp is not connected to the SSID contained in the beacon the esp changes channel and therefore loose its connectivity:

https://github.com/nabovarme/beacon_spammer/