Hello,
I’m implementing a contingency plan to my cayenne project. When my ESP8266 is connected to my main wifi hotspot, and that wifi hotspot is down or crashes (due to internet provider failure, or problems with router), the ESP8266 will instead connect to a secondary wi-fi hotspot (smartphone with 4G mobile internet). This will allow the system to work, even when my main router/internet provider is offline.
I’m using this code, that worked perfectly (when main wi-fi hotspot is down, ESP8266 automaticaly changes connection to secondary hotspot):
#include <ESP8266WiFi.h>
const char* ssid=“Wifi hotspot A”;
const char* password = “xxxx”;const char* ssid2=“Wifi hotspot B”;
const char* password2 = “xxxx”;void setup() {
}
void loop() {
Serial.begin(115200);
delay(1000);
Serial.println();
if( WiFi.status() != WL_CONNECTED ){
WiFi.begin(ssid,password);
Serial.println();
Serial.print("Atempting connection to router: ");
Serial.println();
}
delay(5000);
if( WiFi.status() == WL_CONNECTED ){
Serial.print(“Internet connection estabilished!”);
Serial.println();
Serial.print("NodeMCU IP Address : ");
Serial.println(WiFi.localIP() );
}
delay(1000);
if( WiFi.status() != WL_CONNECTED ){
WiFi.begin(ssid2,password2);
Serial.println();
Serial.print(“Atempting connecting: (4G)”);
}
delay(5000);
if( WiFi.status() != WL_CONNECTED ){
Serial.println();
Serial.print(“Both gateways off!”);
}
}
I tried this code, to use cayenne on my ESP8266, with this contingency:
#include <CayenneMQTTESP8266.h>
// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial// WiFi network info.
char ssid=“Wifi hotspot A”;
char wifiPassword = “xxxx”;
char ssid2 = “Wifi hotspot B”;
char wifiPassword2 = “xxxx”;// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “xxxx”;
char password = “xxxxx”;
char clientID = “xxxx”;unsigned long lastMillis = 0;
void setup() {
Serial.begin(9600);
delay(1000);
pinMode(5, OUTPUT);
}void loop() {
Serial.begin(115200);
Serial.println();
if( WiFi.status() != WL_CONNECTED ){
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
Serial.println();
Serial.print("Atempting connection to router: ");
Serial.println();
}
delay(5000);
if( WiFi.status() == WL_CONNECTED ){
Serial.print(“Internet connection estabilished!”);
Serial.println();
Serial.print("NodeMCU IP Address : ");
Serial.println(WiFi.localIP() );
}
delay(1000);
if( WiFi.status() != WL_CONNECTED ){
Cayenne.begin(username, password, clientID, ssid2, wifiPassword2);
// WiFi.begin();
Serial.println();
Serial.print(“Atempting connecting: (4G)”);
}
delay(5000);
if( WiFi.status() != WL_CONNECTED ){
Serial.println();
Serial.print(“Both gateways off!”);
}
Cayenne.loop();
}CAYENNE_IN(0)
{
int currentValue = getValue.asInt();
if (currentValue == 1) {
digitalWrite(5, HIGH); // GPIO PIN
}
else
{
digitalWrite(5, LOW); // GPIO PIN
}
}
But the ESP8266 doesn’t connect to the secondary wi-fi hotspot (wifi hotspot B) when the main wifi hotspot is down. Sometimes the ESP8266 reboots alone, with no reason.
Any suggestions how to resolve this?
Thank you!