Shane McManusVarnell
Published

MEGR3171 Refrigerator Alarm

This project helps you save food and electricity by telling you when the refrigerator door has been left open.

BeginnerFull instructions provided6 hours645
MEGR3171 Refrigerator Alarm

Things used in this project

Hardware components

Photon
Particle Photon
×2
Photo resistor
Photo resistor
×1
Buzzer, Piezo
Buzzer, Piezo
×1
Breadboard (generic)
Breadboard (generic)
×2
Jumper wires (generic)
Jumper wires (generic)
×1
LED (generic)
LED (generic)
×1
Resistor 220 ohm
Resistor 220 ohm
×2

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
Maker service
IFTTT Maker service
Google Sheets
Google Sheets

Story

Read more

Schematics

Buzzer

Buzzer that indicates when the fridge door is open.

Photo Resistor

This is the sensor that is placed in the refrigerator. It will detect when the light is on or off.

Buzzer Circuit

Photo shows the Buzzer circuit

Fridge Sensor

This photo shows the setup of the photo resister circuit located in the fridge.

LED Backup

This shows the LED lit up inside the fridge indicating the door is closed.

Code

Refrigerator Saver

C/C++
The Refrigerator Saver is placed inside of the refrigerator. This device controlled by a photon and powered by a usb-micro power bank ensures you don't forget it open overnight or while in a rush to leave the house with the help of photoresistors and a piezo buzzer.
// -----------------------------------------
// Refrigerator Saver
// -----------------------------------------
// This app is designed to calibrate temperature and light sensors
// monitor temperature and light levels
// and alarm the user when thresholds have been exceeded for too long.
// The refrigerator sensor will stay inside of the fridge powered by a 
// power bank.

 /*                           +-----+
 *                 +----------| USB |----------+
 *                 |          +-----+       *  |
 *                 | [ ] VIN           3V3 [ ] |
 *                 | [ ] GND           RST [ ] |
 *                 | [ ] TX           VBAT [ ] |
 *                 | [ ] RX  [S]   [R] GND [ ] |
 *                 | [ ] WKP            D7 [ ] |
 *                 | [ ] DAC +-------+  D6 [ ] |
 *   Opensensor -->| [*] A5  |   *   |  D5 [ ] |
 * Closedsensor -->| [*] A4  |Photon |  D4 [*] |<-- DHT21 AM2301 Sensor
 *                 | [*] A3  |       |  D3 [ ] |
 *                 | [ ] A2  +-------+  D2 [ ] |
 *                 | [ ] A1             D1 [ ] |
 *  Analog0     -->| [ ] A0             D0 [*] |<-- Garage Door Switch Relay
 *                 |                           |
 *                  \    []         [______]  /
 *                   \_______________________/
 *
 *
 */

int led = D0; // when led is lit up, other photon will sound alarm
int boardLed = D7; // board led is used for calibration
unsigned long startTime = 0; //initialize timer



int photoresistor = A0; // Photoresistor plugged in pin A0

int power = A5; // Photoresistor plugged in pin A5

// The following values get set up when your device boots up and calibrates:
int strongValue; // This is the average value that the photoresistor reads when the beam is intact.
int weakValue; // This is the average value that the photoresistor reads when the beam is broken.
int beamAverage; // This it the average of the on and off values


// setup function for one time events outside of main loop

void setup() {
  // setup for photon pins and auxiliary
  pinMode(led,OUTPUT); // output pin for LED (D0)
  pinMode(boardLed,OUTPUT); // Our on-board LED is output as well
  pinMode(photoresistor,INPUT);  // input pin for photoresistor (A0)
  pinMode(power,OUTPUT); // output pin for powering photoresistor (A5)
    Particle.subscribe("Level6", myHandler); //subscribe function for IOT cloud communicaton
    

  // Acquire maximum power possible of 4095 in analog.
  digitalWrite(power,HIGH);

  // Begin calibration and user input

  // First, close refrigerator door as D7 indicates and calibrate with light off.
  digitalWrite(boardLed,HIGH);
  delay(2000); //delay two seconds

  // D7 will turn off after two seconds and the LED should be on as you open the door.
  digitalWrite(boardLed,LOW);
  digitalWrite(led,HIGH);
  delay(500); //delay 0.5 seconds

  // Photoresistor will begin reading the refrigerator light
  int on_first = analogRead(photoresistor); // read photoresistor
  delay(500); // delay 0.5 seconds
  int on_second = analogRead(photoresistor); // read photoresistor
  delay(500); // delay 0.5 seconds

  // D7 flashes to initialize light  calibration
  digitalWrite(boardLed,HIGH); //D7 on
  delay(100); //delay one second
  digitalWrite(boardLed,LOW); //D7 off
  delay(100); //delay one second
  digitalWrite(boardLed,HIGH); //D7 on
  delay(100); //delay one second
  digitalWrite(boardLed,LOW); //D7 off
  delay(100); //delay one second

  // D7 indicates door is read to be opened
  digitalWrite(boardLed,HIGH);
  delay(2000); //delay two seconds

  // The D7 LED will turn off...
  digitalWrite(boardLed,LOW);
  digitalWrite(led,LOW);

  // Calibrate photoresistor again with D7 light off.
  int off_first = analogRead(photoresistor); // read photoresistor
  delay(500); // wait .5 seconds
  int off_second = analogRead(photoresistor); // read photoresistor
  delay(1000); // wait 1 second

  // D7 flashes to signal photon is ready for use
  digitalWrite(boardLed,HIGH); //D7 on
  delay(100); //delay one second
  digitalWrite(boardLed,LOW); //D7 off
  delay(100); //delay one second
  digitalWrite(boardLed,HIGH); //D7 on
  delay(100);//delay one second
  digitalWrite(boardLed,LOW); //D7 off
  delay(100);//delay one second

  // Average values to complete calibration
  strongValue = (on_first+on_second)/2;
  weakValue = (off_first+off_second)/2;

  // Calculating average value, below which the light is considered off
  beamAverage = (strongValue+weakValue)/2;

}

