espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.12k stars 7.32k forks source link

SYSTEM_EVENT_STA_GOT_IP shots twice when setting Static IP. #2856

Closed ghost closed 4 years ago

ghost commented 5 years ago

Hardware:

Board: ?node32? Core Installation version: ?1.0.2? IDE name: ?Arduino IDE? Flash Frequency: ?80MHz? PSRAM enabled: ?no? ?yes? Upload Speed: ?115200? Computer OS: ?Windows 10?

Description:

Guys, I'd like to share the issue I'm having looking for some advice/help you could provide:

The project I'm working on consists of a ESP32 mostly on Deep-Sleep with periodical wake-up intervals. I'm using the WiFiClientEvents example as starting point.

The scheme I'm implementing right now is the following: Boot (configure everything and start WiFi) -> Do nothing but waiting for "SYSTEM_EVENT_STA_GOT_IP" -> within that event, perform all the application-related actions (for now just printing WiFi parameters) and disconnect -> wait for "SYSTEM_EVENT_STA_DISCONNECTED" and within that event, go to deep-sleep and wake-up after N seconds.

The thing is that when I assign a Static IP for AP connection, the "SYSTEM_EVENT_STA_GOT_IP" gets triggered twice every time. The problem with that is that the actions I'm trying to perform during wake-up time, are repeated, and that messes up everything. Is this caused by any WiFi misconfiguration I could fix?

Some information that could help troubleshooting:

Sketch: (leave the backquotes for code formatting)


/*

*What it does:*
- Turns on, Connects to AP.
- Sleeps for 5 seconds and gets back on.

*Sources:*
- Espressif's WiFi Client Events example: https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/WiFiClientEvents/WiFiClientEvents.ino
- Espressif's Deepsleep w. TimerWakeUp example: https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/DeepSleep/TimerWakeUp/TimerWakeUp.ino

*Instructions to run:*
- Unzip in Arduino's root-folder.
- Open Arduino IDE
- Set WiFi credentials properly
- Set correct USB Port and Board set to "Node32s".
- Flash it (we had to press BOOT button for it to do it correctly).
- Open Serial terminal set to 115200 bps and evaluate its performance.
*/

/*
* WiFi Events Table

0  SYSTEM_EVENT_WIFI_READY               < ESP32 WiFi ready
1  SYSTEM_EVENT_SCAN_DONE                < ESP32 finish scanning AP
2  SYSTEM_EVENT_STA_START                < ESP32 station start
3  SYSTEM_EVENT_STA_STOP                 < ESP32 station stop
4  SYSTEM_EVENT_STA_CONNECTED            < ESP32 station connected to AP
5  SYSTEM_EVENT_STA_DISCONNECTED         < ESP32 station disconnected from AP
6  SYSTEM_EVENT_STA_AUTHMODE_CHANGE      < the auth mode of AP connected by ESP32 station changed
7  SYSTEM_EVENT_STA_GOT_IP               < ESP32 station got IP from connected AP
8  SYSTEM_EVENT_STA_LOST_IP              < ESP32 station lost IP and the IP is reset to 0
9  SYSTEM_EVENT_STA_WPS_ER_SUCCESS       < ESP32 station wps succeeds in enrollee mode
10 SYSTEM_EVENT_STA_WPS_ER_FAILED        < ESP32 station wps fails in enrollee mode
11 SYSTEM_EVENT_STA_WPS_ER_TIMEOUT       < ESP32 station wps timeout in enrollee mode
12 SYSTEM_EVENT_STA_WPS_ER_PIN           < ESP32 station wps pin code in enrollee mode
13 SYSTEM_EVENT_AP_START                 < ESP32 soft-AP start
14 SYSTEM_EVENT_AP_STOP                  < ESP32 soft-AP stop
15 SYSTEM_EVENT_AP_STACONNECTED          < a station connected to ESP32 soft-AP
16 SYSTEM_EVENT_AP_STADISCONNECTED       < a station disconnected from ESP32 soft-AP
17 SYSTEM_EVENT_AP_STAIPASSIGNED         < ESP32 soft-AP assign an IP to a connected station
18 SYSTEM_EVENT_AP_PROBEREQRECVED        < Receive probe request packet in soft-AP interface
19 SYSTEM_EVENT_GOT_IP6                  < ESP32 station or ap or ethernet interface v6IP addr is preferred
20 SYSTEM_EVENT_ETH_START                < ESP32 ethernet start
21 SYSTEM_EVENT_ETH_STOP                 < ESP32 ethernet stop
22 SYSTEM_EVENT_ETH_CONNECTED            < ESP32 ethernet phy link up
23 SYSTEM_EVENT_ETH_DISCONNECTED         < ESP32 ethernet phy link down
24 SYSTEM_EVENT_ETH_GOT_IP               < ESP32 ethernet got IP from connected AP
25 SYSTEM_EVENT_MAX
*/

#include <WiFi.h>

// Variables used to keep track of wake-up time.
uint32_t start_time, end_time, wake_time; 

