Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Neal Salas ZamudioSeth HuntRahul Guha
Published

Refrigerator Monitoring System

Does anyone in your household leave the refrigerator door open for too long, or simply forget to close it?

IntermediateFull instructions provided466
Refrigerator Monitoring System

Things used in this project

Hardware components

Argon
Particle Argon
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Photo resistor
Photo resistor
×1
I2C 16x2 Arduino LCD Display Module
DFRobot I2C 16x2 Arduino LCD Display Module
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1

Software apps and online services

Particle Build Web IDE
Particle Build Web IDE
ThingSpeak API
ThingSpeak API

Hand tools and fabrication machines

Wire Stripper & Cutter, 22-10 AWG / 0.64-2.6mm Capacity Single & Stranded Wires
Wire Stripper & Cutter, 22-10 AWG / 0.64-2.6mm Capacity Single & Stranded Wires

Story

Read more

Schematics

Refrigerator Monitoring System Physical Wiring

Pictures of the physical wiring connections are provided below

Complete wiring diagram

This diagram includes the complete sensor wiring(US, temp and light) with the LCD

LCD only

Temperature sensor only

Ultrasonic only

Light sensor/photo resistor only

Diagram: Photoresistor

Isolated Diagram of the Photoresistor

Diagram: Temperature

An isolated diagram of the temp/humidity sensor (DHT11)

Diagram: Ultrasonic

Isolated diagram of the HC-SR04 Ultra sonic sensor

The full wiring

This is how the physical connections will look like when you're done

Diagram: LCD

Isolated view of the LCD connections

Code

Refrigerator Monitoring System

C/C++
The refrigerator monitoring system code collects temperature, photoresistor, and ultrasonic readings using the Particle Argon IoT development kit. Ten measurements from these three sensors are collected within the 10 first seconds of every 5 minute intervals. These three sets of 10 readings are then sorted in ascending order to later select the median values which are then used to define the final reading associated with the 5 minute intervals for each sensor. This code was developed with two-way communication between three Particle Argons, and it publishes events that were integrated with IFTTT to send emails. Additionally, final readings are sent to ThingSpeak for plotting the measurements and visualize the three Particle Argons data.
// Date: 04/19/2023
// By NSZ, SH, & RJ
// See HC-SR04 Ultrasonic Sensor tutorial below
// https://www.hackster.io/abdularbi17/ultrasonic-sensor-hc-sr04-with-arduino-tutorial-327ff6
//--------------------- ThingSpeak --------------------------------
#include <ThingSpeak.h>
TCPClient client;
// Temperature, Potentiometer, & Ultrasonic Measurements Channel number and API keys
unsigned long NealChannelNumber_T_P_U = 2086340;           // Neal's Channel Number
unsigned long SethChannelNumber_T_P_U = 2106700;           // Seth's Channel Number
unsigned long RahulChannelNumber_T_P_U = 2106709;          // Rahul's Channel Number
const char * NealWriteAPIKey_T_P_U = "IOZDUH8K2IIA9OQL";   // Neal's Channel Write API key
const char * SethWriteAPIKey_T_P_U = "8WH10SAHTEPVZPVK";   // Seth's Channel Write API key
const char * RahulWriteAPIKey_T_P_U = "0PE5S564WUZNZBSS";  // Rahul's Channel Write API key
//--------------------- LCD Screen --------------------------------
#include <LiquidCrystal.h>
LiquidCrystal lcd(D3, D4, D5, D6, D7, D8);// initialize library w/ the interface pins
//----------------- Temperature Sensor ----------------------------
#include "Adafruit_DHT_Particle.h"
#define DHTPIN A1                  // DHT11 Output Pin
#define DHTTYPE DHT11	           // Reference the DHT 11 Sensor from library
DHT dht(DHTPIN, DHTTYPE);
//------- Photoresistor (Light Dependent Resistor) Sensor ---------
#define PHOPIN A4                  // Photoresistor Output Pin
// Photoresistor Specs: Dark Resistance > 20M Ohms & Light Resistance < 80 Ohms
//----------------- Ultrasonic Sensor -----------------------------
#define echoPin A3 // attach Argon port pin A3 to pin Echo of HC-SR04
#define trigPin A2 // attach Argon port pin A2 to pin Trig of HC-SR04
float echoTime;            // Variable to store the time it takes for a ping 
                           // to bounce off an object.
