-->
Page 1 of 1

Wifi scan available access points -program hangs during scan

PostPosted: Mon Dec 07, 2015 1:22 am
by bfalzarano
Can someone help me figure our what I am doing incorrectly here? Below is my code to scan for available APs. I'm trying to display all available APs to console (there are a total of 7) but the program never fully prints out the 3rd AP (see console output below). I've tried different examples of the scan_done_callback function (examples in these forums and some of the espressif SDK partial examples) but each one also seems to hang. No WDT resets occur. the program seems to be stuck in the while loop as Scan Done never prints to console. How do I print out all available Access Points? Does anyone have working examples for sdk_v1.4.0?

The device I am trying to run this code on is an ESP-12 dev board as shown here http://tronixlabs.com/wireless/esp8266/ ... australia/

SDK version is esp_iot_sdk_v1.4.0
I'm using esptool.py to flash: python /esptools/tools/esptool.py -p /dev/tty.usbserial write_flash 0x00000 0x00000.bin 0x40000 0x40000.bin (when I build the code the bin files default to those addresses. I'm not 100% sure if the 0x40000 is the correct address I should flash to for the ESP-12 however I was able to successfully run and test code to verify GPIO, PWM, ADC, and other functionality)

rl¦scan APs
mode :..¡18:fe:34:a2:cb:43)
add if0
f r0, scandone
1 : SSID:SSITH CH:1 RSSI:-60 Authmode:3
2 : SSID:Home CH:6 RSSI:-94 Authmode:3
3 : SSID:TG111BA2_RE CH:11 RSSI:-721


Code: Select all#include "ets_sys.h"
#include "osapi.h"
#include "gpio.h"
#include "os_type.h"
#include "mem.h"
#include "ip_addr.h"
#include "espconn.h"
#include "user_interface.h"
#include "driver/uart.h"
#include "user_config.h"
#define user_procTaskPrio        0
#define user_procTaskQueueLen    1
os_event_t    user_procTaskQueue[user_procTaskQueueLen];
uint8_t ssid_found;
// function declarations

static void scan_done_callback(void *arg, STATUS status);
static void initDone_cb();

//-------------------------------------------------------------------------------------------------
// loop function will be execute by "os" periodically
static void ICACHE_FLASH_ATTR  loop(os_event_t *events)
{
    os_delay_us(10);
}

// scan wifi callback
static void ICACHE_FLASH_ATTR scan_done_callback(void *arg, STATUS status)
{
    struct bss_info *bss;   
    bss = (struct bss_info *)arg;
    ssid_found = 0;
    while (bss)
    {
       // The SSID may **NOT** be NULL terminated ... so handle that.
       char ssid[sizeof(bss->ssid) + 1];
       os_strncpy((char *)ssid, (char *)bss->ssid, sizeof(bss->ssid));
       ssid[sizeof(ssid)-1] = '\0';
       ssid_found++;
            os_printf("%d : SSID:%s CH:%d RSSI:%d Authmode:%d\n", ssid_found, bss->ssid, bss->channel, bss->rssi, bss->authmode);
        bss = STAILQ_NEXT(bss, next);
    }
   os_printf("Scan Done\r\n");
   //ssid_scan_done;
}


static void ICACHE_FLASH_ATTR  initDone_cb() {
   // Ensure we are in station mode
   wifi_set_opmode(STATION_MODE);
   wifi_station_set_auto_connect(FALSE);
   wifi_station_scan(NULL, scan_done_callback);
}

//-------------------------------------------------------------------------------------------------
//Init function
void ICACHE_FLASH_ATTR  user_init()
{
   // Initialize UART0 to use as debug
   UART_init(BIT_RATE_115200, BIT_RATE_115200);
   //Scan APs
   os_printf("scan APs\r\n");
   struct scan_config config;
   os_memset(&config, 0, sizeof(struct scan_config));
   system_init_done_cb(&initDone_cb);


}


SOLVED: Wifi scan avail access points -program hangs during

