5 Commits

Author SHA1 Message Date
Daniel Svitan
7d2ad1cd49 🐛 Fixes memory overflow and removes server health check
All checks were successful
Gitea Build Action / build (push) Successful in 22s
2025-06-02 20:46:55 +02:00
Daniel Svitan
307b467ac1 🚧 Adds checking and reconnecting when wlan is down
All checks were successful
Gitea Build Action / build (push) Successful in 24s
2025-06-01 21:19:13 +02:00
Daniel Svitan
995bf90efb 🐛 Fixes peripheral delay on door open
All checks were successful
Gitea Build Action / build (push) Successful in 25s
2025-06-01 12:49:30 +02:00
Daniel Svitan
100bab01e4 🐛 Fixes API routes
All checks were successful
Gitea Build Action / build (push) Successful in 25s
2025-06-01 12:40:13 +02:00
Daniel Svitan
2f3ccf14db 🐛 Fixes cannot update state
All checks were successful
Gitea Build Action / build (push) Successful in 29s
2025-06-01 12:30:42 +02:00
5 changed files with 34 additions and 25 deletions

1
.gitignore vendored
View File

@@ -104,3 +104,4 @@ flake.lock
# Before adding new lines, see the comment at the top.
.env*
.idea/

View File

@@ -85,6 +85,7 @@ func makePostReq(url string, body any) ([]byte, error) {
}
req.Header.Set("Authorization", token)
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
@@ -119,8 +120,8 @@ func main() {
Action: test,
},
{
Name: "get",
Usage: "get door state",
Name: "open",
Usage: "get door state (opened or closed)",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "raw",
@@ -200,7 +201,7 @@ func getOpened(ctx context.Context, cmd *cli.Command) error {
return err
}
opened, err := makeGetReq(fmt.Sprintf("%s/read", server))
opened, err := makeGetReq(fmt.Sprintf("%s/open", server))
if err != nil {
return err
}
@@ -244,7 +245,7 @@ func getAlert(context.Context, *cli.Command) error {
return err
}
alert, err := makeGetReq(fmt.Sprintf("%s/alerts", server))
alert, err := makeGetReq(fmt.Sprintf("%s/alert", server))
if err != nil {
return err
}
@@ -305,7 +306,7 @@ func createManageAlert(alert bool) func(context.Context, *cli.Command) error {
data.For = secs
}
_, err = makePostReq(fmt.Sprintf("%s/alerts", server), data)
_, err = makePostReq(fmt.Sprintf("%s/alert", server), data)
if err != nil {
return err
}

View File

@@ -1 +1,3 @@
__pycache__/
.env.json

View File

@@ -1,3 +1,4 @@
import gc
import utime
import ujson
import network
@@ -7,7 +8,6 @@ from machine import Pin
THRESHOLD_DISTANCE = 15 # cm
SOUND_SPEED = 340 * 100 # m/s * centi = cm/s
MAX_CONNECTION_RETRIES = 50
ULTRA_OPENED_THRESHOLD = 3
class App:
@@ -18,7 +18,7 @@ class App:
opened: bool = False
previously_opened: bool = False
ultra_opened_counter: int = 0
wlan: network.WLAN
led = Pin(15, Pin.OUT)
trigger = Pin(2, Pin.OUT)
@@ -57,18 +57,18 @@ class App:
def connect(self):
print("Connecting to Wi-Fi...")
wlan = network.WLAN(network.STA_IF)
self.wlan = network.WLAN(network.STA_IF)
wlan.active(False)
self.wlan.active(False)
utime.sleep_ms(250)
wlan.active(True)
self.wlan.active(True)
utime.sleep_ms(250)
wlan.connect(self.ssid, self.password)
self.wlan.connect(self.ssid, self.password)
utime.sleep_ms(100)
retry_count = 0
while not wlan.isconnected():
while not self.wlan.isconnected():
if retry_count >= MAX_CONNECTION_RETRIES:
print("Max connection retries reached")
exit(1)
@@ -82,12 +82,12 @@ class App:
if retry_count % 10 == 0:
print("Attempting to restart connection...")
wlan.connect(self.ssid, self.password)
self.wlan.connect(self.ssid, self.password)
for _ in range(10):
self.led.toggle()
utime.sleep_ms(50)
print(f"Connected with IP {wlan.ifconfig()[0]}")
print(f"Connected with IP {self.wlan.ifconfig()[0]}")
self.update_server()
def update_server(self):
@@ -101,7 +101,7 @@ class App:
headers={"Authorization": self.token, "Content-Type": "application/json"},
data=raw
)
print(f"State updated [{r.status_code}]")
print(f"State updated [{r.status_code}] {r.content.decode()}")
except Exception as e:
print(f"Error occurred: {e}")
@@ -122,7 +122,7 @@ class App:
delta_time = received_time - sent_time
distance = delta_time / 1_000_000 * SOUND_SPEED / 2
print(f"Distance: {distance} cm")
print(f"Distance: {distance:0<5} cm; mem_free = {gc.mem_free()}")
return distance
@@ -133,35 +133,41 @@ class App:
i = 0
while True:
try:
if not self.wlan.isconnected():
self.connect()
distance = self.measure_distance()
self.opened = distance >= THRESHOLD_DISTANCE
if not opened:
if not self.opened:
self.led.low()
self.ultra_opened_counter = 0
# was it just closed?
if self.previously_opened:
self.update_server()
self.previously_opened = False
else:
self.led.high()
self.ultra_opened_counter += 1
if self.ultra_opened_counter >= ULTRA_OPENED_THRESHOLD:
# was it just opened? +wait delay
if not self.previously_opened:
self.led.high()
self.update_server()
self.previously_opened = True
if i >= 20:
print("Blinking...")
self.led.toggle()
utime.sleep_ms(100)
self.led.toggle()
utime.sleep_ms(400)
i = 0
gc.collect()
else:
utime.sleep_ms(500)
except Exception as e:
print(f"Fatal exception occurred: {e}")
print(f"Fatal error occurred: {e}")
self.previously_opened = self.opened
i += 1
if __name__ == "__main__":

View File

@@ -130,7 +130,6 @@ func main() {
}
if data.Opened == opened {
mut.Unlock()
return c.NoContent(http.StatusOK)
}