202 lines
		
	
	
	
		
			4.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			202 lines
		
	
	
	
		
			4.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
# ssid, password
 | 
						|
import wifi
 | 
						|
import usocket as socket
 | 
						|
from utime import sleep
 | 
						|
 | 
						|
# from machine import Pin, RTC
 | 
						|
import machine
 | 
						|
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 - CONFIG
 | 
						|
# - D4 - Pin02 - LED
 | 
						|
##
 | 
						|
pin_cfg = machine.Pin(5, machine.Pin.IN)
 | 
						|
pin_led = machine.Pin(2, machine.Pin.OUT)
 | 
						|
 | 
						|
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:
 | 
						|
  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)
 | 
						|
 | 
						|
 | 
						|
if (pin_cfg.value() == 1):
 | 
						|
  ssid = wifi.ssid
 | 
						|
  password = wifi.password
 | 
						|
 | 
						|
  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)
 | 
						|
 | 
						|
  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:
 | 
						|
  while True:
 | 
						|
    print("running scheduler...")
 | 
						|
    sleep(1)
 |