- Using MQTT services on the Raspberry Pi 3 to send and receive values of DHT11 Sensor
For this project, you will need
- Raspberry Pi 3 [RPi3] (quantity: 1 no.)
- Micro SD card (quantity: 1 no.)
- Power Adapter (quantity: 1 no.)
- Laptop (quantity: 1 no.)
- VNC Viewer software
- DHT11 (quantity: 1 no.)
- Resistor 4.7K Ohm (quantity: 1 no.)
- Jumper Wires (quantity: 6 no.)
We shall be using a third party VNC Viewer software to remotely access the RPi with our laptop. We will use MQTT for transmitting this temperature data.
MQTT (MQ Telemetry Transport) is a lightweight messaging protocol that provides resource-constrained network clients with a simple way to distribute telemetry information. The protocol, which uses a publish/subscribe communication pattern, is used for machine-to-machine communication (M2M) and plays an important role in the internet of things (IoT). This helps us to transmit and receive data between low power and resource-constrained devices.
MQTT enables IoT devices to send, or publish, information about a given topic to a server that functions as an MQTT message broker. The broker then pushes the information out to those clients that have previously subscribed to the client’s topic. To a human, a topic looks like a hierarchical file path. Clients can subscribe to a specific level of a topic’s hierarchy or use a wild-card character to subscribe to multiple levels.
In this unit, we will be making the Raspberry Pi send data to itself via an MQTT server. However, this idea can be extended to have multiple devices sending data to the Raspberry Pi. A NodeMCU can also be used to send data via MQTT.
Setting up Raspberry Pi for MQTT Communication
We will install paho MQTT to use in Python and Mosquitto to use from the terminal.
Update everything by typing following command,
sudo apt-get update
sudo apt-get install build-essential python-dev python-openssl
Now let’s install a package called Mosquitto which is an MQTT Client. We can do this by running the command
sudo apt-get install mosquitto mosquitto-clients
In order to install paho enter the following command in your terminal.
pip install paho-mqtt
The mqtt client is now installed.
Interfacing the DHT11 Sensor with Raspberry Pi 3
Set up the hardware connection is shown in the below diagram.
We have two codes. One for publishing and one for subscribing.
Make an empty python file
Paste the following code in it. This code takes data from a DHT 11 humidity and temperature sensor and then send it via MQTT to a local broker (The IP address of the Raspberry Pi is set as the broker). It sends the data to a topic called “sensor_data”. Make sure to change the IP address of the broker to the IP address of your Raspberry Pi.
Now let’s save the subscribe code. Make another empty python file.
Paste the following code in it. This code connects to the same broker and subscribes to the topic “sensor_data”. So when any data is published to this topic, it will receive it. Make sure to change the IP address of the broker to the IP address of your Raspberry Pi.
There you go!
Open one terminal window and run the following command
Open another terminal and run the following command