From 74f02e1dc85f39094df445fc9d8e13fd20e7d954 Mon Sep 17 00:00:00 2001 From: Konstantin Koslowski Date: Mon, 20 May 2019 23:17:58 +0200 Subject: [PATCH] scheduler: first steps --- boot.py | 117 ++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 12 +++++ venv/pyvenv.cfg | 3 ++ 3 files changed, 132 insertions(+) create mode 100644 boot.py create mode 100644 main.py create mode 100644 venv/pyvenv.cfg diff --git a/boot.py b/boot.py new file mode 100644 index 0000000..4d509b3 --- /dev/null +++ b/boot.py @@ -0,0 +1,117 @@ +# ssid, password +import wifi +import usocket as socket + +from machine import Pin +import network + +import esp +esp.osdebug(None) + +import re + +import gc +gc.collect() + +CONFIG_FILE='schedule.conf' +def web_page(): + html = """ + + ESP Web Server + + + + + +

CONFIG

+
+

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

+

time (e.g. 18-05)

+

1-on   1-off

+

2-on   2-off

+

+
+ + """ + return html + +# D1 +pin_cfgled = Pin(16, Pin.OUT) +# D0 +pin_cfg = Pin(5, Pin.IN) +# D4 +pin_led = Pin(2, Pin.OUT) + + +try: + f = open(CONFIG_FILE, 'r') + CONFIG=f.read() + print("CONFIG: %s" % CONFIG) + f.close() +except: + print("CONFIG empty") + pass + +if (pin_cfg.value() == 1): + # ON + pin_cfgled.off() + + station = network.WLAN(network.STA_IF) + station.active(True) + station.connect(wifi.ssid, wifi.password) + + while station.isconnected() == False: + pass + + print('Connection successful') + print(station.ifconfig()) + + + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.bind(('', 80)) + s.listen(5) + + print('Configuration now...') + while True: + conn, addr = s.accept() + print('Got a connection from %s' % str(addr)) + 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) + if reg: + request_date = reg.group(0).replace("date=", "") + reg = re.search("time=[0-9-]+", request) + if reg: + request_time = reg.group(0).replace("time=", "") + print("date: %s" % (request_date)) + print("time: %s" % (request_time)) + f = open(CONFIG_FILE, 'w') + f.write("date=%s;time=%s" %(request_date, request_time)) + f.close() + + # led_on = request.find('/?led=on') + # led_off = request.find('/?led=off') + # if led_on == 6: + # print('LED ON') + # ledpin.value(1) + # if led_off == 6: + # print('LED OFF') + # ledpin.value(0) + response = web_page() + conn.send('HTTP/1.1 200 OK\n') + conn.send('Content-Type: text/html\n') + conn.send('Connection: close\n\n') + conn.sendall(response) + conn.close() + +else: + # OFF + pin_cfgled.on() + print('Connection disabled') diff --git a/main.py b/main.py new file mode 100644 index 0000000..60815cb --- /dev/null +++ b/main.py @@ -0,0 +1,12 @@ +import time + +enabled = True +while True: + if enabled: + pin_led.on() + time.sleep + else: + pin_led.off() + + enabled = not enabled + time.sleep(1) \ No newline at end of file diff --git a/venv/pyvenv.cfg b/venv/pyvenv.cfg new file mode 100644 index 0000000..f967d3d --- /dev/null +++ b/venv/pyvenv.cfg @@ -0,0 +1,3 @@ +home = /usr/bin +include-system-site-packages = false +version = 3.7.3