2019-05-20 23:17:58 +02:00
|
|
|
# ssid, password
|
|
|
|
import wifi
|
|
|
|
import usocket as socket
|
2019-05-24 19:34:21 +02:00
|
|
|
from utime import sleep
|
2019-05-20 23:17:58 +02:00
|
|
|
|
2019-05-24 21:20:58 +02:00
|
|
|
from machine import Pin, RTC, I2C
|
2019-05-20 23:17:58 +02:00
|
|
|
import network
|
2019-05-24 19:34:21 +02:00
|
|
|
from DS3231 import DS3231
|
|
|
|
import web
|
2019-05-20 23:17:58 +02:00
|
|
|
|
|
|
|
import esp
|
|
|
|
esp.osdebug(None)
|
|
|
|
|
2019-05-21 15:31:30 +02:00
|
|
|
import ure
|
|
|
|
import uos
|
|
|
|
import ujson
|
|
|
|
|
2019-05-24 19:34:21 +02:00
|
|
|
import gc
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
# global variables
|
|
|
|
## PIN
|
|
|
|
# - D0 - Pin16 - WAKE
|
|
|
|
# - D1 - Pin05 - I2C-SDA
|
|
|
|
# - D2 - Pin04 - I2C-SCL
|
2019-05-24 21:20:58 +02:00
|
|
|
# - D3 - Pin00 -
|
2019-05-24 19:34:21 +02:00
|
|
|
# - D4 - Pin02 - LED
|
2019-05-24 21:20:58 +02:00
|
|
|
# - D5 - Pin14 -
|
|
|
|
# - D6 - Pin12 - CONFIG
|
2019-05-24 19:34:21 +02:00
|
|
|
##
|
2019-05-24 21:20:58 +02:00
|
|
|
pin_cfg = Pin(12, Pin.IN)
|
|
|
|
pin_led = Pin(2, Pin.OUT)
|
2019-05-21 15:31:30 +02:00
|
|
|
|
2019-05-24 21:20:58 +02:00
|
|
|
## CONFIG
|
2019-05-24 19:34:21 +02:00
|
|
|
CONFIG_FILE="schedule.json"
|
2019-05-21 15:31:30 +02:00
|
|
|
config = {}
|
2019-05-24 19:34:21 +02:00
|
|
|
# try loading config from filesystem
|
2019-05-21 15:31:30 +02:00
|
|
|
files = uos.listdir()
|
|
|
|
if CONFIG_FILE in files:
|
2019-05-24 19:34:21 +02:00
|
|
|
f = open(CONFIG_FILE, "r")
|
2019-05-21 15:31:30 +02:00
|
|
|
config_raw=f.read()
|
|
|
|
config = ujson.loads(config_raw)
|
2019-05-20 23:17:58 +02:00
|
|
|
f.close()
|
2019-05-24 19:34:21 +02:00
|
|
|
for i in range(7):
|
|
|
|
if not config.get(i):
|
|
|
|
config[i] = {
|
|
|
|
1: {"on": {"h": 0, "m": 0}, "off": {"h": 0, "m": 0}},
|
|
|
|
2: {"on": {"h": 0, "m": 0}, "off": {"h": 0, "m": 0}}
|
|
|
|
}
|
|
|
|
print("initialized day: ", i)
|
2019-05-24 21:20:58 +02:00
|
|
|
# print("CONFIG: %s" % config)
|
2019-05-21 15:31:30 +02:00
|
|
|
|
2019-05-24 21:20:58 +02:00
|
|
|
## RTC
|
|
|
|
rtc = RTC()
|
|
|
|
i2c = I2C(scl=Pin(4), sda=Pin(5))
|
|
|
|
ds = DS3231(i2c)
|
|
|
|
tmp = ds.DateTime()
|
|
|
|
tmp.append(0)
|
|
|
|
rtc.datetime(tmp)
|
|
|
|
print("rtc", rtc.datetime())
|
|
|
|
## HTML
|
|
|
|
hf = "header.html"
|
|
|
|
ff = "footer.html"
|
2019-05-20 23:17:58 +02:00
|
|
|
|
|
|
|
if (pin_cfg.value() == 1):
|
2019-05-24 21:20:58 +02:00
|
|
|
pin_led.value(0)
|
|
|
|
|
|
|
|
header = open(hf, "r").read()
|
|
|
|
footer = open(ff, "r").read()
|
2019-05-20 23:17:58 +02:00
|
|
|
|
|
|
|
station = network.WLAN(network.STA_IF)
|
|
|
|
station.active(True)
|
|
|
|
station.connect(wifi.ssid, wifi.password)
|
|
|
|
while station.isconnected() == False:
|
|
|
|
pass
|
2019-05-24 19:34:21 +02:00
|
|
|
# print("Connection successful", station.ifconfig())
|
2019-05-20 23:17:58 +02:00
|
|
|
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2019-05-24 19:34:21 +02:00
|
|
|
s.bind(("", 80))
|
2019-05-20 23:17:58 +02:00
|
|
|
s.listen(5)
|
|
|
|
|
2019-05-24 19:34:21 +02:00
|
|
|
print("Configuration now...")
|
2019-05-20 23:17:58 +02:00
|
|
|
while True:
|
|
|
|
conn, addr = s.accept()
|
2019-05-24 19:34:21 +02:00
|
|
|
req_recv = conn.recv(512)
|
|
|
|
req_decode = req_recv.decode()
|
|
|
|
req_split = req_decode.split("\n")
|
|
|
|
request = req_split[0]
|
|
|
|
print("request: ", request)
|
|
|
|
|
|
|
|
response = ""
|
|
|
|
|
|
|
|
mode = "default"
|
|
|
|
if "/config_dt" in request:
|
|
|
|
mode = "config_dt"
|
|
|
|
elif "/config_day" in request:
|
|
|
|
mode = "config_day"
|
|
|
|
elif "/favicon" in request:
|
|
|
|
mode = "none"
|
|
|
|
|
|
|
|
## config_* --> save
|
|
|
|
if ("config" in mode) and ("save=1" in request):
|
|
|
|
update = False
|
|
|
|
## DATE
|
|
|
|
reg = ure.search("date=[0-9-]+", request)
|
|
|
|
if reg:
|
|
|
|
tmp = reg.group(0).replace("date=", "").split("-")
|
|
|
|
if len(tmp) == 3:
|
|
|
|
update = True
|
|
|
|
new_date = [int(tmp[0]), int(tmp[1]), int(tmp[2])]
|
|
|
|
ds.Date(new_date)
|
|
|
|
|
|
|
|
## TIME
|
|
|
|
reg = ure.search("time=[0-9-]+", request)
|
|
|
|
if reg:
|
|
|
|
tmp = reg.group(0).replace("time=", "").split("-")
|
|
|
|
if len(tmp) >= 2:
|
|
|
|
update = True
|
|
|
|
new_time = [int(tmp[0]), int(tmp[1]), 0]
|
|
|
|
if len(tmp) == 3:
|
|
|
|
new_time[2] = int(tmp[2])
|
|
|
|
ds.Time(new_time)
|
|
|
|
|
|
|
|
if update:
|
|
|
|
update = False
|
|
|
|
tmp_dt = ds.DateTime()
|
|
|
|
tmp_dt.append(0)
|
|
|
|
rtc.datetime(tmp_dt)
|
|
|
|
print("Updated datetime", rtc.datetime())
|
|
|
|
|
|
|
|
## DAY
|
|
|
|
reg = ure.search("day=[0-6]+", request)
|
|
|
|
if reg:
|
|
|
|
update = True
|
|
|
|
tmp = reg.group(0).replace("day=", "")
|
|
|
|
day = int(tmp)
|
|
|
|
for p in [1, 2]:
|
|
|
|
# on
|
|
|
|
reg = ure.search(("p%d_on=[0-9-]+" % p), request)
|
|
|
|
if reg:
|
|
|
|
tmp = reg.group(0).replace(("p%d_on=" % p), "").split("-")
|
|
|
|
if len(tmp) == 2:
|
|
|
|
h = int(tmp[0])
|
|
|
|
m = int(tmp[1])
|
|
|
|
config[day][p]["on"] = {"h": h, "m": m}
|
|
|
|
# off
|
|
|
|
reg = ure.search(("p%d_off=[0-9-]+" % p), request)
|
|
|
|
if reg:
|
|
|
|
tmp = reg.group(0).replace(("p%d_off=" % p), "").split("-")
|
|
|
|
if len(tmp) == 2:
|
|
|
|
h = int(tmp[0])
|
|
|
|
m = int(tmp[1])
|
|
|
|
config[day][p]["off"] = {"h": h, "m": m}
|
|
|
|
|
|
|
|
if update:
|
|
|
|
f = open(CONFIG_FILE, "w")
|
|
|
|
f.write(ujson.dumps(config))
|
|
|
|
f.close()
|
2019-05-24 21:20:58 +02:00
|
|
|
print("updated config:\n %s" % (ujson.dumps(config)))
|
|
|
|
# print("updated config")
|
2019-05-24 19:34:21 +02:00
|
|
|
|
|
|
|
## default
|
|
|
|
if mode == "default":
|
|
|
|
print("web_default")
|
|
|
|
response = header + web.default() + footer
|
|
|
|
## config_dt
|
|
|
|
elif mode == "config_dt":
|
|
|
|
response = header + web.config_datetime(rtc) + footer
|
|
|
|
## config_day
|
|
|
|
elif mode == "config_day":
|
|
|
|
reg = ure.search("day=[0-6-]+", request)
|
|
|
|
if reg:
|
|
|
|
tmp = reg.group(0).replace("day=", "")
|
|
|
|
day = int(tmp)
|
|
|
|
print("web_config, day ", day)
|
|
|
|
response = header + web.config_day(config, day) + footer
|
|
|
|
## favicon
|
|
|
|
elif mode == "favicon":
|
|
|
|
print("favicon")
|
|
|
|
response = ""
|
|
|
|
|
|
|
|
## send response
|
|
|
|
try:
|
|
|
|
conn.send("HTTP/1.1 200 OK\n")
|
|
|
|
conn.send("Content-Type: text/html\n")
|
|
|
|
conn.send("Connection: close\n\n")
|
|
|
|
conn.sendall(response)
|
|
|
|
sleep(1)
|
|
|
|
conn.close()
|
|
|
|
# print("connection closed successfully")
|
|
|
|
except:
|
|
|
|
print("connection closed with error")
|
2019-05-20 23:17:58 +02:00
|
|
|
|
|
|
|
else:
|
2019-05-24 21:20:58 +02:00
|
|
|
print("running scheduler...")
|