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

WhatsApp-based home automation: Protocol bridging with MQTT

By Ashutosh Bhatt March 30, 2025

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

  • Python-based WhatsApp API – yowsup
  • MQTT library – paho
  • Python ID
  • Arduino IDE

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

Figure 1 WhatsApp and MQTT protocol bridging

 

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.

https://www.engineersgarage.com/wp-content/uploads/2022/05/Demo.mp4

You may also like:


  • WhatsApp-based home automation

  • Pet feeding system using WhatsApp (protocol bridging with MQTT)

  • Internet based Home Automation System

  • Android Phone Controlled Robot using Arduino

  • Home Automation using RF Module (Part 13/23)

  • How To Get WhatsApp Working On Raspberry Pi?

Filed Under: Electronic Projects
Tagged With: Arduino, IDE, MQTT, protocol bridging, python, Whatsapp
 

Next Article

← Previous Article
Next Article →

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



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

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

  • Reducing "shoot-through" in offline Full Bridge SMPS?
  • High Side current sensing
  • How to simulate power electronics converter in PSpice?
  • Voltage mode pushpull is a nonsense SMPS?
  • Layout IRN reduction in Comparator

RSS Electro-Tech-Online.com Discussions

  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors
  • Siemens large industrial PLC parts

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

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