#include "DHT.h"
#define CAYENNE_DEBUG // Uncomment to show debug messages
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#define DHTPIN 2
#define VIRTUAL_PIN 1
#define VIRTUAL_PIN2 2
#define RELAY_DIGITAL_PIN 4
#define RELAY_DIGITAL_PIN2 7
#include <CayenneEthernet.h>
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "82lc6v3ijh";
byte arduino_mac[] = { 0xFE, 0x6A, 0xCF, 0xAC, 0x9C, 0xD2 };
IPAddress arduino_ip(192, 168, 1, 100);
IPAddress dns_ip(192, 168, 1, 1);
IPAddress gateway_ip(192, 168, 1, 1);
IPAddress subnet_mask(255, 255, 255, 0);
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
// set digital pin to output
pinMode(RELAY_DIGITAL_PIN, OUTPUT);
pinMode(RELAY_DIGITAL_PIN2, OUTPUT);
Serial.begin(9600);
Serial.println("DHT22 test!");
Cayenne.begin(token, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
dht.begin();
}
void loop()
{
Cayenne.run();
// Wait a few seconds between measurements.
delay(5000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
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;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
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");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
}
CAYENNE_IN(VIRTUAL_PIN)
{
// 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(VIRTUAL_PIN2)
{
// 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);
}
}
// These functions are called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(V0)
{
Cayenne.virtualWrite(V0, dht.readTemperature());
}
CAYENNE_OUT(V1)
{
Cayenne.virtualWrite(V1, dht.readHumidity());
}
CAYENNE_OUT(V2)
{
Cayenne.virtualWrite(V2, dht.readTemperature(true));
}
CAYENNE_OUT(V3)
{
Cayenne.virtualWrite(V3, dht.readHumidity());
}
CAYENNE_OUT(V4)
{
Cayenne.virtualWrite(V4, dht.readTemperature(true));
}Preformatted text
The sketch work’s fine for a period of time (for example 1 hour) with the initials static ip definitions, but after the ip of arduino changes to 255.255.255.255.