1

Upon using the Google Cloud IoT Core platform, it seems to be built around the idea of sending configurations down to the device and receiving states back from it.

Google's own documentation suggests using that approach instead of building around sending commands down (as a config) and getting responses back (as a state).

However in the very end of the documentation they show an example of exactly that.

I am struggling to understand how does one support both approaches? I can see the benefit of how it was designed but I am also struggling to understand how would one be able to talk to the device using such an idiom of values and results as the config.

Has anybody implemented a command/response flow? Is it possible to subscribe to the state topic to retrieve the state of the device in my own application?

Alexandre Thenorio
  • 2,288
  • 3
  • 31
  • 50

2 Answers2

2

Edit based on clarifying comment below:

We've got a beta feature we're calling "Commands" which will do the reboot you're talking about. So the combination of config messages (for persistent configuration that you want to send a device on startup/connect to IoT Core) and Commands for fire and forget like a reboot message can do what your'e talking about. Current state is a bit trickier, in that you could either have a callback mechanism where you send a command to ask, and listen on the events/ channel for a response, or have the device report state (/state/ MQTT topic) and just ask IoT Core's admin SDK rather than the device.

Commands just went open beta, you should have access to it now. If you're using the gcloud SDK from command line, you'll need to do a gcloud components update and then gcloud beta iot devices --help will show the commands group. If you're using the console, when you drill down to a single device, you should now see "Send Command" next to "Update Configuration" on the top bar.

Old Answer: As a stab at answering, it sounds like rather than using the state topic, you could/should just use the standard /events/ topic and subscribe to the Pub/Sub topic the devices go into instead?

It really depends on the volume and number of devices we're talking about in terms of keeping that state machine in sync.

Without knowing what specifically you're implementing, I'd probably do something like send configs down, respond from device on the /events/ topic, and have a Cloud Function that tracks the Pub/Sub topic and updates something like a Firestore instance with the state of the devices, rather than using the /state/ topic. Especially if you're doing something in response directly to the state reporting of the device.

Gabe Weiss
  • 3,134
  • 1
  • 12
  • 15
  • Thank you @GabeWells The use case is a bunch of IoT devices, it could range in the hundreds, potentially thousands where each device does have a configuration but I also would be interested in being to send something like "reboot" commands to a device or simply ask for the current state. The mix between a config, something IoT Core keeps track of and also sends down to the device every the device reconnects (In the case of MQTT) vs being able to send a one off command with a simple "reboot" message which I don't want to have it sent to the device when it reconnects – Alexandre Thenorio Oct 08 '18 at 12:30
  • 1
    *nod* We've got a beta feature we're calling "Commands" which will do the reboot you're talking about. So the combination of config messages (for persistent configuration that you want to send a device on startup/connect to IoT Core) and Commands for fire and forget like a reboot message can do what your'e talking about. Current state is a bit trickier, in that you could either have a callback mechanism where you send a command to ask, and listen on the events/ channel for a response, or have the device report state (/state/ MQTT topic) and just ask IoT Core's admin SDK rather than the device. – Gabe Weiss Oct 09 '18 at 14:42
  • Thank you. Sounds like there is already some thinking behind adding native "Commands" flow. That basically answers my question. Mind putting that in the answer? Is there a way on can access this "Commands" beta function? – Alexandre Thenorio Oct 09 '18 at 15:13
  • Added to the main answer. Also added details about the commands availability! – Gabe Weiss Oct 12 '18 at 13:54
1

Send command to device

To send a command to a device, you will need to use the sendCommandToDevice API call.

Receive command from device

To receive a command from a device, subscribe to the /devices/<your-device-d>/commands/# topic.

Full examples will eventually be published to the Google Cloud IoT Core samples repos:

class
  • 8,621
  • 29
  • 30