In this blog, I will explain how to connect our raspberry pi as a gateway device to google cloud IoT platform so that we can able to send our sensor data into a cloud environment. We will use LED light as a sensor device to connected to Raspberry pi and then change the status of LED (ON/OFF) from the Google Cloud. This mean we can able to control our LED light connected to Raspberry Pi using the Internet of Things service provided by Google IoT cloud.
This IoT project will connect you every sensor device from homes like TV, Light, FAN and any other electronic appliance which you will be able to easily connect and control from the IoT service.
Before going to much details you should have basic understanding of how Internet of Things works which can be explained in my blog Internet of Things. Then you need to understand how Google service Google IoT Core Platform allows us to fully manage IoT service in a very flexible and efficient way.
The following diagram gives an overview of the device/gateway architecture for use with the MQTT bridge.
This IoT project will connect you every sensor device from homes like TV, Light, FAN and any other electronic appliance which you will be able to easily connect and control from the IoT service.
Before going to much details you should have basic understanding of how Internet of Things works which can be explained in my blog Internet of Things. Then you need to understand how Google service Google IoT Core Platform allows us to fully manage IoT service in a very flexible and efficient way.
Google IoT Architecture |
The following diagram gives an overview of the device/gateway architecture for use with the MQTT bridge.
Basically, the gateway is a device that connects sensor devices to Cloud IoT core and performs serval tasks on the sensor devices' on behalf, such as communication, authentication, storage, and processing the information on the cloud environment. for more information about Google IoT cloud please find here in link Google IoT Cloud.
In the previous blog, I have described the Installation of Raspberry Pi and how to connect in the local wifi network using SSH connection (set up local WiFi and enable SSH on your Raspberry Pi). This will enable us to connect our raspberry pi to access internet service from global internet service.
So let's begin with connecting our Raspberry pi as a gateway to google cloud platform.
Required Hardware:
- Raspberry Pi
- LED
- 10K Ohm Register
- Bread Board and Jumper Wire
- Git
- Python
- OpenSSL
- Xshell
Before going to hardware configuration lets begin with creating the google cloud platform for setting up the raspberry pi as Device gateway. Below are the steps used for configuring the device gateway and creating a device registry.
- Create a Google Cloud Project
- Setting up the local environment and installation process
- Create Device registry and generate Device Keypair
- Add a new device to registry
google iot quickstart
Later we will extend this to our new feature for LED program for data exchange with Cloud.
- Run an LED program to see telemetry from Gateway (Raspberry Pi) to Google IoT Core.
- Update the Config data from Google IOT to toggle the LED status (ON/OFF) connected to Raspberry Pi.
Create a Google Cloud Project:
Go to Google Cloud Console, on the project selector page you can select or create the new project.
Once the project creates go to page selector page go to select the created new project. You will be able to see the dashboard with all the project related information.
In the Google Cloud IoT Console home page, you will be able to see the different products. Now we will enable the Cloud IoT Core and Pub/Sub API product from the home page. Please click the below link to enable to API.
Once you enable the API, you will be able to use the API service with the Google Cloud Platform.
Create Device Registry:
Once the Google IOT project ready we will create the device registry. This device is a processing unit that is capable of connecting to the internet and exchanging the data with the cloud. This device registry is a container of devices with shared properties.
Let start the below steps to create at least one device registry for this project.
Once the Google IOT project ready we will create the device registry. This device is a processing unit that is capable of connecting to the internet and exchanging the data with the cloud. This device registry is a container of devices with shared properties.
Let start the below steps to create at least one device registry for this project.
- Go to Google cloud console (Link) and click on the IoT Core tab in left navigation bar.
- Click on Create Registry option available in the IoT Core menu. Give Registry ID "pi-registry".
- Select the region associated with your location "asea-east1" and Select the MQTT protocol.
- In the Default telemetry topic available in Cloud Pub/Sub Topics, create the new topic "gateway-telemetry" which can be use as a device topic to exchange the telemetry data (LED status).
- Optionally create topic "gateway-state" using Create Device state Field.
- Leave the remaining option as it is and then click below Create button.
you have just created the device registry with Cloud Pub/Sub topic for subscribing or publishing the telemetry message send between Gateway Device and Google IoT Core using MQTT Bridge.
Now you need to create the "Device" which connects to the internet directly through Gateway.
So now we have created Device Registry "pi-registry" and Device "led-light" we can use this information in Device Gateway to communicate with the Google Cloud IoT platform.
Lets now move to our Raspberry Pi device which we will use as the Device Gateway to communicate with the Google IoT core for data exchange related to the Sensor device (led light).
If you are not aware of how to install raspberry pi and enabling the ssh communication then you can check my other blogs on the installation of the raspberry pi.
Now you need to create the "Device" which connects to the internet directly through Gateway.
- Go to Created Device Registry "pi-registry" in the Cloud IoT Core tab.
- Go to the Devices tab in the menu bar.
- Create the device using "Create A Device" Button.
- Create a device with Device Id "led-light" and keep the rest of the field as it is. you will be understood other things as you go further with other examples.
So now we have created Device Registry "pi-registry" and Device "led-light" we can use this information in Device Gateway to communicate with the Google Cloud IoT platform.
Lets now move to our Raspberry Pi device which we will use as the Device Gateway to communicate with the Google IoT core for data exchange related to the Sensor device (led light).
If you are not aware of how to install raspberry pi and enabling the ssh communication then you can check my other blogs on the installation of the raspberry pi.
Setting up Raspberry Pi as you Gateway:
Here we will use Raspberry pi as the gateway device for the installation and setup process.
To Setup your gateway:
- Connect with Raspberry pi. using SSH connection using a local Wifi network (Link).
- Open the console terminal and install the required software dependencies.
sudo apt update && sudo apt upgrade
sudo apt install git
sudo apt install python
sudo apt install build-essential libssl-dev libffi-dev python-dev
- clone the below repository into a local directory
git clone https://github.com/GoogleCloudPlatform/community.git
- go to the below directory structure
cd community/tutorials/cloud-iot-gateways-rpi
- Generate the RS256 Public/Private Key pair by running the following command.
./generate_keys.sh
This will create the below key files in the current directory which can be used for a secure connection between gateway device (Raspberry Pi) and google IoT core.
- rsa_private.pem (secrete key to be kept in the local machine only)
- rsa_public.pem (public key to be shared with a device which wants to have a secure connection with device gateway)
- Provide the public key information with the Device registry created in Cloud IoT Core. To do that please follow the below steps:
- In Cloud IoT core click on the Registry, you created "pi-registry"
- Click on Gateway tab and then click "Create Gateway"
- Provide the Gateway ID "my-gateway"
- Copy the content of rsa_public.pem (from a raspberry pi local directory) into the public key area.
- Leave the rest of the field as it is and then click on Create.
- Back to Raspberry Pi, Now export Google Cloud Project Id as Environment Variable by running following command.
export GOOGLE_CLOUD_PROJECT=your-project-id-123
- Download the Google CA root certificate into the same current directory using the below command.
wget https://pki.goog/roots.pem
- Now set up is done here we will be updating the "run gateway" script by providing the details for registry_id, gateway_id, cloud_reason, and project_id created before.
python ./cloudiot_mqtt_gateway.py \
--registry_id=pi-registry \
--gateway_id=pi-gateway \
--cloud_region=asia-east1 \
--project_id=project-id-123\
--private_key_file=rsa_private.pem \
--algorithm=RS256 \
--ca_certs=roots.pem \
--mqtt_bridge_hostname=mqtt.googleapis.com \
--mqtt_bridge_port=8883 \
--jwt_expires_minutes=1200
- 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 Device Gateway:
pip install virtualenv
virtualenv env
source env/bin/activate
- You will be able to see the below (env) on the left side of the ssh console.
- Now install the required package by using the below command.
pip install -r requirements-gateway.txt
- Now start the Device Gateway service by running below command.
source run-gateway
Now keep this Gateway running on console separate SSH console. This will provide the interface to communicate created devices (led-light) to communicate with the Google IoT core.
The next part is to use the created "led-light" device to interact with the Gateway. Here we will connect LED light to Raspberry Pi via Ping 14 and Ground PIN. Below is the schematic of the LED connection.
Now you can use the "run-led-light" script to update the LED switch using cloud communication.
this script will update the LED switch based on the data coming from the Google IoT Core Device "led-light".
Manage Device (led-light) Via Config Update:
Now we will manage the LED light connected to Device Gateway (Raspberry Pi) though Google IoT Core config update.
- Go to Google IoT Core console and click on registry created "pi-registry"
- Click on the "pi-gateway" from the Gateway Tab in the registry.
- Click on Bound Devices Tab
- Click on Blind Device and then select the box next to "led-light"
- Confirm by clicking Bind in the lower right.
Once the device is successfully able to Bind. we are ready to run the script which will start sending and receiving the data from the cloud. Please find below the steps to run the device script from different console connected to a raspberry pi.
- Go to raspberry pi ssh console and edit the led-light.py by adding the IP address of the Device gateway (Raspberry PI which already running as a gateway from the run-gateway script.) which is already running on Raspberry Pi.
- Make sure you have already connected LED to Raspberry pi's GPIO pin 14 and ground using the appropriate register.
- Run the following program from the Raspberry pi console
source run-led-light
- Once the program runs it will start to show the status for LED like "Client received OFF".
- Now your LED is connected to the Cloud IoT Core platform so you can update the device status from the Cloud IoT Console.
- Go to Google IoT Console and select your Registry "pi-registry".
- Go to devices and select the device "led-light".
- Click on Update Config at the top of the page. Now you can able to communicate with the LED device by sending the message data for switching ON/OFF LED connected to the Raspberry Pi Gateway device.
- In update Configuration show above you can able to send the using Test message, Enter ON or OFF to toggle the LED state which will reflect LED status connected to Raspberry Pi.
All set then we will be able to see our LED light is getting toggle with each message. And that's how we can connect our LED light to the Google IoT Core platform successfully.
Please try this exercise by yourself so that you will be able to enjoy the IoT project from my blogs.
Next blog, I will be showing how we can attach multiple sensor devices to Raspberry Pi (Device Gateway ) and able to see the sensor data over the cloud environment in real-time.
I hope you have enjoyed this blog, so please do like, comment and share this blog.
Please try this exercise by yourself so that you will be able to enjoy the IoT project from my blogs.
Next blog, I will be showing how we can attach multiple sensor devices to Raspberry Pi (Device Gateway ) and able to see the sensor data over the cloud environment in real-time.
I hope you have enjoyed this blog, so please do like, comment and share this blog.