//Sets pin D7 as the power toggle pin
const int tog = D7;
//initializes temperature
int temperature_f;
int Light_Position;
void setup() {
pinMode(tog, OUTPUT);
//The relay is wired as normally closed and is non-latching so that if the photon loses power the light will remain on, setting the pin value to high defaults the light to the off position by opening the relay circuit.
digitalWrite(tog, HIGH);
//Subscribes the device to the temperature data coming from the first photon and send it to the temperature handler function
Particle.subscribe("TempSensor", temperatureHandler, ALL_DEVICES);
//Compares the temperature value pulled from the cloud to the values set by the user to prevent the well from freezing
}
void loop() {
if (temperature_f >= 60){
//At a temperature greater than 60 degrees F it opens the circuit and turns off the light
digitalWrite(tog, LOW), Light_Position = 0;
}
else if (temperature_f <= 55){
//At temperatures lower than 55 degrees F it will close the circuit and turn on the light
digitalWrite(tog, HIGH), Light_Position = 1;
}
else{
//Reading any temperature in the "deadzone" restarts the loop
}
Particle.publish("Light", String(Light_Position), PUBLIC);
delay(4000);
}
//Takes the string data retrieved from the cloud and converts it to an integer for comparison
void temperatureHandler(const char *event, const char *data){
//sets a new string, which i have defined as "pew" to take in the temperature data
String pew = data;
//Converts this new string "pew" into the previously defined integer "temperature"
temp_f = pew.toInt();
}
Comments