diff --git a/src/wifi.py b/src/wifi.py index d501bb0..834ca5e 100644 --- a/src/wifi.py +++ b/src/wifi.py @@ -1,3 +1,5 @@ +from csms12 import CSMS12 +import socket import network def connect(): @@ -13,3 +15,41 @@ def connect(): pass print('network config:', sta_if.ifconfig()) + +def server(): + addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] + sensor = CSMS12() + + html = """ + + + + ESP8266 Moisture Sensor v1.2 + + + +

Current moisture classification

+

%s

+ + + """ + + s = socket.socket() + s.bind(addr) + s.listen(1) + + print('listening on', addr) + + while True: + cl, addr = s.accept() + print('client connected from', addr) + cl_file = cl.makefile('rwb', 0) + + while True: + line = cl_file.readline() + if not line or line == b'\r\n': + break + + response = html % sensor.getInterval() + cl.send(response) + cl.close()