By following this guide Sending commands to devices.
// const cloudRegion = 'us-central1';
// const deviceId = 'my-device';
// const commandMessage = 'message for device';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
const iot = require('@google-cloud/iot');
const iotClient = new iot.v1.DeviceManagerClient({
// optional auth parameters.
});
const formattedName = iotClient.devicePath(
projectId,
cloudRegion,
registryId,
deviceId
);
const binaryData = Buffer.from(commandMessage);
const request = {
name: formattedName,
binaryData: binaryData,
};
try {
const responses = await iotClient.sendCommandToDevice(request);
console.log('Sent command: ', responses[0]);
} catch (err) {
console.error('Could not send command:', err);
}
I have this function running on Google Cloud Function (thanks Firebase, I love you), sending the command to Google IoT Devices created by Mongoose OS + Google IoT Core framework.
It works as expected most of the time. Now I am looking to improve the system when the device is not responding.
For many reasons, the device may temporarily be lost connectivity and consequent loss a command (see image below):
In this case, the device was not reachable for a moment (for example, poor mobile internet signal, or internet route issues, anyways).
Is there somehow I can trigger a function when the device is back online and available?
My purpose is, if the user sent a command to a device when the device is not responding, I would like to send the command again automatically. Guess what may happen if the user needs to turn off a motor and the device lose the command?
But keep in mind the device is not "disconnected" from the Google IOT MQTT server. The connections are still alive and it is up and running, connected through good wifi signal to fiber internet. The device cannot realize that something wrong happens from the server-side.
I also "guess" the device needs to send a ping before the server understands it is not alive based on MQTT keep-alive interval, but I don't know how to check that.
If you need the device to always receive the payload from IoT Core, even if it was sent while the device is not connected / not responding, you should use configuration instead of a command.
Configuration is persistent in IoT Core, so it will eventually be delivered to the device even if it was non responsive.
Configuration is always sent to the device when it reconnects to IoT Core, so you'll be sure that the device will receive it, even if it was sent while the device was offline.
There is no difference between configuration and commands about the content.
It is true that you can only send 1 config/device/second, but it most cases this is enough.