esp8266-scheduler/boot.py

195 lines
4.8 KiB
Python

# ssid, password
import wifi
import usocket as socket
from utime import sleep
from machine import Pin, RTC, I2C
import network
from DS3231 import DS3231
import web
import esp
esp.osdebug(None)
import ure
import uos
import ujson
import gc
gc.collect()
# global variables
## PIN
# - D0 - Pin16 - WAKE
# - D1 - Pin05 - I2C-SDA
# - D2 - Pin04 - I2C-SCL
# - D3 - Pin00 -
# - D4 - Pin02 - LED
# - D5 - Pin14 -
# - D6 - Pin12 - CONFIG
##
pin_cfg = Pin(12, Pin.IN)
pin_led = Pin(2, Pin.OUT)
## CONFIG
CONFIG_FILE="schedule.json"
config = {}
# try loading config from filesystem
files = uos.listdir()
if CONFIG_FILE in files:
f = open(CONFIG_FILE, "r")
config_raw=f.read()
config = ujson.loads(config_raw)
f.close()
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)
# print("CONFIG: %s" % config)
## 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"
if (pin_cfg.value() == 1):
pin_led.value(0)
header = open(hf, "r").read()
footer = open(ff, "r").read()
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())
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 80))
s.listen(5)
print("Configuration now...")
while True:
conn, addr = s.accept()
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()
print("updated config:\n %s" % (ujson.dumps(config)))
# print("updated config")
## 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")
else:
print("running scheduler...")