Login

Using MQTT on Raspberry Pi

Objective

  • Using MQTT services on the Raspberry Pi 3 to send and receive values of DHT11 Sensor

Things

For this project, you will need

  1. Raspberry Pi 3 [RPi3] (quantity: 1 no.)
  2. Micro SD card (quantity: 1 no.)
  3. Power Adapter (quantity: 1 no.)
  4. Laptop (quantity: 1 no.)
  5. VNC Viewer software
  6. DHT11 (quantity: 1 no.)
  7. Resistor 4.7K Ohm (quantity: 1 no.)
  8. Jumper Wires (quantity: 6 no.)

Let’s begin!

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.

RPi Pinout

 

Interfacing the DHT11 Sensor with Raspberry Pi 3

Set up the hardware connection is shown in the below diagram.

Final Code

We have two codes. One for publishing and one for subscribing.

Make an empty python file

nano publish.py

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.

nano subscribe.py

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

python subscribe.py

Open another terminal and run the following command

python publish.py

Takeaway

Loader Loading...
EAD Logo Taking too long?
Reload Reload document
| Open Open in new tab

We would love to see what you build out of these learnings!

Click here to submit your projects, share it with the world and stand a chance to be rewarded.

top

Knowledge and Content by Li2 Technologies | © 2021 NASSCOM Foundation | All rights reserved

X