Powered By GitBook
Device to Device demo
Demonstrates how to relay messages from one device to another device using Google Cloud functions.

Setup

Make sure that you have enabled the Cloud IoT Core API from the Google Cloud Console.

Running

Create a Google Cloud IoT Core topic

From the Google Cloud Console create a Google Cloud IoT Core Device Registry. When you create it, either set the event notification topic to an existing PubSub topic or create a new topic to be used for the demo.

Upload the Google Cloud Function

​The dev2dev instructions explain how to upload the Google Cloud function to relay messages from one device to another.
Set it to trigger on the topic configured with your device registry.
1
gcloud beta functions deploy relayCloudIot \
2
--stage-bucket=gs://your-gcs-bucket \
3
--trigger-topic=your-topic-id
Copied!

Register a Device

    1.
    Generate an RSA256 keypair:
1
openssl req -x509 -newkey rsa:2048 -days 3650 -keyout rsa_private.pem -nodes -out \
2
rsa_cert.pem -subj "/CN=unused"
Copied!
    1.
    Use the keypair to register your device:
1
gcloud beta iot devices create \
2
--registry=<your-registry-id> \
3
--region "europe-west1" \
4
--public-key path=rsa_cert.pem,type=rs256 <deviceId>
Copied!

Connect your virtual device

1
cd virtualdevice
2
npm install
3
node virtualdev.js --cloudRegion=<region-from-console> \
4
--projectId<your-project-id> \
5
--deviceId=<your-device-id>\
6
--privateKeyFile=../rsa_private.pem \
7
--algorithm=RS256 \
8
--numMessages=1 \
9
--registryId=<your-registry-id>
Copied!
When the sample runs, the virtual device will trigger configuration updates from the Google Cloud Functions.

Troubleshooting

When you run the sample code, run the following command to print the output from the Google Cloud Function:
1
gcloud beta functions logs read
Copied!
This is useful for debugging aspects of the demo that run on the Google Cloud Functions callback.
Last modified 7mo ago