Healthr
This commit is contained in:
112
main.py
112
main.py
@@ -24,6 +24,7 @@ def blink_onboard_led(num_blinks):
|
|||||||
time.sleep_ms(200)
|
time.sleep_ms(200)
|
||||||
|
|
||||||
def connect_wifi():
|
def connect_wifi():
|
||||||
|
print('Connecting to WiFi ' + ssid + '...')
|
||||||
wlan.active(True)
|
wlan.active(True)
|
||||||
# If you need to disable powersaving mode
|
# If you need to disable powersaving mode
|
||||||
# wlan.config(pm = 0xa11140)
|
# wlan.config(pm = 0xa11140)
|
||||||
@@ -43,12 +44,12 @@ def connect_wifi():
|
|||||||
wlan.connect(ssid, pw)
|
wlan.connect(ssid, pw)
|
||||||
|
|
||||||
# Wait for connection with 10 second timeout
|
# Wait for connection with 10 second timeout
|
||||||
timeout = 20
|
timeout = 10
|
||||||
while timeout > 0:
|
while timeout > 0:
|
||||||
if wlan.status() < 0 or wlan.status() >= 3:
|
if wlan.status() < 0 or wlan.status() >= 3:
|
||||||
break
|
break
|
||||||
timeout -= 1
|
timeout -= 1
|
||||||
print('Waiting for connection...')
|
print('Waiting for connection... Status is ' + str(wlan.status()))
|
||||||
blink_onboard_led(2)
|
blink_onboard_led(2)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
# Handle connection error
|
# Handle connection error
|
||||||
@@ -66,12 +67,14 @@ def connect_wifi():
|
|||||||
|
|
||||||
if wlan_status != 3:
|
if wlan_status != 3:
|
||||||
blink_onboard_led(5)
|
blink_onboard_led(5)
|
||||||
raise RuntimeError('Wi-Fi connection failed')
|
print('Wi-Fi connection failed')
|
||||||
|
return False
|
||||||
else:
|
else:
|
||||||
blink_onboard_led(1)
|
blink_onboard_led(1)
|
||||||
print('Connected')
|
print('Connected')
|
||||||
status = wlan.ifconfig()
|
status = wlan.ifconfig()
|
||||||
print('ip = ' + status[0])
|
print('ip = ' + status[0])
|
||||||
|
return True
|
||||||
|
|
||||||
def disconnect_wifi():
|
def disconnect_wifi():
|
||||||
wlan.disconnect()
|
wlan.disconnect()
|
||||||
@@ -98,10 +101,35 @@ def send_notification_to_server(notify_url, title, tags):
|
|||||||
print(request.content)
|
print(request.content)
|
||||||
request.close()
|
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)
|
machine.freq(125000000)
|
||||||
|
print('Frequency is set to 125 MHz')
|
||||||
|
|
||||||
def set_cpu_to_low_freq():
|
def set_cpu_to_low_freq():
|
||||||
|
print('Frequency is set to 20 MHz')
|
||||||
machine.freq(20000000)
|
machine.freq(20000000)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -113,6 +141,9 @@ ota_soft_reset_device=False
|
|||||||
|
|
||||||
ntfy_topic = settings['ntfy_topic']
|
ntfy_topic = settings['ntfy_topic']
|
||||||
|
|
||||||
|
healthr_url = settings['healthr_url']
|
||||||
|
healthr_service_name = settings['healthr_service_name']
|
||||||
|
|
||||||
print('Starting up...')
|
print('Starting up...')
|
||||||
|
|
||||||
wlan = network.WLAN(network.STA_IF)
|
wlan = network.WLAN(network.STA_IF)
|
||||||
@@ -138,14 +169,17 @@ led = Pin('LED', Pin.OUT)
|
|||||||
blink_onboard_led(2)
|
blink_onboard_led(2)
|
||||||
sleep(1)
|
sleep(1)
|
||||||
try:
|
try:
|
||||||
connect_wifi()
|
if connect_wifi():
|
||||||
print("Checking version updates...")
|
print("Checking version updates...")
|
||||||
#micropython_ota.reset_version()
|
#micropython_ota.reset_version()
|
||||||
micropython_ota.ota_update('https://iot-sw.adix.link', 'doorbell', ota_branch)
|
micropython_ota.ota_update('https://iot-sw.adix.link', 'doorbell', ota_branch)
|
||||||
send_notification(title = 'Started (sw: ' + current_version + ')', tags = 'signal_strength')
|
send_notification(title = 'Started (sw: ' + current_version + ')', tags = 'signal_strength')
|
||||||
sleep(2)
|
sleep(2)
|
||||||
disconnect_wifi()
|
disconnect_wifi()
|
||||||
blink_onboard_led(1)
|
blink_onboard_led(1)
|
||||||
|
else:
|
||||||
|
print('Error: Could not connect to WiFi :(')
|
||||||
|
blink_onboard_led(4)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Error: Could not send became-online message :(')
|
print('Error: Could not send became-online message :(')
|
||||||
print(e)
|
print(e)
|
||||||
@@ -153,6 +187,7 @@ except Exception as e:
|
|||||||
|
|
||||||
print('Changing frequency and beginning loop...')
|
print('Changing frequency and beginning loop...')
|
||||||
set_cpu_to_low_freq()
|
set_cpu_to_low_freq()
|
||||||
|
last_update_time = time.time() - 3600;
|
||||||
while True:
|
while True:
|
||||||
doorbell_current_state = door_bell.value()
|
doorbell_current_state = door_bell.value()
|
||||||
debug_current_state = debug_mode.value()
|
debug_current_state = debug_mode.value()
|
||||||
@@ -163,31 +198,42 @@ while True:
|
|||||||
blink_onboard_led(5)
|
blink_onboard_led(5)
|
||||||
|
|
||||||
debug_last_state = debug_current_state
|
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:
|
if doorbell_last_state == False and doorbell_current_state == True:
|
||||||
set_cpu_to_high_freq()
|
set_cpu_to_high_freq()
|
||||||
led.value(1)
|
led.value(1)
|
||||||
print('Connecting to WiFi ' + ssid + '...')
|
if connect_wifi():
|
||||||
connect_wifi()
|
print('Sending notification...')
|
||||||
print('Sending notification...')
|
|
||||||
|
send_notification(title = 'Dany Csengo', tags = 'bell')
|
||||||
send_notification(title = 'Dany Csengo', tags = 'bell')
|
|
||||||
|
time.sleep(1)
|
||||||
time.sleep(1)
|
check_version_update()
|
||||||
version_changed, remote_version = micropython_ota.check_version('https://iot-sw.adix.link', 'doorbell', ota_branch)
|
|
||||||
if version_changed:
|
disconnect_wifi()
|
||||||
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()
|
|
||||||
|
|
||||||
led.value(0)
|
led.value(0)
|
||||||
time.sleep_ms(200)
|
time.sleep_ms(200)
|
||||||
blink_onboard_led(1)
|
blink_onboard_led(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user