Esp relaywith reset

hi, I’m using an esp286, everything works perfectly, but when it goes off the power and restart does not come back as a dashboard setting but starts with the relay all closed, can you help me?
//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include “DHT.h”
#define DHTPIN D2
#define DHTTYPE DHT22
#define VIRTUAL_PIN 3
#define RELAY_DIGITAL_PIN D1
#define VIRTUAL_PIN2 4
#define VIRTUAL_PIN3 5
#define VIRTUAL_PIN4 6
#define RELAY_DIGITAL_PIN2 D3
#define RELAY_DIGITAL_PIN3 D4
#define RELAY_DIGITAL_PIN4 D5
// WiFi network info.
char ssid = “”;
char wifiPassword = “”;

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

unsigned long lastMillis = 0;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
pinMode(RELAY_DIGITAL_PIN, OUTPUT);
pinMode(RELAY_DIGITAL_PIN2, OUTPUT);
pinMode(RELAY_DIGITAL_PIN3, OUTPUT);
pinMode(RELAY_DIGITAL_PIN4, OUTPUT);
dht.begin();
Cayenne.begin(username, password, clientID, ssid, wifiPassword);

}

void loop() {
Cayenne.loop();
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(“Failed to read from DHT sensor!”);
return;

Serial.print(“Humidity: “);
Serial.print(h);
Serial.print(” %\t”);
Serial.print(“Temperature: “);
Serial.print(t);
Serial.print(” *C “);
Serial.print(f);
Serial.print(” *F\t”);

}
//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
if (millis() - lastMillis > 10000) {
lastMillis = millis();
//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
Cayenne.virtualWrite(0, lastMillis);
Cayenne.virtualWrite(1, h);
Cayenne.virtualWrite(2, t);
Serial.print(“Humidity: “);
Serial.print(h);
Serial.print(” %\t”);
Serial.print("Temperature: “);
Serial.print(t);
Serial.println(” *C ");
}
}

//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(3)
{
CAYENNE_LOG(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel, getValue.getId(), getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1

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

CAYENNE_IN(4)
{CAYENNE_LOG(“Got a value: %s”, getValue.asInt());
CAYENNE_LOG(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel, getValue.getId(), getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1

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

}
CAYENNE_IN(5)
{
CAYENNE_LOG(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel, getValue.getId(), getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1

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

}
CAYENNE_IN(6)
{
CAYENNE_LOG(“CAYENNE_IN_DEFAULT(%u) - %s, %s”, request.channel, getValue.getId(), getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(“Error message”);
// get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1

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

}
CAYENNE_CONNECTED()
{
CAYENNE_LOG(“Connection established”);

Cayenne.syncAll();

}

So your trying to get the relay to default to on when the esp restarts?

whatever state you want of the relay, write it in void setup.

no I would like it to start the same way I left it in the dashboard, or at least that the dashboard perceive the status of the relay in order to read the status of the relay, to indent us as does the native dashboard of cayenne with arduino one

You could write in setup after Cayenne.begin to call a virtual write to your relay actuator pin. That way you could sync the state to either on or off when it powers on. I don’t think there is a way using MQTT to get the dashboard to resend its button states on connection. I’m not sure though

How can I then send the status of the real relays to the dashboard?

To get the state of the relay on power on to the dashboard, look at my example below that runs in setup. It sets the dashboard states to the same as the arduino so no matter what the last state was on the dashboard, on reset both match. randomProgram is a button, program and wait are sliders on the dashboard.

If your trying to get the dashboard state on reset, I don’t believe thats possible right now. I don’t believe the Cayenne.syncAll() does anything with MQTT connection, I think thats just for the older Cayenne connection. Someone correct me if I’m wrong :slight_smile:

byte randomProgram = 0;
byte program = 4;
int wait = 200;

void setup()
{
	system_update_cpu_freq(160);
	system_phy_set_max_tpw(82);
	wifi_set_opmode(0x01);

	// Serial.begin(9600);

	Wire.begin(sdaPin,sclPin);
	Wire.setClockStretchLimit(40000);



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

    //below sets the dashboard states, needs to be after Cayenne.begin()

	Cayenne.virtualWrite(2, randomProgram, "digital_actuator", "d");
	Cayenne.virtualWrite(1, program, "analog_actuator", "null");
	Cayenne.virtualWrite(3, wait, "analog_actuator", "null");
	Cayenne.virtualWrite(20, 0, "digital_sensor", "d");
}
1 Like

grazie mille:relaxed: