// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.
#define CAYENNE_PRINT Serial
#define DHTTYPE DHT22
#include <CayenneMQTTESP8266.h>
#include âDHT.hâ
// WiFi network info.
char ssid = âMyNetworkâ;
char wifiPassword = âMyNetworkCodeâ;
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = â";
char password[] = "â;
char clientID = â******â;
unsigned long lastmillis = 0;
// DHT Sensor
const int DHTPin = 5;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);
// Temporary variables
static char celsiusTemp[7];
static char humidityTemp[7];
boolean lamp_state = 0;
void sendSensor()
{
*****Big block of measuring
}
void setup() {
Serial.begin(115200);
delay(10);
// GPIO
pinMode(14, OUTPUT);
digitalWrite(14, 1);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
Cayenne.virtualWrite(3,1,âdigital_sensorâ,âdâ); //change state to ON
Cayenne.virtualWrite(4,1,âdigital_sensorâ,âdâ); //change state to ON
}
void loop() {
Cayenne.loop();
//delay millisecond
if (millis() - lastmillis > 60000){
lastmillis = millis ();
sendSensor();
}
}
// 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());
//Cayenne.virtualwrite(3,lamp_state,âdigital_sensorâ,âdâ);
// Some examples of other functions you can use to send data.
//Cayenne.celsiusWrite(1, 22.0);
//Cayenne.luxWrite(2, 700);
//Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}
// 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_DEFAULT()
{
CAYENNE_LOG(âChannel %u, value %sâ, request.channel, getValue.asString());
//Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError(âError messageâ);
}
CAYENNE_IN(4){
lamp_state = getValue.asInt();
Cayenne.virtualWrite(3,lamp_state,âdigital_sensorâ,âdâ);
if (lamp_state == 1){
digitalWrite(14, 1);
}
else {
digitalWrite(14, 0);
}
Serial.println(â----------------------------------------------â);
Serial.print(âLamp1 on/off:â);
Serial.println(lamp_state);
Serial.println(â----------------------------------------------â);
}