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();
}