#include <DHT.h>
#include <DHT_U.h>
#define CAYENNE_DEBUG
//#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneTemperature.h>
#include <CayenneMQTTEthernet.h>
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";
#define VIRTUAL_CHANNEL_LED2 2
#define ACTUATOR_PIN_LED2 2
#define VIRTUAL_CHANNEL_LED3 3
#define ACTUATOR_PIN_LED3 3
#define VIRTUAL_CHANNEL_FAN1 4
#define ACTUATOR_PIN_FAN1 4
#define VIRTUAL_CHANNEL_Temp 1 //is temp 36 sensor
//#define VIRTUAL_CHANNEL_DHT2 2
#define DHTPIN 8
#define DHTTYPE DHT11
#define TEMPERATURE_VIRTUAL_CHANNEL 12
#define HUMIDITY_VIRTUAL_CHANNEL 11
#define SENSOR_PIN_Motion 9
#define VIRTUAL_CHANNEL_Motion 9
#define VIRTUAL_CHANNEL_DOORLOCK 10
#define ACTUATOR_PIN_DOORLOCK 10
//Motion Sensor
int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;
// Analog pin the TMP36 is connected to.
const int tmpPin = 0;
const float voltage = 5.0;
TMP36 tmpSensor(tmpPin, voltage);
//DHT SENSOR
DHT dht(DHTPIN, DHTTYPE);
void setup() {
pinMode(SENSOR_PIN_Motion, INPUT);
Serial.begin(9600);
pinMode(ACTUATOR_PIN_LED2, OUTPUT);
pinMode(ACTUATOR_PIN_LED3, OUTPUT);
pinMode(ACTUATOR_PIN_DOORLOCK, OUTPUT);
Cayenne.begin(username, password, clientID);
dht.begin();
}
void loop() {
Cayenne.loop();
checkSensor();
}
//Temp sensor
CAYENNE_OUT(VIRTUAL_CHANNEL_Temp)
{
// This command writes the temperature in Celsius to the Virtual Channel.
Cayenne.celsiusWrite(VIRTUAL_CHANNEL_Temp, tmpSensor.getCelsius());
// To send the temperature in Fahrenheit or Kelvin use the corresponding code below.
// Cayenne.fahrenheitWrite(VIRTUAL_CHANNEL_Temp, tmpSensor.getFahrenheit());
//Cayenne.kelvinWrite(VIRTUAL_CHANNEL_Temp, tmpSensor.getKelvin());
}
//motion sensor
void checkSensor()
{
unsigned long currentMillis = millis();
// Check sensor data every 250 milliseconds
if (currentMillis - previousMillis >= 250) {
// Check the sensor state and send data when it changes.
currentState = digitalRead(SENSOR_PIN_Motion);
if (currentState != previousState) {
Cayenne.virtualWrite(VIRTUAL_CHANNEL_Motion, currentState, "digital_sensor", "d");
previousState = currentState;
}
previousMillis = currentMillis;
}
}
//LED2
CAYENNE_IN(VIRTUAL_CHANNEL_LED2)
{
int value = getValue.asInt();
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_LED2, ACTUATOR_PIN_LED2, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_LED2, value);
}
//LED3
CAYENNE_IN(VIRTUAL_CHANNEL_LED3)
{
int value = getValue.asInt();
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_LED3, ACTUATOR_PIN_LED3, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_LED3, value);
}
//FAN
CAYENNE_IN(VIRTUAL_CHANNEL_FAN1)
{
int value = getValue.asInt();
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_FAN1, ACTUATOR_PIN_FAN1, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_FAN1, value);
}
//DHT SENSOR
CAYENNE_OUT(TEMPERATURE_VIRTUAL_CHANNEL)
{
Cayenne.virtualWrite(TEMPERATURE_VIRTUAL_CHANNEL, dht.readTemperature(), "temp", "c");
}
CAYENNE_OUT(HUMIDITY_VIRTUAL_CHANNEL)
{
Cayenne.virtualWrite(HUMIDITY_VIRTUAL_CHANNEL, dht.readHumidity(), "rel_hum", "p");
}
//DOORLOCK
CAYENNE_IN(VIRTUAL_CHANNEL_DOORLOCK)
{
int value = getValue.asInt();
CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_DOORLOCK, ACTUATOR_PIN_DOORLOCK, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_DOORLOCK, value);
}
Arduino uses digital pins 10, 11, 12, and 13 (SPI) to communicate with the W5100 on the ethernet shield. so do not use it.
Other then this, i have not found any issue in the code.
i know you are using arduino uno, but is your arduino uno capable to supply enough power to the fan? You might want to check the specs of both arduino uno pins and the fan.
if you want to check, add the #define CAYENNE_PRINT Serial back in your code and check the serial monitor if you are receiving the data from cayenne when the button is pressed.