In this article we will be creating a pet feeding system that can be controlled via WhatsApp.
Required components
Required tools/required libraries
Linux-based machine
Python-based WhatsApp API
Based on MQTT
Python IDE
Arduino IDE
Technical information
The complete project has three types of communication and devices. XMPP (WhatsApp, MQTT and Arduino Serial) can also be categorized as M2M communication (machine-to-machine communication).
The technical challenges were creating a device that could be controlled with an interface that was readily available to everyone. Therefore, we will use WhatsApp with MQTT to control the pets' food bowl.
Block diagram

Figure 1 Pet feeding system using WhatsApp
The pet feeding box is controlled by a port controlled by Atmega328 (Arduino UNO) and ESP. Arduino is receiving commands via MQTT using ESP. There is a Linux-based machine that runs the WhatsApp API to read WhatsApp messages and send them to the MQTT broker. The ESP receives commands from the corrector to control the servo connected to the power system port.
Circuit Diagram
Servo connection
How the system works
When a message is sent from the pet owner to the pet's feeding system using WhatsApp, the message first goes to the Linux machine where the message is read from WhatsApp using API. The message is checked if any command is available to control the system. The command is extracted and sent to the system using MQTT. The message is received by the ESP and sent to the Arduino via Serial, and if the message says to open the power system door, the servo moves and the door is opened.
Understanding the source code
As the project has two parts
- Read messages via WhatsApp and send them to MQTT: Includes code for Linux machines and WhatsApp API installation and MQTT implementation.
- Read messages via MQTT and control the servo motor: Arduino code to control the servo and ESP for communication via MQTT.
Let's first understand communication with WhatsApp and MQTT.
Code for Python script
The python script is installed with the WhatsApp “Yowsup” API to read and send WhatsApp messages.
There are two files in this script run.py and layer.py .
Understanding the Run.py File
We will call our libraries at the top of the file
from yowsup.stacks import YowStackBuilder
from yowsup.layers.auth import AuthError
from yowsup.layers import YowLayerEvent
from yowsup.layers.network import YowNetworkLayer
from yowsup.env import YowsupEnv
- We will also attach the layer file at the top because the main class “Ecolayer” exists inside this file.
From EchoLayer layer import
- We can name any layer file, but we must put the same name here.
- Inside py, we will declare our main variable for password and events we want to occur.
credentials = (“91xxxxxxxxxx”, “HkhWVW5/Wnr493HXK8NKl/htpno=”)
- Now we move to the layer and build the stack. Additionally, the loop that will keep the connection alive is called.
stack.setCredentials(credentials)
stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CO
NNECT)) #sending the connection signal
stack.loop #this is the mainloop program
Understanding the layer.py file
This file contains the protocol library for MQTT and can receive WhatsApp messages.
Understanding how messages are received from WhatsApp
This file contains the class that will receive all incoming messages for this number, and which will be a callback entity so that any other loops can be executed within the file.
@ProtocolEntityCallback(“message”)
def onMessage(self, messageProtocolEntity):
if it's true:
The message details and the number the message came from can be obtained below.
incomming_message_data = messageProtocolEntity.getBody
This will get the message body, which is the actual message. It will be stored in a string variable “incomming_message_data”
incomming_message_sender = messageProtocolEntity.getFrom
This line will store the contact number of the message received in the string variable “incomming_message_sender”
Understanding MQTT layers for sending and receiving
Firstly, we will import the necessary libraries for MQTT.
import paho.mqtt.client as mqtt
import paho.mqtt.publish how to publish
Now we will declare a variable called client with mqtt client.
client = mqtt.Cliente
Now we will make two function callbacks 1. To receive messages, 2. Do something on successful connection.
client.on_connect=on_connect
client.on_message=on_message
Finally, we will connect to the MQTT broker on a port and start the client inside a non-blocking loop
client.connect(“corretor.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”)
So now when any message is received on WhatsApp, it is stored in a string, and then that string is scanned for some keywords that define that message as a command to turn the servo on/off.
elif(“lights on” in incomming_msg):
#Do something in the match
Now, if the condition is met, we send the control command to the MQTT broker.
publish.single(“ts/dog”, “feed”, hostname=”broker.hivemq.com”)
When any unrecognized message is received, the message on WhatsApp is replied that it is invalid.
Code for Arduino
Arduino is installed with a code that receives data in series and controls the servo accordingly.
First we add a “Servo” library to control the servo easily
#include
myservo.attach(5); // attach the servo on pin 9 to the servo object
myservo.write(0);
Furthermore, when a specific string is received as “feed” it will rotate the Servo where the door will be open and upon receiving “ok” it will close the door.
if (rec == “feed”) {
myservo.write(180);
}
if (rec == “ok”) {
myservo.write(0);
}
Code for ESP
The ESP is connected to the Arduino on the serial port and subscribed to an MQTT topic to receive it. It sends the data received on the MQTT topic to the serial port and the data from the serial to the MQTT topic.
To learn more about ESP and MQTT, see our previous articles.