diff --git a/main.py b/main.py index 09570b3..e704919 100644 --- a/main.py +++ b/main.py @@ -78,6 +78,24 @@ def disconnect_wifi(): wlan.active(False) wlan.deinit() print('Disconnected') + +def send_notification(notify_url): + print('Sending notification to ' + notify_url + '...') + # Send notification + request = requests.post(notify_url, data="Csengo", headers={ + 'Title': 'Dany Csengo', + 'Priority': '5', + 'X-Tags': 'bell' + }) + print(request.content) + request.close() + +def set_cpu_to_high_freq(): + machine.freq(125000000) + +def set_cpu_to_low_freq(): + machine.freq(20000000) + door_bell = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN) debug_mode = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN) @@ -100,30 +118,29 @@ disconnect_wifi() blink_onboard_led(2) print('Changing frequency and beginning loop...') -machine.freq(20000000) +set_cpu_to_low_freq() while True: doorbell_current_state = door_bell.value() debug_current_state = debug_mode.value() if debug_last_state == False and debug_current_state == True: - machine.freq(125000000) + set_cpu_to_high_freq() print('Frequency is set to default') blink_onboard_led(5) - + debug_last_state = debug_current_state + if doorbell_last_state == False and doorbell_current_state == True: - machine.freq(125000000) + set_cpu_to_high_freq() led.value(1) connect_wifi() - - notify_url = 'https://ntfy.sh/adix-dany-doorbell-test' - request = requests.post(notify_url, data="Csengo", headers={ - 'Title': 'Dany Csengo', - 'Priority': '5', - 'X-Tags': 'bell' - }) - print(request.content) - request.close() - + + try: + send_notification('https://ntfy.sh/adix-dany-doorbell-test') + except: + print('Error sending notification') + blink_onboard_led(3) + send_notification('https://ntfy.adix.link/adix-dany-doorbell-test') + disconnect_wifi() led.value(0) @@ -132,7 +149,7 @@ while True: # Sleep for 5 sec to avoid multiple triggers AND too frequent frequency change print('Waiting 5 seconds before changing frequency...') time.sleep(5) - machine.freq(20000000) + set_cpu_to_low_freq() doorbell_last_state = doorbell_current_state time.sleep_ms(200)