scheduler: webinterface works, scheduling works, no sleep
This commit is contained in:
		
							parent
							
								
									74f02e1dc8
								
							
						
					
					
						commit
						dc5185af3c
					
				
					 3 changed files with 161 additions and 45 deletions
				
			
		
							
								
								
									
										6
									
								
								.idea/vcs.xml
									
										
									
										generated
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								.idea/vcs.xml
									
										
									
										generated
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,6 @@ | ||||||
|  | <?xml version="1.0" encoding="UTF-8"?> | ||||||
|  | <project version="4"> | ||||||
|  |   <component name="VcsDirectoryMappings"> | ||||||
|  |     <mapping directory="$PROJECT_DIR$" vcs="Git" /> | ||||||
|  |   </component> | ||||||
|  | </project> | ||||||
							
								
								
									
										157
									
								
								boot.py
									
										
									
									
									
								
							
							
						
						
									
										157
									
								
								boot.py
									
										
									
									
									
								
							|  | @ -2,22 +2,45 @@ | ||||||
| import wifi | import wifi | ||||||
| import usocket as socket | import usocket as socket | ||||||
| 
 | 
 | ||||||
| from machine import Pin | # from machine import Pin, RTC | ||||||
|  | import machine | ||||||
| import network | import network | ||||||
| 
 | 
 | ||||||
| import esp | import esp | ||||||
| esp.osdebug(None) | esp.osdebug(None) | ||||||
| 
 | 
 | ||||||
| import re | import ure | ||||||
|  | import uos | ||||||
|  | import ujson | ||||||
| 
 | 
 | ||||||
| import gc | # import gc | ||||||
| gc.collect() | # gc.collect() | ||||||
| 
 | 
 | ||||||
| CONFIG_FILE='schedule.conf' | CONFIG_FILE='schedule.json' | ||||||
| def web_page(): | 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 = """<html> |   html = """<html> | ||||||
|     <head> |     <head> | ||||||
|       <title>ESP Web Server</title> |       <title>Scheduler</title> | ||||||
|       <meta name="viewport" content="width=device-width, initial-scale=1"> |       <meta name="viewport" content="width=device-width, initial-scale=1"> | ||||||
|       <link rel="icon" href="data:,"> |       <link rel="icon" href="data:,"> | ||||||
|       <style> |       <style> | ||||||
|  | @ -27,12 +50,14 @@ def web_page(): | ||||||
|       </style> |       </style> | ||||||
|     </head> |     </head> | ||||||
|     <body> |     <body> | ||||||
|       <h1>CONFIG</h1>  |       <h1>CONFIG - """ + dt + """</h1>  | ||||||
|       <form action="/" method="GET"> |       <form action="/" method="GET"> | ||||||
|       <p>date (e.g. 2019-05-20) <input type="text" name="date" /></p> |       <p>date (e.g. 2019-05-20) <input type="text" name="date" value=""" + cfg_date + """ /></p> | ||||||
|       <p>time (e.g. 18-05) <input type="text" name="time" /></p> |       <p>time (e.g. 18-05) <input type="text" name="time" value=""" + cfg_time + """ /></p> | ||||||
|       <p>1-on <input type="text" name="1-off" />   1-off <input type="text" name="1-off" /></p> |       <p>1-on <input type="text" name="1on" value=""" + cfg_1on + """ />   | ||||||
|       <p>2-on <input type="text" name="2-off" />   2-off <input type="text" name="2-off" /></p> |          1-off <input type="text" name="1off" value=""" + cfg_1off + """ /></p> | ||||||
|  |       <p>2-on <input type="text" name="2on" value=""" + cfg_2on + """ />   | ||||||
|  |          2-off <input type="text" name="2off" value=""" + cfg_2off + """ /></p> | ||||||
|       <p><input type="submit" value="send" /></p> |       <p><input type="submit" value="send" /></p> | ||||||
|       </form> |       </form> | ||||||
|     </body> |     </body> | ||||||
|  | @ -40,25 +65,41 @@ def web_page(): | ||||||
|   return html |   return html | ||||||
| 
 | 
 | ||||||
| # D1 | # D1 | ||||||
| pin_cfgled = Pin(16, Pin.OUT) | pin_cfgled = machine.Pin(16, machine.Pin.OUT) | ||||||
| # D0 | # D0 | ||||||
| pin_cfg = Pin(5, Pin.IN) | pin_cfg = machine.Pin(5, machine.Pin.IN) | ||||||
| # D4 | # 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') |   f = open(CONFIG_FILE, 'r') | ||||||
|   CONFIG=f.read() |   config_raw=f.read() | ||||||
|   print("CONFIG: %s" % CONFIG) |   config = ujson.loads(config_raw) | ||||||
|   f.close() |   f.close() | ||||||
| except: | print("CONFIG: %s" % config) | ||||||
|   print("CONFIG empty") | 
 | ||||||
