How to send DHT data from Raspberry Pi to IBM cloud

Samuel Adesola
10 min readJun 17, 2022

Detailed explanation on How to send DHT 11 data from Raspberry Pi to IBM cloud Watson IoT Platform. How to send data from Raspberry Pi to IBM Cloud Watson IoT Platform

IoT being an integral part of cloud computing, several cloud vendors make the Internet of Things (IoT) part of the services they render. Also, with the emergence of IoT, there are different platforms that make IoT implementation available even to individuals. In this tutorial, I will be taking you through steps in sending data from Raspberry Pi, an IoT development platform to the IBM Cloud IoT platform. To ensure all necessary things are in place make sure you have access to your Raspberry Pi. You can either access the Raspberry pi in Headed form using Monitor, keyboard, and mouse or in Headless form via SSH and VNC. In this tutorial. The link below explains how you can access your raspberry pi in two forms: Headed Access: And headless Access: The Watson IoT Platform The Watson IoT Platform is a service offered by IBM Cloud that allows data to be sent from various IoT devices to the cloud. You can either connect a simulated device or connect an actual device, in this tutorial, I will be showing you how to connect an actual device which is a Raspberry pi. The Hardware For this tutorial, we will be using the following materials:

  1. Raspberry pi (3, Zero, or newer)
  2. DHT11
  3. 10k ohm resistor
  4. Jumper wires
  5. Breadboard

The Hardware setup

STEP 1: The very first step is to ensure that you can access your raspberry pi, you can use the link above to check how to set up your Raspberry pi in either headed or headless form. Step 2: Connect the DHT 11 to the breadboard, depending on your model of DHT 11, it can be a 3-pin ready-made model or a 4-pin model, in this tutorial, I am using a 4-pin model. If you are using a 3-pin model, connect the VCC to 3V of the Raspberry pi (PIN 1), GND of the DHT 11 to GND on the Raspberry pi (PIN 6), and connect the data pin to GPIO 4 (PIN 7) of the Raspberry pi. If it is the 4-pin model you are using, connect Pin 1 of the DHT11 to 3V of the Raspberry pi (PIN 1), connect the 10K resistor between pin 1 and pin 2 of the DHT 11 and again connect pin 2 of the DHT11 to GPIO 4 (PIN 7) of the Raspberry pi, leave pin 3 of the DHT 11 sensor unconnected and connect pin 4 of the DHT 11 to GND on the Raspberry pi (PIN 6).

Reading DHT sensor Data on your Raspberry Pi

Unlike Arduino or other related boards that have support from the DHT sensor straight out of the box, we need to set up our Raspberry pi first to be able to read data from the DHT sensor. Before we can send the data to IBM Cloud, we need to be able to read the data from the DHT11 sensor.

STEP 1: Installing CircuitPython Libraries on Raspberry Pi Update your Raspberry Pi by running the standard updates and downloading dependencies

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python3-pip
sudo pip3 install --upgrade setuptools

Adafruit put together a script to easily make sure your Pi is correctly configured and install Blinka. It requires just a few commands to run. Most of it is installing the dependencies.

cd ~
sudo pip3 install --upgrade adafruit-python-shell
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/raspi-blinka.py
sudo python3 raspi-blinka.py

If your system default Python is Python 2 (which is likely on a first install), it will ask to confirm that you want to proceed. Choose yes. It may take a few minutes to run. When it finishes, it will ask you if you would like to reboot. Choose yes. Once it reboots, the connection will close. After a couple of minutes, you can reconnect.

STEP 2: Installing the CircuitPython-DHT Library You’ll also need to install a library to communicate with the DHT sensor. Since we’re using Adafruit Blinka (CircuitPython), we can install CircuitPython libraries straight to our small Linux board. In this case, we’re going to install the CircuitPython_DHT library. This library works with both the DHT22 and DHT11 sensors. Run the following command to install the CircuitPython-DHT library:

pip3 install adafruit-circuit python-dhtsudo apt-get install libgpiod2

