Hola, este es el codigo:
#include <SimpleDHT.h>
#include <SimpleTimer.h>
//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
// WiFi network info.
char ssid = “”;
char wifiPassword = “”;
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “”;
char password = “”;
char clientID = “”;
#define DATA_CHANNEL 10 // Canal virtual para publicar datos del sensor.
#define TRIGGER_CHANNEL 1// Canal virtual para publicar el valor de activación.
#define THRESHOLD 1 // Umbral para el activador.
bool sendBelowThreshold = true;
bool crossedThreshold = false;
// DHT11 Pin
int pinDHT11 = D3;
SimpleDHT11 dht11;
// Timer
SimpleTimer timer;
define SENSOR_PIN D6
define SENSOR_PIN D7
define SENSOR_PIN D8
define VIRTUAL_CHANNEL 6
define VIRTUAL_CHANNEL 14
define VIRTUAL_CHANNEL 10
define VIRTUAL_CHANNEL 12
void setup() {
// Iniciamos la comunicación serie
Serial.begin(115200);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
timer.setInterval(3600, transmitData); // Method to execute every 500ms
}
void sendTriggerValue(int channel, int value, int threshold, bool sendBelowThreshold) {
if (((value >= threshold) && !sendBelowThreshold) || ((value < threshold) && sendBelowThreshold)) {
if (!crossedThreshold) {
Cayenne.virtualWrite(channel, 1, “digital_sensor”, “d”); //set trigger two-state widget to 1
crossedThreshold = true;
}
}
else
{
Cayenne.virtualWrite(channel, 0, “digital_sensor”, “d”); //set trigger two-state widget to 0
crossedThreshold = false;
}
}
void loop() {
Cayenne.loop();
timer.run();
// Mandamos comandos para toma de temperatura a los sensores
Serial.println(“Mandando comandos a los sensores”);
}
CAYENNE_OUT(10)
{
Cayenne.virtualWrite(10, digitalRead(D7), “digital_sensor”, “d”);
int sensor_value =digitalRead(D7);
sendTriggerValue(TRIGGER_CHANNEL, sensor_value, THRESHOLD, sendBelowThreshold);
Cayenne.virtualWrite(DATA_CHANNEL, sensor_value , “digital_sensor”, “null”); //widget to display sensor data.
}
CAYENNE_OUT(12)
{
Cayenne.virtualWrite(12, digitalRead(D8), “digital_sensor”, “d”);
}
void transmitData()
{
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Cayenne.virtualWrite(V4, 0);
Cayenne.virtualWrite(V2, 0);
}
else {
Cayenne.virtualWrite(V4, (int)temperature);
Cayenne.virtualWrite(V2, (int)humidity);
}
}