# 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
"""
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')