Looking forward to a ready-made morning (or afternoon) cup of coffee? It’s possible to ensure the ideal cup at the ideal time is ready and waiting for you. This is thanks to an automated system that uses the Internet of things (IoT).
In this tutorial, we’ll develop an IoT-based coffee machine that operates at a set time without human interaction. It will also send notifications — such as when the cup is ready for you — via a desktop dashboard.
The Network Time Protocol (NTP) server will track the real-time, using “hours:minutes:seconds,” which it relays to the system. The system will also be pre-programmed for a set (desired) time. When the real and desired time match, the system sends an “ON” command to the coffee machine using a WiFi chip.
This alerts the coffee machine to work its magic, which will automatically turn “OFF” after a pre-set time to save power. A notification will be sent to your desktop that “Your coffee has been made” when it’s ready.
This covers the basics of how you can enjoy a ready-made coffee, with the ability to change the desired time to suit your preference. Now, let’s review the required components and the technology behind the system.
Components
Here’s what you’ll need to get started…
Dependencies:
- Operating system: Windows / Linux / MacOSx
- Programming languages: Python 2.7
- Communication medium: WiFi
Technology:
- MQTT protocol: message queue telemetry transportation or MQTT is a lightweight protocol for message transmission via the Internet. It works as a client-server architecture. The server is called “broker” and the clients are “clients.”
- NTP server: used to sync the system clocks of the devices that are connected to the Internet with the coordinated universal time (UTC).
- Wi-Fi module – ESP8266: the size of a small chip, this module can connect to any WiFi router and provide the Internet to devices that are not otherwise connected. We will use this to connect the IoT board with a broker on the Internet.
Block diagram
– The coffee machine is connected with a relay circuitry that switches it ON/OFF.
– The relay is controlled by the IoT board (the Atmega 328P controller)
– The board is connected to the Internet via the in-built ESP module.
– The board “communicates” with the MQTT broker (HiveMQ) online, and receives and sends MQTT-based messages.
– A computer desktop uses Python script for sending and receiving messages on the MQTT to the broker, which is connected to the coffee machine.
How it works?
A Linux/Windows-based machine runs the Python script. The script runs in a main loop where it compares the real-time, which is stored in a string, with a desired time. The desired time is written into the script and stored in a string using an “If” condition.
When the real-time matches the desired time string, a function call sends an “ON” signal to the coffee machine — which is connected to the Internet — over the MQTT. When it receives the “ON” command, it automatically turns the relay “ON” to make the coffee.
A time period is also set in the system, after which the coffee machine will turn “OFF.”
The circuit
The code
This project is divided into two parts:
1. The device that’s controlled (the coffee machine)
2. The main computer system, which stores the desired timing of the coffee and signals the machine to turn ON/OFF.
The IoT board code
This code is written and compiled in Arduino IDE. It also has two parts. One is for controlling the device and it uses an ATmega328 microcontroller. The other is for communication via the Internet and it uses the ESP8266 module.
Microcontroller’s control code
The microcontroller is programmed to receive strings on the universal asynchronous receiver/transmitter (UART) port from the ESP module. If the string’s command is “ON” or “OFF,” it switches the GPIO to HIGH or to LOW, respectively.
if(command == “machine/on”){
digitalWrite(coffee_machine_connected_pin,HIGH);
Serial.print(“offee machine is on”);
}
The board also sends the response through the UART to the ESP. Both are programmed so that any message sent on the serial will be published to the MQTT broker.
ESP module code
The ESP is programmed to connect to WiFi using a WiFi manager library, which creates a hotspot when needed. Then, using an HTML page, it’s possible to configure the ESP with SSID and PASS.
wifiManager.autoConnect(“AutoConnectAP”);
Any message received by the ESP on the serial will be published to the MQTT broker. Also, anything the ESP receives from the MQTT broker will be sent to the serial port.
The code is like a network adapter framework.
if (Serial.available()) {
String recivedData = Serial.readString();
temp_str = recivedData;
char temp[temp_str.length() + 2];
temp_str.toCharArray(temp, temp_str.length() + 1);
client.publish(topicPublish, temp);
}
The code for sending data that’s received from the broker to the serial.
for (int i = 0; i < length; i++) {
data_r_in_string = String(data_r_in_string + (char)payload[i]);
//Serial.print((char)payload[i]);
}
Serial.print(data_r_in_string);
The Python script
First, it’s necessary to make a file named “timings.py” in which all of the variables are declared so that it’s easy to change the desired timing for the coffee.
#timings = hh:mm:ss
morning_coffee = “08:00:00”
afternoon_coffee = “12:00:00”
evening_coffee = “17:06:10”
The script named “system.py” imports all the timing from the “timing.py” file.
from timing import *
Then, it’s important to import the libraries required for communication with the MQTT broker.
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
Next, assign two variables with MQTT topics — one to send messages and the other to receive messages.
subscription_topic = “ts/coffee/report”
publish_topic = “ts/coffee/machine”
Now, declare a variable named “client” with the “mqtt client.”
client = mqtt.Client()
Make two function callbacks:
1. For receiving messages
2. For doing something based on a successful connection
client.on_connect = on_connect
client.on_message = on_message
Finally, connect the MQTT broker on a port, and start the “client” inside a non-blocking loop.
client.connect(“broker.hivemq.com”, 1883, 60)
client.loop_start()
After the connection is successful, we can send messages using this:
publish.single(topic, message_data, hostname=”broker.hivemq.com”)
Now it’s possible to create the loop, which will include the current time in a string variable.
current_time = time.strftime(“%H:%M:%S”)
Inside the loop, compare the current system time with all of the previous variables.
if(morning_coffee == current_time):
print morning_coffee
send_signals(“machine/on”)
Machine = “ON”
When the condition is matched, a signal is sent that turns the machine “ON.” After the coffee machine is turned “ON,” it must shut “OFF” after a specific time. For this, another loop is required that only starts after the machine is turned “ON.”
while(Machine==”ON”):
if(second > time_for_which_machine_will_be_on_s):
send_signals(“machine/off”)
Machine = “OFF”
second = 0
print “Machine is OFF”
This is how the system works for programming an IoT-based coffee machine.
You may also like:
Filed Under: Electronic Projects
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.