🚚 Moves files into separate directories
This commit is contained in:
32
peripheral/demo.py
Normal file
32
peripheral/demo.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from machine import Pin
|
||||
import utime
|
||||
|
||||
trigger = Pin(2, Pin.OUT)
|
||||
echo = Pin(3, Pin.IN)
|
||||
|
||||
sound = 340 * 100 # m/s * centi = cm/s
|
||||
|
||||
|
||||
def ultra():
|
||||
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("The distance from object is", distance, "cm")
|
||||
|
||||
|
||||
while True:
|
||||
ultra()
|
||||
utime.sleep(0.1)
|
158
peripheral/main.py
Normal file
158
peripheral/main.py
Normal file
@@ -0,0 +1,158 @@
|
||||
import utime
|
||||
import ujson
|
||||
import network
|
||||
import requests
|
||||
from machine import Pin
|
||||
|
||||
control_button = Pin(16, Pin.IN)
|
||||
led = Pin(15, Pin.OUT)
|
||||
|
||||
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():
|
||||
exit_standby_counter += 1
|
||||
else:
|
||||
exit_standby_counter = 0
|
||||
|
||||
if exit_standby_counter >= exit_standby_threshold:
|
||||
break
|
||||
|
||||
led.toggle()
|
||||
utime.sleep_ms(1000)
|
||||
|
||||
|
||||
def show_ok():
|
||||
led.low()
|
||||
for _ in range(10):
|
||||
led.toggle()
|
||||
utime.sleep_ms(100)
|
||||
led.low()
|
||||
|
||||
|
||||
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(f"Waiting for connection{(i % 3 + 1) * "."}", end="\r")
|
||||
i += 1
|
||||
for _ in range(4):
|
||||
led.toggle()
|
||||
utime.sleep_ms(250)
|
||||
|
||||
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)
|
6
peripheral/test_led.py
Normal file
6
peripheral/test_led.py
Normal file
@@ -0,0 +1,6 @@
|
||||
import utime
|
||||
from picozero import pico_led
|
||||
|
||||
while True:
|
||||
pico_led.toggle()
|
||||
utime.sleep(1)
|
Reference in New Issue
Block a user