From ad1e7cc95f14568898c788fe87886b0a84d12287 Mon Sep 17 00:00:00 2001 From: Matthew Connelly Date: Mon, 11 Jun 2018 15:32:17 +0100 Subject: [PATCH] slight cleanup --- init_sample.py | 33 +++++++++++++-------------- main.py | 59 ++---------------------------------------------- tinyWebServer.py | 58 ----------------------------------------------- uPyhttpd.py | 56 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 132 deletions(-) delete mode 100644 tinyWebServer.py create mode 100644 uPyhttpd.py diff --git a/init_sample.py b/init_sample.py index 941af45..0401665 100644 --- a/init_sample.py +++ b/init_sample.py @@ -1,17 +1,16 @@ -class init_sample: - def __init__(self, hw): - #Basic script to print module family, variant, and IP address - from network import WLAN as wlan, STA_IF as staif - from time import sleep_ms - if hw.features.display.oled: - oled = hw.oled.handle - oled.text("hey",0,0) - oled.text(hw.family,0,8) - oled.text(hw.variant,0,16) - oled.text(wlan(staif).ifconfig()[0],0,24) - oled.show() - else: - print(hw.family) - print(hw.variant) - print(wlan(staif).ifconfig()[0]) - sleep_ms(1000) +def __init__(self, hw): + #Basic script to print module family, variant, and IP address + from network import WLAN as wlan, STA_IF as staif + from time import sleep_ms + if hw.features.display.oled: + oled = hw.oled.handle + oled.text("uPyConfig!",0,0) + oled.text(hw.family,0,8) + oled.text(hw.variant,0,16) + oled.text(wlan(staif).ifconfig()[0],0,24) + oled.show() + else: + print(hw.family) + print(hw.variant) + print(wlan(staif).ifconfig()[0]) + sleep_ms(1000) diff --git a/main.py b/main.py index f37b5af..7fefcdc 100644 --- a/main.py +++ b/main.py @@ -1,63 +1,8 @@ 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) import init_sample -init_sample.init_sample(hw) +init_sample(hw) # 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() \ No newline at end of file diff --git a/tinyWebServer.py b/tinyWebServer.py deleted file mode 100644 index 342ce15..0000000 --- a/tinyWebServer.py +++ /dev/null @@ -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) diff --git a/uPyhttpd.py b/uPyhttpd.py new file mode 100644 index 0000000..c74bacd --- /dev/null +++ b/uPyhttpd.py @@ -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)