scheduler: basic things work

This commit is contained in:
Konstantin Koslowski 2019-05-24 21:20:58 +02:00
parent fb760582ed
commit 3a4a368038
2 changed files with 54 additions and 67 deletions

59
boot.py
View file

@ -3,8 +3,7 @@ import wifi
import usocket as socket
from utime import sleep
# from machine import Pin, RTC
import machine
from machine import Pin, RTC, I2C
import network
from DS3231 import DS3231
import web
@ -24,28 +23,17 @@ gc.collect()
# - D0 - Pin16 - WAKE
# - D1 - Pin05 - I2C-SDA
# - D2 - Pin04 - I2C-SCL
# - D3 - Pin00 - CONFIG
# - D3 - Pin00 -
# - D4 - Pin02 - LED
# - D5 - Pin14 -
# - D6 - Pin12 - CONFIG
##
pin_cfg = machine.Pin(5, machine.Pin.IN)
pin_led = machine.Pin(2, machine.Pin.OUT)
pin_cfg = Pin(12, Pin.IN)
pin_led = Pin(2, Pin.OUT)
## CONFIG
CONFIG_FILE="schedule.json"
hf = open("header.html")
ff = open("footer.html")
header = hf.read()
footer = ff.read()
config = {}
# RTC
rtc = machine.RTC()
i2c = machine.I2C(scl=machine.Pin(4), sda=machine.Pin(5))
ds = DS3231(i2c)
tmp = ds.DateTime()
tmp.append(0)
rtc.datetime(tmp)
print("rtc", rtc.datetime())
# try loading config from filesystem
files = uos.listdir()
if CONFIG_FILE in files:
@ -53,7 +41,6 @@ if CONFIG_FILE in files:
config_raw=f.read()
config = ujson.loads(config_raw)
f.close()
for i in range(7):
if not config.get(i):
config[i] = {
@ -61,25 +48,33 @@ for i in range(7):
2: {"on": {"h": 0, "m": 0}, "off": {"h": 0, "m": 0}}
}
print("initialized day: ", i)
print("CONFIG: %s" % config)
# 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):
ssid = wifi.ssid
password = wifi.password
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())
# ON
pin_led.value(0)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 80))
s.listen(5)
@ -161,8 +156,8 @@ if (pin_cfg.value() == 1):
f = open(CONFIG_FILE, "w")
f.write(ujson.dumps(config))
f.close()
# print("updated config:\n %s" % (ujson.dumps(config)))
print("updated config")
print("updated config:\n %s" % (ujson.dumps(config)))
# print("updated config")
## default
if mode == "default":
@ -197,6 +192,4 @@ if (pin_cfg.value() == 1):
print("connection closed with error")
else:
while True:
print("running scheduler...")
sleep(1)
print("running scheduler...")

62
main.py
View file

@ -1,39 +1,33 @@
import time
# 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()
print("main.py")
state = False
state_prev = True
while True:
dtnow = rtc.datetime()
print("rtc.datetime(): ", dtnow)
dt = rtc.datetime()
print("dt: ", dt)
day = dt[3]
print("day: ", day)
config_day = config[day]
print("config[day]: ", config_day)
print("state: ", state)
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()
for p in [1, 2]:
# On
entry = config_day[p]["on"]
if entry["h"] == dt[4] and entry["m"] == dt[5]:
state = True
# Off
entry = config_day[p]["off"]
if entry["h"] == dt[4] and entry["m"] == dt[5]:
state = False
time.sleep(1)
if state != state_prev:
state_prev = state
if state:
print("turn on", entry)
pin_led.value(0)
else:
print("turn off", entry)
pin_led.value(1)
time.sleep(10)