My logic is that the ESP would try connecting to an Access Point as station for 5 times. It 5 connection attempts fail (wrong password or any other reason), it would switch over to SoftAP mode. Here's my code:
#define WIFI_STATUS_NAME(status) \
(STATION_IDLE == status ? "STATION_IDLE" : \
(STATION_CONNECTING == status ? "STATION_CONNECTING" : \
(STATION_WRONG_PASSWORD == status ? "STATION_WRONG_PASSWORD" : \
(STATION_NO_AP_FOUND == status ? "STATION_NO_AP_FOUND" : \
(STATION_CONNECT_FAIL == status ? "STATION_CONNECT_FAIL" : \
(STATION_GOT_IP == status ? "STATION_GOT_IP" : "Unknown" ))))))
/******************************************************************************
* FunctionName : wifi_event_handler
* Description : handles the event of wifi connection state change
* Parameters : System_Event_t
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
wifi_event_handler(System_Event_t *evt) {
uint8 wifi_status = wifi_station_get_connect_status();
switch (evt->event) {
case EVENT_STAMODE_DISCONNECTED: {
os_printf("-> wifi status : (%d) - ", wifi_status );
switch (wifi_status) {
case STATION_CONNECTING: {
wifi_connect_tries++;
if ( wifi_connect_tries >= wifi_connect_max_tries ) {
os_printf("STATION_CONNECTING..timeout!!!\n");
if (wifi_get_opmode()==STATION_MODE) {
wifi_set_opmode(SOFTAP_MODE);
wifi_connect_tries = 0;
}
else if (wifi_get_opmode()==STATIONAP_MODE) {
}
}
else
os_printf("STATION_CONNECTING..%d\n", wifi_connect_tries);
break;
}
default: {
os_printf("-> %s\n",WIFI_STATUS_NAME(wifi_status));
wifi_connect_tries = 0;
wifi_set_opmode(SOFTAP_MODE);
break;
}
}
break;
} // end EVENT_STAMODE_DISCONNECTED
case EVENT_STAMODE_GOT_IP: {
if (wifi_get_opmode() != STATION_MODE) { // get current mode, if not station
wifi_set_opmode(STATION_MODE); // set current mode to station and save to flash
wifi_connect_tries = 0;
}
break;
}
}
The error I get is
mode : sta(18:fe:34:9d:2b:b3) + softAP(1a:fe:34:9d:2b:b3)
add if1
pm close 7 0 0/175059131
dhcp server start:(ip:192.168.4.1,mask:255.255.255.0,gw:192.168.4.1)
bcn 100
f 0, beacon timeout
state: 5 -> 0 (1)
-> wifi status : (4) - -> STATION_CONNECT_FAIL
scandone
del if0
mode : softAP(1a:fe:34:9d:2b:b3)
Fatal exception (29):
epc1=0x4025ca65, epc2=0x00000000, epc3=0x400043e6, excvaddr=0x000000b8, depc=0x00000000
ets Jan 8 2013,rst cause:1, boot mode:(1,7)
ets Jan 8 2013,rst cause:4, boot mode:(1,7)
wdt reset
The same code works on my home wifi network, but throws exception on other wifi networks.
Any pointers or clue will be much appreciated.