Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

elaboarate on Google Cloud Platform MQTT sample code #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
*.1
*.conf
.DS_Store
*.backup
*.key


70 changes: 63 additions & 7 deletions GoogleIOT/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<p align="center"><img src ="https://github.com/pycom/pycom-libraries/blob/master/img/logo.png" width="300"></p>


# Google Cloud Iot Core MQTT connection library

### requirement
Expand All @@ -11,12 +12,67 @@ You will need to setup a Google IoT core registry as described here: https://clo
During the activation please collect the following informations: 'project_id',
'cloud_region' and 'registry_id'.

### usage

- create a device registry:
https://cloud.google.com/iot/docs/quickstart#create_a_device_registry
- generate a key using the provided tool genkey.sh and add it to the platform
- add the public key to Google IoT Core :
https://cloud.google.com/iot/docs/quickstart#add_a_device_to_the_registry
- copy config.example.py to config.py and edit the variable
### Installation Google Cloud Platform

These are the steps to create the IoT core Registry, create a keypair, create a device, associate the keypair with the device and create the pub/sub topic in Google Cloud Platform.

#### Create a Google Cloud Platform Registry

Create a registry in the IoT core using the following properties

- Registry ID = `goinvent`
- Region = `us-central1`
- Cloud Pub/Sub topics = Create a new Cloud Pub/Sub topic = `events`
- Other settings are default

See figure for details. For the example to work, make sure the topic is specified as ``events`.

![pic of gcp create a registry](../img/create-registry.png)

#### Create Public/Private Keypair for Device and Associate with Registry

Using the genkey.sh script create a public/private Keypair for Device and associate the key with the device for the registry.

1. From the IoT Core section of Google Cloud Platform, click 'Devices' in the left hand sidebar.
2. Click `CREATE A DEVICE`
3. Specify Device ID = `pysense2`

![pic of create a device](../img/create-a-device.png)

4. In terminal create the public/private key. The device name given as a parameter to the script should match the name of the Device ID in step 3.
`GoogleIOT$ ./genkey.sh pysense2`
4. Click `COMMUNICATION, STACKDRIVER LOGGING, AUTHENTICATION`
5. In the Authentication (optional) Input method select `Enter manually`
6. In the `Public key format` ensure the setting is `RSA256`
7. In the terminal, select the genkey.sh output for the publick key. Make sure to copy the text lines with `BEGIN` and `END`. Paste this text in the edit box on Google Cloud Platform for the `Public key value`
8. Other settings are default.

![pic of add a public key for the device](../img/add-public-key.png)

#### References for Google Cloud Platform

- Creating a device registry:

https://cloud.google.com/iot/docs/quickstart#create_a_device_registry
- Adding the public key to Google IoT Core:

https://cloud.google.com/iot/docs/quickstart#add_a_device_to_the_registry

### Usage of Pycom Code

Assuming you have setup the Google Cloud Project as above.


- copy config.example.py to config.py and edit the variables:
- `wifi_ssid`
- `wifi_password`
- `project_id`

The steps in the google cloud project assume you are using a device name of `pysense2`. If a different name is provided in the google cloud setup portion then you will need to modify the device name in config.py accordingly. The topic needs to be event so that the sample code will read the device configuration and upload/publish to the events topic in pub/sub. Changing the topic name to something other than `events` breaks receipt of the configuration read message in the python code on the pycom device.

- In atom, open the project at the `flash` folder.

- upload the project using pymakr


Binary file added img/add-public-key.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/create-a-device.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/create-registry.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.