|   pass | 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): | if (pin_cfg.value() == 1): | ||||||
|   # ON |   ssid = wifi.ssid | ||||||
|   pin_cfgled.off() |   password = wifi.password | ||||||
| 
 | 
 | ||||||
|   station = network.WLAN(network.STA_IF) |   station = network.WLAN(network.STA_IF) | ||||||
|   station.active(True) |   station.active(True) | ||||||
|  | @ -67,9 +108,11 @@ if (pin_cfg.value() == 1): | ||||||
|   while station.isconnected() == False: |   while station.isconnected() == False: | ||||||
|     pass |     pass | ||||||
| 
 | 
 | ||||||
|   print('Connection successful') |   print('Connection successful', station.ifconfig()) | ||||||
|   print(station.ifconfig()) |  | ||||||
| 
 | 
 | ||||||
|  |   # ON | ||||||
|  |   pin_cfgled.value(0) | ||||||
|  |   pin_led.value(1) | ||||||
| 
 | 
 | ||||||
|   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||||||
|   s.bind(('', 80)) |   s.bind(('', 80)) | ||||||
|  | @ -82,18 +125,33 @@ if (pin_cfg.value() == 1): | ||||||
|     request = conn.recv(1024) |     request = conn.recv(1024) | ||||||
|     request = request.decode() |     request = request.decode() | ||||||
|     print('Content = %s' % request) |     print('Content = %s' % request) | ||||||
|     request_date="YYYY-MM-DD" |     reg = ure.search("date=[0-9-]+", request) | ||||||
|     request_time="HH-MM" |  | ||||||
|     reg = re.search("date=[0-9-]+", request) |  | ||||||
|     if reg: |     if reg: | ||||||
|       request_date = reg.group(0).replace("date=", "") |       config["date"] = reg.group(0).replace("date=", "") | ||||||
|     reg = re.search("time=[0-9-]+", request) |     reg = ure.search("time=[0-9-]+", request) | ||||||
|     if reg: |     if reg: | ||||||
|       request_time  = reg.group(0).replace("time=", "") |       config["time"] = reg.group(0).replace("time=", "") | ||||||
|     print("date: %s" % (request_date)) | 
 | ||||||
|     print("time: %s" % (request_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 = open(CONFIG_FILE, 'w') | ||||||
|     f.write("date=%s;time=%s" %(request_date, request_time)) |     f.write(ujson.dumps(config)) | ||||||
|     f.close() |     f.close() | ||||||
| 
 | 
 | ||||||
|     # led_on = request.find('/?led=on') |     # led_on = request.find('/?led=on') | ||||||
|  | @ -104,7 +162,7 @@ if (pin_cfg.value() == 1): | ||||||
|     # if led_off == 6: |     # if led_off == 6: | ||||||
|     #   print('LED OFF') |     #   print('LED OFF') | ||||||
|     #   ledpin.value(0) |     #   ledpin.value(0) | ||||||
|     response = web_page() |     response = web_page(config, rtc.datetime()) | ||||||
|     conn.send('HTTP/1.1 200 OK\n') |     conn.send('HTTP/1.1 200 OK\n') | ||||||
|     conn.send('Content-Type: text/html\n') |     conn.send('Content-Type: text/html\n') | ||||||
|     conn.send('Connection: close\n\n') |     conn.send('Connection: close\n\n') | ||||||
|  | @ -112,6 +170,31 @@ if (pin_cfg.value() == 1): | ||||||
|     conn.close() |     conn.close() | ||||||
| 
 | 
 | ||||||
| else: | 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 |   # OFF | ||||||
|   pin_cfgled.on() |   # pin_cfgled.on() | ||||||
|   print('Connection disabled') |   # print("Connection disabled") | ||||||
|  |   print("running scheduler...") | ||||||
|  |  | ||||||
							
								
								
									
										43
									
								
								main.py
									
										
									
									
									
								
							
							
						
						
									
										43
									
								
								main.py
									
										
									
									
									
								
							|  | @ -1,12 +1,39 @@ | ||||||
| import time | import time | ||||||
| 
 | 
 | ||||||
| enabled = True | # print(config) | ||||||
| while True: | WEEKDAY=0 | ||||||
|   if enabled: | # ON | ||||||
|     pin_led.on() | dton = {} | ||||||
|     time.sleep | tmp = config["1on"].split("-") | ||||||
|   else: | dton[0] = [int(tmp[0]), int(tmp[1]), int(tmp[2]), WEEKDAY, int(tmp[3]), int(tmp[4]), int(tmp[5])] | ||||||
|     pin_led.off() | 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) |   time.sleep(1) | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue