I’m using code from cayenne sketch file
#include <Adafruit_Sensor.h>
#include <CayenneMQTTSerial.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 3
#define DHTTYPE DHT22
#define VIRTUAL_CHANNEL 4
#define VIRTUAL_CHANNEL1 3
#define ACTUATOR_PIN 5
#define SENSOR_PIN 4
DHT_Unified dht (DHTPIN, DHTTYPE);
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “c0dfc5c0-4770-11e8-8927-a713813a39dc”;
char password = “4f2757dd88577c48c133cc80231ac94498e0e148”;
char clientID = “3b3e27a0-5741-11e8-86cb-01fd27f5d39c”;
void setup()
{
//Baud rate can be specified by calling Cayenne.begin(username, password, clientID, 9600);
Serial.begin(9600);
Cayenne.begin(username, password, clientID);
dht.begin();
pinMode(ACTUATOR_PIN, OUTPUT);
sensor_t sensor;
}
void loop() {
Cayenne.loop();
checkSensor();
}
int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;
// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
void checkSensor()
{
unsigned long currentMillis = millis();
// Check sensor data every 150 milliseconds
if (currentMillis - previousMillis >= 150) {
// Check the sensor state and send data when it changes.
currentState = digitalRead(SENSOR_PIN);
if (currentState != previousState) {
Cayenne.virtualWrite(VIRTUAL_CHANNEL, currentState);
previousState = currentState;
}
previousMillis = currentMillis;
}
}
// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
//Cayenne.virtualWrite(0, millis());
sensors_event_t event;
dht.temperature().getEvent(&event);
Cayenne.celsiusWrite(1, event.temperature);
dht.humidity().getEvent(&event);
Cayenne.virtualWrite(2, event.relative_humidity, “rel_hum”, “p”);
}
// 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(VIRTUAL_CHANNEL1)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN, LOW);
}
}