Esp8266 reset if connexion lost

Hello, is there a way to set my esp to reset when it loses connection with cayenne? There are cases in which I do not have a very stable connection to wifi and the relays remain on. Thank you!

you can turn OFF relay here:

CAYENNE_DISCONNECTED() {
    digitalWrite(relay_pin, LOW);
}

Thanks for the reply. Unfortunately i can’t implement the code. This is the error message.

"Arduino: 1.8.13 (Windows Store 1.8.42.0) (Windows 10), Board: “NodeMCU 0.9 (ESP-12 Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:none OTA:~1019KB), v2 Lower Memory, Disabled, None, Only Sketch, 115200”

In file included from C:\Users\Darius\Documents\Arduino\libraries\CayenneMQTT\src/CayenneArduinoDefines.h:128:0,

             from C:\Users\Darius\Documents\Arduino\libraries\CayenneMQTT\src/CayenneArduinoMQTTClient.h:21,

             from C:\Users\Darius\Documents\Arduino\libraries\CayenneMQTT\src/CayenneMQTTWiFiClient.h:21,

             from C:\Users\Darius\Documents\Arduino\libraries\CayenneMQTT\src/CayenneMQTTESP8266.h:27,

             from C:\Users\Darius\Desktop\config_controller\ControllerNicu\sketch_sep05a\Controller1\proba_relee_reset\sketch_sep08b\sketch_sep08b.ino:4:

C:\Users\Darius\Desktop\config_controller\ControllerNicu\sketch_sep05a\Controller1\proba_relee_reset\sketch_sep08b\sketch_sep08b.ino: In function ‘void CayenneDisconnected()’:

C:\Users\Darius\Documents\Arduino\libraries\CayenneMQTT\src/CayenneHandlers.h:78:37: error: redefinition of ‘void CayenneDisconnected()’

#define CAYENNE_DISCONNECTED() void CayenneDisconnected()

                                 ^

C:\Users\Darius\Desktop\config_controller\ControllerNicu\sketch_sep05a\Controller1\proba_relee_reset\sketch_sep08b\sketch_sep08b.ino:209:1: note: in expansion of macro ‘CAYENNE_DISCONNECTED’

CAYENNE_DISCONNECTED()

^

C:\Users\Darius\Documents\Arduino\libraries\CayenneMQTT\src/CayenneHandlers.h:78:37: error: ‘void CayenneDisconnected()’ previously defined here

#define CAYENNE_DISCONNECTED() void CayenneDisconnected()

                                 ^

C:\Users\Darius\Desktop\config_controller\ControllerNicu\sketch_sep05a\Controller1\proba_relee_reset\sketch_sep08b\sketch_sep08b.ino:71:3: note: in expansion of macro ‘CAYENNE_DISCONNECTED’

