Creating a Real-Time Coffee Level Indicator with IoT
Written on
Chapter 1: Introduction to IoT and Coffee Level Measurement
In this guide, we will explore how to create a real-time coffee level indicator utilizing an ultrasonic sensor and NodeMCU. You might be wondering, "What is NodeMCU?" Don't worry; we'll clarify this shortly.
First, let’s discuss the concept of IoT, or the 'Internet of Things.' Essentially, IoT involves enhancing everyday devices to operate more intelligently. By integrating sensors into these devices, we enable them to communicate with one another. But how does this communication work?
To connect different devices, they require an internet connection to share data. This is a straightforward illustration of IoT.
So, why is IoT more than just a trend?
# Benefits of IoT
Implementing IoT in businesses can lead to numerous advantages, such as:
- Expanded business prospects
- Enhanced utilization of assets
- Streamlined processes
- Improved safety and security measures
- Increased productivity
- Significant cost reductions
Now that we've covered the basics of IoT, let’s dive into our project: a real-time coffee level indicator!
Section 1.1: Understanding Arduino and NodeMCU
Before we proceed, it's essential to understand 'Arduino.'
What is Arduino?
Arduino is an open-source platform designed for developing electronic projects. It comprises a microcontroller and an Integrated Development Environment (IDE) that allows users to write and upload code to various IoT microcontrollers, like Arduino or similar devices. In this project, we will focus on using the NodeMCU module.
What is NodeMCU?
In simple terms, NodeMCU is an affordable microcontroller that offers an open-source IoT platform, complete with a Wi-Fi module for our project.
Required Components
- Ultrasonic Sensor (HC-SR04) — This sensor measures the current water level, with a range of 2 cm to 400 cm.
- NodeMCU (ESP8266) — This device supports Arduino programming and offers internet connectivity.
- Arduino IDE — Download and install it from the official site.
- A micro USB cable (for connecting NodeMCU to your PC).
- Four female-to-female jumper wires (for connecting the ultrasonic sensor to the NodeMCU).
Here’s the circuit diagram:
After assembling the components, we need to program them accordingly. To do this, we will open the Arduino IDE and install the ESP8266 board by following these steps:
- Launch Arduino IDE and open the 'Preferences' window.
- Go to Tools > Boards Manager > and search for ESP8266.
- Select version 2.5.0 from the drop-down menu (as later versions may have issues) and click 'Install.'
- After installation, select the Generic ESP8266 module from the Tools > Board menu.
Next, paste the following program into the IDE:
#include <ESP8266WiFi.h>
const char* location = "office";
char* ssid;
char* password;
const char* host = "ipaddress";
// Define pins for the ultrasonic sensor
const int trigPin = 2; // D4
const int echoPin = 0; // D3
long duration;
float distance;
const float container_height = 31.0; // Height of the container in cm
int diff;
float level;
void setup() {
if (location == "home") {
ssid = "xxxxxx";
password = "xxxxxx";
} else if (location == "office") {
ssid = "xxxxx";
password = "xxxxxx";
} else if (location == "phone") {
ssid = "xxxxxx";
password = "xxxxx";
}
pinMode(LED_BUILTIN, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.println(WiFi.status());
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
diff = container_height - distance;
Serial.print("diff: ");
Serial.println(diff);
if (diff > 0) {
level = (float)diff / container_height;
Serial.print("level: ");
Serial.println(level);
} else {
level = 1;}
// Use WiFiClient class to create connections
WiFiClient client;
Serial.print("connecting to ");
Serial.println(host);
const int httpPort = 3000;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
String url = "/";
url += "level?level=";
url += level;
Serial.print("Requesting URL: ");
Serial.println(host + url);
// Make GET Call
client.print(String("GET ") + url + " HTTP/1.1rn" +
"Host: " + host + "rn" +
"Connection: closernrn");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read the response and print to serial monitor
while (client.available()) {
String line = client.readStringUntil('n');
Serial.println(line);
if (line.substring(0, 7) == "Success") {
Serial.write("Y");} else if (line.substring(0, 6) == "Failed") {
Serial.write("N");} else if (line.substring(0, 4) == "User") {
Serial.write("O");}
}
Serial.println("closing connection");
}
Once the code is compiled successfully, click the arrow button to upload it to the Arduino. You can then monitor the logs to see the distance readings. By placing an object in front of the ultrasonic sensor, you can determine the distance between the object and the sensor. This setup allows us to measure the coffee level in our coffee machine or pot.
Visualizing the Project Flow
Before we wrap up, take a look at the end-to-end flow diagram of this project for better understanding:
In our next blog post, we will explore how to integrate this project with a mobile application! Stay tuned for the complete implementation. Until then, happy learning!
Arduino is a trademark or registered trademark in the United States and/or other countries.
Author — Arun Kashyap Karri, DLT Labs™
About the author: Arun is a passionate learner and a skilled Web/Mobile Developer. He has experience with technologies such as Angular, Node.js, Flutter, RPA, and Chatbots. At DLT Labs, he has contributed to applications like Element DB, DL Certify, and We Agree.
Disclaimer: This article was originally published on the DLT Labs Blog page:
References
Chapter 2: Video Demonstrations
In this chapter, we will delve into video tutorials that provide practical insights into building our IoT coffee level indicator.
This video demonstrates how to create a DIY IoT coffee pot that monitors coffee levels using an Atmel MCU and an ESP8266. It's a great resource for understanding the practical application of IoT in coffee machines.
In this tutorial, you'll learn how to build an IoT coffee machine. The video outlines the steps involved in creating a smart coffee maker, showcasing the integration of IoT technologies.