Using WiFi events on Arduino can be very useful, as I previously wrote in the article Best Practices on Connecting ESP8266 to WiFi Network. It helps in managing the wifi connection in the sketch’s code.

In this post, I wanted to share with you the list (as far as I know) of events.

event list

WiFi Event Description
onStationModeGotIP This event fires up when the chip gets to its final step of the connection: getting is network IP address.
onStationModeConnected This event fires up when the chip has passed the password check of the connection part.
onStationModeAuthModeChanged Fires up when the chip can’t log in with previously saved credentials.
onStationModeDisconnected This event fires up when the chip detects that it is no longer connected to an AP
onStationModeDHCPTimeoutNumbers This event fires when the microcontroller can’t get an IP address, from a timeout or other errors.

What are these events? well, events differ from other elements in your sketch by the time and order they are called. Every other element is called immediately after the previous line of code ended. An event, on the other hand, is called async when something happens, and in our case, the wifi connection state changed. This is not something which we can exactly time or synchronize, so that’s why it is called asynchronously.

How to use WiFi events in Arduino

How to use these event in your sketch? For instance, if you want to detect disconnection events:

  1. Declare a global variable: WiFiEventHandler mDisConnectHandler;
  2. On the setup() function register the handler and assign it to a function  mDisConnectHandler = WiFi.onStationModeDisconnected(onDisconnect);
  3. Write the function onDisconnect, with the signiture void onDisconnect(const WiFiEventStationModeDisconnected& event)

 

Your code should look something link this:

Let me know in the comments below if something is missing or needs more explaining.

Leave a comment