uPyLibs/main.py

64 lines
1.8 KiB
Python

import uPyConfig
hw = uPyConfig.esp8266(variant='d1-r2')
#print family, variant and IP address (using oled, if available on-board)
#import init_sample
#init_sample.init_sample(hw)
# Main app
import uPySensor
sensors={
'bme280': uPySensor.BME280(hw.i2c.bus),
'lm75a': uPySensor.LM75A(hw.i2c.bus),
'sht21': uPySensor.SHT21(hw.i2c.bus),
}
from microWebSrv import MicroWebSrv
ws = MicroWebSrv()
ws.WebSocketThreaded = False
wshead={
'Server':'horny',
}
wsctype='application/json'
wscharset='UTF-8'
@MicroWebSrv.route('/')
def get_root(wscl, wsres):
wsres.WriteResponseOk(
headers=wshead,
contentType=wsctype,
contentCharset=wscharset,
content='{"result":"error","message":"use /bme280, /lm75a or /sht21 for sensor readings"}'
)
@MicroWebSrv.route('/bme280')
def get_bme280(wscl, wsres):
sensors['bme280'].update_sensor()
json = '{"temperature":"%0.2f","humidity":"%0.2f","pressure":"%0.2f"}' % (
sensors['bme280'].temperature, sensors['bme280'].humidity, sensors['bme280'].pressure)
wsres.WriteResponseOk(
headers=wshead,
contentType=wsctype,
contentCharset=wscharset,
content=None
)
@MicroWebSrv.route('/lm75a')
def get_lm75a(wscl, wsres):
json = '{"temperature":"%0.1f"}' % sensors['lm75a'].read_tempC()
wsres.WriteResponseOk(
headers=wshead,
contentType=wsctype,
contentCharset=wscharset,
content=json
)
@MicroWebSrv.route('/sht21')
def sht21(wscl, wsres):
json = '{"temperature":"%0.3f","humidity":"%0.3f"}' % (sensors['sht21'].read_tempC(), sensors['sht21'].read_hum())
wsres.WriteResponseOk(
headers=wshead,
contentType=wsctype,
contentCharset=wscharset,
content=json
)
ws.Start(threaded=False, stackSize=8192)