Adds connecting to server

This commit is contained in:
Daniel Svitan 2025-05-08 12:35:21 +02:00
parent 5d0a59dc86
commit b31382fcda
4 changed files with 136 additions and 23 deletions

2
.gitignore vendored
View File

@ -102,3 +102,5 @@ flake.lock
/default.nix
# Before adding new lines, see the comment at the top.
.env*

View File

@ -1,8 +1,8 @@
from machine import Pin
import utime
trigger = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
trigger = Pin(2, Pin.OUT)
echo = Pin(3, Pin.IN)
sound = 340 * 100 # m/s * centi = cm/s

View File

@ -37,7 +37,7 @@ var gotifyURL string
var mut sync.Mutex
type WriteReq struct {
opened bool `body:"opened"`
Opened bool `json:"opened"`
}
func main() {
@ -120,12 +120,12 @@ func main() {
}
mut.Lock()
if data.opened == opened {
if data.Opened == opened {
mut.Unlock()
return c.NoContent(http.StatusOK)
}
opened = data.opened
opened = data.Opened
if locked && alert {
var action string
if opened {

147
main.py
View File

@ -1,47 +1,158 @@
import network
import utime
import ujson
import network
import requests
from machine import Pin
control_button = Pin(16, Pin.IN)
led = Pin("LED", Pin.OUT)
test_mode = False
led = Pin(15, Pin.OUT)
# in the first five seconds, we want to give the
# sysadmin an option to enter standby test mode
for _ in range(50):
print(control_button.value())
trigger = Pin(2, Pin.OUT)
echo = Pin(3, Pin.IN)
sound = 340 * 100 # m/s * centi = cm/s
threshold = 15 # cm
exit_standby_counter = 0
exit_standby_threshold = 3
# start in standby mode, break if user holds button for 3s
print("Starting in standby mode...")
while True:
if control_button.value():
test_mode = True
print("Entering test mode")
exit_standby_counter += 1
else:
exit_standby_counter = 0
if exit_standby_counter >= exit_standby_threshold:
break
led.toggle()
utime.sleep_ms(100)
utime.sleep_ms(1000)
if test_mode:
while True:
def show_ok():
led.low()
for _ in range(10):
led.toggle()
utime.sleep_ms(500)
utime.sleep_ms(100)
led.low()
ssid = 'HUAWEI-2EEt-2G'
password = 'hxtU2dvx'
print("Starting door alarm...")
show_ok()
ssid = "HUAWEI-2EEt-2G"
password = "hxtU2dvx"
server = "http://192.168.100.4:1323"
token = "test123"
ultra_counter = 0
ultra_threshold = 3
# opened state from previous iteration
previous_opened = False
def connect():
print("Connecting...")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
i = 0
while not wlan.isconnected():
print("Waiting for connection...")
print(f"Waiting for connection{(i % 3 + 1) * "."}", end="\r")
i += 1
for _ in range(4):
led.toggle()
utime.sleep_ms(250)
led.on()
print(wlan.ifconfig())
print(f"Connected with IP {wlan.ifconfig()[0]}")
show_ok()
# update server to default value
send_req(False)
def send_req(opened: bool):
print("Updating state...", end="\r")
data = {"opened": opened}
raw = ujson.dumps(data)
r = requests.post(f"{server}/write",
headers={"Authorization": token, "Content-Type": "application/json"},
data=raw)
print(f"State updated [{r.status_code}]")
def ultra():
global ultra_counter, previous_opened
trigger.low()
utime.sleep_us(2)
trigger.high()
utime.sleep_us(10)
trigger.low()
# echo goes high as soon as sound wave is sent
# and returns to low once sound gets back
while echo.value() == 0:
sent_time = utime.ticks_us() # us
while echo.value() == 1:
received_time = utime.ticks_us() # us
delta_time = received_time - sent_time # us
distance = delta_time / 1_000_000 * sound / 2
print(f"distance: {distance} cm")
if distance < threshold:
# door closed
led.low()
ultra_counter = 0
# was it closed just now?
if previous_opened:
send_req(False)
previous_opened = False
else:
# door opened
led.high()
ultra_counter += 1
# was it opened just now?
if not previous_opened:
send_req(True)
previous_opened = True
connect()
i = 0
exit_counter = 0
exit_threshold = 6
while True:
ultra()
if i == 6:
led.toggle()
utime.sleep_ms(200)
led.toggle()
utime.sleep_ms(300)
i = 0
else:
utime.sleep_ms(500)
i += 1
if control_button.value():
exit_counter += 1
else:
exit_counter = 0
if exit_counter >= exit_threshold:
print("Entering glide mode...")
break
while True:
led.toggle()
utime.sleep_ms(1000)