// Now for the loop.

void loop() {
  /* This loop function, checks to see if the refrigerator light is on or off.
  When the light goes off it triggers the nobuzz command is published.
  When the light is on for too long the buzz command is published.
  */
while (analogRead(photoresistor)<beamAverage)//while refrigerator light off

{
    digitalWrite(led,LOW); //LED initially off
    startTime = millis(); //begin timer essentially at zero
    Particle.publish("Level5", "nobuzz"); //publish buzzer off to Level 6
    Particle.publish("Level5", String(millis()-startTime)); //publish time door closed to Level 6
     if(digitalRead(led) == LOW) //when receving low LED signal from Level 6
     {digitalWrite(led, HIGH); //use to turn LED when refrigerator is dark to indicate blown bulb as failsafe
     }

}
 while (analogRead(photoresistor)>beamAverage) //while refrigerator light on
    {
        Particle.publish("Level5", "Dooropen"); //Publish that the door has been opened to Level 6
        digitalWrite(led,LOW); //turn LED off when light is on
        
    if (millis()-startTime > 10000UL) //10000UL = 10 seconds
                                        //begin timer as long as door is open
                                        //default time is 1 minute, but easily
                                        //adjustable to suit the customers needs
    {
    
    
    Particle.publish("Level5", "buzz");             //after one minute signal buzzer to alert owner
    Particle.publish(String(millis()-startTime));   //publish time open for data tracking
    delay(1000);                                    //delay one second
    
    
        
}
   
    }
    
}

// The myHandler function tells the Level 5 that Level 6 has published an event.
void myHandler(const char *event, const char *data)
{

  if (strcmp(data,"longled")==0) 
  {
    digitalWrite(led,LOW); //Level 6 tells Level 5 to turn off the LED 
   
    
  }
  
  else {
    // not other actions before loop begins again
    
  }}

Refrigerator Buzzer

C/C++
The other piece to the Refrigerator Saver makes use of IOT and cloud communication to alert you and potentially save you hundreds of dollars.
// -----------------------------------------
// Refrigerator Buzzer
// -----------------------------------------


// Unlike the Refrigerator Saver the Buzzer will remain charging in
// an AC outlet, mostly likely on a kitchen counter.  This device
// has an alarm buzzer that also sends feedback to the Saver.  Both
// devices work in unison to ensure failsafes and reliabilty.


int buzzer = D0; //DO is used to power the piezo buzzer
int boardLed = D7; //currently unsused

// We start with the setup function.

void setup() {
  // setup for the photon pins and auxiliary:
  pinMode(buzzer,OUTPUT); // output pin for piezo buzzer (D0)
  pinMode(boardLed,OUTPUT); // currently unused

  // subscribe function for IOT cloud communication
  Particle.subscribe("Level5", myHandler);



}

void loop() {
  // This loop sends a signal that the buzzer is to stop
  // and trigger the LED on Level 5.




  if (digitalRead(buzzer) == LOW) //while the buzzer is off, reset LED on Level 5
 {
 digitalWrite(buzzer, LOW);
 
 if (digitalRead(buzzer) == HIGH)
 {digitalWrite(buzzer,HIGH);
 Particle.publish("Level6","longled" );
}
 }

}
// The myHandler function tells the Level 5 that Level 6 has published an event.
void myHandler(const char *event, const char *data)
{
  //this handler receives two separate messages from Level 5

  if (strcmp(data,"nobuzz")==0) //if read no buzz from Level 5
  {
    // buzzer signal is to be cut off when door is closed
    digitalWrite(buzzer,LOW);
  }
  else if (strcmp(data,"buzz")==0) //else read buzzer to be turned on from Level 5
  {
    // buzzer signal is to be turned on after a minute of the door being open
    digitalWrite(buzzer,HIGH);
  }
  else {
    // no other actions before loop begins again
  }
}

Credits

Shane McManus

Shane McManus

1 project • 0 followers
Varnell

Varnell

1 project • 0 followers

Comments