#include "DHT.h"//DHT Sensor Library
#define switchPin 22
#define relayPin 17
#define presencePin 33
#define tempPin 27
DHT dht(tempPin, DHT22);
int physicalSwitchState = 0;
boolean relayState = false;
boolean systemRelayState = false;
float temperature = 0;
boolean presenceState = 0;
void setup() {
pinMode(switchPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
pinMode(presencePin, INPUT);
dht.begin();
}
void loop() {
checkForUpdates();
int physicalSwitchRead = digitalRead(switchPin);
if (physicalSwitchRead!=physicalSwitchState) physicalSwitchToggled(physicalSwitchRead);
physicalSwitchState=physicalSwitchRead;
digitalWrite(relayPin, (relayState ? HIGH : LOW));
float temperatureRead = dht.readTemperature();
if (temperatureRead != temperature) temperatureChanged(temperatureRead);
temperature = temperatureRead;
int presenceRead = digitalRead(presencePin);
if (presenceRead != presenceState) presenceChanged(presenceRead);
presenceState = presenceRead;
delay(250);
}
void physicalSwitchToggled(int state){
/*physical switch is manual override
keeping the boiler turned on but cannot force
it to turn off*/
if (!systemRelayState){toggleRelay();}
/*connect to IoT hub
to update it about the physical switch
has been toggled*/
}
void temperatureChanged(float temp){
/*connect to IoT hub
to update it that the temp has changed*/
}
void presenceChanged(int state){
/*connect to IoT hub
to update it that the presence has changed
so the room has become occupied or unoccupied*/
}
void toggleRelay(){
relayState = !relayState;
}
void checkForUpdates(){
/*connect to IoT hub
check if light value has updated
if has updated then toggle the relay to keep in line*/
/*not actually doing any IoT for this example but would
call toggleRelay if the state had changed and
update the systemRelayState*/
}