See https://cloud.google.com/solutions/iot/ for more information on Google Cloud IoT
This project shows you how to use Google Cloud IoT with resin.io to automatically create a Cloud IoT device on first boot. It also shows you how to run a sample to connect a device and publish device telemetry events.
-
In the GCP Console, go to the Manage resources page and select or create a new project.
-
Make sure that billing is enabled for your project.
-
Install and initialize the Cloud SDK. Cloud IoT Core requires version 173.0.0 or higher of the SDK.
-
Set up a Node.js development environment.
-
Go to the Google Cloud IoT Core page in GCP Console.
-
Click Create a registry.
-
Enter
my-registry
for the Registry ID. -
Select
us-central1
for the Cloud region. -
Select
MQTT
for the Protocol. -
In the Telemetry topic dropdown list, select Create a topic.
-
In the Create a topic dialog, enter
my-device-events
in the Name field. -
Click Create in the Create a topic dialog.
-
The Device state topic and Certificate value fields are optional, so leave them blank.
-
Click Create on the Cloud IoT Core page.
You've just created a device registry with a Cloud Pub/Sub topic for publishing device telemetry events.
- Go to the GCP Roles page
- Click Create role
- Name it
Create IoT Device
- Click Add permission
- Enter
cloudiot.devices.create
and save - Go to the GCP Credentials page
- Click Create Credentials and select
Service account key
from the drop down - Create a new service account, and assign only the
Create IoT Device
role to it, this limited scope is required as these credentials will be available on the device, and could potentially get exposed if the device is physically compromised - Download the credentials json file
Go to the resin dashboard and create or select your project
Click Environment Variables and create the following keys and matching values:
GOOGLE_IOT_PROJECT
and enter the Project Id for your GCP Project, you can find that on the GCP Home pageGOOGLE_IOT_REGION
and enter the GCP region you selected above (us-central1
)GOOGLE_IOT_REGISTRY
and enter the device registry name you've selected above (my-registry
)GOOGLE_IOT_SERVICE_JSON
and paste the entire content of the credentials json file you've downloaded above as value
You're now ready to provision your resin.io device and push the code to the application. Once it's started up it'll automatically register it's self with Google Cloud IoT as a device, and allow you to push telemetry data to the pubsub channel you've created.
Once the device is online, the sample app will start pushing event messages with the CPU load and memory usage for the device, which will be visible in the logs viewer in the resin dashboard.
You can retrieve and view published messages from Pub/Sub using the gcloud CLI:
- Go to the GCP Pub/Sub page and click on
my-device-events
topic. - Click
Create Subscription
in the top toolbar. - Enter
my-subscription
as the subscription name. - Click Create.
You can then view the messages by running the following command in your terminal, replacing PROJECT_ID with your project ID:
# gcloud pubsub subscriptions pull --limit 100 --auto-ack projects/PROJECT_ID/subscriptions/my-subscription
Build your own application using this sample app, or the Google samples for C, Java, NodeJS and Python available at https://cloud.google.com/iot/docs/samples/mqtt-samples
When building your app, or using one of the samples, use the private key available at /data/rsa-priv.pem
, and GOOGLE_IOT_REGION, GOOGLE_IOT_PROJECT and GOOGLE_IOT_REGISTRY
environment variables to configure your client.
An overview of Google's cloud services that can be used to ingest, transform, and run analytics on the data is available at: https://cloud.google.com/solutions/iot-overview