Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

WiFi Based Real-Time Data Logger for Cupboard or Fridge

By Ganesh Selvaraj February 27, 2022

Introduction

Do you find it annoying when the cake you set in the freezer doesn’t set itself on time? Maybe someone is opening the fridge frequently when you aren’t noticing. Or you may be having a personal cupboard from where your things just vanish or gets misplaced when you are away. So, how to know when your fridge/ cupboard’s door get opened?
Well, we have a simple solution for that. Using this technique you can check at what time and also for how many times it was opened,  while sitting anywhere with the help of internet.
Components Required
1. Arduino UNO
2. ESP 8266 Wi-Fi module
3. Ultrasonic sensor SR-04 (4 pin version)
4. Wires
How are we going to do it?

We will measure the distance between the cupboard/fridge door and Ultrasonic sensor constantly and update it on the web. Whenever the cupboard door is opened, the distance would show a spike.

Calculating Distance Using Ultrasonic Waves

An Ultrasonic sensor measures distance using speed of sound. Basically, the transmitter sends an Ultrasonic sound wave towards an object and the receiver part receives the sound wave which is reflected from the same object. We know that the speed of sound is 330 meters per second. The sensor gives us the time taken by the wave to hit the object and getting back to the receiver. Since we know the time and speed, we can easily calculate the distance of the object from the sensor.

Image representing working of Ultrasonic Sensor

Fig. 1: Image representing working of Ultrasonic Sensor

Let us say that the object is at a distance=‘d’  meters from the sensor. The time taken by the ultrasonic wave (shown in red) to hit the object and reflect back be T seconds and speed of the wave = ‘S’ m/s. Then,

2 X d = S X T

=> d = S X T / 2

We take twice the distance because the wave travels from Tx to object and then object to Rx. And since we know that S=330m/s we get,

d = 165 X T meters (Where T is in seconds)

So all we need to know is the time taken by the wave to travel to and fro. This time duration is provided by the ‘Echo’ pin of the SR-04 module.

ESP8266 In Client Mode

In our last project we saw how to use the ESP8266 module in server mode.  It served as a webpage to any device which was dialed in its IP address. Now, here the module would act as a client, which means it would connect to the internet, to a particular website and update some value onto the website.
Basically we are sending the data obtained from the ultrasonic sensor to a website named thingspeak.com through the Wi-Fi module.
Algorithm:
– Test module using “AT” command
– Set mode of operation using “AT+CWMODE=<mode number>” command
– Connect to a Wi-Fi network using command “AT+CWJAP=<NETWORK SSID>, <PASSWORD>”
– Connect to a website in TCP mode using the command “AT+CIPSTART=<mode>, <IP Address>, <port number>”
– Using the command “AT+CIPSEND= <data length>”, send the data you want to update on the site.
– After completion, close the connection using “AT+CIPCLOSE” command.
About Thingspeak.com

Thingspeak.com is a website where you can upload certain data to a ‘channel’ you created and can also see the same data in form of a graph and that too with real-time update. When you create an account and a channel, you get a code which is also known as a ‘Key’ which would be used in order to upload date into the particular channel.

Procedure:

1.  Go to www.thingspeak.com and proceed to sign up for a free account.

2.  In the sign up page, don’t forget to select the correct time zone. The time zone will  allow the uploaded data to get a time-stamp.
3.  Once the account is created, you get an option to create a channel. Click on that and leave all entries to default.

Screenshot of API Key Generation on Thingspeak Server

Fig. 2: Screenshot of API Key Generation on Thingspeak Server

4.     Now, navigate to ‘API’ tab and note down the “API key”

5.     To test the channel you just created, type in the following on the address bar in your browser:

https://api.thingspeak.com/update?key=<API_KEY>&field1=0

Substitute <API_KEY> with the one you got in 4th step.

And then press enter. You should see a blank page with just ‘1’ written on it.

6.     Now come back to the channel page and navigate to the ‘Private view’ tab and you should see something like this:

Screenshot of Channel Statistics displayed on Thingspeak Server

Fig. 3: Screenshot of Channel Statistics displayed on Thingspeak Server

Code Explanation

The Arduino basically collects data from the ultrasonic sensor, calculates the distance and uses the Wi-Fi module to update the same onto the thingspeak.com channel. Here are the steps  to be followed:
– Initiate serial connection at 9600 baud rate. Send AT and check if OK is received. This is to ensure the presence of the module.
– Set the module in Station mode using the command AT+CWMODE=1
– Connect the module to your home network using the command AT+CWJAP=”HOME NETWORK NAME”,”PASSWORD”
– Check if the module replied OK. If it did, proceed further
– Enter infinite loop
– Assign 16th pin (which is connected to TRIGGER & ECHO pin of sensor) as output and then set it to LOW. Wait for 2 micro seconds
– Set the pin to HIGH, wait for 5 micro seconds and again set it to LOW (Basically it is creating a pulse of duration 5 microseconds)
– Now assign the same pin to INPUT mode and read the duration of pulse coming from the ECHO pin of sensor
– Use the duration to calculate the distance using microsecondsToCentimeters() function. Convert the  obtained value into a string type and store it in a variable ‘cmstr’
– If the value changes (i.e. when the cupboard is opened) then pass the cmstr to updateDist() function
– Connect to thingspeak.com in TCP mode using the command, AT+CIPSTART=”TCP”,”184.106.153.149”,80
– Post the distance value using the function AT+CIPSEND=<datalength> followed by GET /update?key=<API KEY OF THE THINGSPEAK ACCOUNT CHANNEL>&field1=cmstr
– Close the connection using AT+CIPCLOSE

