# main.py -- put your code here! from umqttsimple import MQTTClient, MQTTException import time import ubinascii from machine import WDT import network import os import config last_message = 0 message_interval = 5 counter = 0 ringFlag = False wdt = None wdtCnt = 0 client = None def init(): wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(config.SSID, config.WPA_KEY) connectCnt = 0 while (not wlan.isconnected() or connectCnt > config.MAX_CONNECT_RETRY_CNT): connectCnt += 1 if (not wlan.isconnected()): print("Failed to connect to wlan, restarting") machine.reset() print('Connected to WLAN') global client clientId = ubinascii.hexlify(bytearray(os.urandom(16))).decode() client = MQTTClient(clientId, config.MQTT_SERVER, keepalive=60, ssl=True, user=config.MQTT_LOGIN, password=config.MQTT_PASSWORD) client.set_callback(mqtt_callback) client.connect() print(f"Connected to MQTT broker {config.MQTT_SERVER} ") client.subscribe(config.WATCHDOG_TOPIC) print(f"Subscribed to {config.WATCHDOG_TOPIC}") global wdt wdt = WDT(timeout=5000) wdt.feed() print("Watchdog enabled") def mqtt_callback(topic, msg): global wdt, wdtCnt print(f"Received: {topic}: {msg}") if topic == config.WATCHDOG_TOPIC: wdtCnt += 1 print(f"feed watchdog {wdtCnt}") wdt.feed() def main(): global client print("Hello") time.sleep(1) try: init() except Exception as e: print(f"Some error when starting: {e.__class__.__name__}, {e}") time.sleep(15) machine.reset() last_time = time.ticks_ms() while True: current_time = time.ticks_ms() if (time.ticks_diff(current_time, last_time) > 250): last_time = current_time client.ping() client.wait_msg() client.disconnect() if __name__ == "__main__": main()