PostPosted: Fri Dec 11, 2015 2:32 pm
by bfalzarano
Hi, the problem turned out to be with os_printf and there is some good info here where others have reported similar issues with os_printf https://www.reddit.com/r/esp8266/commen ... debugging/

Below are the specific changes that were added to get this to work:
Code: Select allextern int ets_uart_printf(const char *fmt, ...);


and then replace
Code: Select allos_printf
with
Code: Select allets_uart_printf


example:
Code: Select allets_uart_printf("%d : SSID:%s CH:%d RSSI:%d Authmode:%d\n", ssid_found, bss->ssid, bss->channel, bss->rssi, bss->authmode);


the console output and full code is included below. hope this info helps others.
cheers!

console output:
f r0, scandone
1 : SSID:SSITH CH:1 RSSI:-68 Authmode:3
2 : SSID:Home CH:11 RSSI:-82 Authmode:3
3 : SSID:TG111BA2_RE CH:11 RSSI:-75 Authmode:3 <--this line never fully displayed when using os_printf
Scan Done <--this never displayed in the console when using os_printf

Code: Select all#include "ets_sys.h"
#include "osapi.h"
#include "gpio.h"
#include "os_type.h"
#include "mem.h"
#include "ip_addr.h"
#include "espconn.h"
#include "user_interface.h"
#include "driver/uart.h"
#include "user_config.h"
#define user_procTaskPrio        0
#define user_procTaskQueueLen    1
os_event_t    user_procTaskQueue[user_procTaskQueueLen];
extern int ets_uart_printf(const char *fmt, ...);
uint8_t ssid_found;
//WiFi access point data
typedef struct {
   char ssid[32];
   char rssi;
   char enc;
} ApData;

//Scan resolt
typedef struct {
   char scanInProgress;
   ApData **apData;
   int noAps;
} ScanResultData;

//Static scan status storage.
ScanResultData cgiWifiAps;
// function declarations

static void scan_done_callback(void *arg, STATUS status);
static void initDone_cb();

//-------------------------------------------------------------------------------------------------
// loop function will be execute by "os" periodically
static void ICACHE_FLASH_ATTR  loop(os_event_t *events)
{
    os_delay_us(10);
}

// scan wifi callback
void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) {
       struct bss_info *bss;   
       bss = (struct bss_info *)arg;
       ssid_found = 0;
       while (bss)
       {
          // The SSID may **NOT** be NULL terminated ... so handle that.
          char ssid[sizeof(bss->ssid) + 1];
          os_strncpy((char *)ssid, (char *)bss->ssid, sizeof(bss->ssid));
          ssid[sizeof(ssid)-1] = '\0';
          ssid_found++;
               //ets_uart_printf("SSID:%s \n",bss->ssid);
          ets_uart_printf("%d : SSID:%s CH:%d RSSI:%d Authmode:%d\n", ssid_found, bss->ssid, bss->channel, bss->rssi, bss->authmode);
          bss = STAILQ_NEXT(bss, next);
       }
      ets_uart_printf("Scan Done\r\n");
      //ssid_scan_done;}
}

static void ICACHE_FLASH_ATTR  initDone_cb() {
   // Ensure we are in station mode
   wifi_set_opmode(STATION_MODE);
   wifi_station_set_auto_connect(FALSE);
   int x;
   cgiWifiAps.scanInProgress=1;
   x=wifi_station_get_connect_status();
   if (x!=STATION_GOT_IP) {
      //Unit probably is trying to connect to a bogus AP. This messes up scanning. Stop that.
      ets_uart_printf("STA status = %d. Disconnecting STA...\n", x);
      wifi_station_disconnect();
   }
   wifi_station_scan(NULL, wifiScanDoneCb);
}

//-------------------------------------------------------------------------------------------------
//Init function
void ICACHE_FLASH_ATTR  user_init()
{
   // Initialize UART0 to use as debug
   UART_init(BIT_RATE_115200, BIT_RATE_115200);
   //Scan APs
   ets_uart_printf("scan APs\r\n");
   struct scan_config config;
   os_memset(&config, 0, sizeof(struct scan_config));
   system_init_done_cb(&initDone_cb);


}