Saturday, February 22, 2020

Using Google Cloud IoT Core Platform with a Raspberry Pi





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.

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 
Required Software:
  • 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
This is above steps which can be found with the quick-start guide of the google could page:
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.

  • 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.
  • 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. 
  1. rsa_private.pem (secrete key to be kept in the local machine only)
  2. 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:
  1. In Cloud IoT core click on the Registry, you created "pi-registry"
  2. Click on Gateway tab and then click "Create Gateway"
  3. Provide the Gateway ID "my-gateway"
  4. Copy the content of rsa_public.pem (from a raspberry pi local directory) into the public key area.
  5. 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.




Sunday, February 24, 2019

Introduction to Raspberry Pi





Hi everyone.

Let's start something interesting with Raspberry Pi wonderful and powerful electronic device for IOT projects. This is a Raspberry Pi, small portable size computer in your hand. It is a single board computer developed by Raspberry Pi foundation educational and electronic project study in institutes. Please visit the Raspberry Pi site for more information 

In this tutorial, I am going to provide the basic information about the Raspberry Pi which is a tiny and affordable computer that can be used to learn programming and interesting project on the internet of things (IoT) projects. 
Raspberry Pi has many release version, first generation (Raspberry Pi 1 Model B) which was released in 2012, followed by other cheaper version with Model A support.  Raspberry Pi 3 Model B+ which is latest in all the Raspberry Pi model release in 2018. Their board is approximately credit card size which supported all the configuration need to as a personal computer. For information about all product you can visit official site of raspberry Pi.

Now let's get deep details into configuration of the Raspberry  Pi 3 Model B+.

Hardware Configuration:

 The Raspberry Pi 3 Model B+ is the latest product in the Raspberry Pi 3 range, boasting a 64-bit quad core processor running at 1.4GHz, dual-band 2.4GHz and 5GHz wireless LAN, Bluetooth 4.2/BLE, faster Ethernet, and PoE capability via a separate PoE HAT


Please find the details of the Raspberry Pi 3 Model B+ specification.

  • Processor : Broadcom 64-bit SoC @ 1.4GHz
  • Memory: 1GB LPDDR2 SDRAM
  • Connectivity: 
        • WiFi 802.11.b/g/n/ac wireless LAN
        • Bluetooth 4. Gigabit Ethernet over USB 2.0
        • 4 × USB 2.0 ports

  • Video & Sound: 
        • HDMI port support
        • MIPI DSI display port
        • MIPI CSI camera port
  • SD Card Support: Micro SD format for loading operating system and data storage
  • GPIO Access: Extended 40-pin GPIO header
  • Input Power: 
        • 5V/2.5A DC via a micro USB connector
        • 5V DC via GPIO header
        • Power over Ethernet (PoE)–enabled (requires separate PoE HAT)
  • Environment: Operating temperature, 0–50°C


General Purpose Input-Output (GPIO) connector:

With Raspberry Pi GPIO pin we can able to communicate with external circuitry as the extension board. We can connect and interface the electronic circuits which are used in an embedded system using the Raspberry Pi GPIO interface



More details about the interface and understanding the GPIO pinout can be at Raspberry pi pinout web page.

Each pin has the functionality to carry the analog and digital signal to the external circuit and communication using controlled and configured interface.

Using GPIO standard pin you can able to turn devices on and off such as LED light can be controlled using these GPIO digital pins.


Along with the standard GPIO input-output pin some other interface communication is also supported by GPIO interface.


  • I2C (Inter-Integrated Circuit): pins allows you to connect and talk to any hardware module circuit that supported this I2C protocol communication. Example If we wanted to connect DHT (temperature and humidity) sensor to our raspberry pi it can use I2C interface to communicate with Raspberry pi.
  • SPI  (Serial Peripheral Interface Bus): pins can be used to connect and talk to SPI devices which usually used for data transmission between Raspberry pi to SPI supported devices such as SD card.
  • UART Universal Asynchronous Receiver/Transceiver):  it is the serial pin used to communicate with connected other devices.

Software Support:

Raspberry Pi can be loaded with various distributions of the operating system (OS).  Raspberry Pi foundation provides the Raspbian, Linux distribution as well as the third party Ubuntu, Windows 10 IOT Core, AndroidThings and many more based on your required platform for IoT.  this Operating system can be installed on SD card which can be used as the storage devices for operating system and applications.

Once the operating system installed we will be able to access like home computer. There are multiple devices can be connected to Rasberry pi to access like the computer. We can connect Computer desktop using HDMI card, also we can connect the Mouse and Keyboard with a supported USB board.

We can connect this raspberry pi device to our home network using the LAN port or using Wifi support. Once this device is able to connect to the internet we can also able to access the internet service using a raspberry pi.





This is what Raspberry Pi is able to provide the most of the required functionality to start projects on IOT.

This is the Raspberry Pi version which I have got from the Thingsbits online shopping which packed with power cable and USB cable. It cost me around Rs. 2540 Indian rupees which can be affordable for compared to a large computer system.


Additionally, I ordered Heatsink to protect from heat due to the processor which can reach up to 85-degree Celsius to avoid the damage to my raspberry pi board.  

Please watch this video about the introductoin of Raspberry Pi:



So this is how I got this my first product of raspberry pi and seems very interesting to me with its design and compatibility.







Friday, December 21, 2018

Connecting Things



Welcome to my personal blogs for sharing my knowledge and experience with you and to the internet world. I am very interested in learning new things over the internet especially how the new internet technologies which are going to change the future worlds.

Today with the use of the internet we are able to communicate with each other all over the world within instance click. not only the communication so we are able to share every aspect of life from waking up with a mobile device and opening the app to an early morning workout. making our calendar to be reminded by mobile devices or google assistance. Ordering the food online with instant delivery at home or office. automation car which is ready to come up to home at pick you up to the office. This is how wonderful technology changing human life in today's phase.

The machine is able to communicate to human, robots are being designed to serve humans in automation and machine pipeline with high accuracy and quality work.

How this technology is going to change the future ear. This is the era of the Internet of Things where everything is connected and can communicate with each other.

With the internet of things now not only mobile or computer device communicate with each other also it possible to every small device can be able to send the information, process information an ability to generate output as expected without human intervention.

With new era of computer technology and the software services we are not limited to use of the computer system for solving the human problems. Today we are able to communicate using intern

With the advance in today's internet world, technology has taken us to the next level of an era where human survival depends on this technology.

I am creating these blogs to share my personal experiences and understanding of new technology emerging in this internet world.  I will share some of the interesting stuff related to internet and technology. 





This is educational blogs where you will see a lot of interesting things which can be learned and understand today's advanced technology and development work ongoing in the field of the internet of things.

Hope you will like my blogs and the useful information which I will be sharing on this blog.
Please do subscribe and like my page so that you can get the updates whenever I will be posting my new personal blog on this site. If you have any query related to any topic please provide your comments.

Hope you will enjoy this learning blog.

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 ...