// Deep-Sleep Parameters:
#define uS_TO_S_FACTOR 1000000  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  5        /* Time ESP32 will go to sleep (in seconds) */

#define WIFI_SSID "put_your_ssid"
#define WIFI_PASSWORD "put_your_password"

#define USE_STATIC_IP // Comment if using DHCP. 

#define WIFI_IP           IPAddress(172, 20, 10, 2)
#define WIFI_GW           IPAddress(172, 20, 10, 1)
#define WIFI_DNS          IPAddress(8, 8, 4, 4)
#define WIFI_SUBNET       IPAddress(255, 255, 255, 240)

// WiFi Credentials
const char* ssid     = WIFI_SSID;
const char* password = WIFI_PASSWORD;

void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info)
{
    Serial.printf("[WiFi-event] event: %d -> ", event);

    switch (event) {
        case SYSTEM_EVENT_WIFI_READY: 
            Serial.println("WiFi interface ready");
            break;
        case SYSTEM_EVENT_SCAN_DONE:
            Serial.println("Completed scan for access points");
            break;
        case SYSTEM_EVENT_STA_START:
            Serial.println("WiFi client started");
            break;
        case SYSTEM_EVENT_STA_STOP:
            Serial.println("WiFi clients stopped");
            break;
        case SYSTEM_EVENT_STA_CONNECTED:
            Serial.println("Connected to access point");
            break;
        case SYSTEM_EVENT_STA_DISCONNECTED:
            Serial.println("Disconnected from WiFi access point");
            ESP32_GoToDeepSleep();
            break;
        case SYSTEM_EVENT_STA_AUTHMODE_CHANGE:
            Serial.println("Authentication mode of access point has changed");
            break;
        case SYSTEM_EVENT_STA_GOT_IP:
          static uint8_t got_ip = 0;
            Serial.println("Obtained IP address");                        
            WiFi_PrintConnectionParameters();
            WiFi_Disconnect();
            break;
        case SYSTEM_EVENT_STA_LOST_IP:
            Serial.println("Lost IP address and IP address is reset to 0");
            break;
        case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
            Serial.println("WiFi Protected Setup (WPS): succeeded in enrollee mode");
            break;
        case SYSTEM_EVENT_STA_WPS_ER_FAILED:
            Serial.println("WiFi Protected Setup (WPS): failed in enrollee mode");
            break;
        case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
            Serial.println("WiFi Protected Setup (WPS): timeout in enrollee mode");
            break;
        case SYSTEM_EVENT_STA_WPS_ER_PIN:
            Serial.println("WiFi Protected Setup (WPS): pin code in enrollee mode");
            break;
        default: 
        break;    
    }
  }

// Created by LuisF, not part of example-code.
void WiFi_Disconnect(void){
  Serial.println("Disconnecting from WiFi...");
  WiFi.disconnect();
  }

// Created by LuisF, not part of example-code.
void ESP32_GoToDeepSleep(void){

  // Configure Sleep-Time
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  Serial.println("Going to sleep... Coming back in " + String(TIME_TO_SLEEP) +
  " Seconds");

  wake_time = millis() - start_time;  // Calculate how many ms we were awake.
  Serial.println("Wake duration: " + String(wake_time) + " ms");
  Serial.println();

 // Send it sleeping.
 esp_deep_sleep_start();  
}

// Created by LuisF, not part of example-code.
// For debugging purposes.
void WiFi_PrintConnectionParameters(void){
    Serial.print("Channel: "); Serial.println(WiFi.channel());
    Serial.print("IP Addr: "); Serial.println(WiFi.localIP());
    Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
    Serial.print("Subnet Mask: "); Serial.println(WiFi.subnetMask());
    Serial.print("DNS: "); Serial.println(WiFi.dnsIP());
    String BSSID_Array; BSSID_Array = WiFi.BSSIDstr();
    Serial.println("BSSID: " + BSSID_Array);
}

void setup()
{
    start_time = millis();  // Get start-time.

    Serial.begin(115200);
    Serial.println();

    // delete old config
    WiFi.disconnect(true);
// This delay was included in the example, but in the documentation doesn't say it should have it. 
// Removed it and is still working.
    delay(10);      
    WiFi.onEvent(WiFiEvent);    // This is a call-back that will trigger on any WiFi event and handle them appropriately.  
#ifdef USE_STATIC_IP
    WiFi.config(WIFI_IP, WIFI_GW, WIFI_SUBNET, WIFI_DNS);
#endif
    WiFi.begin(ssid, password);
}

void loop()
{
;
}

Debug Messages:

ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:928
ho 0 tail 12 room 4
load:0x40078000,len:8424
ho 0 tail 12 room 4
load:0x40080400,len:5868
entry 0x4008069c

