I'm working on a small router that connects via LTE/3G to an AWS IoT MQTT Broker to publish messages on a regular basis. When internet is working and the router's AWSIoTMQTTClient could connect to the broker, messages are sent and everything works fine.
But when the device lose internet connection, I want to store the messages that should have been sent while the device was offline on local disk. As soon as connection to AWS is possible, then continue sending to the broker and do not save on disk.
On my test so far, the onOnline and onOffline functions have not worked correctly when I disconnect the internet.
When I start my test with internet connection, the "on_online" function gets called and messages are being sent. While the device is working and sending messages, I turn off the internet but the "on_offline" functions never gets called. So all messages will be transferred to the broker as soon as internet/connection to AWS works again. (Because I set my client to "Infinite offline Publish queueing").
When I start my test without internet connection, the "on_offline" function
never gets called.
from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
from time import time
def on_online():
global awsClient_Online
awsClient_Online = True
def on_offline():
global awsClient_Online
global awsClient_OfflineSince
awsClient_Online = False
awsClient_OfflineSince = time()
def main():
# Init AWSIoTMQTTClient
awsClient = None
awsClient = AWSIoTMQTTClient(Aws_ClientId)
awsClient.configureEndpoint(Aws_ApiEndpoint, Aws_Port)
awsClient.configureCredentials(Aws_RootCa, Aws_PrivateKey, Aws_CertFile)
# AWSIoTMQTTClient connection configuration
awsClient.configureAutoReconnectBackoffTime(1, 32, 20)
awsClient.configureOfflinePublishQueueing(-1) # Infinite offline Publish queueing
awsClient.configureDrainingFrequency(2) # Draining: 2 Hz
awsClient.configureConnectDisconnectTimeout(10) # 10 sec
awsClient.configureMQTTOperationTimeout(5) # 5 sec
# Register a onOnline/onOffline callback
awsClient.onOnline = on_online
awsClient.onOffline = on_offline
global awsClient_Online
global awsClient_OfflineSince
# Connect to AWS IoT
try:
awsClient.connect()
'''
According to documentation should return True when connection OK,
False when not OK.. but I'm getting error "socket.gaierror: [Errno -3]
Temporary failure in name resolution" when no internet
https://s3.amazonaws.com/aws-iot-device-sdk-python-docs/html/index.html#AWSIoTPythonSDK.MQTTLib.AWSIoTMQTTClient.connect
'''
except:
awsClient_Online = False
awsClient_OfflineSince = time()
# Cyclic program
while True:
if not awsClient_Online:
try:
awsClient.connect()
except:
awsClient_Online = False
awsClient_OfflineSince = time()
if awsClient_Online:
# Send messages
else:
# Save on disk
I thought the variable "awsClient._mqtt_core._client_status._status" would update whenever the connection was lost/back again, but it does not. For instance when it is "4" (connected, as long I've seen) and then I disconnect the internet, it remains on "4".
How can I effectively know when the connection to AWS failed?