Fallback ntfy server

This commit is contained in:
Ádám Kovács
2023-11-05 21:05:16 +01:00
parent db76621806
commit 60a2fb37ee

41
main.py
View File

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