re-commit old webserver

This commit is contained in:
Maff 2018-05-31 23:54:21 +01:00
parent 7ed40e46a7
commit f8b9f7f560
1 changed files with 75 additions and 41 deletions

116
main.py
View File

@ -2,52 +2,86 @@ import uPyConfig
hw = uPyConfig.esp8266(variant='d1-r2') hw = uPyConfig.esp8266(variant='d1-r2')
#print family, variant and IP address (using oled, if available on-board) #print family, variant and IP address (using oled, if available on-board)
#import init_sample import init_sample
#init_sample.init_sample(hw) init_sample.init_sample(hw)
# Main app # Main app
wshead={
'Server':'horny',
}
wsctype='application/json'
wscharset='UTF-8'
import uPySensor import uPySensor
bme280=uPySensor.BME280(hw.i2c.bus) bme280=uPySensor.BME280(hw.i2c.bus)
lm75a =uPySensor.LM75A(hw.i2c.bus) lm75a =uPySensor.LM75A(hw.i2c.bus)
sht21 =uPySensor.SHT21(hw.i2c.bus) sht21 =uPySensor.SHT21(hw.i2c.bus)
from microWebSrv import MicroWebSrv #MicroWebSrv disabled until i work out how to make it fit into an esp8266's memory space
@MicroWebSrv.route('/') #wshead={
def get_root(wscl, wsres): # 'Server':'horny',
wsres.WriteResponseOk( #}
headers=wshead, #wsctype='application/json'
contentType=wsctype, #wscharset='UTF-8'
content='{"result":"error","message":"use /bme280, /lm75a or /sht21 for sensor readings"}' #from microWebSrv import MicroWebSrv
) #@MicroWebSrv.route('/')
@MicroWebSrv.route('/bme280') #def get_root(wscl, wsres):
def get_bme280(wscl, wsres): # wsres.WriteResponseOk(
bme280.update_sensor() # headers=wshead,
wsres.WriteResponseOk( # contentType=wsctype,
headers=wshead, # content='{"result":"error","message":"use /bme280, /lm75a or /sht21 for sensor readings"}'
contentType=wsctype, # )
content='{"temperature":"%0.2f","humidity":"%0.2f","pressure":"%0.2f"}' % ( #@MicroWebSrv.route('/bme280')
bme280.temperature, bme280.humidity, bme280.pressure) #def get_bme280(wscl, wsres):
) # bme280.update_sensor()
@MicroWebSrv.route('/lm75a') # wsres.WriteResponseOk(
def get_lm75a(wscl, wsres): # headers=wshead,
wsres.WriteResponseOk( # contentType=wsctype,
headers=wshead, # content='{"temperature":"%0.2f","humidity":"%0.2f","pressure":"%0.2f"}' % (
contentType=wsctype, # bme280.temperature, bme280.humidity, bme280.pressure)
content='{"temperature":"%0.1f"}' % lm75a.read_tempC() # )
) #@MicroWebSrv.route('/lm75a')
@MicroWebSrv.route('/sht21') #def get_lm75a(wscl, wsres):
def sht21(wscl, wsres): # wsres.WriteResponseOk(
wsres.WriteResponseOk( # headers=wshead,
headers=wshead, # contentType=wsctype,
contentType=wsctype, # content='{"temperature":"%0.1f"}' % lm75a.read_tempC()
content='{"temperature":"%0.3f","humidity":"%0.3f"}' % (sht21.read_tempC(), sht21.read_hum()) # )
) #@MicroWebSrv.route('/sht21')
#def sht21(wscl, wsres):
# wsres.WriteResponseOk(
# headers=wshead,
# contentType=wsctype,
# content='{"temperature":"%0.3f","humidity":"%0.3f"}' % (sht21.read_tempC(), sht21.read_hum())
# )
#
#ws = MicroWebSrv()
#ws.Start()
ws = MicroWebSrv() #fallback microserver
ws.Start() 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 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]
wss=socket.socket()
wss.bind(wssock)
wss.listen(1)
print("Server started on 0.0.0.0:80")
#main loop
while True:
wcl, wcsock = wss.accept()
print("Client connected")
wclfh = wcl.makefile('rwb', 0)
jsn=''
while True:
wcline = wclfh.readline()
if not wcline or wcline == b'\r\n': break
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()
wcl.send(header()+jsn)
wcl.close()