Thank you @scaprile for your suggestion here.
Internet issue is more recurrent if you are using mobile internet from sim card, for instance.
My device is connected through a wifi router (very near and good signal), using optical fiber to the Internet. But the device may not be online for a second and lose some command. My device is commanded to turn a water pump on and off. There was one time when the device did not receive the command (see image) to turn off and my mother-in-law’s kitchen was flooded
THAT IS WHAT I WANT TO AVOID
I am working to capture this error from the user interface. When the command doesn’t achieve the device, the interface is going to raise a friendly user message, but I need somehow to re-send the command asap.
That said, i tried GCP.isConnected()
and MQTT.isConnected()
. You can check by yourself. Turn on your wifi internet router from your cellphone. Connect your device to your cellphone. After some time the GCP.isConnected() and MQTT.isConnected() will return “true”. That is fine.
Then disable your mobile internet (keep the wifi, but without internet access). The GCP.isConnected() MQTT.isConnected() and MQTT.pub() still return “true” . Event.on(Event.CLOUD_DISCONNECTED, function() {})
also doesn’t work here. After some time, you are going to see “MQTT0 queue overflow”.
Here is what i discovered:
When the device sends data using MQTT.pub() with QoS > 0, after some milliseconds is possible to capture the “ack event” for that published message like this
MQTT.setEventHandler(function(conn, ev, edata) {
if (ev !== 0) {
if (ev === 204) {
print('MQTT event handler: got', ev, '. Ack for publishing of a message with QoS > 0.');
lastEventHandler.puback = upTime;
GPIO.write(led, 0);
}
}
}, null);
I can consider the internet issue when the ACK event does not return before 5 seconds. In this case, the device is going to stop sending data until it receives the ACK event again.
I really would like a better solution here. I can also consider a workaround from the GCP side.