Department of Electrical and Computer Engineering
Florida International University
EEE 4717 Introduction to IoT Security
INSTRUCTOR: Dr. MOHAMMAD SHAH ALAM
Contributors: Dr. KEMAL AKKAYA
YACOUB HANNA
LAB # 1 – RECEVING DATA USING RASPBERRY PI || DHT22 || MQTT
1. Objective
The objective of this lab is to introduce students to the Internet of Things Platform
(IoT) by receiving a certain data using a sensor through a communication protocol.
2. Tools/Software Needed
• Raspberry Pi 4
• DHT22 Temperature Sensor
• MQTT Communication Protocol
• ThingsBoard Platform
3. Setup
The Internet of Things (IoT) is a system that is consist of multiple objects that are
internet-connected, interlinked, and able to receive and collect data over the network
without human interaction. One of the great platforms that can be used as an IoT device
and be able to learn about it is the Raspberry Pi. Therefore, for this lab we are going to
use the Pi and Temperature Sensor to collect data using one of the most popular IoT
communication protocol MQTT. In order to see the collected data, we will be using
ThingsBoard platform which is an open-source server-side that allows the user to
monitor and control IoT devices.
4. Setting up the device and Sample Exercise
After setting up the Pi, connect the DHT22 Sensor as shown in the figure.
Figure 1 Schematic Diagram of Raspberry Pi 4
Connect the negative (-) from the sensor to the pin 6 (ground) on the RPi.
Connect the positive (+) from the sensor to the pin 2 (VCC 5V) on the RPi.
Connect the (output) from the sensor to the pin 7 (GPIO 4) on the RPi.
Let us install the required libraries for DHT Sensor and MQTT.
Open the Terminal and type.
$ cd Desktop
$ mkdir lab1-mqtt
$ cd lab1-mqtt
$ sudo pip install paho-mqtt
$ sudo apt-get install mosquitto-clients
$ sudo apt-get install python-dev
$ pip3 install adafruit-circuitpython-dht
$ sudo apt-get install libgpiod2
$ sudo pip3 install Adafruit_Python_DHT
$ sudo apt install git
$ git clone https://github.com/adafruit/Adafruit_Python_DHT.git
$ cd Adafruit_Python_DHT
$ sudo python setup.py install
$ cd ..
Now download the python script that is included with the lab document from canvas.
Place it in lab1-mqtt folder.
$ ls
You should have these two Directories inside the lab1-mqtt folder.
Adafruit_Python_DHT lab1-mqtt
Now let’s create an account with ThingsBoard.
Go to this link https://demo.thingsboard.io/signup .
Figure 2 Log in screen of Thingsboard
After creating an account sign in and you should get something like this.
Figure 3 Thingsboard Menus
Now we need to Test if we can send data to ThingsBoard Platform from RPi.
Go to devices tab and add a new device with a name Test-DHT22 and click add.
Figure 4 Thingsboard Devices
Figure 5 Adding a device on Thingsboard
After that we can see a device Test-DHT22 is created! Now, to connect to this device, we need the
access token, so click on the Test-DHT22 and you should see something like this, after Click on Copy
access token.
Figure 6 Finding Access Token for a device on Thingsboard Platform
Now let us test it!
Open the terminal.
Important: you must replace “access token” with your access token that you copied!
$ mosquitto_pub -d -q 1 -h "demo.thingsboard.io" -p "1883" -t
"v1/devices/me/telemetry" -u "access token" -m {"temperature":38}
Note: if you got an error that says port already in use.
Run this command.
$ sudo pkill mosquitto
And try again!
$ mosquitto_pub -d -q 1 -h "demo.thingsboard.io" -p "1883" -t
"v1/devices/me/telemetry" -u "access token" -m {"temperature":38}
By running this command, we are publishing a message using a mosquitto-client on a
port “1883” on a topic “v1/devices/me/telemetry” and using an access token of the
device and publishing the message {"temperature":38}
After running this command successfully, the output should look like this.
Client mosqpub|xxx sending CONNECT
Client mosqpub|xxx received CONNACK
Client mosqpub|xxx sending PUBLISH (d0, q1, r0, m1, 'v1/devices/me/telemetry', ... (16 bytes))
Client mosqpub|xxx received PUBACK (Mid: 1)
Client mosqpub|xxx sending DISCONNECT
After that open, the device Test-DHT22, then click on Latest telemetry and you
should see temperature:38
We have a successful communication between RPi and ThingsBoard!
For ‘Raspberry Pi 4 Users Only’ you must do the following.
You must go to
“/usr/local/lib/python3.7/dist-packages/Adafruit_DHT/platform_detect.py”
Then go all the way down. And you have to add Raspberry Pi 4 as shown in the
picture below:
Now let us see the real data from the DHT22 sensor (if you already have one).
Go to Devices tab on ThingsBoard and look for a device that is already created with
a name of DHT22 Demo Device, click on it and copy the access token and
follow these steps.
Go to the directory that you created lab1-mqtt, there is a python script.
$ cd lab1-mqtt
$ gedit mqtt-dht22.py
You will be able to see the whole code, Paste the copied access token where it says
‘DHT22_DEMO_TOKEN’
THINGSBOARD_HOST = 'demo.thingsboard.io'
ACCESS_TOKEN = 'DHT22_DEMO_TOKEN'
Save the file, exit, and run.
$ python3 mqtt-dht22.py
To see the real Temperature and Humidity. Go to ThingsBoard, then Dashboards
Tab, click on DHT22: Temperature & Humidity Demo Dashboard, then open
Dashboard.
Figure 7 Dashboard of the Thingsboard Platform
Figure 8 Dashboard Showing the Captured Parameter Values
You should be getting data like this.
If you have any questions, send me an email.
Email:
[email protected]