-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
88 lines (79 loc) · 2.06 KB
/
main.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
try:
import usocket as socket
except ImportError:
import socket
import network
from time import sleep
from config import (
WIFI_SSID,
WIFI_PASSWORD,
status_led,
wifi_led,
wake_on_lan_pin,
input_pin
)
def blink(pin, delay):
global counter
pin.on()
sleep(delay)
pin.off()
sleep(delay)
def awake_server(is_awaken):
if is_awaken:
print('Already awaken.')
for _ in range(3):
blink(wifi_led, 0.125)
wifi_led.on()
return
wake_on_lan_pin.on()
sleep(2)
wake_on_lan_pin.off()
def run():
status_led.off()
wifi_led.off()
wake_on_lan_pin.off()
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print(f"Connecting to {WIFI_SSID}...")
sta_if.active(True)
sta_if.connect(WIFI_SSID, WIFI_PASSWORD)
while not sta_if.isconnected():
pass
print(f"Successfully connected to {WIFI_SSID}!")
print('Network config:', sta_if.ifconfig())
wifi_led.on()
soc = socket.socket()
soc.bind(('', 9))
soc.setblocking(False)
soc.listen(5)
is_awaken = False
client = None
while True:
value = input_pin.read()
voltage = value * 3.3 / 1024
is_awaken = True if voltage > 1.7 else False
if is_awaken:
status_led.off()
sleep(60)
continue
try:
if client is None:
print('Waiting for connection...')
client, addr = soc.accept()
print('Got connection from', addr)
else:
print('Waiting for data...')
data = client.recv(1024)
print('Data received:', data.decode())
if data == b'awake':
awake_server(is_awaken)
client.close()
client = None
else:
client.send(b'Invalid command.')
client.close()
client = None
except Exception:
pass
blink(status_led, 0.5)
run()