//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTYun.h>
char username =
char password = "
char clientID = "
#include “DHT.h”
#define DHTPIN 2
#define DHTTYPE DHT22
#define VIRTUAL_PIN 3
#define RELAY_DIGITAL_PIN 3
#define VIRTUAL_PIN2 4
#define VIRTUAL_PIN3 5
#define VIRTUAL_PIN4 6
#define RELAY_DIGITAL_PIN2 4
#define RELAY_DIGITAL_PIN3 5
#define RELAY_DIGITAL_PIN4 6
#define RELAY_DIGITAL_PIN5 7
#define RELAY_DIGITAL_PIN6 8
#define RELAY_DIGITAL_PIN7 9
#define RELAY_DIGITAL_PIN8 10
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
pinMode(RELAY_DIGITAL_PIN, OUTPUT);
pinMode(RELAY_DIGITAL_PIN2, OUTPUT);
pinMode(RELAY_DIGITAL_PIN3, OUTPUT);
pinMode(RELAY_DIGITAL_PIN4, OUTPUT);
pinMode(RELAY_DIGITAL_PIN5, OUTPUT);
pinMode(RELAY_DIGITAL_PIN6, OUTPUT);
pinMode(RELAY_DIGITAL_PIN7, OUTPUT);
pinMode(RELAY_DIGITAL_PIN8, OUTPUT);
dht.begin();
}
void loop() {
Cayenne.loop();
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) ) {
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 ");
}
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 ");
}
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(“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);
}
}