Connecting via wi-fi to cayenne

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!

not sure this will work, but try connecting to wifi in the disconnect loop

CAYENNE_CONNECTED() {
  Serial.println("connected.......");
}

CAYENNE_DISCONNECTED() {
  Serial.println("disconnected......");
}

Like this?

CAYENNE_DISCONNECTED() {
Serial.println(“disconnected…”);
Cayenne.begin(username, password, clientID, ssid2, wifiPassword2);
}

no, what you used for connecting to wifi.

Like this?

CAYENNE_DISCONNECTED() {
Serial.println(“disconnected…”);
WiFi.begin(ssid2,password2);
Cayenne.begin(username, password, clientID,);
}

Does it work?

Using the code below, the ESP8266 connectes sucessufuly to main wifi hotspot, but when that hotspot goes down, it does not connect to secondary hotspot.

#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 = “MAIN”;
char wifiPassword = “xxx”;
char ssid2=“Secondary”;
char wifiPassword2 = “xxxx”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = "xxx;
char password = “xxx”;
char clientID = “xxx”;

unsigned long lastMillis = 0;

void setup() {
Serial.begin(9600);
delay(1000);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(5, OUTPUT);
delay(5000);
}

void loop() {
Cayenne.loop();

}
CAYENNE_DISCONNECTED() {
Serial.print(“disconnected…”);
WiFi.begin(ssid2,wifiPassword2);
}

CAYENNE_IN(0)
{
int currentValue = getValue.asInt();
if (currentValue == 1) {
digitalWrite(5, HIGH); // GPIO PIN
}
else
{
digitalWrite(5, LOW); // GPIO PIN
}
}

i guess it wont work. let me try something out.

Thank you.

The ideia would be to replicate this code I showed, meaning it would run in the loop function, to check internet connection every x seconds, and if internet connection is off, the internet connection would pass to secondary wifi:

#include <ESP8266WiFi.h>

const char* ssid=“MAIN”;
const char* password = “xxxx”;

const char* ssid2=“SECONDARY”;
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!”);
}
}

Any suggestions?

this works for me:

/*
  This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.

  The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

  Steps:
  1. If you have not already installed the ESP8266 Board Package install it using the instructions here: https://github.com/esp8266/Arduino#installing-with-boards-manager.
  2. Select your ESP8266 board from the Tools menu.
  3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  4. Set the network name and password.
  5. Compile and upload the sketch.
  6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
*/

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid[] = "xxxx";
char wifiPassword[] = "xxx";

char ssid2[] = "xxxx";
char wifiPassword2[] = "xxxx";


// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "xxxx";
char password[] = "xxxx";
char clientID[] = "2xxxx";


void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop() {
  Cayenne.loop();
}

CAYENNE_DISCONNECTED() {
  WiFi.begin(ssid2, wifiPassword2);
}
// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
  Cayenne.virtualWrite(0, millis());
  // Some examples of other functions you can use to send data.
  //Cayenne.celsiusWrite(1, 22.0);
  //Cayenne.luxWrite(2, 700);
  //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

When the main wifi hotspot went off, the internet connection passed to the secondary wifi hotspot sucessfuly.
Is there any way to make a loop, as I shown in the previous code, where if the secondary wifi hotspot goes off, cayenne internet connection goes back to the main wifi hotspot?

Just use a variable and set it true or false and check it in if or else and set the appropriate wifi.

Will try it! Thank you for the support.

2 Likes