diff --git a/.gitignore b/.gitignore index 9c7d1e9..cee105b 100644 --- a/.gitignore +++ b/.gitignore @@ -95,3 +95,4 @@ ENV/ .ropeproject .idea/ +.vscode/ diff --git a/main.py b/main.py index 65f8229..aa5a69d 100644 --- a/main.py +++ b/main.py @@ -6,10 +6,10 @@ import init_sample init_sample.init_sample(hw) # Main app -import uPySensor -bme280=uPySensor.BME280(hw.i2c.bus) -lm75a =uPySensor.LM75A(hw.i2c.bus) -sht21 =uPySensor.SHT21(hw.i2c.bus) +from uPySensor import BME280, LM75A, SHT21 +bme280=BME280(hw.i2c.bus) +lm75a =LM75A(hw.i2c.bus) +sht21 =SHT21(hw.i2c.bus) #MicroWebSrv disabled until i work out how to make it fit into an esp8266's memory space #wshead={ @@ -53,12 +53,12 @@ sht21 =uPySensor.SHT21(hw.i2c.bus) #ws.Start() #fallback microserver -def bme280(): +def _bme280(): bme280.update_sensor() return '{"temperature":"%0.2f","humidity":"%0.2f","pressure":"%0.2f"}' % ( bme280.temperature, bme280.humidity, bme280.pressure) -def lm75a(): return '{"temperature":"%0.1f"}' % lm75a.read_tempC() -def sht21(): return '{"temperature":"%0.3f","humidity":"%0.3f"}' % (sht21.read_tempC(), sht21.read_hum()) +def _lm75a(): return '{"temperature":"%0.1f"}' % lm75a.read_tempC() +def _sht21(): return '{"temperature":"%0.3f","humidity":"%0.3f"}' % (sht21.read_tempC(), sht21.read_hum()) def header(): return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nServer: horny\r\n\r\n" import socket wssock=socket.getaddrinfo("0.0.0.0",80)[0][-1] @@ -80,8 +80,13 @@ while True: wcline=wcline.split(b' ') if len(wcline) == 3 and wcline[2].startswith('HTTP'): if wcline[0] != b'GET': wcl.close() - elif wcline[1] == b'/': jsn=bme280() - elif wcline[1] == b'/sht21': jsn=sht21() - elif wcline[1] == b'/lm75a': jsn=lm75a() + elif wcline[1] == b'/': jsn='{"result":"error","message":"use /bme280, /lm75a or /sht21 for sensor readings"}' + elif wcline[1] == b'/sht21': jsn=_sht21 () + elif wcline[1] == b'/lm75a': jsn=_lm75a () + elif wcline[1] == b'/bme280': jsn=_bme280() wcl.send(header()+jsn) - wcl.close() \ No newline at end of file + wcl.close() + +ws = tinyWebServer() + + diff --git a/tinyWebServer.py b/tinyWebServer.py new file mode 100644 index 0000000..02872cb --- /dev/null +++ b/tinyWebServer.py @@ -0,0 +1,34 @@ +#tinyWebServer, because microWebSrv wasn't small enough. +# no decorated route definitions, but whatever +class tinyWebServer: + import socket + _routes={} + def __init__(self, bind_ip='0.0.0.0', bind_port=80): + self.bind_ip=bind_ip + self.bind_port=bind_port + self.wssock=socket.getaddrinfo(bind_ip, bind_port)[0][-1] + + def add_route(self, path, handler): + self._routes.update({b'%s' % path: handler}) + + def start(self): + self.wss=self.socket.socket() + self.wss.bind(self.wssock) + self.wss.listen(1) + while True: self.handle_request(*self.wss.accept()) + + def handle_request(self, reqclient, reqsocket): + reqmethod = None + reqpath = None + reqclf = reqclient.makefile('rwb', 0) + while True: + reqline = reqclf.readline() + if not reqline or reqline == b'\r\n': break + if reqmethod == None or reqpath == None and len(reqline.split(' ')) == 3 and reqline.split(' ')[2].startswith('HTTP'): + reqmethod, reqpath = reqline.split(' ')[:-1] + if reqmethod == None or reqpath == None: reqclient.close() + _routes.get(reqpath, self.no_route)(reqclient) + + def no_route(self, request): + request.close() + \ No newline at end of file