How to send DHT11 Sensor Data to IBM Watson Cloud Platform using Raspberry Pi

How to send DHT11 Sensor Data to IBM Watson Cloud Platform using Raspberry Pi

We are already familiar with some of the popular IoT platforms like ThingSpeakAdafruit IOBlynk etc. and already used these platforms to build many IoT projects. But apart from these platforms there are some industry grade IoT platforms like Amazon Web Services, Microsoft Azure and IBM Watson which are frequently used by big industries to store and analyse the data gathered from IoT devices. These platforms are more secure and offer many useful services, from getting data to analyse it using Machine learning algorithms.

 IBM Watson is one of powerful platform which offers services like speech to text, text to speech, visual recognition, natural language processing and cognitive computing functionalities to your product. This platform can help in analyzing and understanding your data which can be images, audio, video or even simple text and numbers. Chatbots can also be created using this platform and they can talk with the user. IBM Watson is a highly powerful machine learning framework and can be used with Raspberry Pi to build more powerful applications. This platform is compatible with MQTT Protocol, HTTP and REST APIs. 

In this tutorial, we will setup IBM Watson Account and send the DHT11 Temperature and humidity data to it using Raspberry Pi. We will also visualize this data with the help of graphs. Previously we used Raspberry Pi to build many IoT based applications.

 

Requirements

  1. Raspberry Pi with Raspbian OS Installed in it.
  2. DHT11 Temperature and Humidity Sensor
  3. Jumper Wires

 

Here, we will use SSH to access our Raspberry Pi on the laptop. If you have monitor then it will be very easy to start with but if you don’t have a monitor then setup raspberry pi in headless mode or use VNC server to get Raspberry Pi desktop on Laptop

Raspberry Pi is very popular for building IoT based projects as it has all the necessary support for Internet of Things. It is a palm size computer having inbuilt Wi-Fi, Bluetooth, USB port, Audio/video port, HDMI port, camera port etc. You can check all the Raspberry Pi based Iot Projects here.

 

Circuit Diagram

Hardware only involves Raspberry Pi and DHT11 sensor. Connect DHT11 signal pin to GPIO17 of Raspberry Pi and Vdd & GND pin of DHT11 to 5v & GND pin of Raspberry Pi respectively.

Circuit Diagram for Sending DHT11 Sensor Data to IBM Watson Cloud Platform using Raspberry Pi

 

Circuit Hardware for Sending DHT11 Sensor Data to IBM Watson Cloud Platform using Raspberry Pi

 

We previously send DHT11 sensor data to many other IoT clouds to make weather station

 

Setup MQTT Client on Raspberry Pi

In previous tutorial of ESP32 and IBM Watson, we used pubsubclient to send and receive MQTT messages over IBM platform. In Raspberry Pi also, we need an MQTT client to send and receive message so first we install the libraries for the same. Here paho-mqtt library is used with Raspberry Pi.

  1. First, update your raspberry pi OS using sudo apt-get update.
  2. Now, Install paho mqtt client library by running the below command
pip install paho-mqtt

 

To know more about MQTT and how it works with Raspberry Pi, follow the article.

 

Setup Account for IBM Watson IoT Platform

1. To create an account on IBM Watson platform, Go to on IBM cloud page and click on Create an IBM Account. Enter your Details and Click on Create Account as shown below.

Setup Account for IBM Watson IoT Platform

Create a IBM Watson Account

 

2. To use any service on IBM platform we have to create resource for it. So, Login with your credentials (Note that IBMid is your e-mail id that you have used for creating account) and you will be redirected to IBM cloud website. Now, click on Create Resource.

Create Resources in IBM Watson Account

 

3. There are many services offered by IBM Cloud, all of them can be seen in Resource list. As we want to use Internet of Things service. So, click on Internet of Things and then click on Internet of Things Platform.

Internet of Things with IBM Watson

 

4. On next page there are some pre-filled information, leave that information as it is and click on Create.

Setup IoT on IBM Watson

 

5. Now we are ready with the IoT resource. Launch the created resource by clicking on Launch.

Launch IoT Resources with IBM Watson

 

6. Now, you will be redirected to Internet of things page. Here, add your device to connect it with the IoT platform. Click on Add device.

Add Device to IoT IBM Watson

 

7. Fill the Device details on next page. Give any name in Device Type and your Device ID will be MAC address of that device. You can find MAC address of raspberry pi using the ifconfig as shown below. Use this address without colon.

Filling Device Details in IBM Watson

 

MAC address will be print on the Serial monitor, copy that number and paste in Device ID. Click on Next.

Device ID for IoT IBM Watson with Pi

 

8. On next page, there is some information related to device. Leave that information unfilled and click on next. Same for Security section, leave it unfilled and click on next as shown.

Security Section for IoT IBM Watson

Finish Security Setting for IoT IBM Watson

 

9. On last page as Summary, click on Finish.

Finish Setup for IoT IBM Watson with Pi

 

10. Now, we have successfully created the device, and Device credentials will be shown on next page. Copy all the credentials and save it somewhere. They will be needed in the code.

Device Credentials-for IoT IBM Watson

 

11. Click on Devices and here you will see Raspberry Pi is successfully created but it is not connected. Now, we will connect the Raspberry Pi with this platform by writing code for it.

ESP32 is successfully created

 

12. To display Temperature and Humidity in Graph format, create a template by going into Devices and click on Usage Overview Board.

Display Temperature and Humidity Graph in IBM Watson

 

13. Now, click on Add New Card. Here you will find different type of graphs to display. We will use Line chart graph so go ahead and click on Line chart.

Add New Card to IBM Watson

Create New Card to IBM Watson

 

