200 lines
5.4 KiB
Python
200 lines
5.4 KiB
Python
# ssid, password
|
|
import wifi
|
|
import usocket as socket
|
|
|
|
# from machine import Pin, RTC
|
|
import machine
|
|
import network
|
|
|
|
import esp
|
|
esp.osdebug(None)
|
|
|
|
import ure
|
|
import uos
|
|
import ujson
|
|
|
|
# import gc
|
|
# gc.collect()
|
|
|
|
CONFIG_FILE='schedule.json'
|
|
def web_page(config, datetime):
|
|
print(datetime)
|
|
dt = ("%04d-%02d-%02d %02d-%02d-%02d" % (datetime[0], datetime[1], datetime[2], datetime[4], datetime[5], datetime[6]))
|
|
cfg_date = "2019-01-01"
|
|
cfg_time = "15-45"
|
|
cfg_1on = "00-00"
|
|
cfg_1off = "00-00"
|
|
cfg_2on = "00-00"
|
|
cfg_2off = "00-00"
|
|
if "date" in config:
|
|
cfg_date = config["date"]
|
|
if "time" in config:
|
|
cfg_time = config["time"]
|
|
if "1on" in config:
|
|
cfg_1on = config["1on"]
|
|
if "1off" in config:
|
|
cfg_1off = config["1off"]
|
|
if "2on" in config:
|
|
cfg_2on = config["2on"]
|
|
if "2off" in config:
|
|
cfg_2off = config["2off"]
|
|
html = """<html>
|
|
<head>
|
|
<title>Scheduler</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 - """ + dt + """</h1>
|
|
<form action="/" method="GET">
|
|
<p>date (e.g. 2019-05-20) <input type="text" name="date" value=""" + cfg_date + """ /></p>
|
|
<p>time (e.g. 18-05) <input type="text" name="time" value=""" + cfg_time + """ /></p>
|
|
<p>1-on <input type="text" name="1on" value=""" + cfg_1on + """ />
|
|
1-off <input type="text" name="1off" value=""" + cfg_1off + """ /></p>
|
|
<p>2-on <input type="text" name="2on" value=""" + cfg_2on + """ />
|
|
2-off <input type="text" name="2off" value=""" + cfg_2off + """ /></p>
|
|
<p><input type="submit" value="send" /></p>
|
|
</form>
|
|
</body>
|
|
</html>"""
|
|
return html
|
|
|
|
# D1
|
|
pin_cfgled = machine.Pin(16, machine.Pin.OUT)
|
|
# D0
|
|
pin_cfg = machine.Pin(5, machine.Pin.IN)
|
|
# D4
|
|
pin_led = machine.Pin(2, machine.Pin.OUT)
|
|
|
|
|
|
config = {}
|
|
|
|
files = uos.listdir()
|
|
if CONFIG_FILE in files:
|
|
f = open(CONFIG_FILE, 'r')
|
|
config_raw=f.read()
|
|
config = ujson.loads(config_raw)
|
|
f.close()
|
|
print("CONFIG: %s" % config)
|
|
|
|
if (not "date" in config) and (not "time" in config):
|
|
config["date"] = "2010-01-01"
|
|
config["time"] = "00-00"
|
|
d = config["date"].split("-")
|
|
t = config["time"].split("-")
|
|
WEEKDAY = 0
|
|
dt = (int(d[0]), int(d[1]), int(d[2]), WEEKDAY, int(t[0]), int(t[1]), 0, 0)
|
|
|
|
rtc = machine.RTC()
|
|
rtc.datetime(dt)
|
|
|
|
print("dt", dt)
|
|
print("rtc.datetime()", rtc.datetime())
|
|
|
|
|
|
if (pin_cfg.value() == 1):
|
|
ssid = wifi.ssid
|
|
password = wifi.password
|
|
|
|
station = network.WLAN(network.STA_IF)
|
|
station.active(True)
|
|
station.connect(wifi.ssid, wifi.password)
|
|
|
|
while station.isconnected() == False:
|
|
pass
|
|
|
|
print('Connection successful', station.ifconfig())
|
|
|
|
# ON
|
|
pin_cfgled.value(0)
|
|
pin_led.value(1)
|
|
|
|
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)
|
|
reg = ure.search("date=[0-9-]+", request)
|
|
if reg:
|
|
config["date"] = reg.group(0).replace("date=", "")
|
|
reg = ure.search("time=[0-9-]+", request)
|
|
if reg:
|
|
config["time"] = reg.group(0).replace("time=", "")
|
|
|
|
reg = ure.search("1on=[0-9-]+", request)
|
|
if reg:
|
|
config["1on"] = reg.group(0).replace("1on=", "")
|
|
|
|
reg = ure.search("1off=[0-9-]+", request)
|
|
if reg:
|
|
config["1off"] = reg.group(0).replace("1off=", "")
|
|
|
|
reg = ure.search("2on=[0-9-]+", request)
|
|
if reg:
|
|
config["2on"] = reg.group(0).replace("2on=", "")
|
|
|
|
reg = ure.search("2off=[0-9-]+", request)
|
|
if reg:
|
|
config["2off"] = reg.group(0).replace("2off=", "")
|
|
|
|
print("config:\n %s" % (ujson.dumps(config)))
|
|
|
|
f = open(CONFIG_FILE, 'w')
|
|
f.write(ujson.dumps(config))
|
|
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(config, rtc.datetime())
|
|
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:
|
|
# # set alarms
|
|
# WEEKDAY=0
|
|
# # ON
|
|
# alarm_on = {}
|
|
# tmp = config["1on"].split("-")
|
|
# alarm_on[0] = (int(tmp[0]), int(tmp[1]), int(tmp[2]), WEEKDAY, int(tmp[3]), int(tmp[4]), int(tmp[5]), 0)
|
|
# tmp = config["2on"].split("-")
|
|
# alarm_on[1] = (int(tmp[0]), int(tmp[1]), int(tmp[2]), WEEKDAY, int(tmp[3]), int(tmp[4]), int(tmp[5]), 0)
|
|
# print("alarm_on: ", alarm_on)
|
|
#
|
|
# # OFF
|
|
# alarm_off = {}
|
|
# tmp = config["1off"].split("-")
|
|
# alarm_off[0] = (int(tmp[0]), int(tmp[1]), int(tmp[2]), WEEKDAY, int(tmp[3]), int(tmp[4]), int(tmp[5]), 0)
|
|
# tmp = config["2off"].split("-")
|
|
# alarm_off[1] = (int(tmp[0]), int(tmp[1]), int(tmp[2]), WEEKDAY, int(tmp[3]), int(tmp[4]), int(tmp[5]), 0)
|
|
# print("alarm_off: ", alarm_off)
|
|
|
|
# rtc.alarm(0, alarm_on[0])
|
|
# rtc.alarm(1, alarm_on[1])
|
|
# rtc.alarm(2, alarm_off[0])
|
|
# rtc.alarm(3, alarm_off[1])
|
|
# RTC.irq(RTC.ALARM0, handler=None, wake=machine.DEEPSLEEP)
|
|
# machine.deepsleep()
|
|
# OFF
|
|
# pin_cfgled.on()
|
|
# print("Connection disabled")
|
|
print("running scheduler...")
|