-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f19a28
commit bdfb8eb
Showing
17 changed files
with
275 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# bin/bash | ||
if [[ -z "$1" ]]; then | ||
echo "usage: $0 [device_id]" | ||
mpremote devs | ||
exit 1; | ||
fi | ||
|
||
mpremote connect id:$1 reset | ||
cd ./src | ||
echo "Copying files to device $1 ..." | ||
mpremote connect id:$1 cp -r ./* : | ||
echo "You can now disconnect your device" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,25 @@ | ||
# bin/bash | ||
if [[ -z "$1" ]]; then | ||
echo "usage: $0 [device_id]" | ||
echo "usage: $0 [device_id] [flash]" | ||
mpremote devs | ||
exit 1; | ||
fi | ||
|
||
echo "Resetting device $1 ..." | ||
mpremote connect id:$1 reset | ||
mpremote connect id:$1 cp -r src/* : | ||
mpremote connect id:$1 run ./src/main.py | ||
|
||
echo "Copying files to device $1 ..." | ||
cd ./src | ||
mpremote connect id:$1 cp -r lib/* : | ||
mpremote connect id:$1 cp ./hub.py : | ||
mpremote connect id:$1 cp ./wifi.py : | ||
mpremote connect id:$1 cp ./env.py : | ||
|
||
if [[ -z "$2" ]]; then | ||
echo "Running main.py remotely ..." | ||
mpremote connect id:$1 run ./main.py | ||
else | ||
echo "Copying main.py into place ..." | ||
mpremote connect id:$1 cp ./main.py : | ||
echo "Device has been updated" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
WIFI_SSID = "" | ||
WIFI_PASSWORD = "" | ||
SHARED_PASSWORD = "access-front-door-psk" | ||
HOSTNAME = "access-front-door" | ||
HUB_IP_ADDRESS = "" | ||
DEFAULT_UNLOCK_DURATION = 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import urequests | ||
|
||
|
||
class Hub(): | ||
def __init__(self, env): | ||
self.env = env | ||
|
||
def register_device(self, id): | ||
url = 'http://{}/register'.format(self.env.HUB_IP_ADDRESS) | ||
data = {'id': id, 'psk': self.env.SHARED_PASSWORD} | ||
encoded_data = '&'.join(["{}={}".format(k, v) for k, v in data.items()]) | ||
response = urequests.post(url, data=encoded_data, headers={'Content-Type': 'application/x-www-form-urlencoded'}) | ||
response.close() | ||
return response.status_code | ||
|
||
def perform_action(self, id, affect, params): | ||
url = 'http://{}/action'.format(self.env.HUB_IP_ADDRESS) | ||
data = {'id': id, 'psk': self.env.SHARED_PASSWORD, affect: affect, params: params} | ||
encoded_data = '&'.join(["{}={}".format(k, v) for k, v in data.items()]) | ||
response = urequests.post(url, data=encoded_data, headers={'Content-Type': 'application/x-www-form-urlencoded'}) | ||
response.close() | ||
return response.status_code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import network | ||
import uasyncio as asyncio | ||
|
||
|
||
class Wifi(): | ||
def __init__(self, env): | ||
self.env = env | ||
self.wifi = network.WLAN(network.STA_IF) | ||
|
||
async def connect(self, timeout_ms=60*1000): | ||
if self.is_connected(): | ||
return True | ||
|
||
# Connect to WiFi | ||
self.wifi.active(True) | ||
self.wifi.connect(self.env.WIFI_SSID, self.env.WIFI_PASSWORD) | ||
try: | ||
await asyncio.wait_for_ms(self.wait_for_connected(), timeout_ms) | ||
return True | ||
except asyncio.TimeoutError: | ||
self.wifi.disconnect() | ||
self.wifi.active(False) | ||
return False | ||
|
||
def ip(self): | ||
return self.wifi.ifconfig()[0] | ||
|
||
def is_connected(self): | ||
return self.wifi.isconnected() | ||
|
||
async def wait_for_connected(self, connection_state=True): | ||
while self.is_connected() is not connection_state: | ||
await asyncio.sleep_ms(500) | ||
|
||
async def stay_connected(self): | ||
while True: | ||
await self.connect() | ||
await asyncio.sleep_ms(500) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
esptool.py --chip esp32 --port /dev/cu.usbserial-21527DFA12 --baud 115200 write_flash -z 0x1000 /Users/alistairbrown/Downloads/ESP32_GENERIC-20240602-v1.23.0.bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# bin/bash | ||
if [[ -z "$1" ]]; then | ||
echo "usage: $0 [device_id] [flash]" | ||
mpremote devs | ||
exit 1; | ||
fi | ||
|
||
echo "Resetting device $1 ..." | ||
mpremote connect id:$1 reset | ||
|
||
echo "Copying files to device $1 ..." | ||
cd ./src | ||
mpremote connect id:$1 cp ./hub.py : | ||
mpremote connect id:$1 cp ./wifi.py : | ||
mpremote connect id:$1 cp ./env.py : | ||
|
||
if [[ -z "$2" ]]; then | ||
echo "Running main.py remotely ..." | ||
mpremote connect id:$1 run ./main.py | ||
else | ||
echo "Copying main.py into place ..." | ||
mpremote connect id:$1 cp ./main.py : | ||
echo "Device has been updated" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
WIFI_SSID = "farset-guest" | ||
WIFI_PASSWORD = "donationswelcome" | ||
SHARED_PASSWORD = "psk-button-door" | ||
HUB_IP_ADDRESS = "192.168.1.244" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import urequests | ||
|
||
|
||
class Hub(): | ||
def __init__(self, env): | ||
self.env = env | ||
|
||
def register_device(self, id): | ||
url = 'http://{}/register'.format(self.env.HUB_IP_ADDRESS) | ||
data = {'id': id, 'psk': self.env.SHARED_PASSWORD} | ||
encoded_data = '&'.join(["{}={}".format(k, v) for k, v in data.items()]) | ||
response = urequests.post(url, data=encoded_data, headers={'Content-Type': 'application/x-www-form-urlencoded'}) | ||
response.close() | ||
return response.status_code | ||
|
||
def perform_action(self, id, affect, params): | ||
url = 'http://{}/action'.format(self.env.HUB_IP_ADDRESS) | ||
print(">> url", url) | ||
data = {'id': id, 'psk': self.env.SHARED_PASSWORD, 'affect': affect, 'params': params} | ||
print(">> data", data) | ||
encoded_data = '&'.join(["{}={}".format(k, v) for k, v in data.items()]) | ||
response = urequests.post(url, data=encoded_data, headers={'Content-Type': 'application/x-www-form-urlencoded'}) | ||
response.close() | ||
return response.status_code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import uasyncio as asyncio | ||
import utime as time | ||
from machine import Pin | ||
|
||
import env | ||
from hub import Hub | ||
from wifi import Wifi | ||
|
||
device_id = 'button-door' | ||
|
||
class Button(): | ||
def __init__(self, hub): | ||
self.last_tick_time = None | ||
self.hub = hub | ||
|
||
def on_press(self): | ||
status_code = self.hub.perform_action(device_id, 'access-front-door', 'duration%3D2') | ||
if status_code == 200: | ||
print('Action sent successfully') | ||
else: | ||
print('Error sending action:', status_code) | ||
|
||
|
||
def tick(self, pin): | ||
self.last_tick_time = time.ticks_ms() | ||
|
||
def check_tick(self): | ||
current_time = time.ticks_ms() | ||
if self.last_tick_time and current_time - self.last_tick_time > 500: | ||
self.last_tick_time = None | ||
self.on_press() | ||
|
||
async def run(self): | ||
button = Pin(39, Pin.IN) | ||
button.irq(trigger=Pin.IRQ_FALLING, handler=self.tick) | ||
while True: | ||
self.check_tick() | ||
await asyncio.sleep_ms(10) | ||
|
||
|
||
async def main(): | ||
hub = Hub(env) | ||
button = Button(hub) | ||
|
||
wifi = Wifi(env) | ||
while not await wifi.connect(): | ||
print('Connecting...') | ||
print('Connected:', wifi.ip()) | ||
|
||
print("Registering with the hub ...") | ||
status_code = hub.register_device(device_id) | ||
if status_code == 200: | ||
print('Device registered successfully') | ||
else: | ||
print('Error registering device:', status_code) | ||
|
||
await button.run() | ||
|
||
if __name__ == '__main__': | ||
asyncio.run(main()) |
Oops, something went wrong.