SlideShare a Scribd company logo
Google Cloud & Raspberry Pi
Please Follow my Youtube Account for video tutorials.
http://www.youtube.com/c/FaisalMehmoodAvan
Also follow me on my LinkedIn Account
https://kr.linkedin.com/in/faisalavan
Contact Information:
Phone: +82-10-6876-6805
Email: faisal.avan@Hotmail.com
Table of Content
S.No Topic
1. Google Cloud Messaging (GCM) Architecture
2. Google Cloud IoT Core
3. Service Components and the flow of data
4. Configure Google Cloud IoT Platform
5. Install Google Cloud SDK in RPI
6. Registering a sample device
7. MQTT Client in RPI
Google Cloud Messaging (GCM) Architecture
A GCM implementation includes a Google connection server, an app server in your
environment that interacts with the connection server via HTTP or XMPP protocol, and a
client app.
• Google GCM Connection Servers accept downstream messages from your app server and
send them to a client app. The XMPP connection server can also accept messages sent
upstream from the client app and forward them to your app server.
• On your App Server, you implement the HTTP and/or XMPP protocol to communicate with
the GCM connection server(s). App servers send downstream messages to a GCM
connection server; the connection server enqueues and stores the message, and then
sends it to the client app. If you implement XMPP, your app server can receive messages
sent from the client app.
• The Client App is a GCM-enabled client app. To receive and send GCM messages, this app
must register with GCM and get a unique identifier called a registration token. For more
information on how to implement the client app, see the documentation for your
platform.
Google Cloud IoT Core
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. Cloud IoT Core, in
combination with other services on Google Cloud IoT
platform, provides a complete solution for collecting,
processing, analyzing, and visualizing IoT data in real time to
support improved operational efficiency.
Service Components and the flow of data
Device telemetry data is forwarded to a Cloud Pub/Sub topic,
which can then be used to trigger Cloud Functions. You can
also perform streaming analysis with Cloud Dataflow or
custom analysis with your own subscribers.
Configure Google Cloud IoT Platform (1/5)
1. Select or create a Cloud Platform project
Step1: In the Cloud Platform Console, click the project
Step2: At the project list page, select a project or click the Create project
Step3: Input a Project name, Project name will be used in glcoud initialization in Raspberry
pi.
Step4: Click Create
Note:
Remember the project_id which will be used in both Device Manager and Device.
project_id
Step 1
Step 2 Step 3
Step 4
Click to Add new Project
List of Projects
Meaningufl Name
Configure Google Cloud IoT Platform (2/5)
2. Enable the billing for your project to use Google Cloud Platform
features.
Note: Google may charge you to enable this service. Google
cloud gives $300 credit for 12 month
Step 1: Go To Big Data and Select IoT Core
Step 2: Enable the Cloud IoT Core API, Register your application
for Google Cloud IoT API in Google Cloud Platform.
Enable IoT API
Step 1 Step 2
Select IoT core
from Big Data
Google gives
free $300 credit
to use within 12
months
Configure Google Cloud IoT Platform (3/5)
4. Create a Cloud Pub/Sub topic in the Cloud Platform Console for the Pub/Sub service.
Step 1: Go to the Cloud PUB/SUB topics page
Step 2: Enable API
Step 3: Create a topic
Step 2Step 1 Step 3
Enable Pub/Sub API
Create a
Publish/Subscribe
Topic
Configure Google Cloud IoT Platform (4/5)
4. Step 4: Input a topic name and click CREATE, remember the
topic name for later usage.
Step 5: In the Cloud Platform Console, select the topic and
click the PERMISSIONS button. This will open the IAM
permissions editor in the right side panel.
Step 4 Step 5
Name of the topic
Configure Google Cloud IoT Platform (5/5)
4. Step 5: Add the member cloud-
iot@system.gserviceaccount.com with the role Pub/Sub
Publisher. Then click Add.
Member allowed to Publish/Subscribe
List of Members Permission
Install Google Cloud SDK in RPI (1/2)
1. Make sure that your system has Python 2 with a release
number of Python 2.7.9 or later installed on your system, as
the Google Cloud SDK doesn't currently support Python 3. You
may still have other versions of Python installed.
python2 –version
2. There are two optionsn to install Google Cloud SDK
Option 1:
Download package for Linux system from
https://cloud.google.com/sdk/docs/#linux
3. Extract the file to any location on your file system.
4. Optional. Run the install script to add SDK tools to your
path, enable command-completion in your bash shell, and/or
and enable usage reporting.
./google-gloud-sdk/install.sh
5. Run gcloud init to initialize the SDK:
./google-cloud-sdk/bin/gcloud init
Install Google Cloud SDK in RPI (2/2)
6. Update the gcloud components, and install the beta
component.
$ gcloud components update
$ gcloud components install beta
Option 2:
If you're using a Compute Engine VM, the default installation
of gcloud will not allow you to update the components. To
enable Cloud IoT Core on a Compute Engine VM, reinstall
gcloud by running the following commands:
$ sudo apt-get remove google-cloud-sdk
$ curl https://sdk.cloud.google.com | bash
$ exec -l $SHELL
$ gcloud init
Registering a sample device (1/2)
1. Create a device registry, a registry is a collection of devices. All device
telemetry is published to the registry's Cloud Pub/Sub topic, using Cloud
Pub/Sub attributes to distinguish between different devices. Remember
the registry_id which value is ‘my_registry’ in the above example
command.
$ gcloud beta iot registries create my-registry 
--project=raspberrypi-187702 
--region=asia-east1 
--pubsub-topic=projects/raspberrypi-187702/topics/raspberry
2. Create a public/private RSA key pair. Each device should be created with
its own unique key. When connecting to Cloud IoT Core, each device
creates a JWT signed with its private key, which Cloud IoT Core
authenticates using the devices public key. Using OpenSSL to make an RSA
key pair:
$ openssl req -x509 -newkey rsa:2048 -keyout rsa_private.pem -nodes -out 
rsa_cert.pem -subj "/CN=unused“
You should now have two files: rsa_private.pem and rsa_cert.pem.
Registering a sample device (2/2)
3. Create an RS256 authenticated device using the RSA certificate key,
remember the device_id which value is ‘my-raspberry01’ in the above
example command.
$ gcloud beta iot devices create my-raspberry01 
--project=raspberrypi-187702 
--region=asia-east1 
--registry=my-registry 
--public-key path=rsa_cert.pem,type=rs256
4. Create a Cloud Pub/Sub subscription to receive telemetry events,
remember the subscription_id which value is ‘my-subscription-rpi’ in the
above example command.
$ gcloud beta pubsub subscriptions create 
projects/raspberrypi-187702/subscriptions/my-subscription-rpi 
--topic raspberry
MQTT Client in RPI (1/6)
1. Get code from https://github.com/GoogleCloudPlatform/python-docs-
samples/tree/master/iot/api-client/mqtt_example
2. Install pip and virtualenv if you do not already have them. You may want
to refer to the Python Development Environment Setup Guide for Google
Cloud Platform for instructions.
3. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
$ virtualenv env
$ source env/bin/activate
4. Install the dependencies needed to run the samples.
$ pip install -r requirements.txt
Error: Invalid environment marker: python_version < ‘3’
Solution
MQTT Client in RPI (2/6)
5. Install Adafruit_Python_DHT library
sudo apt-get install build-essential python-dev git
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT/ ; sudo python setup.py install )
6. Connect DHT11 sensor to Raspberry Pi.
MQTT Client in RPI (3/6)
7. Change the personal project information in code.
8. There are some errors with original code, the way of
importing packages also need to be change.
The way of import jwt
Project information
MQTT Client in RPI (4/6)
9. Add DHT11 sensor related code.
MQTT Client in RPI (5/6)
10. Python script result:
MQTT Client in RPI (6/6)
11. Result in Google Cloud Shell of Google Cloud Platform
gcloud beta pubsub subscriptions pull my-subscription-rpi
gcloud beta pubsub topics publish raspberry "It's a TEST!" --
attribute KEY1=VAL1,KEY2=VAL2
Contact Information
Please Follow my Youtube Account for video tutorials.
http://www.youtube.com/c/FaisalMehmoodAvan
Also follow me on my LinkedIn Account
https://kr.linkedin.com/in/faisalavan
Contact Information:
Phone: +82-10-6876-6805
Email: faisal.avan@Hotmail.com

