0

I have developed a device that is able to connect to Google IoT Core through the MQTT protocol, I can publish MQTT messages on telemetry topics (/devices/DEVICE_ID/events).

But I can't figure out how to subscribe my device to a generic topic and send messages from cloud to device via this topic. Through the console I created the topic MyTestTopic and a subscription MyTestTopic-sub, I guess that somehow I have to subscribe my device to the subscription MyTestTopic-sub, but I don't know to which topic I have to subscribe my device.

I tried to subscribe my device to the following topics:

  • projects/PROJECT_ID/topics/MyTestTopic
  • projects/PROJECT_ID/topics/MyTestTopic-sub
  • /devices/DEVICE_ID/events/MyTestTopic
  • /devices/DEVICE_ID/events/MyTestTopic-sub

the subscription to these topics seems to be successful, but the device does not receive the messages I send from the console.

Any suggestions please?

I found this thread (Google Cloud IoT - Invalid MQTT publish topic) from 4 years ago, from which I understand that it is possible to subscribe only to some predefined topics, also from the various documents I have read I understand that Google IoT Core allows devices to subscribe to only two topics: /devices/{device-id}/config and /devices/{device-id}/commands/#

Has anything changed since then?

1 Answers1

1

So to be clear, there are two different "topics" in play here. There's the MQTT topics that are described in the docs you found, config and commands. Those are subscribed to on the device using an MQTT client (e.g. Paho MQTT), and sent down to the device from somewhere else via the IoT Core Admin APIs. Then there's Pub/Sub topics. Out of the box, a device using IoT Core does not directly interact with Pub/Sub at all. They publish/receive via the MQTT topics only. So if you register a device with IoT Core, all messages come and go via the MQTT bridge (or HTTPS).

What Gambit support linked to in that github repo is a direct subscription to a Pub/Sub topic (not MQTT). If you look at the Python code, it's establishing proper Google Cloud Credentials via a json token (service account bearer token) that needs to be present on the device or a proxy device. Depending on the device you're using, that may not be possible of course (implementing GCP APIs on a microcontroller is difficult at best).

The easiest way I've seen this done is to implement a Cloud Function that subscribes to a Pub/Sub topic you want to use, and it implements the proper IoT Core Admin calls to push the Pub/Sub messages down to the device as needed. But of course, it all depends on what you're trying to do.

Gabe Weiss
  • 3,134
  • 1
  • 12
  • 15
  • Thanks for the reply. Do you mean that there are two ways to receive messages from the Pub/Sub service? 1. Use the Google API (could be complicated to implement on embedded device) 2. Use an external gateway through which the device connects to the Pub/Sub service – Parminder Singh Jan 05 '22 at 11:25
  • 1
    Basically, yes. To interact with Pub/Sub you need to use something like what Gambit had posted: github.com/gambitcomminc/gcp-iot. That code will allow something that's capable of implementing the GCP SDK to receive messages from a Pub/Sub topic. The thing with IoT Core is it doesn't use the APIs, it uses a simple endpoint to communicate with GCP. So there's no extra service account/authorization requirements. When you talk directly to GCP resources other than IoT Core, you have to go through the GCP auth instead of just the JWT auth that IoT Core requires. – Gabe Weiss Jan 05 '22 at 19:15