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

Centralized control system for IoT devices

By engineersgarage June 24, 2021

In this project, we will be creating a single control center for all of our IoT devices. We will control all of our IoT devices with just one single tool and get the status of all of them live on a single screen.

We can implement these types of control centers for most of the protocol. In this article, we will be using MQTT to control and monitor.

Tools Required/ libraries required
Python 2.7
Python library for MQTT – paho

Technical Insights
Basically, we will be working with MQTT protocol and python methods to create our dashboard.

In this project, we will create a control system to control our IoT devices using python and MQTT. Setting up IoT devices can be easy if they are set up in a controlled manner. All the devices need a controlling station. 

Block Diagram

Figure 1 Communication diagram of the system

All the sensors and IoT devices are connected to a broker, “broker.hivemq.com” on different and unique topics of their own.

The IoT dashboard is also connected to the same broker, but it is subscribed to all the topics on which the devices are subscribed.

How the system works

  • All the devices are subscribed to some topics. We make entries of those devices in a python file “devices.py”.
  • From that file, our main code imports the names and topics of the devices.
  • Then it subscribes our script to receive messages to those topics
  • Now we add callbacks for each topic with their names; it must be done manually, then the method also needs to be made.
  • After adding callbacks and writing the function definitions (fn_callbacks.py), we send data from each device to the display function, which shows the data to the screen.
  • Executing the dashboard script “py” also launches a script for sending the commands to the devices called “Command_control.py” by using this script, we can send data to those devices.
  • The controlling script gives us full access to those devices, and it also imports the device information from the file “py”.
  • So, we can select the device with their ID number and interact with them on the screen.

Understanding the source code:

To understand the python code, we must first understand how MQTT is implemented in python for receiving and publishing the data from and to the broker.

  1. MQTT Implementation
  2. Creating Devices entries
  3. Creating multiple subscriptions
  4. Displaying the data
  5. Sending the data to devices
  6. Saving data to database

MQTT Implementation
First of all, we will import libraries that are necessary for MQTT communication.
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish

now we will declare a variable named client with mqtt client entity.
client = mqtt.Client()

We will connect to the MQTT broker on a port and start the client inside a nonblocking loop
client.connect(“broker.hivemq.com”, 1883, 60)

The MQTT connects the broker, and for receiving, it gives us a way in which we can add one callback function for one receiving topic.
client.on_message = on_message
client.message_callback_add(“topic”,method_name)

Also, if we want to send a message to any client, we can do that by calling the publish method anywhere in the code.
publish.single(topic_pub, command, hostname=broker)

So, this was about MQTT and the features we will be using in this code.

Now let’s learn about adding device entries to the dashboard and subscribe to topics dynamically.

Creating Devices entries
First of all, we will create a file named devices.py and declare our devices as variables and, then make a List of those variables.
device_1 = “Device_1”
device_2 = “Device_2”
devices_names = [device_1,device_2]

This is the only way we can easily add device names in the code. They must be added before we run the code.

Creating multiple subscriptions
Subscription can be done at any time in the process, but we do it inside the method we reach just after the connection is made.
There we run a loop until the devices are present
for topic in devices_names:
client.subscribe(topic)

After we have subscribed to each device’s topic, we need to add a callback function for each device to receive data for each device.
client.message_callback_add(devices_names[0],Device_1)
client.message_callback_add(devices_names[1],Device_2)

Each functions’ definition is added in a file named “py”, Which is listening for three types of messages.

  1. Messages containing “debug:” string at the starting of message: This is for devices debug messages.
  2. Messages containing “sensor” string at the starting point: This is if a device’s sensor is sending some data.
  3. The third type of message is just a ON or OFF message which tells that if the device is ONLINE or OFFLINE.

Now we at this point, we have enabled our script to listen for messages for each device. Now let’s see how we will show this bulk data in an arranged way on the screen.

Displaying the data
To display the data on the screen, we create a dynamic function which takes the device id and the device data variable, which holds the data about each device.

The above print statement in python always prints the data to the places which are defined.

We call this function in our main while loop to display information about each device.
for i in devices_names:
global device_number
status(i,device_number)

Rest is self-explained in the code.

After that, we start receiving the data from the sensor we now need to send the data to them.

Sending the data to devices
To send the data to devices, we create a separate file that runs with the dashboard.py. The file is “command_control.py”

In this file, we continuously ask the user to select a device and then send the command to that device in a while loop.
send_command(input_cmd,devices_names[i-1])

Now we can also keep records of what is the command we have sent.

Saving data to database
We have created a function which takes data string and save them one by one to a file named database.txt

After we send the command, we make a string which stores the command with the data sent to it, and it is shown in the command.

So this way, we learned how to create a dashboard for our IoT devices using python. We can also use any other protocol in it.

https://www.engineersgarage.com/wp-content/uploads/2021/06/Demo.mp4

You may also like:

  • Raspberry Pi 4
    RPi Python Programming 01: Introduction to Raspberry Pi 4

  • What are the top 4G modems available in India?

  • What are LoRa gateways and what types are available?

  • How does LoRa modulation enable long-range communication?

  • What is the role of embedded software in electric vehicles?

  • What are the top tools for developing embedded software?

Filed Under: Applications, Electronic Projects, Featured, IoT applications
Tagged With: IoT, MQTT, projects, python
 

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

  • Voltage mode pushpull is a nonsense SMPS?
  • Input impedance matching network
  • High Side current sensing
  • The comparison of different Tcl script checkers
  • Reducing "shoot-through" in offline Full Bridge SMPS?

RSS Electro-Tech-Online.com Discussions

  • Is AI making embedded software developers more productive?
  • 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

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