float calculatedDistance;  // Variable to store the distance calculated from 
                           // the echo time.
//------------------ Global Variables -----------------------------
float Temperature = 0.00;       // Initialize variable to record temperature
float Photoresistor = 0.00;     // Initialize variable to record photoresistor values
float Ultrasonic = 0.00;        // Initialize variable to record ultrasonic values
// Initialize variables to compare with sensors threshold and make decisions
char statusTemperature = 'LOW';
char oldstatusTemperature = 'LOW';
char statusPhotoresistor = 'LOW';
char oldstatusPhotoresistor = 'LOW';
char statusUltrasonic = 'LOW';
char oldstatusUltrasonic = 'LOW';
//---------------------- Setup() ----------------------------------
void setup() {
//----------------- 2-way Communication ---------------------------
//  ***************************************************************
//  MAKE SURE YOU SUBSCRIBE TO YOUR PEERS PUBLICATIONS (Neal-Seth, Neal-Rahul, or Seth-Rahul)
//  ***************************************************************
//    Particle.subscribe("Message4Neal", NealHandler);
    Particle.subscribe("Message4Seth", SethHandler);
    Particle.subscribe("Message4Rahul", RahulHandler);
//  ***************************************************************
//--------------------- ThingSpeak --------------------------------
    ThingSpeak.begin(client);
//--------------------- LCD Screen --------------------------------    
    lcd.begin(16, 2);              // Set up the LCD's number of columns and rows:
    lcd.print("Temp. (Deg. F)");   // Print a message to the LCD.
//------------------- Serial Monitor ------------------------------  
	Serial.begin(9600);          // Initialize serial communication 
    delay(1000);                 // Allow time for the serial monitor to settle.
    Serial.println("Starting Refrigerator Monitoring System...");
//----------------- Temperature Sensor ----------------------------      
	dht.begin();
//----------------- Ultrasonic Sensor ----------------------------- 	
    pinMode(trigPin, OUTPUT);    // The trigger pin will output pulses of electricity
    pinMode(echoPin, INPUT);     // The echo pin will measure the duration of pulses 
                                 // coming back from the distance sensor.
}
//---------------------- loop() -----------------------------------
void loop() {
//----------------- Sensor Measurements ---------------------------
//	delay(2000);                  //Wait a few seconds between measurements.
//  Reading temperature or humidity takes about 250 milliseconds!
//  Sensor readings may also be up to 2 seconds 'old' (its a // very slow sensor)
  float h = dht.getHumidity();     // Read Humidity
  float t = dht.getTempCelcius();  // Read temperature (Celsius units)
  float readings_T[10];            // Create array to store temperature measurements
  float readings_P[10];            // Create array to store photoresistor measurements
  float readings_U[10];            // Create array to store ultrasonic measurements
  float temperature;               // Variable to sort the temperature measurements
  float photoresistor;             // Variable to sort the photoresistor rmeasurements
  float ultrasonic;                // Variable to sort the ultrasonic measurements
// Collect 10 temperature, 10 photoresistor, and 10 ultrasonic readings    
  for (int i=0;i<10;i++){
    temperature = dht.getTempFarenheit();  // Read temperature (Farenheit units)
    photoresistor = analogRead(PHOPIN);    // Read photoresistor value (unitless)
    //send out an ultrasonic pulse that's 10ms long
    digitalWrite(trigPin, LOW);    // Send low to get a clean pulse beforehand
    delayMicroseconds(10);         // Let it settle
    digitalWrite(trigPin, HIGH);   // Send high to trigger device
    delayMicroseconds(10);         // Let it settle
    digitalWrite(trigPin, LOW);    // Send low to get a clean pulse
    delayMicroseconds(10);         // Let it settle
    echoTime = pulseIn(echoPin, HIGH); // Measure pulse coming back in microseconds
    calculatedDistance = echoTime / 57.971;  // Calculate the distance (in cm) of the 
                                           // object that reflected the pules.
    // 1/(345*100*10^(-6)/2)=57.9710
    ultrasonic = calculatedDistance;    // Ultrasonic value (cm)
    // Assign a value of zero to nan measurements
    if (isnan(temperature)){
        temperature = 0.0;
    }
    if (isnan(photoresistor)){
       photoresistor = 0;
    }
    if (isnan(ultrasonic)){
       ultrasonic = 0.00;
    }  
    readings_T[i]=temperature;         // Store temperature values in an array
    readings_P[i]=photoresistor;       // Store photoresistor values in an array
    readings_U[i]=ultrasonic;          // Store ultrasonic values in an array
    delay(1000);                       // Collect data every second
  }
  // Order the 10 temperature, 10 photoresistor, and 10 ultrasonic readings in ascending order  
  for (int i=0;i<10;i++){                  // Loop for ascending order
    for (int j=0;j<10;j++){                // Loop for comparing other values
      if (readings_T[j]>readings_T[i]){    // Comparing other array elements
        float tmpT = readings_T[i];        // Using temp var for storing last value
        readings_T[i]=readings_T[j];       // Replacing value
        readings_T[j]=tmpT;                // Storing last value
      }
    }
  }
  for (int i=0;i<10;i++){                  // Loop for ascending order
    for (int j=0;j<10;j++){                // Loop for comparing other values
      if (readings_P[j]>readings_P[i]){    // Comparing other array elements
        float tmpP = readings_P[i];        // Using temp var for storing last value
        readings_P[i]=readings_P[j];       // Replacing value
        readings_P[j]=tmpP;                // Storing last value
      }
    }
  }
  for (int i=0;i<10;i++){                  // Loop for ascending order
    for (int j=0;j<10;j++){                // Loop for comparing other values
      if (readings_U[j]>readings_U[i]){    // Comparing other array elements
        float tmpU = readings_U[i];        // Using temp var for storing last value
        readings_U[i]=readings_U[j];       // Replacing value
        readings_U[j]=tmpU;                // Storing last value
      }
    }
  }
  // For debugging puposes
  /*  Serial.print("reading 1  "); Serial.println(readings_T[0]); Serial.println(readings_P[0]); Serial.println(readings_U[0]);
      Serial.print("reading 2  "); Serial.println(readings_T[1]); Serial.println(readings_P[1]); Serial.println(readings_U[1]);
      Serial.print("reading 3  "); Serial.println(readings_T[2]); Serial.println(readings_P[2]); Serial.println(readings_U[2]);
      Serial.print("reading 4  "); Serial.println(readings_T[3]); Serial.println(readings_P[3]); Serial.println(readings_U[3]);
      Serial.print("reading 5  "); Serial.println(readings_T[4]); Serial.println(readings_P[4]); Serial.println(readings_U[4]);
      Serial.print("reading 6  "); Serial.println(readings_T[5]); Serial.println(readings_P[5]); Serial.println(readings_U[5]);
      Serial.print("reading 7  "); Serial.println(readings_T[6]); Serial.println(readings_P[6]); Serial.println(readings_U[6]);
      Serial.print("reading 8  "); Serial.println(readings_T[7]); Serial.println(readings_P[7]); Serial.println(readings_U[7]);
      Serial.print("reading 9  "); Serial.println(readings_T[8]); Serial.println(readings_P[8]); Serial.println(readings_U[8]);
      Serial.print("reading 10 "); Serial.println(readings_T[9]); Serial.println(readings_P[9]); Serial.println(readings_U[9]); */
  // Select and print the median value of the arrays
  Temperature = readings_T[5];
  Photoresistor = readings_P[5];
  Ultrasonic = readings_U[5]; 
  Serial.print("Temperature    ");    Serial.print(Temperature,1);      Serial.println(" deg. F.");
  Serial.print("Photoresistor  ");    Serial.print(Photoresistor,0);    Serial.println(" (unitless)");
  Serial.print("Ultrasonic     ");    Serial.print(Ultrasonic,2);       Serial.println(" cm\n");
  // Program Logic for decision making
  // Temperature Decision Making
  if (Temperature>40.00) {
      statusTemperature = HIGH;
  }
  else {
      statusTemperature = LOW;
  }
  
  if (oldstatusTemperature == LOW && statusTemperature == HIGH) {
      Serial.println("Refrigerator temperature changed to unsafe\n\n");
  }
  else if (oldstatusTemperature == HIGH && statusTemperature == LOW) {
      Serial.println("Refrigerator temperature changed to safe\n\n");
  }
  else {
    // if the data is something else, don't do anything.
  }
  
 // Photoresistor Decision Making
  if (Photoresistor>3200.00) {
      statusPhotoresistor = LOW;
  }
  else {
      statusPhotoresistor = HIGH;
  }
  
  if (oldstatusPhotoresistor == LOW && statusPhotoresistor == HIGH) {
      Serial.println("Refrigerator Light was turned On\n\n");
  }
  else if (oldstatusPhotoresistor == HIGH && statusPhotoresistor == LOW) {
      Serial.println("Refrigerator Light was turned Off\n\n");
  }
  else {
    // if the data is something else, don't do anything.
  }

 // Ultrasonic Decision Making
  if (Ultrasonic<20.00) {
      statusUltrasonic = LOW;
  }
  else {
      statusUltrasonic = HIGH;
  }
  
  if (oldstatusUltrasonic == LOW && statusUltrasonic == HIGH) {
      Serial.println("Refrigerator Door changed to Open\n\n");
  }
  else if (oldstatusUltrasonic == HIGH && statusUltrasonic == LOW) {
      Serial.println("Refrigerator Door changed to Closed\n\n");
  }
  else {
    // if the data is something else, don't do anything.
  }

  // Decision making for the Temperature, the Photoresistor, and the Ultrasonic Sensor Readings Altogether
  if (statusTemperature == HIGH && statusPhotoresistor == HIGH && statusUltrasonic == HIGH) {
  // if (statusPhotoresistor == HIGH && statusUltrasonic == HIGH) { // for testing purposes
    Serial.println("Refrigerator door is open and needs to be closed\n\n");
    //  ***************************************************************
    //  MAKE SURE YOU COMMENT OUT THE TWO CHANNELS YOU ARE NOT USING (Neal-Seth, Neal-Rahul, or Seth-Rahul)
    //  ***************************************************************
    Particle.publish("Message4Neal","Refrigerator door needs to be closed");
    //  Particle.publish("Message4Seth","Refrigerator door needs to be closed");
    //  Particle.publish("Message4Rahul","Refrigerator door needs to be closed");
    //  ***************************************************************
  }
  else if (statusTemperature == LOW && statusPhotoresistor == LOW && statusUltrasonic == LOW) {
    Serial.println("Refrigerator door is currently closed\n\n");
  }
  else {
    // if the data is something else, don't do anything.
  }
  oldstatusTemperature = statusTemperature;
  oldstatusPhotoresistor = statusPhotoresistor;
  oldstatusUltrasonic = statusUltrasonic;
//--------------------- ThingSpeak --------------------------------
  ThingSpeak.setField(1,Temperature);
  ThingSpeak.setField(2,Photoresistor);
  ThingSpeak.setField(3,Ultrasonic);
//  ***************************************************************
//  MAKE SURE YOU COMMENT OUT THE TWO CHANNELS YOU ARE NOT USING (Neal-Seth, Neal-Rahul, or Seth-Rahul)
//  ***************************************************************
    ThingSpeak.writeFields(NealChannelNumber_T_P_U, NealWriteAPIKey_T_P_U);
//    ThingSpeak.writeFields(SethChannelNumber_T_P_U, SethWriteAPIKey_T_P_U);    
//    ThingSpeak.writeFields(RahulChannelNumber_T_P_U, RahulWriteAPIKey_T_P_U);
//  ***************************************************************
//  delay(20000); // ThingSpeak will only accept updates every 15 seconds.
//----------------- Sensor Measurements ---------------------------
//	Serial.println(Time.timeStr());       // if needed later
//	String timeStamp = Time.timeStr();    // if needed later
//  ***************************************************************
//  MAKE SURE YOU COMMENT OUT THE TWO CHANNELS YOU ARE NOT USING (Neal-Seth, Neal-Rahul, or Seth-Rahul)
//  ***************************************************************
	Particle.publish("NealRefrigeratorData", String::format("Temp(°F): %4.1f, Photo(Ohms): %4.0f, Ultras(cm): %4.2f", Temperature, Photoresistor, Ultrasonic));
//	Particle.publish("SethRefrigeratorData", String::format("Temp(°F): %4.1f, Photo(Ohms): %4.0f, Ultras(cm): %4.2f", Temperature, Photoresistor, Ultrasonic));
//	Particle.publish("RahulRefrigeratorData", String::format("Temp(°F): %4.1f, Photo(Ohms): %4.0f, Ultras(cm): %4.2f", Temperature, Photoresistor, Ultrasonic));
//  ***************************************************************
//--------------------- ThingSpeak --------------------------------
    delay(10000); // ThingSpeak will only accept updates every 15 seconds.
//    delay(10000); //   Collect data every 30 seconds (10s+10s+10s)
    delay(280000); //   Collect data every 5 minutes (280s+10s+10s)
//--------------------- LCD Screen --------------------------------
    lcd.setCursor(0, 1);  // set the cursor to column 0, line 1
//  (note: line 1 is the second row, since counting begins with 0):
    lcd.print(Temperature);      // print the temperature using the LCD Screen
} // Loop end bracket 
//----------------- 2-way Communication ---------------------------
// Now for the myHandler function, which is called when the cloud tells us that our buddy's event is published.
void NealHandler(const char *event, const char *data)
{
  if (strcmp(data,"Refrigerator door needs to be closed")==0) {
    Particle.publish("Message4Seth","Please remind Neal to close the refrigerator door");
    Particle.publish("Message4Rahul","Please remind Neal to close the refrigerator door");
  }
  else {
    // if the data is something else, don't do anything.
  }
}

void SethHandler(const char *event, const char *data)
{
  if (strcmp(data,"Refrigerator door needs to be closed")==0) {
    Particle.publish("Message4Neal","Please remind Seth to close the refrigerator door");
    Particle.publish("Message4Rahul","Please remind Seth to close the refrigerator door");
  }
  else {
    // if the data is something else, don't do anything.
  }
}

void RahulHandler(const char *event, const char *data)
{
  if (strcmp(data,"Refrigerator door needs to be closed")==0) {
    Particle.publish("Message4Neal","Please remind Rahul to close the refrigerator door");
    Particle.publish("Message4Seth","Please remind Rahul to close the refrigerator door");
  }
  else {
    // if the data is something else, don't do anything.
  }
}

Credits

Neal Salas Zamudio

Neal Salas Zamudio

1 project • 0 followers
Seth Hunt

Seth Hunt

1 project • 0 followers
Hello!
Rahul Guha

Rahul Guha

1 project • 0 followers
aa

Comments