uPyLibs/main.py

54 lines
1.5 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
wshead={
'Server':'horny',
}
wsctype='application/json'
wscharset='UTF-8'
import uPySensor
bme280=uPySensor.BME280(hw.i2c.bus)
lm75a =uPySensor.LM75A(hw.i2c.bus)
sht21 =uPySensor.SHT21(hw.i2c.bus)
from microWebSrv import MicroWebSrv
@MicroWebSrv.route('/')
def get_root(wscl, wsres):
wsres.WriteResponseOk(
headers=wshead,
contentType=wsctype,
content='{"result":"error","message":"use /bme280, /lm75a or /sht21 for sensor readings"}'
)
@MicroWebSrv.route('/bme280')
def get_bme280(wscl, wsres):
bme280.update_sensor()
wsres.WriteResponseOk(
headers=wshead,
contentType=wsctype,
content='{"temperature":"%0.2f","humidity":"%0.2f","pressure":"%0.2f"}' % (
bme280.temperature, bme280.humidity, bme280.pressure)
)
@MicroWebSrv.route('/lm75a')
def get_lm75a(wscl, wsres):
wsres.WriteResponseOk(
headers=wshead,
contentType=wsctype,
content='{"temperature":"%0.1f"}' % lm75a.read_tempC()
)
@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()