In this guide you set up an end-to-end IoT ingestion pipeline, from a simulated client device to Cloud Functions via Cloud IoT Core. The main purpose is to show-case usage to Protocol Buffers to serialize and deserialize the messages send over MQTT connection.
The Python protocol buffer class is already generated for this tutorial. The class is generated based on the proto definition file. Follow the Python Generated Code to understand the process of Python protocol buffer class.
- Select or create a GCP project.
- Make sure that billing is enabled for your GCP project.
- Enable the Cloud IoT, Cloud Pub/Sub and Cloud Functions.
-
In the GCP Console, open Cloud Shell.
-
Clone the source code repository:
cd $HOME git clone https://github.com/kingman/cloud-iot-core-protobuf-sample.git
To make it easier to run commands when you create cloud resources, set environment variables in Cloud Shell to hold the names and properties of the resources:
export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
export EVENT_TOPIC=proto-event
export REGISTRY_ID=proto-reg
export REGION=europe-west1
export DEVICE_ID=proto-device
export PRIVATE_KEY_FILE='rsa_private.pem'
export ALGORITHM='RS256'
export CA_CERTS='roots.pem'
In cloud shell run:
cd $HOME/cloud-iot-core-protobuf-sample/client
openssl req -x509 -nodes -newkey rsa:2048 -keyout rsa_private.pem -out rsa_cert.pem -subj "/CN=unused"
-
Create the Cloud Pub/Sub topic:
gcloud pubsub topics create $EVENT_TOPIC
-
Create the Cloud IoT Core registry:
gcloud iot registries create $REGISTRY_ID \ --region $REGION \ --event-notification-config=topic=$EVENT_TOPIC
-
Create the the sensor board identity in the newly created Cloud IoT Core registry with the public key:
cd $HOME/cloud-iot-core-protobuf-sample/client gcloud iot devices create $DEVICE_ID \ --region=$REGION \ --registry=$REGISTRY_ID \ --public-key=path=rsa_cert.pem,type=rsa-x509-pem
-
Deploy Cloud Functions
cd $HOME/cloud-iot-core-protobuf-sample/cf gcloud functions deploy process_proto \ --trigger-topic $EVENT_TOPIC \ --runtime python37 \ --region $REGION
In cloud shell run:
cd $HOME/cloud-iot-core-protobuf-sample/client
virtualenv env && source env/bin/activate
pip install -r requirements.txt
wget https://pki.google.com/roots.pem
In cloud shell run:
python run_client.py
In Cloud Functions log verify the messages are being logged as JSON