14. Select device that we have created previously and click on Next. On next page Click on Connect new data set.

 

Create Line Chart Card for IBM Watson

 

15. Now, enter the data to be displayed on the graph. We will send the temperature value on status1 event (status1 variable is mentioned in the code). Give property and name as temperature, type as Number, Unit as degree Celsius and click on Next.

Created Line Chart Card for IBM Watson

 

16.  Choose the Graph size and click on Next then on next page choose color for graph and click on Submit.

Created Value Card for IBM Watson

Color Value Card for IBM Watson

 

17. Now, you will see a graph as shown below. Repeat above steps for making another graph for humidity and give event as status2, property and name as humidity, value as Number and unit as %.

Repeating steps for making graph for humidity

 

18. To enable the communication between IBM cloud and Raspberry Pi. Go to security option and click on Connection Security.

Setting Up Connection in IBM Watson with Pi

Checking Policies in Connection Security

 

19. Under Default Rule, Choose security level as TLS optional and click on Save.

Choose Security Level as TLS optional

 

That’s it. We have successfully setup IBM Watson IoT platform for Raspberry Pi. Now, we have to write Python code for sending value of temperature and humidity to this platform.

 

Code and Explanation

Before proceeding further, install DHT sensor library in our Raspberry Pi by following the steps below:

  1. Download DHT library from github by running below command
git clone https://github.com/adafruit/Adafruit_Python_DHT.git

 

  1. Now to go to Adafruit_Python_DHT directory using cd Adafruit_Python_DHT and install the library using following command
sudo python setup.py install

 

After installing the library, you can test the DHT sensor by running the example code in the same directory.

Complete python code with a working video is given at the end of this tutorial, here we are explaining the complete program to understand the working of the project.

First, import all the required libraries for time, DHT sensor and MQTT client

from time import sleep
import Adafruit_DHT
import paho.mqtt.client as mqtt

 

Initialize pin number on which DHT sensor is attached and declare a variable to store sensor data then this data will be shared in two different variables - humidity and temperature.

gpio=17
sensor=Adafruit_DHT.DHT11
humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)

 

Then define all the credentials that you have copied while adding a device on Watson platform as shown below.

ORG = "******"
DEVICE_TYPE = "raspi" 
TOKEN = "*******"
DEVICE_ID = "***********"

 

Now, provide server link and topics on which data will be published. We have 2 topics, 1 for temperature and other for humidity, give name as status1 and status2 respectively. Give AuthMethod as “use-token-auth” and then define clientID as shown below.

server = ORG + ".messaging.internetofthings.ibmcloud.com";
pubTopic1 = "iot-2/evt/status1/fmt/json";
pubTopic2 = "iot-2/evt/status2/fmt/json";
authMethod = "use-token-auth";
token = TOKEN;
clientId = "d:" + ORG + ":" + DEVICE_TYPE + ":" + DEVICE_ID;

 

Initialize a variable for MQTT client for using functionalities of MQTT. Set port number and server name in connect function.

mqttc = mqtt.Client(client_id=clientId)
mqttc.username_pw_set(authMethod, token)
mqttc.connect(server, 1883, 60)

 

Now, in a while loop we will continuously send temperature and humidity data to IBM cloud after every 5 sec.

while True:
    mqttc.publish(pubTopic1, temperature)
    mqttc.publish(pubTopic2, humidity)
    print ("Published")
    sleep(5);

 

Now the Python code for sending data to IBM cloud is complete. You can find the complete code with working video at the end of this tutorial.

 

Finally, connect the DHT sensor with gpio17 as shown in connection diagram and run the script using python filename.py command.

 

Checking Uploaded data on IBM Watson IoT Platform

1. Now, its time to check the uploaded data on the IBM Watson platform. As soon as your module is connected with the Wi-Fi, you will see connected status on IBM Watson platform as shown below.

Checking Uploaded data on IBM Watson IoT Platform

 

1. Go to Boards and click on Usage Overview option.

Setup Cards for Updating Value graph on IBM Watson

 

2. There are two graphs here that are created to see the value of temperature and humidity updating after every second as shown below.

Graphs for temperature and humidity on IBM Watson

 

3. Values can be seen in different format like chart form or Gauge meter by selecting the proper options in the dashboard.

Dashboard for ESP32 with IBM Watson Cloud Platform

 

You can also check incoming data in Recent Events tab as shown below.

Check incoming data on IBM Watson

 

There are lots of other features to explore with IBM Watson platform. So this is how Raspberry Pi can be used to send the data to IBM Watson IoT cloud platform

Check the complete python code and Video below.

Code

from time import sleep
import Adafruit_DHT
import paho.mqtt.client as mqtt

gpio=17
sensor=Adafruit_DHT.DHT11
ORG = "******"
DEVICE_TYPE = "raspi" 
TOKEN = "************"
DEVICE_ID = "*********"

server = ORG + ".messaging.internetofthings.ibmcloud.com";
pubTopic1 = "iot-2/evt/status1/fmt/json";
pubTopic2 = "iot-2/evt/status2/fmt/json";
subTopic = "iot-2/type/+/id/+/evt/+/fmt/+";
authMethod = "use-token-auth";
token = TOKEN;
clientId = "d:" + ORG + ":" + DEVICE_TYPE + ":" + DEVICE_ID;

humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)

mqttc = mqtt.Client(client_id=clientId)
mqttc.username_pw_set(authMethod, token)
mqttc.connect(server, 1883, 60)


while True:
    mqttc.publish(pubTopic1, temperature)
    mqttc.publish(pubTopic2, humidity)
    print ("Published")
    sleep(5);


mqttc.loop_forever()
 

Video