Connecting Raspberry pi to google Cloud IoT platform with python

https://en.wikipedia.org/wiki/Internet_of_things#/media/File:Internet_of_Things.jpg
Wiki: IoT

Let's make today interesting stuff.. 

we are going to connect small sensor devices to the internet. With IOT (Internet of things) today we can able to connect any device to the internet. devices are nothing but the processing unit which is capable of collecting data (sensor data) and able to connect to the internet. 


To connect these sensor devices which are usually embedded in electronic devices or home appliance. these devices are capable of getting data from the environment. 

How these devices can talk to the internet then?
this is a big question now.

Like our mobile and laptop which are getting connected to internet protocol supported by big hardware device with high computing power. but for the small lightweight and small computing devices will not able to connect directly to the internet. So we need someone who should act as intermediate to connect these devices to linter. 
IOT gateway solution here. They are capable of providing the service and functionality to connecting  any devices to the internet.  There are many IOT gateway services available with IOT infrastructure. Please follow below link for more available IOT platform.

https://da-14.com/blog/10-best-iot-platforms-iot-technology-forecast

Google IOT platform provide the service to connect your devices to the cloud. using these devices you can communicate and exchange the data between devices.

Google IoT core is most is one of the best IOT platforms which allows connecting various devices and gather sensor data for processing and analytics over the internet.


Let's make use of this technology to connect our small devices to the internet.  In this tutorial, I will be showing how to set up the google IOT platform with Raspberry Pi so that you can play with it.




Table of Content

1. Raspberry Pi setup
2 providing the library support (Python and MQTT)
3. installing and preparing the Google platform setup
4. play with the code


Raspberry Pi Setup


 
      If you have a raspberry pi model with Wifi support then you can directly connect your raspberry pi using SSH/VNC server. if you are able to access raspberry pi in wifi network the well and good. if not please find below a link to set up the Raspberry Pi with Wifi Network.






Adding the dependent libraries:

Update the advance package tool when you wanted to install any new package. so that we can get the latest package available in the repository.

      sudo apt-get update


 It will always good to do this at first place when installing any package in the operating system. We are using Rasbian OS (operating system) which will keep update every day so it is always good to be updated.

JWT:

to connect with the google IOT platform we need to have a secure connection. so instead of typing username and password the console there is always the better way we can establish a secure way. JWT provides a better way of authentication using JSON web token between two or multiple parties.
JWT is part of the python library with cryptography support. also, you need to install few base libraries before python. please run below command to install these dependent libraries along with python.


sudo apt-get install build-essential
sudo apt-get install libssl-dev
sudo apt-get install python-dev
sudo apt-get install libffi-dev


Once you install python and dependance libraries you can check the installed version of the python using below command.
this python version updated by the Raspbian team support which will support the raspberry pi OS.

if you want to install a specific package then you can go to the python web link (Latest python version) and install the package manually.


MQTT Protocol:

We need python MQTT protocol support to connect to google IOT platform via IoT gateway. google IOT gateway support the multiple protocols like MQTT and HTTP protocol to establish connectivity.
We will be using MQTT protocol towards the google IOT platform so that our device can send the data via this lightweight protocol. For this, we will be using the python mqtt library (paho mqtt) which is part of the python package.

Install paho mqtt package using PIP which is package management system used to install and manage the software package managed in python.

sudo pip insall paho-mqtt

Install JWT package suing same PIP command

sudo pip install pyjwt

Now install the cryptography package which is a dependency for JWT package to an established secure connection.


Preparing and connecting Google cloud platform

If you already use of the Google cloud account then you can access the Google IOT services from the Google cloud console (google cloud platform).
If you are new to google cloud please create the developer account using below link to access the Google cloud services. You can create the developer account with an existing Gmail account and access the available free services. create google Cloud platform account will credit free ammount which is enough to get started with google IOT platform.

Once you create an account the Google cloud service can be access from the Google cloud console.




You will be able to see the console with mulple services related to your google cloud services.

To srart with google IOT core you can search the same in the search button for "IOT CORE" you will be able to see once you typed in search bar.




Enable IOT Core API:

        Once you click on the IOT Core link you will be prompt to enable IOT Core API for the first time.


