hamburger helper's kid wine
This commit is contained in:
parent
f8b9f7f560
commit
b70d7f846f
|
@ -95,3 +95,4 @@ ENV/
|
||||||
.ropeproject
|
.ropeproject
|
||||||
|
|
||||||
.idea/
|
.idea/
|
||||||
|
.vscode/
|
||||||
|
|
27
main.py
27
main.py
|
@ -6,10 +6,10 @@ import init_sample
|
||||||
init_sample.init_sample(hw)
|
init_sample.init_sample(hw)
|
||||||
|
|
||||||
# Main app
|
# Main app
|
||||||
import uPySensor
|
from uPySensor import BME280, LM75A, SHT21
|
||||||
bme280=uPySensor.BME280(hw.i2c.bus)
|
bme280=BME280(hw.i2c.bus)
|
||||||
lm75a =uPySensor.LM75A(hw.i2c.bus)
|
lm75a =LM75A(hw.i2c.bus)
|
||||||
sht21 =uPySensor.SHT21(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
|
#MicroWebSrv disabled until i work out how to make it fit into an esp8266's memory space
|
||||||
#wshead={
|
#wshead={
|
||||||
|
@ -53,12 +53,12 @@ sht21 =uPySensor.SHT21(hw.i2c.bus)
|
||||||
#ws.Start()
|
#ws.Start()
|
||||||
|
|
||||||
#fallback microserver
|
#fallback microserver
|
||||||
def bme280():
|
def _bme280():
|
||||||
bme280.update_sensor()
|
bme280.update_sensor()
|
||||||
return '{"temperature":"%0.2f","humidity":"%0.2f","pressure":"%0.2f"}' % (
|
return '{"temperature":"%0.2f","humidity":"%0.2f","pressure":"%0.2f"}' % (
|
||||||
bme280.temperature, bme280.humidity, bme280.pressure)
|
bme280.temperature, bme280.humidity, bme280.pressure)
|
||||||
def lm75a(): return '{"temperature":"%0.1f"}' % lm75a.read_tempC()
|
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 _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"
|
def header(): return "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nServer: horny\r\n\r\n"
|
||||||
import socket
|
import socket
|
||||||
wssock=socket.getaddrinfo("0.0.0.0",80)[0][-1]
|
wssock=socket.getaddrinfo("0.0.0.0",80)[0][-1]
|
||||||
|
@ -80,8 +80,13 @@ while True:
|
||||||
wcline=wcline.split(b' ')
|
wcline=wcline.split(b' ')
|
||||||
if len(wcline) == 3 and wcline[2].startswith('HTTP'):
|
if len(wcline) == 3 and wcline[2].startswith('HTTP'):
|
||||||
if wcline[0] != b'GET': wcl.close()
|
if wcline[0] != b'GET': wcl.close()
|
||||||
elif wcline[1] == b'/': jsn=bme280()
|
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'/sht21': jsn=_sht21 ()
|
||||||
elif wcline[1] == b'/lm75a': jsn=lm75a()
|
elif wcline[1] == b'/lm75a': jsn=_lm75a ()
|
||||||
|
elif wcline[1] == b'/bme280': jsn=_bme280()
|
||||||
wcl.send(header()+jsn)
|
wcl.send(header()+jsn)
|
||||||
wcl.close()
|
wcl.close()
|
||||||
|
|
||||||
|
ws = tinyWebServer()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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()
|
||||||
|
|
Loading…
Reference in New Issue