117 lines
2.8 KiB
Python
117 lines
2.8 KiB
Python
# ssid, password
|
|
import wifi
|
|
import usocket as socket
|
|
|
|
from machine import Pin
|
|
import network
|
|
|
|
import esp
|
|
esp.osdebug(None)
|
|
|
|
import re
|
|
|
|
import gc
|
|
gc.collect()
|
|
|
|
CONFIG_FILE='schedule.conf'
|
|
def web_page():
|
|
html = """<html>
|
|
<head>
|
|
<title>ESP Web Server</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<link rel="icon" href="data:,">
|
|
<style>
|
|
html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
|
|
h1{color: #0F3376; padding: 2vh;}
|
|
p{font-size: 1.5rem;}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>CONFIG</h1>
|
|
<form action="/" method="GET">
|
|
<p>date (e.g. 2019-05-20) <input type="text" name="date" /></p>
|
|
<p>time (e.g. 18-05) <input type="text" name="time" /></p>
|
|
<p>1-on <input type="text" name="1-off" /> 1-off <input type="text" name="1-off" /></p>
|
|
<p>2-on <input type="text" name="2-off" /> 2-off <input type="text" name="2-off" /></p>
|
|
<p><input type="submit" value="send" /></p>
|
|
</form>
|
|
</body>
|
|
</html>"""
|
|
return html
|
|
|
|
# D1
|
|
pin_cfgled = Pin(16, Pin.OUT)
|
|
# D0
|
|
pin_cfg = Pin(5, Pin.IN)
|
|
# D4
|
|
pin_led = Pin(2, Pin.OUT)
|
|
|
|
|
|
try:
|
|
f = open(CONFIG_FILE, 'r')
|
|
CONFIG=f.read()
|
|
print("CONFIG: %s" % CONFIG)
|
|
f.close()
|
|
except:
|
|
print("CONFIG empty")
|
|
pass
|
|
|
|
if (pin_cfg.value() == 1):
|
|
# ON
|
|
pin_cfgled.off()
|
|
|
|
station = network.WLAN(network.STA_IF)
|
|
station.active(True)
|
|
station.connect(wifi.ssid, wifi.password)
|
|
|
|
while station.isconnected() == False:
|
|
pass
|
|
|
|
print('Connection successful')
|
|
print(station.ifconfig())
|
|
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
s.bind(('', 80))
|
|
s.listen(5)
|
|
|
|
print('Configuration now...')
|
|
while True:
|
|
conn, addr = s.accept()
|
|
print('Got a connection from %s' % str(addr))
|
|
request = conn.recv(1024)
|
|
request = request.decode()
|
|
print('Content = %s' % request)
|
|
request_date="YYYY-MM-DD"
|
|
request_time="HH-MM"
|
|
reg = re.search("date=[0-9-]+", request)
|
|
if reg:
|
|
request_date = reg.group(0).replace("date=", "")
|
|
reg = re.search("time=[0-9-]+", request)
|
|
if reg:
|
|
request_time = reg.group(0).replace("time=", "")
|
|
print("date: %s" % (request_date))
|
|
print("time: %s" % (request_time))
|
|
f = open(CONFIG_FILE, 'w')
|
|
f.write("date=%s;time=%s" %(request_date, request_time))
|
|
f.close()
|
|
|
|
# led_on = request.find('/?led=on')
|
|
# led_off = request.find('/?led=off')
|
|
# if led_on == 6:
|
|
# print('LED ON')
|
|
# ledpin.value(1)
|
|
# if led_off == 6:
|
|
# print('LED OFF')
|
|
# ledpin.value(0)
|
|
response = web_page()
|
|
conn.send('HTTP/1.1 200 OK\n')
|
|
conn.send('Content-Type: text/html\n')
|
|
conn.send('Connection: close\n\n')
|
|
conn.sendall(response)
|
|
conn.close()
|
|
|
|
else:
|
|
# OFF
|
|
pin_cfgled.on()
|
|
print('Connection disabled')
|