Arrangement

1. Connect the parts according to the circuit diagram.
2. Place it inside a cupboard such that the sensor is at least 4 cm away from the door.
3. Connect a power source to the circuit (A battery) and in a minute you should see the output on your thingspeak.com channel.

Prototype of Arduino and ESP8266 Wi-Fi Modem based Circuit used for Data Logging

Fig. 4: Prototype of Arduino and ESP8266 Wi-Fi Modem based Circuit used for Data Logging

Image showing Ultrasonic Sensor Interfaced to Arduino Uno

Fig. 5: Image showing Ultrasonic Sensor Interfaced to Arduino Uno

Here are some outputs I got:

Screenshot of Cupboard Statistics displayed on Thingspeak Server

Fig. 6: Screenshot of Cupboard Statistics displayed on Thingspeak Server

As you can see, when I opened and took time to setup the system there properly, it already started sending data since there was a difference in distance of the cupboard’s door.

After that, at around 12:00, it stopped uploading data since the cupboard was closed and hence there was no change in distance between the sensor and the door for a while. And then at around 12:52 when I open the door, it senses the change of distance and again starts uploading data.

Troubleshooting

In case the live update is not being shown on the channel:
– Check and make sure that your internet and Wi-Fi connection is working fine.
– Check if you have given the correct network name and password in the code.
– Check if you have substituted the correct API KEY in the code
In case update  is taking place but  is shown as zero:
– Make sure that the ultrasonic sensor is working and the connections are tight.

You may also like:


  • What is the LoRaWAN network and how does it work?

  • What are the top 4G modems available in India?

  • What are the top development boards for AI and ML?

  • What are the top technologies enabling M2M in 2023?

  • What is LoRa and LoRaWAN?

  • What are LoRa gateways and what types are available?

Project Source Code

 
int pingPin = 16; // A2 - Connected to both Trig & ECHO Pins of the Sensor
unsigned int cm_old=0;
 
void setup()
{
  Serial.begin(9600);
  Serial.println("AT");
  delay(5000);
  if(Serial.find("OK")){
    connectWiFi();
  }
}
 
void loop(){
  long duration,cm;
 
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH); // Sending a High Pulse on the Trig Pin of the Sensor
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
 
  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
 
  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  String cmstr = String(cm);
  
 
 
if(cm!=cm_old) updateDist(cmstr);
  delay(2000);
  cm_old=cm;
}
 
void updateDist(String cmstr){
  String cmd = "AT+CIPSTART="TCP","";
  cmd += "184.106.153.149";
  cmd += "",80";
  Serial.println(cmd);
  delay(2000);
  if(Serial.find("Error")){
    return;
  }
  cmd = "GET /update?key=&field1="; //edit the key and copy paste with your own one
  cmd += cmstr;
  cmd += "rn";
  Serial.print("AT+CIPSEND=");
  Serial.println(cmd.length());
  if(Serial.find(">")){
    Serial.print(cmd);
  }else{
    Serial.println("AT+CIPCLOSE");
  }
}
 
 
boolean connectWiFi(){
  Serial.println("AT+CWMODE=1");
  delay(2000);
  String cmd="AT+CWJAP="";
  cmd+="";
  cmd+="","";
  cmd+="";
  cmd+=""";
  Serial.println(cmd);
  delay(5000);
  if(Serial.find("OK")){
    return true;
  }else{
    return false;
  }
}
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Circuit Diagrams

Circuit-Diagram-Arduino-ESP8266-Wi-Fi-Modem-Based-Data-Logging-Prototype


Filed Under: Electronic Projects

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Diode recovery test Irrm timing.
  • How to make string LEDs?
  • The Analog Gods Hate Me
  • Battery Deep Discharge – IC Workarounds?
  • Safe Current and Power Density Limits in PCB Copper(in A/m² and W/m³) simulation

RSS Electro-Tech-Online.com Discussions

  • Raise your hand if your car had one of these:
  • Tektronix 2235 channel 1 trace unstable
  • How to make string LEDs?
  • Wideband matching an electrically short bowtie antenna; 50 ohm, 434 MHz
  • The Analog Gods Hate Me

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • Tria modules integrate edge AI processing with multi-core processors
  • pSemi introduces RF switch with 52 dBm PMAX,PEAK and 90-dBm IIP3 linearity
  • XP Power launches 1.3 kW power supply with 58.9 W/cm³ density
  • How to enable Wi-Fi provisioning in ESP32-based IoT products
  • Amphenol RF introduces FAKRA to SMA adapters with 4 GHz operating frequency

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe