#include <OneWire.h>
#include <DallasTemperature.h>
// If you’re not using the Ethernet W5100 shield, change this to match your connection type. See Communications examples.
//#include <CayenneEthernet.h>
#include <CayenneMQTTEthernet.h>
//GitHub - JChristensen/Timer: A fork of Simon Monk's Arduino Timer library
#include <Timer.h>
// Virtual Pin of the DS18B20 widget.
const int VIRTUAL_PIN20 =20;
const int VIRTUAL_PIN21 =21;
// Virtual Pin of the Relay widget.
const int VIRTUAL_PIN5 =5;
const int VIRTUAL_PIN7 =7;
const int VIRTUAL_PIN8 =8;
#define RELAY_DIGITAL_PIN5 5
// Virtual PIN input
const int VIRTUAL_PIN3 =3; // VirtualPin Stato Climatizzazione (Fotoresistenza)
#define DIGITAL_INPUT3 3 //Stato Climatizzazione (Fotoresistenza)
#define DIGITAL_INPUT9 9 //Stato centrale ( OC - inserito/disinserito )
// Digital pin the DS18B20 is connected to. Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define tmpPin 2 // PIN onewire
OneWire oneWire(tmpPin);
DallasTemperature sensors(&oneWire);
// Indirizzi sonde
DeviceAddress sondaext = {0x28, 0xFF, 0x96, 0xB2, 0x85, 0x16, 0x03, 0x06};//sonda esterna
DeviceAddress sondaint = {0x28, 0xF5, 0xD6, 0xAD, 0x08, 0x00, 0x00, 0x54};//sonda interna
// contatore
unsigned long counter = 0;// contatore per il tempo
// Variabili temperatura
float temp_ext = 0; // Temperatura esterna
float temp_int = 0; // Temperatura interna
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
// Cayenne MQTTauthentication token.
char username = “xxxxxxxxxxxxxxxx”;
char password = “xxxxxxxxxxxxxxxx”;
char clientID = “xxxxxxxxxxxxxxxx”;
// Mac address should be different for each device in your LAN
byte arduino_mac = { xxxxxxxxxxxxxxxx};
IPAddress arduino_ip(192, 168, 0, 210);
IPAddress dns_ip(8, 8, 8, 8);
IPAddress gateway_ip(192, 168, 0, 1);
IPAddress subnet_mask(255, 255, 255, 0);
// Timer
Timer t;
//
byte pw=0;
byte pw1=0;
void setup()
{
// set digital pin to output
pinMode(RELAY_DIGITAL_PIN5, OUTPUT);
// set relay to low —> OFF
digitalWrite(RELAY_DIGITAL_PIN5, LOW);
//
counter=millis();
//serial
Serial.begin(9600);
Cayenne.begin(username, password, clientID, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
// Dallas sensor begin
sensors.begin();
}
void loop()
{
Cayenne.loop();
// Update timer t
t.update();
//
if((millis()-counter)>1000){
syncData();
Temperatura();
counter=millis();
}
// delay
delay(1);
}
// FUNZIONE RICHIESTA TEMPERATURA A SENSORE DS18B20
void Temperatura(){
sensors.requestTemperatures(); //Richiesta temperatura
temp_ext = sensors.getTempC(sondaext); // sonda esterna
temp_int = sensors.getTempC(sondaint); // sonda interna
temp_int-=0.6; // Calibro la sonda interna … differenza misurata 0.4-0.7
}
// This function syncs the Virtual Pin 5 data.
void syncData()
{
//CAYENNE_LOG(“Sync data”);
// Sync the Virtual Pin. This sends a request to the server to resend data for the pin.
// This will cause the CAYENNE_IN function to be called.
Cayenne.syncVirtual(VIRTUAL_PIN5);
Cayenne.syncVirtual(VIRTUAL_PIN7);
Cayenne.syncVirtual(VIRTUAL_PIN8);
}
//
// This function is called when the Cayenne widget requests data for the Virtual Pin.
void function_temperatura_Interna(int VIRTUAL_PIN20)
{
//This command writes the temperature in Celsius to the Virtual Pin.
Cayenne.celsiusWrite(VIRTUAL_PIN20, temp_int);
}
void function_temperatura_Esterna(int VIRTUAL_PIN21)
{
//This command writes the temperature in Celsius to the Virtual Pin.
Cayenne.celsiusWrite(VIRTUAL_PIN21, temp_ext);
}
void function_climatizzazione(int VIRTUAL_PIN3)
{
//IF DIGITAL PIN IS LOW THEN VIRTUAL PIN IS HIGH
bool stato=digitalRead (DIGITAL_INPUT3);
if (stato == HIGH) {
Cayenne.virtualWrite (VIRTUAL_PIN3, 0);
}
else{
Cayenne.virtualWrite (VIRTUAL_PIN3, 1);
}
}
CAYENNE_IN(VIRTUAL_PIN7)
{
pw = getValue.asInt(); // 0 to 1
}
CAYENNE_IN(VIRTUAL_PIN8)
{
pw1 = getValue.asInt(); // 0 to 1
}
CAYENNE_IN(VIRTUAL_PIN5)
{
if (pw == 1 && pw1 == 1) {
//get value sent from dashboard
int currentValue = getValue.asInt(); // 0 to 1
// assuming you wire your relay as normally open
if (currentValue != digitalRead(DIGITAL_INPUT9) ) {
t.pulse(RELAY_DIGITAL_PIN5, 1500, 0); //pin, time(milliseconds), initial state
Cayenne.virtualWrite(VIRTUAL_PIN5, currentValue);
Cayenne.virtualWrite(VIRTUAL_PIN7, 0);
Cayenne.virtualWrite(VIRTUAL_PIN8, 0);
}
else{
Cayenne.virtualWrite(VIRTUAL_PIN5, digitalRead(DIGITAL_INPUT9) );
}
}
else {
Cayenne.virtualWrite(VIRTUAL_PIN5, digitalRead(DIGITAL_INPUT9) );
}
}