diff --git a/boot.py b/boot.py index af0ed91..d9d7775 100644 --- a/boot.py +++ b/boot.py @@ -1,14 +1,15 @@ -# 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 +## wifi.py +# ssid = xxx +# password = yyy +import wifi + esp.osdebug(None) import ure @@ -16,6 +17,7 @@ import uos import ujson import gc + gc.collect() # global variables @@ -32,22 +34,22 @@ pin_cfg = Pin(12, Pin.IN) pin_led = Pin(2, Pin.OUT) ## CONFIG -CONFIG_FILE="schedule.json" +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() + 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) + 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 @@ -63,133 +65,133 @@ hf = "header.html" ff = "footer.html" if (pin_cfg.value() == 1): - pin_led.value(0) + pin_led.value(0) - header = open(hf, "r").read() - footer = open(ff, "r").read() + 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()) + 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) + 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) + 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 = "" + 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" + 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) + ## 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) + ## 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()) + 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} + ## 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") + 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 = "" + ## 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") + ## 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...") + print("running scheduler...")