The goal behind the project is to set a baseline example of how to setup communication between multiple IoT devices using MQTT protocolIntroduction
Internet of Things (IoT) has seen unprecedented change over the past few years changing the way people live and work. IoT encourages companies to rethink the ways they approach their businesses and gives them the tools to improve their business strategies. There are numerous real-world applications of the internet of things, ranging from consumer IoT and enterprise IoT to manufacturing and industrial IoT (IIoT). IoT applications span numerous verticals, including automotive, telecom and energy. One area that will be crucial going forward is the ability to understand how IoT and IIoT will eventually merge. In the below example we try demonstrating one such example where we try to put together the world of IoT and IIoT.ESP32 dev kit
ESP32 is a low-cost, low-power system on a chip (SoC) series with Wi-Fi & dual-mode Bluetooth capabilities. It comprises of a dual core or a single core Tensilica Xtensa LX6 microprocessor with a clock rate of up to 240 MHz with integrated Wi-Fi and dual-mode Bluetooth.
- Wi-Fi: 802.11 b/g/n/e/i (802.11n @ 2.4 GHz up to 150 Mbit/s)
- Bluetooth: v4.2 BR/EDR and Bluetooth Low Energy (BLE)
- Flash: 4 MB
- SRAM: 520 KB
Micro Python is a lean and efficient implementation of Python3 which includes a small subset of Python standard library and is optimized to run on micro controllers. It consists of an interactive prompt (REPL) to execute commands immediately. It also can import and run scripts from built in filesystem. In addition to a selection of core Python libraries it also implements modules such as “machine” for accessing low-level hardware. MicroPython runs on a variety of systems and hardware platforms.MQTT (Message Queue Telemetry Transport)
For IoT devices connecting to internet is a crucial aspect of their functionality. This allows the devices to communicate with each other and the backend services. MQTT, which is built on top of the TCP/IP stack is a light weight, open and a simple messaging protocol for small sensors and mobile devices. It is easy to implement and makes it ideal for use in many situations, especially in resource constraint environments where a "small code footprint" is required such as for communication in Internet of Things (IoT). MQTT protocol has become a standard for IoT communications and can run on SSL/TLS, which is a secure protocol on top of TCP/IP to ensure that all data communication between the devices is encrypted and secure. MQTT is a publish-subscribe network protocol which defines two types of entities: a message broker and multiple clients. The broker is a server which receives all the messages from the clients and routes the messages to the corresponding destination clients. On the other hand, the client can be anything from an IoT device to machine learning model that makes predictions based on this data.
The main advantages of MQTT broker are:
1. Use of the publish/subscribe message pattern which provides one-to-many message distribution and decoupling of applications.
2. Eliminates vulnerable and insecure client connections.
3. Can easily scale from a single device to thousands.
4. Manages and tracks all client connection states, including security credentials and certificates.
5. Reduced network strain without compromising the security (cellular or satellite network)
Each ESP32 dev kit is connected with a RGB common anode LED and a push button switch. The switch simulates the purpose of an action being performed for example like a door button pressed to open it and the LED serves the purpose of displaying result of the action being performed. In our case the following indicates the color of led and their corresponding states of the door,
We connect the LED and switch as in the circuit diagram. In the below setup we connected the RGB LED to GPIO pins 13, 12 and 14, respectively. The input switch is connected to the GPIO pin 2 and is connected to the 5V supply.
Once we connect the circuit, we now setup the software. For this we will use MicroPython. The following steps will setup a fresh copy of MicroPython,
- Download the latest stable release of MicroPython binary from the official website,MicroPython - Python for microcontrollers
- To program the board we need to install the esptool.py program which can be found here, https://github.com/espressif/esptool/
$ pip install esptool
- Before we flash the MicroPython firmware we need to erase the entire flash using,
$ esptool.py --chip esp32 --port /dev/ttyUSB0 erase_flash
- Flash the MicroPython firmware to the board using the following command
(Note these commands are only for ESP32)
esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 115200 write_flash -z 0x1000 esp32-xxxxxxxx-vx.xx.bin
(replace the x’s with the appropriate firmware version number that you downloaded)
- Once we successfully flash the firmware, we can connect to the board using picocom,
picocom /dev/ttyUSB0 -b115200
Once we get the python terminal, we can establish the Wi-Fi connection using the following piece of code. Provide the SSID and Password of your wireless accordingly,
SSID = ""
station = network.WLAN(network.STA_IF)
if station.isconnected() == True:
while station.isconnected() == False:
We can include this piece of code in boot.py or place it in a separate file and call it from boot.py to initiate the Wi-Fi connection as we power up the board.
We use MQTT to establish communication between multiple devices.
For the MQTT server we used the eclipse-mosquitto using the docker image from eclipse-moqsuitto Docker Hub
To run this image, use the following command,
$ docker run -it -p 1883:1883 -p 9001:9001 \
-v mosquitto.conf:/mosquitto/config/mosquitto.conf \
-v /mosquitto/data -v /mosquitto/log eclipse-mosquitto
For further information regarding other MQTT server configurations refer to the official documentation Eclipse Mosquitto
To use MQTT in MicroPython we need to install the libraries related to MQTT using upip,
Each device uses both publish and subscribe methods in MQTT to communicate with other devices.
We define an interrupt handler for the switch. When the switch is pressed the processor stops the execution of the main program to execute a task associated with the interrupt request and then gets back to the main program. In our case we use the interrupt handler to publish a message to the broker that the switch at a specific controller has been pressed.
We initialize the variable switch to the GPIO pin 2 to which the switch is connected to
switch = Pin(2, Pin.IN)
We then define an interrupt request handler for the switch which upon pressed publishes its DEVICE_ID to a specific topic on the broker.
switch_pressed = pin.value()
client.publish(MQTT_SUB_TOPIC, DEVICE_ID, True)
We then attach the interrupt handler call back to the switch variable by using the irq()method
On the main loop the device subscribes to the topic to which all the devices publish data. To achieve this, we define a call back method and set it to the MQTT client.
def sub_cb(topic, msg):
We initialize the MQTT client with a unique DEVICE_ID for each of the devices and the MQTT server IP address. We try to establish the connection with the server and set the call back to the above declared method. We then subscribe to the topic to which all the devices publish data
client = MQTTClient(DEVICE_ID, MQTT_SERVER_IP)
As part of the main loop we use the wait_msg() method, which is a blocking call to check for messages in the subscribed topic,
except Exception as e:
Upon successful startup the devices will have the Blue LED lit up indicating that they are ready.
When the switch is pressed on one of the devices the LED turns ‘Green’ indicating that its open and the LED on all the other devices turn to RED indicating they are locked.
After a configurable predefined interval of 10 seconds, the status is reset to ready and the LED turns to Blue as shown above. We then press the button on the other controller to see the same behavior as seen previously.
We can dynamically add newer devices to the existing system without much effort and configuration.
As you can see above we added one more device to the setup and upon pressing the switch on it, the LED turns Green and on the earlier existing devices the LED turns Red. And we press the switch from the earlier setup and the functionality is achieved on all three devices.Usage
ISO clean rooms which are extensively used in industries such as semiconductor manufacturing, pharmaceuticals, biotech, medical device and life sciences to reduce particulate contamination and control other environmental parameters such as temperature, humidity and pressure.Future Scope
Status Ping – where each of the device will regularly send a status message to the broker which is used as a live check indicator.
OTA updates – implement over the air updates as these devices are deployed in remote locations and updating each of the device by going to their physical location is going to be difficult.
Data store/Audit – store the data in an IoT database or timeseries database for auditing purposes,
Dashboard – build a dashboard to display all the different controllers, their statuses, and the entire network system for admin access.