More Related Content

Raspberry pi and Google Cloud

  • 1. Google Cloud & Raspberry Pi Please Follow my Youtube Account for video tutorials. http://www.youtube.com/c/FaisalMehmoodAvan Also follow me on my LinkedIn Account https://kr.linkedin.com/in/faisalavan Contact Information: Phone: +82-10-6876-6805 Email: faisal.avan@Hotmail.com
  • 2. Table of Content S.No Topic 1. Google Cloud Messaging (GCM) Architecture 2. Google Cloud IoT Core 3. Service Components and the flow of data 4. Configure Google Cloud IoT Platform 5. Install Google Cloud SDK in RPI 6. Registering a sample device 7. MQTT Client in RPI
  • 3. Google Cloud Messaging (GCM) Architecture A GCM implementation includes a Google connection server, an app server in your environment that interacts with the connection server via HTTP or XMPP protocol, and a client app. • Google GCM Connection Servers accept downstream messages from your app server and send them to a client app. The XMPP connection server can also accept messages sent upstream from the client app and forward them to your app server. • On your App Server, you implement the HTTP and/or XMPP protocol to communicate with the GCM connection server(s). App servers send downstream messages to a GCM connection server; the connection server enqueues and stores the message, and then sends it to the client app. If you implement XMPP, your app server can receive messages sent from the client app. • The Client App is a GCM-enabled client app. To receive and send GCM messages, this app must register with GCM and get a unique identifier called a registration token. For more information on how to implement the client app, see the documentation for your platform.
  • 4. Google Cloud IoT Core 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. Cloud IoT Core, in combination with other services on Google Cloud IoT platform, provides a complete solution for collecting, processing, analyzing, and visualizing IoT data in real time to support improved operational efficiency.
  • 5. Service Components and the flow of data Device telemetry data is forwarded to a Cloud Pub/Sub topic, which can then be used to trigger Cloud Functions. You can also perform streaming analysis with Cloud Dataflow or custom analysis with your own subscribers.
  • 6. Configure Google Cloud IoT Platform (1/5) 1. Select or create a Cloud Platform project Step1: In the Cloud Platform Console, click the project Step2: At the project list page, select a project or click the Create project Step3: Input a Project name, Project name will be used in glcoud initialization in Raspberry pi. Step4: Click Create Note: Remember the project_id which will be used in both Device Manager and Device. project_id Step 1 Step 2 Step 3 Step 4 Click to Add new Project List of Projects Meaningufl Name
  • 7. Configure Google Cloud IoT Platform (2/5) 2. Enable the billing for your project to use Google Cloud Platform features. Note: Google may charge you to enable this service. Google cloud gives $300 credit for 12 month Step 1: Go To Big Data and Select IoT Core Step 2: Enable the Cloud IoT Core API, Register your application for Google Cloud IoT API in Google Cloud Platform. Enable IoT API Step 1 Step 2 Select IoT core from Big Data Google gives free $300 credit to use within 12 months
  • 8. Configure Google Cloud IoT Platform (3/5) 4. Create a Cloud Pub/Sub topic in the Cloud Platform Console for the Pub/Sub service. Step 1: Go to the Cloud PUB/SUB topics page Step 2: Enable API Step 3: Create a topic Step 2Step 1 Step 3 Enable Pub/Sub API Create a Publish/Subscribe Topic
  • 9. Configure Google Cloud IoT Platform (4/5) 4. Step 4: Input a topic name and click CREATE, remember the topic name for later usage. Step 5: In the Cloud Platform Console, select the topic and click the PERMISSIONS button. This will open the IAM permissions editor in the right side panel. Step 4 Step 5 Name of the topic
  • 10. Configure Google Cloud IoT Platform (5/5) 4. Step 5: Add the member cloud- iot@system.gserviceaccount.com with the role Pub/Sub Publisher. Then click Add. Member allowed to Publish/Subscribe List of Members Permission
  • 11. Install Google Cloud SDK in RPI (1/2) 1. Make sure that your system has Python 2 with a release number of Python 2.7.9 or later installed on your system, as the Google Cloud SDK doesn't currently support Python 3. You may still have other versions of Python installed. python2 –version 2. There are two optionsn to install Google Cloud SDK Option 1: Download package for Linux system from https://cloud.google.com/sdk/docs/#linux 3. Extract the file to any location on your file system. 4. Optional. Run the install script to add SDK tools to your path, enable command-completion in your bash shell, and/or and enable usage reporting. ./google-gloud-sdk/install.sh 5. Run gcloud init to initialize the SDK: ./google-cloud-sdk/bin/gcloud init
  • 12. Install Google Cloud SDK in RPI (2/2) 6. Update the gcloud components, and install the beta component. $ gcloud components update $ gcloud components install beta Option 2: If you're using a Compute Engine VM, the default installation of gcloud will not allow you to update the components. To enable Cloud IoT Core on a Compute Engine VM, reinstall gcloud by running the following commands: $ sudo apt-get remove google-cloud-sdk $ curl https://sdk.cloud.google.com | bash $ exec -l $SHELL $ gcloud init
  • 13. Registering a sample device (1/2) 1. Create a device registry, a registry is a collection of devices. All device telemetry is published to the registry's Cloud Pub/Sub topic, using Cloud Pub/Sub attributes to distinguish between different devices. Remember the registry_id which value is ‘my_registry’ in the above example command. $ gcloud beta iot registries create my-registry --project=raspberrypi-187702 --region=asia-east1 --pubsub-topic=projects/raspberrypi-187702/topics/raspberry 2. Create a public/private RSA key pair. Each device should be created with its own unique key. When connecting to Cloud IoT Core, each device creates a JWT signed with its private key, which Cloud IoT Core authenticates using the devices public key. Using OpenSSL to make an RSA key pair: $ openssl req -x509 -newkey rsa:2048 -keyout rsa_private.pem -nodes -out rsa_cert.pem -subj "/CN=unused“ You should now have two files: rsa_private.pem and rsa_cert.pem.
  • 14. Registering a sample device (2/2) 3. Create an RS256 authenticated device using the RSA certificate key, remember the device_id which value is ‘my-raspberry01’ in the above example command. $ gcloud beta iot devices create my-raspberry01 --project=raspberrypi-187702 --region=asia-east1 --registry=my-registry --public-key path=rsa_cert.pem,type=rs256 4. Create a Cloud Pub/Sub subscription to receive telemetry events, remember the subscription_id which value is ‘my-subscription-rpi’ in the above example command. $ gcloud beta pubsub subscriptions create projects/raspberrypi-187702/subscriptions/my-subscription-rpi --topic raspberry
  • 15. MQTT Client in RPI (1/6) 1. Get code from https://github.com/GoogleCloudPlatform/python-docs- samples/tree/master/iot/api-client/mqtt_example 2. Install pip and virtualenv if you do not already have them. You may want to refer to the Python Development Environment Setup Guide for Google Cloud Platform for instructions. 3. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. $ virtualenv env $ source env/bin/activate 4. Install the dependencies needed to run the samples. $ pip install -r requirements.txt Error: Invalid environment marker: python_version < ‘3’ Solution
  • 16. MQTT Client in RPI (2/6) 5. Install Adafruit_Python_DHT library sudo apt-get install build-essential python-dev git git clone https://github.com/adafruit/Adafruit_Python_DHT.git cd Adafruit_Python_DHT/ ; sudo python setup.py install ) 6. Connect DHT11 sensor to Raspberry Pi.
  • 17. MQTT Client in RPI (3/6) 7. Change the personal project information in code. 8. There are some errors with original code, the way of importing packages also need to be change. The way of import jwt Project information
  • 18. MQTT Client in RPI (4/6) 9. Add DHT11 sensor related code.
  • 19. MQTT Client in RPI (5/6) 10. Python script result:
  • 20. MQTT Client in RPI (6/6) 11. Result in Google Cloud Shell of Google Cloud Platform gcloud beta pubsub subscriptions pull my-subscription-rpi gcloud beta pubsub topics publish raspberry "It's a TEST!" -- attribute KEY1=VAL1,KEY2=VAL2
  • 21. Contact Information Please Follow my Youtube Account for video tutorials. http://www.youtube.com/c/FaisalMehmoodAvan Also follow me on my LinkedIn Account https://kr.linkedin.com/in/faisalavan Contact Information: Phone: +82-10-6876-6805 Email: faisal.avan@Hotmail.com