hamburger helper's kid wine

This commit is contained in:
Matthew Connelly 2018-06-01 21:48:09 +01:00
parent f8b9f7f560
commit b70d7f846f
3 changed files with 51 additions and 11 deletions

1
.gitignore vendored
View File

@ -95,3 +95,4 @@ ENV/
.ropeproject
.idea/
.vscode/

25
main.py
View File

@ -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()
ws = tinyWebServer()

34
tinyWebServer.py Normal file
View File

@ -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()