// This app will read the brightness reading off of the photoresistor. Then, if the read exceeds the threshold level, it will publish the variable stating that the frigde is open
int led = D0; // This is where your LED is plugged in. The other side goes to a resistor connected to GND.
int boardLed = D7; // This is the LED that is already on your device.
int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).
int power = A5;
int lightlevel;
int Fridge;
// The following values are the read values of the photoresistor
int Closedvalue=5; // This is the average value that the photoresistor reads while in the fridge.
int Openvalue=260; // This is the average value that the photoresistor reads when exposed to average lighting.
int Cutoff=((Closedvalue+Openvalue)/2);
bool Dooropen = false;
void setup() {
pinMode(led,OUTPUT); // Our LED pin is output (lighting up the LED)
pinMode(boardLed,OUTPUT); // Our on-board LED is output as well
pinMode(photoresistor,INPUT); // Our photoresistor pin is input (reading the photoresistor)
pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power)
digitalWrite(power,HIGH);
Particle.variable("analogvalue", &lightlevel, INT);
}
void loop() {
/* In this loop function, it checks to see if the light level means the door is open
When the status of the door changes, we'll send a Particle.publish()
It will also turn the LEDs on when the Photoresistor detects a light
*/
lightlevel=analogRead(photoresistor);
// Get some data
String data = String(10);
// Trigger the integration
//Particle.publish("Fridge", data, PRIVATE);
// Wait 60 seconds
//updateThingspeak();
delay(2000);
if (analogRead(photoresistor)<Cutoff){
if (Dooropen==true) {
// If the door was already open, then we assume you have closed it
// We will send a publish to the cloud and blink LED on.
// Send a publish to your devices...
Particle.publish("doorStatus","Closed",60);
// And flash the on-board LED on and off.
digitalWrite(boardLed,HIGH);
digitalWrite(led,HIGH);
delay(3000);
digitalWrite(boardLed,LOW);
digitalWrite(led,LOW);
// And change the status of the door
Dooropen=false;
}
else {
// Otherwise, this isn't a new status, and we don't have to do anything.
// This way, the device will not continue to send door open and door closed notifications if it is held open
}
}
else
// If you are above the threshold, assume the door is open
if (Dooropen==false) { // If the door is already closed, we must have opened it
// Send a publish...
Particle.publish("doorStatus","Open",60);
// And flash the LED on and off.
digitalWrite(boardLed,HIGH);
digitalWrite(led,HIGH);
delay(3000);
digitalWrite(boardLed,LOW);
digitalWrite(led,LOW);
// and change the status of the door
Dooropen=true;
}
else {
// Otherwise, this isn't a new status, and we don't have to do anything.
}
}
bool updateThingspeak() {
bool success = Particle.publish("Fridge",+ "{ \"1\": \"" + String(lightlevel) + "\"}",60,PRIVATE);
return success;
}
Comments