diff --git a/cloud4rpi.py b/cloud4rpi.py index a3afd76..6e17492 100644 --- a/cloud4rpi.py +++ b/cloud4rpi.py @@ -8,7 +8,7 @@ # https://raw.githubusercontent.com/micropython/micropython-lib/mqtt/umqtt.simple/umqtt/simple.py from mqtt import MQTTClient -PING_INTERVAL = 40 # seconds. Should be less then 55 +PING_INTERVAL = 29 # seconds. Should be less then 55 C4R_BROKER_HOST = 'mq.cloud4rpi.io' C4R_BROKER_PORT = 1883 @@ -43,14 +43,17 @@ def __on_message(self, topic, msg): self.__variables[var_name]['value'] = ret self.__publish('data', {var_name: ret}) - def connect(self): + def mqtt_ping_reset(self): + self.next_ping_time = time() + PING_INTERVAL + print("Next MQTT ping at", self.next_ping_time) + + def connect(self, timeout=5): self.__mqtt = MQTTClient(client_id=self.__device_token, server=C4R_BROKER_HOST, port=C4R_BROKER_PORT) - self.__mqtt.set_callback(self.__on_message) try: - self.__mqtt.connect() + self.__mqtt.connect(timeout=timeout) self.__mqtt.subscribe(C4R_TOPIC_FORMAT % (self.__device_token, 'commands'), qos=1) except Exception as e: @@ -63,10 +66,6 @@ def ping(self): self.__mqtt.ping() self.mqtt_ping_reset() - def mqtt_ping_reset(self): - self.next_ping_time = time() + PING_INTERVAL - print("Next MQTT ping at", self.next_ping_time) - def declare(self, variables): self.__variables = variables