-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_pico.py
127 lines (104 loc) · 2.94 KB
/
client_pico.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import json
import network
import aioble
import bluetooth
import machine
import asyncio
from time import sleep
import requests
from microdot import Microdot # type: ignore
from cryptolib import aes
from client_setup import ClientSetupManager
MODE_CBC = 2
BLOCK_SIZE = 16
status = ""
fname = "client_data.json"
setup = True
def isfile(path):
# check if there is a file at path
try:
with open(path) as f:
return True
except:
return False
client_data = {}
if isfile(fname):
setup = False
with open(fname) as f:
client_data = json.load(f)
server_ip = client_data["server_ip"]
_type = ""
# should be preset by developer
id_file = "IDENTITY"
with open(id_file, "r") as f:
_type = f.read()
print("Setup: ",setup)
if setup:
setup_manager = ClientSetupManager(fname)
setup_manager.start_setup()
else:
def send_identification():
k = {
"type": _type,
"location": client_data["location"],
}
try:
r = requests.post(f"http://{server_ip}/api/net/id", json=k)
print(r.json())
print("ID sent")
except Exception as e:
print("Failed to send ID:" + str(e))
def connect():
#Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(client_data["ssid"], client_data["pwd"])
while wlan.isconnected() == False:
print('Waiting for connection...')
sleep(1)
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
return ip
print("Connecting to Wifi")
led = machine.Pin("LED", machine.Pin.OUT)
led.toggle()
try:
ip = connect()
except KeyboardInterrupt:
machine.reset()
pass
led.toggle()
forward_pin = machine.Pin(27, machine.Pin.OUT)
backward_pin = machine.Pin(28, machine.Pin.OUT)
print("Sending identification..")
send_identification()
print("Done!")
app = Microdot()
calibrations = {
"closed_to_open": 10,
"open_to_closed": 10
}
my_id = 0
async def change_state(status):
if status == "open":
forward_pin.on()
backward_pin.off()
await asyncio.sleep(calibrations["closed_to_open"])
forward_pin.off()
elif status == "close":
forward_pin.off()
backward_pin.on()
await asyncio.sleep(calibrations["open_to_closed"])
backward_pin.off()
# TODO: this is where we actually move the motors
r = requests.post(f"http://{server_ip}/api/net/state", json={"status": status, "id": my_id})
@app.route("/status",methods=["POST"])
async def status_change(request):
data = request.json
status = data["status"]
my_id = data["id"]
await change_state(status)
#{"new_status": "open" | "close"}
return {"success": True}
app.run(host=ip, port=80)