Hi,
I’m not a programmer, but I’m evolving well with the Thinger.io library for Esp8266 (NodeMcu). I’m trying to implement a button that, when pressed for 3 seconds, the device goes back to Access Point mode.
I was able to implement the button when the device is in the loop () ;, running the thing.handle () ;.
But it is unable to implement the button when the device loses its connection or when the router / modem stops working for some electronic reason. This apparently causes an infinite loop, as the ClientListener is trying to connect to the configured router / modem without success infinitely.
The problem is if that router / modem has to be exchanged for another device. How to go back to Access Point mode, and change the SSID and password to connect to a new router / modem?
I already searched in other posts (with the words ClientListener and button), but I didn’t see anything about this issue.
The code for my button is this:
#define BUTTON_PIN D8
void buttonPressThreeSeconds(){
// Read the pin on the button (with pulldown resistor). If true, run the code below:
if(digitalRead(D8)){
// Disconnect from the router / modem and return to Access Point mode.
WiFi.persistent(true);
WiFi.disconnect(true);
WiFi.persistent(false);
delay (1000);
ESP.restart();
}
}
I handled exceptions with this code, already provided in other posts:
class ClientListener : public ThingerWebConfig{
public:
ClientListener(const char* user, const char* device, const char* device_credential) :
ThingerWebConfig(user, device, device_credential){}
protected:
virtual void thinger_state_listener(THINGER_STATE state){
ThingerWebConfig::thinger_state_listener(state);
switch(state){
case NETWORK_CONNECT_ERROR:
digitalWrite(LED_PIN, HIGH);
buttonPressThreeSeconds();
break;
case NETWORK_CONNECTING:
digitalWrite(LED_PIN, HIGH);
buttonPressThreeSeconds();
break;
}
}
};
The above solution works, but I have to wait for ClientListener to start a new attempt for the button to be clicked. This can vary from 1 to 20 s, depending on the stage of the process.
What I need is for the button above to be pressed after being pressed for 3 seconds, for example.
If anyone has any suggestions, I appreciate it and I think other people can use this solution in their projects.