[WiFi-event] event: 0 -> WiFi interface ready
[WiFi-event] event: 2 -> WiFi client started
[WiFi-event] event: 7 -> Obtained IP address
Channel: 1
IP Addr: 172.20.10.2
Gateway: 172.20.10.1
Subnet Mask: 255.255.255.240
DNS: 8.8.4.4
BSSID: 76:33:CB:8E:FC:82
Disconnecting from WiFi...
[WiFi-event] event: 7 -> Obtained IP address
Channel: 1
IP Addr: 172.20.10.2
Gateway: 172.20.10.1
Subnet Mask: 255.255.255.240
DNS: 8.8.4.4
BSSID: 
Disconnecting from WiFi...
[WiFi-event] event: 5 -> Disconnected from WiFi access point
Going to sleep... Coming back in 5 Seconds
Wake duration: 964 ms 
lbernstone commented 5 years ago

I think you are probably seeing an autoreconnect attempt. If you want to stay offline, set WiFi.setAutoReconnect(false). Otherwise, turn logging level to verbose so you can see the full transaction with the AP.

ghost commented 5 years ago

@lbernstone Thanks for your reply. Did try the WiFi.setAutoReconnect(false) but the issue didn't stop. Here I'm posting the Serial Output for the Sketch runnin with Core Debug Level to Verbose.

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:928
ho 0 tail 12 room 4
load:0x40078000,len:8424
ho 0 tail 12 room 4
load:0x40080400,len:5868
entry 0x4008069c

[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 2 - STA_START
[WiFi-event] event: 2 -> WiFi client started
[WiFi-event] event: 0 -> WiFi interface ready
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 7 - STA_GOT_IP
[D][WiFiGeneric.cpp:379] _eventCallback(): STA IP: 172.20.10.2, MASK: 255.255.255.240, GW: 172.20.10.1
Disconnecting from WiFi...
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 7 - STA_GOT_IP
[W][WiFiGeneric.cpp:351] _eventCallback(): Reason: 8 - ASSOC_LEAVE
Going to sleep... Coming back in 5 Seconds
Wake duration: 970 ms

Also, here's the output as well without sending the ESP32 to deep-sleep:

Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:928
ho 0 tail 12 room 4
load:0x40078000,len:8424
ho 0 tail 12 room 4
load:0x40080400,len:5868
entry 0x4008069c

[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 0 - WIFI_READY
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 2 - STA_START
[WiFi-event] event: 2 -> WiFi client started
[WiFi-event] event: 0 -> WiFi interface ready
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 7 - STA_GOT_IP
[D][WiFiGeneric.cpp:379] _eventCallback(): STA IP: 172.20.10.2, MASK: 255.255.255.240, GW: 172.20.10.1
[D][WiFiGeneric.cpp:336] _eventCallback(): Event: 7 - STA_GOT_IP
[D][WiFiGeneric.cpp:379] _eventCallback(): STA IP: 172.20.10.2, MASK: 255.255.255.240, GW: 172.20.10.1

In both scenarios, the STA_GOT_IP is triggered twice, but both connection parameters are displayed the same. Still wondering what could be the cause.

pglen commented 5 years ago

I had a similar occurrence in 'C', the client connection reported twice. The error was that the event loop got initialized twice. I suspect something similar going on here with the onevent / config combo.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

stale[bot] commented 4 years ago

This stale issue has been automatically closed. Thank you for your contributions.

tokra commented 3 years ago

i am experiencing this issue too. anyone solved it ?

rhamnett commented 3 years ago

I get this problem as well when using a static IP

pglen commented 3 years ago

The newer IDFs do not have this error (3+)

On Mon, Feb 15, 2021 at 11:40 AM Richard Hamnett notifications@github.com wrote:

I get this problem as well when using a static IP

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/espressif/arduino-esp32/issues/2856#issuecomment-779336928, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEMBG5CLI4FE7HB73WQOOQDS7FE7BANCNFSM4HS5Q74A .

-- -- Peter Glen Software developer. Author / Inventor of official US Patent 'Adaptive Alpha Blending'. (US20070103483). This email message and any attachments to it is hereby declared confidential under the Electronic Communications Privacy Act and the EU Lisbon Treaty.

rhamnett commented 3 years ago

I needed to use

SYSTEM_EVENT_AP_STAIPASSIGNED
pglen commented 3 years ago

Glad to hear it!

On Tue, Feb 16, 2021 at 11:36 PM Richard Hamnett notifications@github.com wrote:

I needed to use

SYSTEM_EVENT_AP_STAIPASSIGNED

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/espressif/arduino-esp32/issues/2856#issuecomment-780291812, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEMBG5DWMTRBXHAK2DWUPUDS7NBVJANCNFSM4HS5Q74A .

-- -- Peter Glen Software developer. Author / Inventor of official US Patent 'Adaptive Alpha Blending'. (US20070103483). This email message and any attachments to it is hereby declared confidential under the Electronic Communications Privacy Act and the EU Lisbon Treaty.

jgompis commented 3 years ago

Same problem here, I tried this:

I needed to use

SYSTEM_EVENT_AP_STAIPASSIGNED

but the problem persists.

rhamnett commented 3 years ago

SYSTEM_EVENT_AP_STAIPASSIGNED replaces SYSTEM_EVENT_STA_GOT_IP you should only get one message @juliangomp

radzimir commented 5 months ago

I can reproduce SYSTEM_EVENT_STA_GOT_IP by restarting my AP, which simulates weak, flaky signal.