AWSIoTMQTTClient - how can I tell if the client is offline?


Hello all,
I am new to aws IoT and AWSIoTPythonSDK :)

I have a python script (which is based on that reads small files from directory and send their content to topic using :
self.myAWSIoTMQTTClient.publish(topic, message_to_send, 1)

I have long disconnection periodד, and i want to stop sending message to the topic while the device is offline (I dont want the messages to be send to the offline queue).
Can I tell the client status using the SDK?

I thought about copy the logic from MqttCore.publish() method, but it based on:
and I dont think I can access this data...

I can NOT use greengrass


asked 4 years ago35 views
1 Answer

I found the solution:

    self.myAWSIoTMQTTClient.onOffline = self.myOnOfflineCallback  
    self.myAWSIoTMQTTClient.onOnline = self.myOnOnlineCallback  

def myOnOfflineCallback(self):  
    self.sent_to_topic = False  

def myOnOnlineCallback(self):  
    self.sent_to_topic = True  

def publish(self, topic, message_to_send):  
    if self.sent_to_topic:  
        self.myAWSIoTMQTTClient.publish(topic, message_to_send, 1)  
        print(f"Published topic {topic}")
answered 4 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions