In the last Blog Using Google Cloud IoT Core Platform with a Raspberry Pi, we have seen How to connect the Raspberry Pi as Device Gateway to the Goole IoT Platform. We were able to connect the LED and able to configure the status of LED from Google IoT service.
In this blog Using the DHT sensor data like humidity and temperature can be collected from the room environment. This sensor data are given to connected Raspberry Pi and then sends this sensor information to the Google IoT Platform using the MQTT Bridge protocol.
Cloud IoT Core is a fully managed service that allows you to easily and securely connect, manage, and ingest data from millions of globally dispersed devices This Device gateway will send the sensor data to the google cloud platform for data storage and processing which can be further use in may services related to IoT.
Hardware Requirement:
- Raspberry Pi 3 Model.
- DHT11 Sensor.
- Jumper Wire
Software Tool Requirement:
- Xshell Software
- WinSCP
- Openssl
- Python3
Circuit Diagram:
In this circuit diagram, Raspberry Pi will collect the room temperature and humidity information using the DHT sensor connected via the GPIO pin. Python program running on Raspberry pi will read this sensor information and send it to the Google Cloud using the MQTT protocol (MQTT Bridge).
Please find below basic information related to cloud IoT Project
- Create an IoT cloud project and enable Cloud IoT Core and Cloud Pub/Sub APIs (quickstart).
- Creating registries and devices (Link)
- Setting up your Raspberry Pi (Tutorial Link).
In my previous blog (Using Google Cloud IoT Core Platform with a Raspberry Pi) I have shown how to connect the Raspberry Pi as Device Gateway with an example of connecting LED light to Google IoT Core. So I hope you are aware of basic information related to creating the project related to Google IoT core.
Objective:
- Create a Device Registry and Device topic.
- Create Subscription on created device topic.
- Set up your Raspberry Pi with DHT.
- Sending DHT sensor data from Device to Google Cloud IoT.
- Show Received Data with Subscription message.
Create a Device Registry and Device topic:
Let start the below steps to create at least one device registry for this project.
- Click on "Create Registry" to create the new Device Registry "iot-sensor" and and select the region "asia-east1".
- Now add the topic to this registry by selecting the option in "Cloud Pub/Sub topics" dropdown. Create the new device "iot_sensor_dht" and then click "Create Topic".
- Select default HTTP and MQTT protocol.
- Leave the remaining option as it is and then click below "Create".
Create a subscription to the registry's Pub/Sub topic:
Now we will add the new subscription to the created registry topic "iot_sensor_dht". This is used to subscribe to the data exchange on this device topic.
- Go to google cloud IoT page in the Google cloud console (Link)
- Then go to the "Pub/Sub" tab available in the left navigation bar and click on topics.
- Once you click on Create Subscription you need to provide the subscription details for the topic "iot_sensor_dht".
- Give the subscription name "pub_sub_iot_sensor_dht" send the delivery type to pull to receive data on this device.
- Leave the remaining option as it is and then click below "Create".
- Now we can able to see our subscription name in the Pub/Sub subscription details.
Now we have successfully created the Device registry "iot_sensor", Device topic "iot_sensor_dht", and subscription "pub_sub_iot_sensor_dht", next step is to send the sensor data to this topic from Raspberry pi. So lets set up and configure Raspberry pi to establish communication with Google IoT.
Secure Account Service:
There are different ways to impersonate a service account to access Google APIs:
Here we will create the service account to access the specific google service need to access the resource by creating Cloud IAM policies. Let's see how to create a service account and download the service account access file.
Service account intended to represent user that needs to get authentication and authorizatoin to access google cloud API's or google cloud service. it is a way of secure communication between the google cloud service and the end-user human or non-human. It simplifies the process of authentication of the end-users devices which can be communicated using this secure way of communication by providing authentication and authorization.
- Authentication using RSA private keys (Uses the Encryption Algorithm)
- Authorization using Cloud IAM policies
Authorization using Cloud IAM policies:
All service accounts have Cloud IAM policies that grant access to the service account. Some permissions allow users to impersonate, or become, the service account based on the users' credentials.
- Open Google Cloud Console and then got to the IAM & admin tab available in navigation bar in the left. Click on the "Service Accounts" link.
- Now create a Service account by clicking on "CREATE SERVICE ACCOUNT" from the IAM & Admin console.
- Now furnish the details with Name field "serviceaccount" and then click "create"
- In the next step, we will create the rules to give different access to a user from this service account. Here we will add roles for "Cloud IoT Admin" and "Pub/Sub-admin" from the Role select bar.
- Now click on the "Create Key" for the created service account to download the service account file for user.
- Select the JSON format for the service account file.
- Service account JSON file will get download to your local directory which will use for authorization and authentication of the device to the google cloud service. Keep this file very securely as it gives you access to your Cloud Service Platform.
- Now copy this file to your device directory [Raspberry Pi] so that this file can be used in the device program while communication to Google Cloud service.
- I used WinSCP to copy this JSON file to the Raspberry pi Directory.
Set up your Raspberry Pi with DHT sensor:
Connect to your raspberry pi using the SSH connection. Before connecting to Raspberry pi make sure your raspberry pi is connected to a local wifi network and able to access the internet service. Please find details regarding the SSH connection. Once your Raspberry Pi device is in the local network you will be able to access it using VNC Viewer or SSH connection (How to set up WiFi and enable SSH on your Raspberry Pi).
Here I am using the XHELL software to connect to the raspberry pi using the Wifi IP address assign to the Raspberry Pi device. Provide the connection related information before setting up the ssh connection which includes the username and password credential of Rasberry Pi user.
Once you were able to connect via SSH you will be able to see below screen after a successful connection to Raspberry Pi.
- Go to Desktop and create the new folder "DHT".
- Now you can use the virtual environment to keep the installation local to the workspace instead of installing libraries into the raspberry pi system library which causes extra space and overload on the system.
- Use below command to create the virtual environment for running the DHT Sensor Program to send sensor data to Google Cloud:
pip install virtualenv
virtualenv env
source env/bin/activate
- Once successfully virtual environment is created you will be to below screen over the console.
- Now run below commands to download and install the required package for creating device setup on the raspberry pi.
sudo apt update && sudo apt upgrade
sudo apt install git
sudo apt install python
sudo apt install build-essential python3-dev
- Now download the DHT Adafruit library "Adafruit_Python_DHT", Python library to read the DHT series of humidity and temperature sensors on a Raspberry Pi.
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
- Now install this library using the below command
cd Adafruit_Python_DHT
sudo python setup.py install
After successful, this will build and install the library to the system.
- Now go back to the previous folder "DHT" and download the program to publish the DHT sensor data to google cloud.
(env) pi@raspberrypi:~/Desktop/IOT/DHT $ git clone https://github.com/sonwaneganesh/Sensor-data-to-google-cloud (env) pi@raspberrypi:~/Desktop/IOT/DHT $ cd Sensor-data-to-google-cloud
(env) pi@raspberrypi:~/Desktop/IOT/DHT/Sensor-data-to-google-cloud $ export GOOGLE_APPLICATION_CREDENTIAL="/home/pi/Desktop/IOT/DHT/prefab-builder-267610-85095c6e6a55.json"
(env) pi@raspberrypi:~/Desktop/IOT/DHT/Sensor-data-to-google-cloud $ export TOPIC_NAME="iot_sensor_dht"
(env) pi@raspberrypi:~/Desktop/IOT/DHT/Sensor-data-to-google-cloud $ export PROJECT_ID=prefab-builder-267610
- Before running the program we need to do some configuration for setting the Project details and Topic details using an export environment variable to system configuration. We have export TOPIC_NAME and PROJECT_ID details which have created using Google Cloud Console.
- Additionally, you need to copy the downloaded service account JSON file to raspberry pi and provide the path the variable "GOOGLE_APPLICATION_CREDENTIAL"
(env) pi@raspberrypi:~/Desktop/IOT/DHT/Sensor-data-to-google-cloud $ export GOOGLE_APPLICATION_CREDENTIAL="/home/pi/Desktop/IOT/DHT/prefab-builder-267610-85095c6e6a55.json"
- Now download the DHT sensor code from my below git repo Sensor-data-to-google-cloud. Please follow the README file to update the DHT sensor and PIN configuration with Raspberry Pi.
- You are ready to run our python program to send the sensor data by importing the google cloud and Adafruit DHT library function.
(env) pi@raspberrypi:~/Desktop/IOT/DHT/Sensor-data-to-google-cloud $ python publish_dht.py
- After this script will show the result data which have been sent continuously to the Google Cloud.
- To verify whether data is on google cloud you can pull the data using subscription command on google cloud shell.
- Run below command for pull data from the DHT sensor device.
rakhishamkure@cloudshell:~$ gcloud pubsub subscriptions pull pub_sub_iot_sensor_dht --auto-ack --project=prefab-builder-267610 --limit=100
- You will see the Data received on the google console successfully.
Now we are able to see sensor data Temperature and Humidity from the DHT sensor connected to Raspberry pi is visible on the google IoT shell console.
We have successfully able to send sensor data to the Google cloud environment where this information can be processed and analyze to take further different actions based on application we wanted to develop.
Let's see one use case where if the temperature reaches a certain degree then we can turn on AC or FAN to maintain the conditional temperature environment in the room.
In the next blog we will use Google Cloud service to control other devices, So please keep yourself updated with my blogs.
SO please do like, comment and share this to make available to everyone are interesting in making such an interesting IoT project.
No comments:
Post a Comment