In this article, we will be controlling devices that do not support WhatsApp but support other communication protocols like MQTT, TCP, IMAP, etc. If a house is installed with home automation devices that do not support Whatsapp, we will communicate with these devices (controllers) using protocols supported by these devices.
Thus, we will be able to control home appliances connected to those devices.
Components required
Tools Required/ libraries required
Technical insights
Protocol bridging can also control devices that do not support WhatsApp API. This means we will simply send the data from one protocol to another. This article will demonstrate MQTT and WhatsApp Bridge to control the devices.
Block diagram
All the communication will be through a Python script, which is installed in Linux based system. The Python script will have WhatsApp API and MQTT library to communicate with both protocols.
The script can send and receive messages on WhatsApp and MQTT.
A microcontroller (ATmega 328p) is connected to the home appliances through relay circuits. For communication purposes, the ESP is connected to the microcontroller.
The ESP is installed with code that receives messages on a specific topic and sends commands to the microcontroller through serial UART.
So, now when any user sends messages on WhatsApp, it goes to the microcontroller through our Python script.
Circuit diagram
This board is connected to a light switch with a relay circuit. We can also take the Arduino Uno board instead of our customized board 328 Board.
How the system works
When a user sends a message to our Linux system on WhatsApp, the script reads the message. The IoT device, which supports the MQTT protocol, listens to the messages on a specific topic. These messages command the device to turn an appliance ON and OFF.
So, now the messages read by the Python script are scanned for commands if found. Specific commands are sent to the device on the MQTT protocol. When the device reads those commands, it acts upon them by turning pins HIGH (ON), LOW (OFF)
Understanding the source code
We have two types of source code, one for Arduino + ESP and another for Python script installed in Linux.
Code for Arduino
Arduino is installed with a code that receives data on serial. When specific strring is received like “ON” it will turn the relay pin ON (HIGH) and on receiving “OFF” it turns the relay OFF.
if (rec == “ON”) {
digitalWrite(relay, HIGH);
Serial.print(“Relay is on”);
rec = “”;
}
if (rec == “OFF”) {
digitalWrite(relay, LOW);
Serial.print(“Relay is off”);
rec = “”;
}
Code for the ESP
ESP is connected with Arduino on a serial port and also subscribed on an MQTT topic to receive data from it. Basically, it sends the data received on the MQTT topic to the serial port and data from the serial to the MQTT topic.
To know more about ESP and MQTT, refer to our previous articles.
Code for the Python script
The Python script is installed with “Yowsup” WhatsApp API to read and send messages from WhatsApp. There are two files in this script run.py and layer.py.
Understanding File Run.py
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 on the top because the main class “Echolayer” exists inside that file.
from layer import EchoLayer
We can name the layer file anything, but we have to put the same name here.
Inside the py, we will declare our main variable for password and events we want to occur.
credentials = (“91xxxxxxxxxx”, “HkhWVW5/Wnr493HXK8NKl/htpno=”)
Now we pass them to the layer and build the stack. Also, the loop which will keep the connection live is called.
stack.setCredentials(credentials)
stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT)) #sending the connect signal
stack.loop() #this is the program mainloop
Understanding File layer.py
This file contains the Protocol library for MQTT and is capable of receiving messages from WhatsApp.
Understating how messages are received from WhatsApp
This file contains the class which will receive any messages incoming to this number, and that will be a callback entity so any other loop can be run inside the file.
@ProtocolEntityCallback(“message”)
def onMessage(self, messageProtocolEntity):
if True:
Message data and number form from which the message came can be obtained below.
incomming_message_data = messageProtocolEntity.getBody()
This will get the message body which is the real message. It will store in a string variable “incomming_message_data”
incomming_message_sender = messageProtocolEntity.getFrom()
This line will store the incoming message contact number in the string variable “incomming_message_sender”
Understanding the MQTT layers for sending and receiving
First of all, we will import libraries that are necessary for MQTT.
import paho.mqtt.client as MQTT
import paho.mqtt.publish as publish
now we will declare a variable named client with mqtt client.
client = mqtt.Client()
Now we will make two function callbacks 1. For receiving messages, 2. Doing something on connection successful.
client.on_connect = on_connect
client.on_message = on_message
Finally, we will connect to MQTT broker on a port, and start the client inside a nonblocking 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”)
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 light ON/OFF.
elif(“lights on” in incomming_msg): #Do something on match
If the condition is matched, we send the control command to the MQTT broker.
publish.single(“ts/light”, “ON”, hostname=”broker.hivemq.com”)
When any unrecognized message has been received, the message on WhatsApp replies that this is invalid.
And this is how we can use protocol bridging to control devices with WhatsApp.
You may also like:
Filed Under: Electronic Projects, Featured
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.