slight cleanup
This commit is contained in:
parent
e083828dfa
commit
ad1e7cc95f
|
@ -1,17 +1,16 @@
|
||||||
class init_sample:
|
def __init__(self, hw):
|
||||||
def __init__(self, hw):
|
#Basic script to print module family, variant, and IP address
|
||||||
#Basic script to print module family, variant, and IP address
|
from network import WLAN as wlan, STA_IF as staif
|
||||||
from network import WLAN as wlan, STA_IF as staif
|
from time import sleep_ms
|
||||||
from time import sleep_ms
|
if hw.features.display.oled:
|
||||||
if hw.features.display.oled:
|
oled = hw.oled.handle
|
||||||
oled = hw.oled.handle
|
oled.text("uPyConfig!",0,0)
|
||||||
oled.text("hey",0,0)
|
oled.text(hw.family,0,8)
|
||||||
oled.text(hw.family,0,8)
|
oled.text(hw.variant,0,16)
|
||||||
oled.text(hw.variant,0,16)
|
oled.text(wlan(staif).ifconfig()[0],0,24)
|
||||||
oled.text(wlan(staif).ifconfig()[0],0,24)
|
oled.show()
|
||||||
oled.show()
|
else:
|
||||||
else:
|
print(hw.family)
|
||||||
print(hw.family)
|
print(hw.variant)
|
||||||
print(hw.variant)
|
print(wlan(staif).ifconfig()[0])
|
||||||
print(wlan(staif).ifconfig()[0])
|
sleep_ms(1000)
|
||||||
sleep_ms(1000)
|
|
||||||
|
|
59
main.py
59
main.py
|
@ -1,63 +1,8 @@
|
||||||
import uPyConfig
|
import uPyConfig
|
||||||
|
|
||||||
hw = uPyConfig.esp8266(variant='heltec')
|
hw = uPyConfig.esp32(variant='wemos-lolin32')
|
||||||
#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(hw)
|
||||||
|
|
||||||
# Main app
|
# Main app
|
||||||
#from uPySensor import BME280, LM75A, SHT21
|
|
||||||
#bme280=BME280(hw.i2c.bus)
|
|
||||||
#lm75a =LM75A(hw.i2c.bus)
|
|
||||||
#sht21 =SHT21(hw.i2c.bus)
|
|
||||||
from uPySensor import DS18B20
|
|
||||||
hw.owc.__init__(hw.owc, 12)
|
|
||||||
ds18b20=DS18B20(hw.owc.bus)
|
|
||||||
|
|
||||||
|
|
||||||
#fallback microserver
|
|
||||||
#commented out to use tinyWebServer instead
|
|
||||||
#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='{"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()
|
|
||||||
|
|
||||||
import tinyWebServer
|
|
||||||
ws = tinyWebServer.tinyWebServer(verbose=True)
|
|
||||||
|
|
||||||
def _get(request):
|
|
||||||
return ws.respond(
|
|
||||||
'{"error":"sensors available on /ds18b20"}',
|
|
||||||
status=404)
|
|
||||||
|
|
||||||
def _get_ds18b20(request):
|
|
||||||
return ws.respond(
|
|
||||||
'{"temperature":"%0.3f"}' % ds18b20.read_tempC()
|
|
||||||
)
|
|
||||||
|
|
||||||
ws.add_route('/', _get)
|
|
||||||
ws.add_route('/ds18b20', _get_ds18b20)
|
|
||||||
ws.start()
|
|
|
@ -1,58 +0,0 @@
|
||||||
#tinyWebServer, because microWebSrv wasn't small enough.
|
|
||||||
# no decorated route definitions, but whatever
|
|
||||||
class tinyWebServer:
|
|
||||||
import socket
|
|
||||||
_routes={}
|
|
||||||
_HTTPStatusCodes={
|
|
||||||
100: 'Continue',
|
|
||||||
101: 'Switching Protocols',
|
|
||||||
200: 'OK',
|
|
||||||
201: 'Created',
|
|
||||||
202: 'Accepted',
|
|
||||||
204: 'No Content',
|
|
||||||
301: 'Moved Permanently',
|
|
||||||
302: 'Found',
|
|
||||||
404: 'Not Found',
|
|
||||||
500: 'Not Implemented',
|
|
||||||
}
|
|
||||||
def __init__(self, bind_ip='0.0.0.0', bind_port=80, verbose=False):
|
|
||||||
self.verbose=verbose
|
|
||||||
self.bind_ip=bind_ip
|
|
||||||
self.bind_port=bind_port
|
|
||||||
self.wssock=self.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)
|
|
||||||
print("tinyWebServer started on %s:%d" % (self.bind_ip, self.bind_port))
|
|
||||||
while True: self.handle_request(*self.wss.accept())
|
|
||||||
|
|
||||||
def handle_request(self, reqclient, reqsocket):
|
|
||||||
if reqclient == None or reqsocket == None: return
|
|
||||||
if self.verbose: print(reqclient)
|
|
||||||
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 reqline.find(b' ') >= 0 and len(reqline.split(b' ')) == 3 and reqline.split(b' ')[2].startswith(b'HTTP'):
|
|
||||||
reqmethod, reqpath = reqline.split(b' ')[:-1]
|
|
||||||
if reqmethod == None or reqpath == None:
|
|
||||||
if self.verbose: print("closing attempted keepalive")
|
|
||||||
reqclient.close()
|
|
||||||
return
|
|
||||||
if self.verbose: print("client connected from %s: %s %s" % (reqsocket[0], reqmethod, reqpath))
|
|
||||||
reqclient.send(self._routes.get(reqpath, self.no_route)(reqclient))
|
|
||||||
reqclient.close()
|
|
||||||
|
|
||||||
def respond(self, body='', status=200, ctype="application/json"):
|
|
||||||
return "HTTP/1.1 %d %s\r\nContent-Type: %s\r\nConnection: close\r\nServer: tinyWebServer (horny)\r\n\r\n%s" % (
|
|
||||||
status, self._HTTPStatusCodes[status], ctype, body)
|
|
||||||
|
|
||||||
def no_route(self, request):
|
|
||||||
return self.respond(status=404)
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
#tinyWebServer, because microWebSrv wasn't small enough.
|
||||||
|
import socket
|
||||||
|
_routes={}
|
||||||
|
_HTTPStatusCodes={
|
||||||
|
100: 'Continue',
|
||||||
|
101: 'Switching Protocols',
|
||||||
|
200: 'OK',
|
||||||
|
201: 'Created',
|
||||||
|
202: 'Accepted',
|
||||||
|
204: 'No Content',
|
||||||
|
301: 'Moved Permanently',
|
||||||
|
302: 'Found',
|
||||||
|
404: 'Not Found',
|
||||||
|
500: 'Not Implemented',
|
||||||
|
}
|
||||||
|
def __init__(self, bind_ip='0.0.0.0', bind_port=80, verbose=False):
|
||||||
|
self.verbose=verbose
|
||||||
|
self.bind_ip=bind_ip
|
||||||
|
self.bind_port=bind_port
|
||||||
|
self.wssock=self.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)
|
||||||
|
print("tinyWebServer started on %s:%d" % (self.bind_ip, self.bind_port))
|
||||||
|
while True: self.handle_request(*self.wss.accept())
|
||||||
|
|
||||||
|
def handle_request(self, reqclient, reqsocket):
|
||||||
|
if reqclient == None or reqsocket == None: return
|
||||||
|
if self.verbose: print(reqclient)
|
||||||
|
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 reqline.find(b' ') >= 0 and len(reqline.split(b' ')) == 3 and reqline.split(b' ')[2].startswith(b'HTTP'):
|
||||||
|
reqmethod, reqpath = reqline.split(b' ')[:-1]
|
||||||
|
if reqmethod == None or reqpath == None:
|
||||||
|
if self.verbose: print("closing attempted keepalive")
|
||||||
|
reqclient.close()
|
||||||
|
return
|
||||||
|
if self.verbose: print("client connected from %s: %s %s" % (reqsocket[0], reqmethod, reqpath))
|
||||||
|
reqclient.send(self._routes.get(reqpath, self.no_route)(reqclient))
|
||||||
|
reqclient.close()
|
||||||
|
|
||||||
|
def respond(self, body='', status=200, ctype="application/json"):
|
||||||
|
return "HTTP/1.1 %d %s\r\nContent-Type: %s\r\nConnection: close\r\nServer: tinyWebServer (horny)\r\n\r\n%s" % (
|
||||||
|
status, self._HTTPStatusCodes[status], ctype, body)
|
||||||
|
|
||||||
|
def no_route(self, request):
|
||||||
|
return self.respond(status=404)
|
Loading…
Reference in New Issue