diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/boot.py b/boot.py index 4d509b3..bde15ec 100644 --- a/boot.py +++ b/boot.py @@ -2,22 +2,45 @@ import wifi import usocket as socket -from machine import Pin +# from machine import Pin, RTC +import machine import network import esp esp.osdebug(None) -import re +import ure +import uos +import ujson -import gc -gc.collect() +# import gc +# gc.collect() -CONFIG_FILE='schedule.conf' -def web_page(): +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 = """ - ESP Web Server + Scheduler -

CONFIG

+

CONFIG - """ + dt + """

-

date (e.g. 2019-05-20)

-

time (e.g. 18-05)

-

1-on   1-off

-

2-on   2-off

+

date (e.g. 2019-05-20)

+

time (e.g. 18-05)

+

1-on   + 1-off

+

2-on   + 2-off

@@ -40,25 +65,41 @@ def web_page(): return html # D1 -pin_cfgled = Pin(16, Pin.OUT) +pin_cfgled = machine.Pin(16, machine.Pin.OUT) # D0 -pin_cfg = Pin(5, Pin.IN) +pin_cfg = machine.Pin(5, machine.Pin.IN) # D4 -pin_led = Pin(2, Pin.OUT) +pin_led = machine.Pin(2, machine.Pin.OUT) -try: +config = {} + +files = uos.listdir() +if CONFIG_FILE in files: f = open(CONFIG_FILE, 'r') - CONFIG=f.read() - print("CONFIG: %s" % CONFIG) + config_raw=f.read() + config = ujson.loads(config_raw) f.close() -except: - print("CONFIG empty") - pass +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): - # ON - pin_cfgled.off() + ssid = wifi.ssid + password = wifi.password station = network.WLAN(network.STA_IF) station.active(True) @@ -67,9 +108,11 @@ if (pin_cfg.value() == 1): while station.isconnected() == False: pass - print('Connection successful') - print(station.ifconfig()) + 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)) @@ -82,18 +125,33 @@ if (pin_cfg.value() == 1): 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) + reg = ure.search("date=[0-9-]+", request) if reg: - request_date = reg.group(0).replace("date=", "") - reg = re.search("time=[0-9-]+", request) + config["date"] = reg.group(0).replace("date=", "") + reg = ure.search("time=[0-9-]+", request) if reg: - request_time = reg.group(0).replace("time=", "") - print("date: %s" % (request_date)) - print("time: %s" % (request_time)) + 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("date=%s;time=%s" %(request_date, request_time)) + f.write(ujson.dumps(config)) f.close() # led_on = request.find('/?led=on') @@ -104,7 +162,7 @@ if (pin_cfg.value() == 1): # if led_off == 6: # print('LED OFF') # ledpin.value(0) - response = web_page() + 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') @@ -112,6 +170,31 @@ if (pin_cfg.value() == 1): 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') + # pin_cfgled.on() + # print("Connection disabled") + print("running scheduler...") diff --git a/main.py b/main.py index 60815cb..7adee8f 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,39 @@ import time -enabled = True -while True: - if enabled: - pin_led.on() - time.sleep - else: - pin_led.off() +# print(config) +WEEKDAY=0 +# ON +dton = {} +tmp = config["1on"].split("-") +dton[0] = [int(tmp[0]), int(tmp[1]), int(tmp[2]), WEEKDAY, int(tmp[3]), int(tmp[4]), int(tmp[5])] +tmp = config["2on"].split("-") +dton[1] = [int(tmp[0]), int(tmp[1]), int(tmp[2]), WEEKDAY, int(tmp[3]), int(tmp[4]), int(tmp[5])] +print(dton) + +# OFF +dtoff = {} +tmp = config["1off"].split("-") +dtoff[0] = [int(tmp[0]), int(tmp[1]), int(tmp[2]), WEEKDAY, int(tmp[3]), int(tmp[4]), int(tmp[5])] +tmp = config["2off"].split("-") +dtoff[1] = [int(tmp[0]), int(tmp[1]), int(tmp[2]), WEEKDAY, int(tmp[3]), int(tmp[4]), int(tmp[5])] +print(dtoff) +pin_led.on() + +while True: + dtnow = rtc.datetime() + print("rtc.datetime(): ", dtnow) + + for i in dton: + dt = dton[i] + if (dt[0] == dtnow[0] and dt[1] == dtnow[1] and dt[2] == dtnow[2] and + dt[4] == dtnow[4] and dt[5] == dtnow[5] and dt[6] == dtnow[6]): + print("power on: ", dt, " at ", dtnow) + pin_led.off() + for i in dtoff: + dt = dtoff[i] + if (dt[0] == dtnow[0] and dt[1] == dtnow[1] and dt[2] == dtnow[2] and + dt[4] == dtnow[4] and dt[5] == dtnow[5] and dt[6] == dtnow[6]): + print("power off: ", dt, " at ", dtnow) + pin_led.on() - enabled = not enabled time.sleep(1) \ No newline at end of file