CAYENNE_DISCONNECTED() {

^

Multiple libraries were found for “CayenneMQTTESP8266.h”

Used: C:\Users\Darius\Documents\Arduino\libraries\CayenneMQTT

Not used: C:\Users\Darius\Documents\Arduino\libraries\Cayenne-MQTT-ESP-master

exit status 1

Error compiling for board NodeMCU 0.9 (ESP-12 Module).

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

This is my code, if you notice other mistakes please let me know.

// #define CAYENNE_DEBUG Serial
// #define CAYENNE_PRINT Serial

#include <CayenneMQTTESP8266.h>
#include <DHT.h>
#include “ESP8266WiFi.h”
#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#define DHTPIN 13
#define DHTTYPE DHT22 // DHT 22 (AM2302)

#define RELAY1_PIN D1 // RELAY1 PIN
#define RELAY2_PIN D2 // RELAY2 PIN
#define RELAY3_PIN D3 // RELAY3 PIN
#define RELAY4_PIN D4 // RELAY4 PIN
#define RELAY5_PIN D5 // RELAY5 PIN
#define RELAY6_PIN D6 // RELAY6 PIN

char ssid = “xxxx”;
char wifiPassword = “xxxx”;
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “xxxxx”;
char password = “xxxxx”;
char clientID = “xxxxx”;

bool online = false;

unsigned long onlineTime;
unsigned long prevTime;
unsigned long time4;

int disco = 0;

byte restartChip = 0;

DHT dht(DHTPIN, DHTTYPE);

void setup()
{
dht.begin();
pinMode(BUILTIN_LED, OUTPUT);

// Serial.begin(9600);

Cayenne.begin(username, password, clientID, ssid, wifiPassword);

time4 = millis();

pinMode(RELAY1_PIN, OUTPUT);
pinMode(RELAY2_PIN, OUTPUT);
pinMode(RELAY3_PIN, OUTPUT);
pinMode(RELAY4_PIN, OUTPUT);
pinMode(RELAY5_PIN, OUTPUT);
pinMode(RELAY6_PIN, OUTPUT);

digitalWrite(RELAY1_PIN, HIGH);
digitalWrite(RELAY2_PIN, HIGH);
digitalWrite(RELAY3_PIN, HIGH);
digitalWrite(RELAY4_PIN, HIGH);
digitalWrite(RELAY5_PIN, LOW);
digitalWrite(RELAY6_PIN, LOW);
}

CAYENNE_OUT_DEFAULT()
{
Cayenne.virtualWrite(0, millis());
}

CAYENNE_DISCONNECTED() {
digitalWrite(RELAY1_PIN, HIGH);
digitalWrite(RELAY2_PIN, HIGH);
digitalWrite(RELAY3_PIN, HIGH);
digitalWrite(RELAY4_PIN, HIGH);
digitalWrite(RELAY5_PIN, LOW);
digitalWrite(RELAY6_PIN, LOW);
}

void loop()
{
Cayenne.loop();
int sensorValue = analogRead(A0);
Serial.println(analogRead(A0));
float temp = dht.readTemperature(true);
float hum = dht.readHumidity();
Cayenne.virtualWrite(13, temp, TYPE_TEMPERATURE, UNIT_CELSIUS);
Cayenne.virtualWrite(14, hum, TYPE_RELATIVE_HUMIDITY, UNIT_PERCENT);

if (millis() - time4 > 20000) // used to send data to dashboard every 20 seconds
{
execute();
}

if (online == true) // blink builtin LED if online
{
digitalWrite(BUILTIN_LED, LOW);
delay(10);
digitalWrite(BUILTIN_LED, HIGH);
}
else
{
digitalWrite(BUILTIN_LED, HIGH); //turn off LED if offline
}
}

CAYENNE_IN(2)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1

// assuming you wire your relay as normally open
if (currentValue == 0) {
digitalWrite(RELAY1_PIN, HIGH);
} else {
digitalWrite(RELAY1_PIN, LOW);
}
}

CAYENNE_IN(4)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1

// assuming you wire your relay as normally open
if (currentValue == 0) {
digitalWrite(RELAY2_PIN, HIGH);
} else {
digitalWrite(RELAY2_PIN, LOW);
}
}

CAYENNE_IN(6)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1

// assuming you wire your relay as normally open
if (currentValue == 0) {
digitalWrite(RELAY3_PIN, HIGH);
} else {
digitalWrite(RELAY3_PIN, LOW);
}
}

CAYENNE_IN(8)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1

// assuming you wire your relay as normally open
if (currentValue == 0) {
digitalWrite(RELAY4_PIN, HIGH);
} else {
digitalWrite(RELAY4_PIN, LOW);
}
}

CAYENNE_IN(10)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1

// assuming you wire your relay as normally open
if (currentValue == 0) {
digitalWrite(RELAY5_PIN, LOW);
} else {
digitalWrite(RELAY5_PIN, HIGH);
}
}

CAYENNE_IN(12)
{
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1

// assuming you wire your relay as normally open
if (currentValue == 0) {
digitalWrite(RELAY6_PIN, LOW);
} else {
digitalWrite(RELAY6_PIN, HIGH);
}
}

void execute()
{
time4 = millis();

Cayenne.virtualWrite(23, disco, “counter”, “null”);

int time2 = (millis() / 1000) / 60;
Cayenne.virtualWrite(24, time2, “counter”, “null”);

onlineTime = ((millis() - prevTime) / 1000) / 60;
Cayenne.virtualWrite(25, onlineTime, “counter”, “null”);
}

CAYENNE_CONNECTED()
{
online = true;
prevTime = millis();
}

CAYENNE_DISCONNECTED()
{
online = false;
disco++;
}

CAYENNE_IN(1)
{
restartChip = getValue.asInt();

delay(1500);

if (restartChip == 1) // if button is pressed
{
Cayenne.virtualWrite(1, 0, “digital_sensor”, “d”); // put button back to off on dashboard
Cayenne.loop(); //updates the above virtualWrite to the dashboard
delay(1500); //wait for data to transmit just encase
ESP.restart(); //triggers a restart
}
}

CAYENNE_OUT(V15)
{
int sensorValue = analogRead(A0); // rain sensor
Cayenne.virtualWrite(V15, sensorValue);
delay(1000);
}

you have two CAYENNE_DISCONNECTED functions