Project v1

This commit is contained in:
Ádám Kovács
2023-11-02 16:02:16 +01:00
parent 70e8d2d79a
commit db76621806
171 changed files with 23617 additions and 0 deletions

138
main.py Normal file
View File

@@ -0,0 +1,138 @@
import rp2
import network
import ubinascii
import machine
from machine import Pin
from time import sleep
import urequests as requests
import time
from secrets import secrets
import socket
# Set country to avoid possible errors
rp2.country('HU')
print('Starting up...')
wlan = network.WLAN(network.STA_IF)
# Define blinking function for onboard LED to indicate error codes
def blink_onboard_led(num_blinks):
led = machine.Pin('LED', machine.Pin.OUT)
for i in range(num_blinks):
led.value(1)
time.sleep_ms(200)
led.value(0)
time.sleep_ms(200)
def connect_wifi():
wlan.active(True)
# If you need to disable powersaving mode
# wlan.config(pm = 0xa11140)
# See the MAC address in the wireless chip OTP
mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
print('mac = ' + mac)
# Other things to query
# print(wlan.config('channel'))
# print(wlan.config('essid'))
# print(wlan.config('txpower'))
# Load login data from different file for safety reasons
ssid = secrets['ssid']
pw = secrets['pw']
wlan.connect(ssid, pw)
# Wait for connection with 10 second timeout
timeout = 10
while timeout > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
timeout -= 1
print('Waiting for connection...')
time.sleep(1)
# Handle connection error
# Error meanings
# 0 Link Down
# 1 Link Join
# 2 Link NoIp
# 3 Link Up
# -1 Link Fail
# -2 Link NoNet
# -3 Link BadAuth
wlan_status = wlan.status()
blink_onboard_led(wlan_status)
if wlan_status != 3:
raise RuntimeError('Wi-Fi connection failed')
else:
print('Connected')
status = wlan.ifconfig()
print('ip = ' + status[0])
def disconnect_wifi():
wlan.disconnect()
wlan.active(False)
wlan.deinit()
print('Disconnected')
door_bell = machine.Pin(14, machine.Pin.IN, machine.Pin.PULL_DOWN)
debug_mode = machine.Pin(15, machine.Pin.IN, machine.Pin.PULL_DOWN)
doorbell_last_state = False
doorbell_current_state = False
debug_last_state = False
debug_current_state = False
""" led = Pin('LED', Pin.OUT)
print('Blinking LED Example')
while True:
led.value(not led.value())
sleep(0.5) """
led = Pin('LED', Pin.OUT)
disconnect_wifi()
blink_onboard_led(2)
print('Changing frequency and beginning loop...')
machine.freq(20000000)
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)
print('Frequency is set to default')
blink_onboard_led(5)
if doorbell_last_state == False and doorbell_current_state == True:
machine.freq(125000000)
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()
disconnect_wifi()
led.value(0)
time.sleep_ms(200)
blink_onboard_led(1)
# 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)
doorbell_last_state = doorbell_current_state
time.sleep_ms(200)