scheduler: basic things work
This commit is contained in:
		
							parent
							
								
									fb760582ed
								
							
						
					
					
						commit
						3a4a368038
					
				
					 2 changed files with 54 additions and 67 deletions
				
			
		
							
								
								
									
										57
									
								
								boot.py
									
										
									
									
									
								
							
							
						
						
									
										57
									
								
								boot.py
									
										
									
									
									
								
							|  | @ -3,8 +3,7 @@ import wifi | ||||||
| import usocket as socket | import usocket as socket | ||||||
| from utime import sleep | from utime import sleep | ||||||
| 
 | 
 | ||||||
| # from machine import Pin, RTC | from machine import Pin, RTC, I2C | ||||||
| import machine |  | ||||||
| import network | import network | ||||||
| from DS3231 import DS3231 | from DS3231 import DS3231 | ||||||
| import web | import web | ||||||
|  | @ -24,28 +23,17 @@ gc.collect() | ||||||
| # - D0 - Pin16 - WAKE | # - D0 - Pin16 - WAKE | ||||||
| # - D1 - Pin05 - I2C-SDA | # - D1 - Pin05 - I2C-SDA | ||||||
| # - D2 - Pin04 - I2C-SCL | # - D2 - Pin04 - I2C-SCL | ||||||
| # - D3 - Pin00 - CONFIG | # - D3 - Pin00 - | ||||||
| # - D4 - Pin02 - LED | # - D4 - Pin02 - LED | ||||||
|  | # - D5 - Pin14 - | ||||||
|  | # - D6 - Pin12 - CONFIG | ||||||
| ## | ## | ||||||
| pin_cfg = machine.Pin(5, machine.Pin.IN) | pin_cfg = Pin(12, Pin.IN) | ||||||
| pin_led = machine.Pin(2, machine.Pin.OUT) | pin_led = Pin(2, Pin.OUT) | ||||||
| 
 | 
 | ||||||
|  | ## CONFIG | ||||||
| CONFIG_FILE="schedule.json" | CONFIG_FILE="schedule.json" | ||||||
| hf = open("header.html") |  | ||||||
| ff = open("footer.html") |  | ||||||
| header = hf.read() |  | ||||||
| footer = ff.read() |  | ||||||
| config = {} | 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 | # try loading config from filesystem | ||||||
| files = uos.listdir() | files = uos.listdir() | ||||||
| if CONFIG_FILE in files: | if CONFIG_FILE in files: | ||||||
|  | @ -53,7 +41,6 @@ if CONFIG_FILE in files: | ||||||
|   config_raw=f.read() |   config_raw=f.read() | ||||||
|   config = ujson.loads(config_raw) |   config = ujson.loads(config_raw) | ||||||
|   f.close() |   f.close() | ||||||
| 
 |  | ||||||
| for i in range(7): | for i in range(7): | ||||||
|   if not config.get(i): |   if not config.get(i): | ||||||
|     config[i] = { |     config[i] = { | ||||||
|  | @ -61,25 +48,33 @@ for i in range(7): | ||||||
|       2: {"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("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): | if (pin_cfg.value() == 1): | ||||||
|   ssid = wifi.ssid |   pin_led.value(0) | ||||||
|   password = wifi.password | 
 | ||||||
|  |   header = open(hf, "r").read() | ||||||
|  |   footer = open(ff, "r").read() | ||||||
| 
 | 
 | ||||||
|   station = network.WLAN(network.STA_IF) |   station = network.WLAN(network.STA_IF) | ||||||
|   station.active(True) |   station.active(True) | ||||||
|   station.connect(wifi.ssid, wifi.password) |   station.connect(wifi.ssid, wifi.password) | ||||||
| 
 |  | ||||||
|   while station.isconnected() == False: |   while station.isconnected() == False: | ||||||
|     pass |     pass | ||||||
| 
 |  | ||||||
|   # print("Connection successful", station.ifconfig()) |   # print("Connection successful", station.ifconfig()) | ||||||
| 
 | 
 | ||||||
|   # ON |  | ||||||
|   pin_led.value(0) |  | ||||||
| 
 |  | ||||||
|   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||||||
|   s.bind(("", 80)) |   s.bind(("", 80)) | ||||||
|   s.listen(5) |   s.listen(5) | ||||||
|  | @ -161,8 +156,8 @@ if (pin_cfg.value() == 1): | ||||||
|         f = open(CONFIG_FILE, "w") |         f = open(CONFIG_FILE, "w") | ||||||
|         f.write(ujson.dumps(config)) |         f.write(ujson.dumps(config)) | ||||||
|         f.close() |         f.close() | ||||||
|         # print("updated config:\n %s" % (ujson.dumps(config))) |         print("updated config:\n %s" % (ujson.dumps(config))) | ||||||
|         print("updated config") |         # print("updated config") | ||||||
| 
 | 
 | ||||||
|     ## default |     ## default | ||||||
|     if mode == "default": |     if mode == "default": | ||||||
|  | @ -197,6 +192,4 @@ if (pin_cfg.value() == 1): | ||||||
|       print("connection closed with error") |       print("connection closed with error") | ||||||
| 
 | 
 | ||||||
| else: | else: | ||||||
|   while True: |  | ||||||
|   print("running scheduler...") |   print("running scheduler...") | ||||||
|     sleep(1) |  | ||||||
|  |  | ||||||
							
								
								
									
										62
									
								
								main.py
									
										
									
									
									
								
							
							
						
						
									
										62
									
								
								main.py
									
										
									
									
									
								
							|  | @ -1,39 +1,33 @@ | ||||||
| import time | import time | ||||||
| 
 | print("main.py") | ||||||
| # print(config) | state = False | ||||||
| WEEKDAY=0 | state_prev = True | ||||||
| # 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: | while True: | ||||||
|   dtnow = rtc.datetime() |   dt = rtc.datetime() | ||||||
|   print("rtc.datetime(): ", dtnow) |   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: |   for p in [1, 2]: | ||||||
|     dt = dton[i] |     # On | ||||||
|     if (dt[0] == dtnow[0] and dt[1] == dtnow[1] and dt[2] == dtnow[2] and |     entry = config_day[p]["on"] | ||||||
|             dt[4] == dtnow[4] and dt[5] == dtnow[5] and dt[6] == dtnow[6]): |     if entry["h"] == dt[4] and entry["m"] == dt[5]: | ||||||
|       print("power on: ", dt, " at ", dtnow) |       state = True | ||||||
|       pin_led.off() |     # Off | ||||||
|   for i in dtoff: |     entry = config_day[p]["off"] | ||||||
|     dt = dtoff[i] |     if entry["h"] == dt[4] and entry["m"] == dt[5]: | ||||||
|     if (dt[0] == dtnow[0] and dt[1] == dtnow[1] and dt[2] == dtnow[2] and |       state = False | ||||||
|               dt[4] == dtnow[4] and dt[5] == dtnow[5] and dt[6] == dtnow[6]): |  | ||||||
|       print("power off: ", dt, " at ", dtnow) |  | ||||||
|       pin_led.on() |  | ||||||
| 
 | 
 | ||||||
|   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) | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue