From 9a6bdc57389f1aa084906bfdf225529e8b2593ce Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Mon, 10 Oct 2022 17:25:24 +0200 Subject: [PATCH] not yet working so far --- config.py | 7 ++++++- main.py | 43 ++++++++++++++++++++++++++++++++----------- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/config.py b/config.py index ca564ee..774d099 100644 --- a/config.py +++ b/config.py @@ -2,10 +2,15 @@ import secrets SSID = 'telemetry' WPA_KEY = secrets.WPA_KEY + MQTT_SERVER = '172.16.10.47' MQTT_LOGIN = 'thermo-anna' MQTT_PASSWORD = secrets.MQTT_PASSWORD + HEARTBEAT_TOPIC = b'IoT/rpipwThermometer/0/Heartbeat' WATCHDOG_TOPIC = b'IoT/Watchdog' +PAYLOAD_TOPIC = b'IoT/rpipwThermometer/0/Payload' +STARTUP_TOPIC = b'IoT/rpipwThermometer/0/Startup' -MAX_CONNECT_RETRY_CNT = 1000000 \ No newline at end of file +MAX_CONNECT_RETRY_CNT = 25 +MAIN_PERIOD_MS = 60000 \ No newline at end of file diff --git a/main.py b/main.py index 5c9f04d..27856e0 100644 --- a/main.py +++ b/main.py @@ -15,20 +15,32 @@ ringFlag = False wdt = None wdtCnt = 0 client = None +wlan = None + def init(): + global wdt + wdt = WDT(timeout=8000) + wdt.feed() + print("Watchdog enabled") + + global wlan 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): + print("Not yet connected") + wdt.feed() connectCnt += 1 + time.sleep(1) + if (not wlan.isconnected()): print("Failed to connect to wlan, restarting") machine.reset() - print('Connected to WLAN') + print(f"Connected to WLAN: {machine.reset_cause()} {connectCnt} {wlan.ifconfig()}") global client clientId = ubinascii.hexlify(bytearray(os.urandom(16))).decode() @@ -39,19 +51,15 @@ def init(): 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") + client.publish(config.STARTUP_TOPIC, f"Hello, rpipw thermometer is alive now at {wlan.ifconfig()}") def mqtt_callback(topic, msg): global wdt, wdtCnt - print(f"Received: {topic}: {msg}") + # print(f"Received: {topic}: {msg}") if topic == config.WATCHDOG_TOPIC: wdtCnt += 1 - print(f"feed watchdog {wdtCnt}") + # print(f"feed watchdog {wdtCnt}") wdt.feed() def main(): @@ -66,15 +74,28 @@ def main(): time.sleep(15) machine.reset() - last_time = time.ticks_ms() + last_time_mqtt_loop = time.ticks_ms() + last_time_1_second = time.ticks_ms() + last_time_main_period = time.ticks_ms() + payload_cnt = 0 while True: current_time = time.ticks_ms() - if (time.ticks_diff(current_time, last_time) > 250): - last_time = current_time + if (time.ticks_diff(current_time, last_time_mqtt_loop) > 250): + last_time_mqtt_loop = current_time client.ping() client.wait_msg() + if (time.ticks_diff(current_time, last_time_1_second) > 1000): + last_time_1_second = current_time + client.publish(config.HEARTBEAT_TOPIC, f"still alive {current_time}") + + if (time.ticks_diff(current_time, last_time_main_period) > config.MAIN_PERIOD_MS): + last_time_main_period = current_time + client.publish(config.PAYLOAD_TOPIC, f"measurement {payload_cnt}") + payload_cnt += 1 + print("Payload sent") + client.disconnect()