From aaa3923caba831ab369b0f3a715cdc43786f0b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Kov=C3=A1cs?= Date: Tue, 23 Jan 2024 19:22:58 +0100 Subject: [PATCH] Healthr --- main.py | 112 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 79 insertions(+), 33 deletions(-) diff --git a/main.py b/main.py index aa83afa..280b45a 100644 --- a/main.py +++ b/main.py @@ -24,6 +24,7 @@ def blink_onboard_led(num_blinks): time.sleep_ms(200) def connect_wifi(): + print('Connecting to WiFi ' + ssid + '...') wlan.active(True) # If you need to disable powersaving mode # wlan.config(pm = 0xa11140) @@ -43,12 +44,12 @@ def connect_wifi(): wlan.connect(ssid, pw) # Wait for connection with 10 second timeout - timeout = 20 + timeout = 10 while timeout > 0: if wlan.status() < 0 or wlan.status() >= 3: break timeout -= 1 - print('Waiting for connection...') + print('Waiting for connection... Status is ' + str(wlan.status())) blink_onboard_led(2) time.sleep(1) # Handle connection error @@ -66,12 +67,14 @@ def connect_wifi(): if wlan_status != 3: blink_onboard_led(5) - raise RuntimeError('Wi-Fi connection failed') + print('Wi-Fi connection failed') + return False else: blink_onboard_led(1) print('Connected') status = wlan.ifconfig() print('ip = ' + status[0]) + return True def disconnect_wifi(): wlan.disconnect() @@ -98,10 +101,35 @@ def send_notification_to_server(notify_url, title, tags): print(request.content) request.close() -def set_cpu_to_high_freq(): +def send_health_update(): + try: + health_url = healthr_url + '/health/' + healthr_service_name + print("Sending health update to '" + health_url + "...") + request = requests.post(health_url) + request.close() + except: + print('Error sending health update') + blink_onboard_led(3) + +def check_version_update(): + version_changed, remote_version = micropython_ota.check_version('https://iot-sw.adix.link', 'doorbell', ota_branch) + if version_changed: + send_notification(title = 'Updating to ' + remote_version, tags = 'new') + if ota_soft_reset_device: + print(f'Found new version {remote_version}, soft-resetting device...') + machine.soft_reset() + else: + print(f'Found new version {remote_version}, hard-resetting device...') + machine.reset() + else: + print('No new version available') + +def set_cpu_to_high_freq(): machine.freq(125000000) + print('Frequency is set to 125 MHz') def set_cpu_to_low_freq(): + print('Frequency is set to 20 MHz') machine.freq(20000000) pass @@ -113,6 +141,9 @@ ota_soft_reset_device=False ntfy_topic = settings['ntfy_topic'] +healthr_url = settings['healthr_url'] +healthr_service_name = settings['healthr_service_name'] + print('Starting up...') wlan = network.WLAN(network.STA_IF) @@ -138,14 +169,17 @@ led = Pin('LED', Pin.OUT) blink_onboard_led(2) sleep(1) try: - connect_wifi() - print("Checking version updates...") - #micropython_ota.reset_version() - micropython_ota.ota_update('https://iot-sw.adix.link', 'doorbell', ota_branch) - send_notification(title = 'Started (sw: ' + current_version + ')', tags = 'signal_strength') - sleep(2) - disconnect_wifi() - blink_onboard_led(1) + if connect_wifi(): + print("Checking version updates...") + #micropython_ota.reset_version() + micropython_ota.ota_update('https://iot-sw.adix.link', 'doorbell', ota_branch) + send_notification(title = 'Started (sw: ' + current_version + ')', tags = 'signal_strength') + sleep(2) + disconnect_wifi() + blink_onboard_led(1) + else: + print('Error: Could not connect to WiFi :(') + blink_onboard_led(4) except Exception as e: print('Error: Could not send became-online message :(') print(e) @@ -153,6 +187,7 @@ except Exception as e: print('Changing frequency and beginning loop...') set_cpu_to_low_freq() +last_update_time = time.time() - 3600; while True: doorbell_current_state = door_bell.value() debug_current_state = debug_mode.value() @@ -163,31 +198,42 @@ while True: blink_onboard_led(5) debug_last_state = debug_current_state + + if last_update_time + 3000 < time.time(): + try: + set_cpu_to_high_freq() + if connect_wifi(): + send_health_update() + sleep(1) + check_version_update() + sleep(1) + disconnect_wifi() + blink_onboard_led(1) + last_update_time = time.time() + else: + blink_onboard_led(3) + # If we can't connect to WiFi, try again in 5 minutes (we lie that last update was 55 minutes ago) + last_update_time = time.time() - 3300 + + except Exception as e: + print('Error: Could not send health update') + print(e) + blink_onboard_led(4) + set_cpu_to_low_freq() if doorbell_last_state == False and doorbell_current_state == True: set_cpu_to_high_freq() led.value(1) - print('Connecting to WiFi ' + ssid + '...') - connect_wifi() - print('Sending notification...') - - send_notification(title = 'Dany Csengo', tags = 'bell') - - time.sleep(1) - version_changed, remote_version = micropython_ota.check_version('https://iot-sw.adix.link', 'doorbell', ota_branch) - if version_changed: - send_notification(title = 'Updating to ' + remote_version, tags = 'new') - if ota_soft_reset_device: - print(f'Found new version {remote_version}, soft-resetting device...') - machine.soft_reset() - else: - print(f'Found new version {remote_version}, hard-resetting device...') - machine.reset() - else: - print('No new version available') - - disconnect_wifi() - + if connect_wifi(): + print('Sending notification...') + + send_notification(title = 'Dany Csengo', tags = 'bell') + + time.sleep(1) + check_version_update() + + disconnect_wifi() + led.value(0) time.sleep_ms(200) blink_onboard_led(1)