2

I am using Google IOT core with mongoose os. I wanted to update device connection status to firestore. But i am unable to find event which reports mqtt connection status to pub/sub like when device disconnects or reconnect i.e if device is offline or not.

I am stuck on this problem for days.Any help will be appreciated

Honney Goyal
  • 247
  • 1
  • 5
  • 13

4 Answers4

1

Unfortunately, there's no built in way to do this right now as there aren't events on this state.

However, you could implement a hack by sending a message on connect/disconnect from the device that you have a Cloud Function subscribed to the Pub/Sub topic listening for. It's not perfect as it would fail in the case where the device disconnected unexpectedly.

Gabe Weiss
  • 3,134
  • 1
  • 12
  • 15
  • Does any other cloud iot service has this option like aws or azure. – Honney Goyal Feb 05 '18 at 16:09
  • I don't know offhand. I work at Google, so I'm not as familiar with their systems. I did a quick look at AWS' events, and I don't see one for connect/disconnect, but I definitely could just be missing it in their docs. – Gabe Weiss Feb 05 '18 at 17:19
1

Update

As @devunwired mentioned in this response it is now possible to monitor Stackdriver logs for disconnect events. You must have at a minimum enabled INFO level logging on your project in IoT Core > Registries > [your registry] > Edit Registry > Select "Info" log level > Click save.

Original Response

There are a few values you can look at that are tracked in device configuration metadata that you could use to know when a device last was online:

  1. Last Configuration Send time - sent anytime your device connects / configuration is posted
  2. Last Event Time - Last time an event was sent from the device
  3. Last State Time - Last time state was sent from the device
  4. Last Heartbeat time - Last time MQTT heartbeat was sent

To get you started, here is an example using API explorer that you can fill-in with your project ID, region, registry, and device to query for a specific device's metadata.

For 1...3 you have control over these through device manager and by publishing data. MQTT heartbeat is updated if your device sends an MQTT_PINGREQ message during the "ping period" without other messages getting sent.

At any rate, you could use any of these update time values to see the last time a device was online / functioning. You could query the states of your devices after listing the devices in a registry and could update a Firebase RTDB periodically if that's how you want to report (e.g. using AppEngine TaskQueue). Note that you also just can get these "last connected" values from the Google Cloud Console.

It was said before but we don't have an event for disconnect, just configuration ack, which generally is the connection event. If you want to share state between a device and the device manager, use state messages.

class
  • 8,621
  • 29
  • 30
  • Thanks i think this may work. I am unable to access google iot v1beta1 api. https://cloudiot.googleapis.com/$discovery/rest?version=v1beta1 whenever i try this discovery api this shows Discovery document not found for API service. Can you please help – Honney Goyal Feb 07 '18 at 04:36
  • My bad, I need to update those samples to instead use v1 (we're moving on from v1beta1). – class Feb 07 '18 at 19:10
  • Sorry to ask a another question in comment, I just wanted to know how to send iot config messages from firebase function with qos 0. I could not find anything in docs. – Honney Goyal Feb 08 '18 at 04:25
  • @HonneyGoyal might be worth asking another question but you could use the NodeJS manager example ensuring you add the dependencies in package.json for your cloud function. – class Feb 18 '18 at 10:00
  • We shouldn't have to do something like this just check if a device is online .. it's as simple as an MQTT connect/disconnect but i'm finding more and more how incomplete Google's IoT functionality is .. which makes it difficult to just use core iot and firebase without having to write a bunch of helper functions for basic iot stuff – sMyles Dec 28 '18 at 17:10
  • @class How is this still not something available? All other IoT providers have some way of doing this? So we literally have to constantly run a fn to check based on those values, instead of just adding LWT support or some kind of event on MQTT disconnect? I can't believe we have to jump through this many hoops just for device offline state! – sMyles Feb 19 '19 at 00:11
0

There currently is no way to do this, that i've been able to find (a year later after this original post). I posted a question here on SO regarding this as well, with more details and link to example code I had to use for handling this: Google Core IoT Device Offline Event or Connection Status

sMyles
  • 2,418
  • 1
  • 30
  • 44
-1

The AWS IoT platform publishes messages on a special MQTT topic (prefixed with $aws) when your device connects/disconnects. You can easily use these to monitor these events - however, you should be aware that the MQTT protocol is designed to be robust to a poor networking conditions and the broker on the AWS side probably doesn't think it's a bit deal to disconnect a client. The broker expects that the client will just reconnect and queue messages for a moment during that process (which can be a big deal on a microcontroller).

All that being said, the AWS topics you would watch are:

$aws/events/presence/connected/{clientId}

and

$aws/events/presence/disconnected/{clientId}

and the documentation for these (and other) lifecycle events are located: https://docs.aws.amazon.com/iot/latest/developerguide/life-cycle-events.html

pherris
  • 17,195
  • 8
  • 42
  • 58