STEP 3: Testing the DHT Library To test the working of the DHT11 download the code from this repository: https://github.com/adesolasamuel/Raspberry-pi-DHT11-Sensor If you are able to set it up correctly, running the code should give you an output as below:

Setting up IBM Cloud

STEP 1: After making sure that our Raspberry pi is able to read DHT11 data correctly, the next thing is to head over to IBM cloud to set up communication with our device. Since the Raspberry pi is a full-blown computer, you can decide to either run the setup on your Raspberry Pi or another computer. To access the IBM cloud portal, enter the following address in your Browser window www.ibm.com/cloud, if you do not have an account, create a new one or you can sign in with an already existing account by clicking on the console at the top right-hand corner.

STEP 2: In the IBM Cloud console, we are going to be using the Internet of Things Platform service, on the search bar, simply type in IoT and it should bring out the Internet of Things Platform as part of the results, and click on Internet of Things Platform.

On the Internet of Things Platform page, you can leave everything as default, accept the license agreement and click on Create.

On the next page, you will have your service summary, to launch the service, click Launch.

STEP 3: Another window will open which is the IBM Watson IoT Platform, again you will have to sign in by clicking sign in at the top right-hand corner. Sign in with the same IBMid email and password

After signing in click on your profile at the top right-hand corner to access the Watson IoT dashboard.

STEP 4: The next thing to do is to set up IBM Watson IoT to receive data from Raspberry pi. We are going to be doing three things on Watson, creating a device type, creating a device, and setting up security. To create a device type, select Devices from the options and click on the Device Types tab.

Click on Add Device Type and give your device type a name and description, then click next, leave everything blank and click Finish.

Next is to add a device to our device type, again click on the Devices option from the left panel and click on Add Device.

STEP 5: Select the new device type you just created as the device type and the device ID will be our Raspberry pi MAC address. To get your raspberry pi MAC address, type ifconfig in your Raspberry pi shell and it will display an address that starts with the keyword ether.

After that click next, you can fill out the device information if you look or leave them blank,

Click next and set the security to Automatic key generation by leaving the entry as blank.

Click next and then Finish. It is going to display your device connectivity credentials, make sure you copy this down because you can’t see it again after leaving the page.

.Click on the device in the left panel again and you will see a device with the status Disconnected has been added to your device list.

STEP 6: The next thing we will do is to create a dashboard to monitor our data, click on Boards in the left panel, we will be creating two cards, Humidity, and Temperature inside the Usage overview board, and click on Usage overview and click Add New Cards.

From the card type, select Line Chart to monitor Temperature.

Select our MAC address as the Card source data

Click on connect new dataset and type Temperature in the Event. You can give the property any name of your choice and click next.

Click next and select a chart size and then give your chart a name. With these, we are done creating a widget to monitor our data, and click Submit.

Follow the exact process and create a card for Humidity.

STEP 7: To set up security, click on security in the left panel and click on the pencil icon to edit the connectivity settings, and select TLS Optional among the drop-down options the click save.

STEP 8: We are now ready to run the code, first we need to install the MQTT client, Go back to your Raspberry pi terminal and run

pip3 install paho-mqtt

After that our Raspberry pi is ready to send data to the Watson IoT Platform. Grab the code from the repository: https://github.com/adesolasamuel/How-to-send-DHT-data-from-Raspberry-Pi-to-IBM-cloud Edit the ORG, DEVICE_TYPE, TOKEN, DEVICE_ID to the device credentials you copied while creating a device on Watson platform.

After editing all necessary details, run the code either from thorny IDE or from the terminal.

STEP 9: To monitor our data we head over to IBM Watson studio and our device status should show Connected, to view our data we can click on the dropdown arrow and select Recent events.

With that you are done sending data from our raspberry pi to IBM Cloud, at the time of recording this tutorial, there is not much support for non-JSON data to be viewed on the dashboard so we will not be able to view our data from the widget we created.

--

--

Samuel Adesola

I am an embedded system engineer. Most of my works focus on IoT, AI and Machine Learning. With years of experience in programming and electronics