Local exception handling of Wi-Fi connection issues

I’m trying to have my device (ESP32) become aware of when its Wi-Fi connection drops, and take some action when it’s the case: e.g. light a LED to indicate temporary disfunction. Basic exception handling basically.

What is the best approach to doing this? The handle method returns void. I suppose I could create a dummy Endpoint and ping it from the loop block at periodic intervals, but that seems like overkill.

Hello @14geronimo,

Please check this post: Check Cloud Connection on Arduino

The adaptation of this code to ESP32 is really easy to do:

#include <ThingerESP32.h>

#define USERNAME "your_user_name"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"

#define SSID "your_wifi_ssid"
#define SSID_PASSWORD "your_wifi_ssid_password"


class ClientListener : public ThingerESP32{
public:
    ClientListener(const char* user, const char* device, const char* device_credential) : 
      ThingerESP32(user, device, device_credential){}
protected:
  virtual void thinger_state_listener(THINGER_STATE state){
    // call current implementation (debug)
    ThingerESP32::thinger_state_listener(state);
    switch(state){
        case THINGER_AUTHENTICATED:
            // turn on your led
            break;
        default:
            // turn off your led
            break;
    }
  }
};

ClientListener thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);


 

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

  thing.add_wifi(SSID, SSID_PASSWORD);

  // digital pin control example (i.e. turning on/off a light, a relay, configuring a parameter, etc)
  thing["led"] << digitalPin(LED_BUILTIN);

  // resource output example (i.e. reading a sensor value)
  thing["millis"] >> outputValue(millis());

  // more details at http://docs.thinger.io/arduino/
}

void loop() {
  thing.handle();
}

2 Likes