We are already familiar with some of the popular IoT platforms like ThingSpeak, Adafruit IO, Blynk 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.
- Raspberry Pi with Raspbian OS Installed in it.
- DHT11 Temperature and Humidity Sensor
- 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.
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.
We previously send DHT11 sensor data to many other IoT clouds to make weather station
- IoT based Temperature and Humidity Monitoring using BLYNK, ESP8266 and DHT11 Sensor
- IoT Based Raspberry Pi Weather Station using DHT11 and BMP180 Sensor
- Temperature and Humidity Monitoring over Cloud using Raspberry Pi and Cayenne
- Interface Arduino with Node-RED to monitor the Temperature and Humidity on a Webpage
- IoT Wireless Weather Station using Arduino, ESP8266 and ThingSpeak
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.
- First, update your raspberry pi OS using sudo apt-get update.
- 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.
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.
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.
4. On next page there are some pre-filled information, leave that information as it is and click on Create.
5. Now we are ready with the IoT resource. Launch the created resource by clicking on Launch.
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.
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.
MAC address will be print on the Serial monitor, copy that number and paste in Device ID. Click on Next.
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.
9. On last page as Summary, click on Finish.
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.
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.
12. To display Temperature and Humidity in Graph format, create a template by going into Devices and click on Usage Overview Board.
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.
14. Select device that we have created previously and click on Next. On next page Click on Connect new data set.
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.
16. Choose the Graph size and click on Next then on next page choose color for graph and click on Submit.
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 %.
18. To enable the communication between IBM cloud and Raspberry Pi. Go to security option and click on Connection Security.
19. Under Default Rule, Choose security level as TLS optional and click on Save.
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:
- Download DHT library from github by running below command
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
- 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.
1. Go to Boards and click on Usage Overview option.
2. There are two graphs here that are created to see the value of temperature and humidity updating after every second as shown below.
3. Values can be seen in different format like chart form or Gauge meter by selecting the proper options in the dashboard.
You can also check incoming data in Recent Events tab as shown below.
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.
from time import sleep
import paho.mqtt.client as mqtt
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.connect(server, 1883, 60)