Makerdemy/Introduction-to-Google-Cloud-IoT-Core

Device ID?

Closed this issue · 1 comments

Using the IOT client libraries how can you specify the device name to match with what is in the registry so you can post events, state and config tele-metrics?

Hi kilabyte,

First and Foremost, we are sorry for the late response on the GitHub Page.In the course, we have used the pubsub_v1 class from google.cloud library and use the PublisherClient() API to publish the payload. For your use case, where you need to use the device name inside the registry and map it to post events, state also do telemetry you can’t use the same library, as it doesn’t support device wise mapping for the library in the Raspberry Pi. Moreover, this library will get deprecated in the near future once Python 2.7 reaches its expiry.

If you skip ahead to Section 3, you can see the exact use case you mentioned being implemented but on the ESP32. Thus, coming back to your query, the best way to implement this from the Raspberry Pi, is to use the Paho MQTT Client. First, install/upgrade all dependencies to work with Paho.

sudo apt-get install build-essential
sudo apt-get install libssl-dev
sudo apt-get install python-dev
sudo apt-get install libffi-dev
sudo pip install pyjwt
sudo pip install cryptography
Now, Install Paho MQTT: sudo pip install paho-mqtt

After setting everything up, please follow this guide. Also, check out this GitHub Link to understand how to work with your use case. In the 01_basics.py code given in the repository; you should give attention to the following lines of code. This is where you need to modify for your use case.

ssl_private_key_filepath = '<ssl-private-key-filepath>'
ssl_algorithm = '<algorithm>' # Either RS256 or ES256
root_cert_filepath = '<root-certificate-filepath>'
project_id = '<GCP project id>'
gcp_location = '<GCP location>'
registry_id = '<IoT Core registry id>'
device_id = '<IoT Core device id>'
_CLIENT_ID = 'projects/{}/locations/{}/registries/{}/devices/{}'.format(project_id, gcp_location, registry_id, device_id)

client = mqtt.Client(client_id=_CLIENT_ID)

We hope this was helpful.

Thank You.