Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions cloud4rpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down