Clicking the enable to the button will allow providing the permission to default security rules to allow your users to user IOT core. These permissions handled in the IAM part of the console which you can latter modified based on the user permission you wanted to set for all cloud service users.

Creating registries and devices
  
After enabling the IOT Core API you will need to register IoT devices (could be your sensor device which is capable of supporting MQTT protocol with python). In this case, I will be using the Raspberry Pi as my device.
For details please below link to see how to create, edit and delete device registries and device within IOT core.

visit: https://cloud.google.com/iot/docs/how-tos/devices

Registries are the logical collection of devices which can communicate to the internet (google cloud platform). you can group multiple devices in to like all sensors inside one infrastructure for the smart home. In smart home infrastructure, all devices like fan, oven and other sensing devices can be grouped together and register in the logical domain.



Now we will create the registries where you wanted to connect IOT devices to visible.


1. Create a device registry from the console visible above. Same can be done from the IOT Core console.

2. Provide the registry id name, which can be any name you wanted to suggest for your project
3. Keep default bothselected ass of now, but we will be using MQTT protocol to communicate.
4. Select the "Cloud Pub/Sub topic" under default elementary topic and create topic option will allow creating the topic on which device will be able to communicate. provide a suitable name to your topic. in this case it is "device_speak"
5. skip the CA certificate part. 
5. Then you can create registry "Create Registry" button and end of the pages.
Once the you create registry you need to create the devices under created registry "raspberrypi".


Now create the device and update the required parameters.


  • Provide the device ID name like "sensor_led"
  • Keep authentication default
  • Select the public key format "RS256_X509"
  • Add the public key Value with below openSSL support


W need the Public key to establish secure connectivity of sensor devices to IOT core Platform. 
With OpenSSL libary, we will able to generate the certificate and public key parameter on our Raspberry Pi. OpenSSL library is by default installed with the Raspbian OS and can generate using the OpenSSL command line tool. Please find more on openSSL 

Run below command in raspberry pi ssh console.


openssl req -x509 -newkey rsa:2048 -keyout device_private.pem -nodes -out device.pub -subj "/CN=unused"



Once you will able to see the private (device_private.pem) and public key (device.pub) are generated in the same directory.


device_private.pem: This device certifacate file contains the private key. as the name suggest this is private to the device must be secreted and  not to be revealed

device.pub: This file contains the public key which can be shared with IOT core gateway so that both device and IOT core gateway can communicate securely over the internet.

Next to copy the content of the device.pub file to public key value field in Create device console opened to create a device.

rub below command to see the content of the device.pub :

  cat device.pub




Copy the above conted to device public key value.




Then select the "Create Device" button below the page. Keep other entry default.

On successful device creation you will be able to see the device details:


Once devices is created we can able to see other details along with public key value shared with a device created.

Now to handle the authentication from the google IOT side confirming the device is ready to talk to Google IoT core, we need to share the Google certificate into the device so that device knows it is talking to google IoT core cloud. Google keep the root certificate in below path which can store in our local raspberry pi.

path: https://pki.google.com/roots.pem

In raspberry, I can run the below command to fetch the above file.

wget https://pki.google.com/roots.pem



Now you will be able to see the root.pem file in the same current directory where we have created device private certificate and public keys.


The last thing is to enable the Pub/Sub messaging service from the IoT Core console. Search "Pub/Sub" in the search bar you will ask to enable the Pub/Sub service for the first time in case not done.


Once you enable it will show you topic details you have subribed to which we have already created with the registry . example below shows "device_speak" as my Sub/Pub topic.





Now we can connect the sensor device to Raspberry pi via GPIO inteface.

Below find below setup details:



Now you have all setup ready to get your device talk to Google IOT core. Lets move towards the code part.



Please find the below git link for source code.

Please follow the below steps to get started with code and exection:


  • clone the the source from git repo to raspberry pi via ssh console
  • Please follow steps in README file for running the python script 



No comments:

Post a Comment

Smart Home Gardening solution with IOT

Food is an essential part of day-to-day life. So with agriculture, we can cultivate the plant and made food for a living. We have a very ...