103 lines
2.8 KiB
Python
Raw Normal View History

2022-10-09 11:38:51 +02:00
# main.py -- put your code here!
from umqttsimple import MQTTClient, MQTTException
2022-10-09 11:38:51 +02:00
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
2022-10-10 17:25:24 +02:00
wlan = None
2022-10-09 11:38:51 +02:00
def init():
2022-10-10 17:25:24 +02:00
global wdt
wdt = WDT(timeout=8000)
wdt.feed()
print("Watchdog enabled")
global wlan
2022-10-09 11:38:51 +02:00
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(config.SSID, config.WPA_KEY)
2022-10-09 11:38:51 +02:00
connectCnt = 0
while (not wlan.isconnected() or connectCnt > config.MAX_CONNECT_RETRY_CNT):
2022-10-10 17:25:24 +02:00
print("Not yet connected")
wdt.feed()
connectCnt += 1
2022-10-10 17:25:24 +02:00
time.sleep(1)
if (not wlan.isconnected()):
print("Failed to connect to wlan, restarting")
machine.reset()
2022-10-09 11:38:51 +02:00
2022-10-10 17:25:24 +02:00
print(f"Connected to WLAN: {machine.reset_cause()} {connectCnt} {wlan.ifconfig()}")
2022-10-09 11:38:51 +02:00
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)
2022-10-09 11:38:51 +02:00
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}")
2022-10-10 17:25:24 +02:00
client.publish(config.STARTUP_TOPIC, f"Hello, rpipw thermometer is alive now at {wlan.ifconfig()}")
2022-10-09 11:38:51 +02:00
def mqtt_callback(topic, msg):
global wdt, wdtCnt
2022-10-10 17:25:24 +02:00
# print(f"Received: {topic}: {msg}")
if topic == config.WATCHDOG_TOPIC:
2022-10-09 11:38:51 +02:00
wdtCnt += 1
2022-10-10 17:25:24 +02:00
# print(f"feed watchdog {wdtCnt}")
2022-10-09 11:38:51 +02:00
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}")
2022-10-09 11:38:51 +02:00
time.sleep(15)
machine.reset()
2022-10-10 17:25:24 +02:00
last_time_mqtt_loop = time.ticks_ms()
last_time_1_second = time.ticks_ms()
last_time_main_period = time.ticks_ms()
payload_cnt = 0
2022-10-09 11:38:51 +02:00
while True:
current_time = time.ticks_ms()
2022-10-10 17:25:24 +02:00
if (time.ticks_diff(current_time, last_time_mqtt_loop) > 250):
last_time_mqtt_loop = current_time
2022-10-09 11:38:51 +02:00
client.ping()
client.wait_msg()
2022-10-10 17:25:24 +02:00
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")
2022-10-09 11:38:51 +02:00
client.disconnect()
if __name__ == "__main__":
main()