not yet working so far

This commit is contained in:
2022-10-10 17:25:24 +02:00
parent 84ce8640e9
commit 9a6bdc5738
2 changed files with 38 additions and 12 deletions

View File

@ -2,10 +2,15 @@ import secrets
SSID = 'telemetry' SSID = 'telemetry'
WPA_KEY = secrets.WPA_KEY WPA_KEY = secrets.WPA_KEY
MQTT_SERVER = '172.16.10.47' MQTT_SERVER = '172.16.10.47'
MQTT_LOGIN = 'thermo-anna' MQTT_LOGIN = 'thermo-anna'
MQTT_PASSWORD = secrets.MQTT_PASSWORD MQTT_PASSWORD = secrets.MQTT_PASSWORD
HEARTBEAT_TOPIC = b'IoT/rpipwThermometer/0/Heartbeat' HEARTBEAT_TOPIC = b'IoT/rpipwThermometer/0/Heartbeat'
WATCHDOG_TOPIC = b'IoT/Watchdog' WATCHDOG_TOPIC = b'IoT/Watchdog'
PAYLOAD_TOPIC = b'IoT/rpipwThermometer/0/Payload'
STARTUP_TOPIC = b'IoT/rpipwThermometer/0/Startup'
MAX_CONNECT_RETRY_CNT = 1000000 MAX_CONNECT_RETRY_CNT = 25
MAIN_PERIOD_MS = 60000

43
main.py
View File

@ -15,20 +15,32 @@ ringFlag = False
wdt = None wdt = None
wdtCnt = 0 wdtCnt = 0
client = None client = None
wlan = None
def init(): def init():
global wdt
wdt = WDT(timeout=8000)
wdt.feed()
print("Watchdog enabled")
global wlan
wlan = network.WLAN(network.STA_IF) wlan = network.WLAN(network.STA_IF)
wlan.active(True) wlan.active(True)
wlan.connect(config.SSID, config.WPA_KEY) wlan.connect(config.SSID, config.WPA_KEY)
connectCnt = 0 connectCnt = 0
while (not wlan.isconnected() or connectCnt > config.MAX_CONNECT_RETRY_CNT): while (not wlan.isconnected() or connectCnt > config.MAX_CONNECT_RETRY_CNT):
print("Not yet connected")
wdt.feed()
connectCnt += 1 connectCnt += 1
time.sleep(1)
if (not wlan.isconnected()): if (not wlan.isconnected()):
print("Failed to connect to wlan, restarting") print("Failed to connect to wlan, restarting")
machine.reset() machine.reset()
print('Connected to WLAN') print(f"Connected to WLAN: {machine.reset_cause()} {connectCnt} {wlan.ifconfig()}")
global client global client
clientId = ubinascii.hexlify(bytearray(os.urandom(16))).decode() clientId = ubinascii.hexlify(bytearray(os.urandom(16))).decode()
@ -39,19 +51,15 @@ def init():
print(f"Connected to MQTT broker {config.MQTT_SERVER} ") print(f"Connected to MQTT broker {config.MQTT_SERVER} ")
client.subscribe(config.WATCHDOG_TOPIC) client.subscribe(config.WATCHDOG_TOPIC)
print(f"Subscribed to {config.WATCHDOG_TOPIC}") print(f"Subscribed to {config.WATCHDOG_TOPIC}")
client.publish(config.STARTUP_TOPIC, f"Hello, rpipw thermometer is alive now at {wlan.ifconfig()}")
global wdt
wdt = WDT(timeout=5000)
wdt.feed()
print("Watchdog enabled")
def mqtt_callback(topic, msg): def mqtt_callback(topic, msg):
global wdt, wdtCnt global wdt, wdtCnt
print(f"Received: {topic}: {msg}") # print(f"Received: {topic}: {msg}")
if topic == config.WATCHDOG_TOPIC: if topic == config.WATCHDOG_TOPIC:
wdtCnt += 1 wdtCnt += 1
print(f"feed watchdog {wdtCnt}") # print(f"feed watchdog {wdtCnt}")
wdt.feed() wdt.feed()
def main(): def main():
@ -66,15 +74,28 @@ def main():
time.sleep(15) time.sleep(15)
machine.reset() 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: while True:
current_time = time.ticks_ms() current_time = time.ticks_ms()
if (time.ticks_diff(current_time, last_time) > 250): if (time.ticks_diff(current_time, last_time_mqtt_loop) > 250):
last_time = current_time last_time_mqtt_loop = current_time
client.ping() client.ping()
client.wait_msg() 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() client.disconnect()