text stringlengths 1 93.6k |
|---|
n += len(d)
|
# Send 'not found' error to client
|
def put_http_404(self):
|
self.send_data(self.client_sock, NOT_FOUND)
|
self.send_end(self.client_sock)
|
# Send text response to client
|
def put_http_text(self, text):
|
resp = HTTP_OK + CONTENT_LEN%len(text)
|
resp += CONTENT_TYPE%"text/html" + HEAD_END + text
|
self.put_data(resp)
|
self.send_end(self.client_sock)
|
self.txcount += 1
|
# Send file from filesystem to client
|
def put_http_file(self, fname, content="text/html; charset=utf-8", hdr=""):
|
try:
|
f = open(fname)
|
except:
|
f = None
|
if not f:
|
self.put_http_404()
|
else:
|
flen = os.stat(fname)[6]
|
resp = HTTP_OK + CONTENT_LEN%flen + CONTENT_TYPE%content + hdr + HEAD_END
|
self.send_data(self.client_sock, resp)
|
n = 0
|
while n < flen:
|
data = f.read(MAX_SPI_DLEN)
|
self.send_data(self.client_sock, data)
|
n += len(data)
|
self.send_end(self.client_sock)
|
# Initialise TCP server, return class instance
|
def server_init():
|
print("Connecting to %s..." % WIFI_SSID)
|
e = ESP32()
|
status = 0
|
while status != STATUS_OK:
|
if status==0 or status==STATUS_FAIL:
|
e.connect(WIFI_SSID, WIFI_PASS)
|
time.sleep(0.2)
|
status = e.check_wifi_status()
|
e.start_server(HTTP_PORT)
|
while not e.get_server_status():
|
pass
|
ip_addr = e.get_ip_address()
|
print("Server socket {0}, {2}.{3}.{4}.{5}:{1}".format(e.server_sock.socknum, HTTP_PORT, *ip_addr))
|
return e
|
if __name__ == "__main__":
|
esp = server_init()
|
adcs = [AnalogIn(pin) for pin in ADC_PINS]
|
while True:
|
req = esp.get_http_request()
|
if req:
|
r = req.split("\r")[0]
|
print(r, end="")
|
if ICON_FNAME in r:
|
print(" [not found]")
|
esp.put_http_404()
|
elif TEST_FNAME in r:
|
print(" [test page]")
|
heads = [str(pin)[-2:] for pin in ADC_PINS]
|
vals = [("%1.3f" % (adc.value * ADC_SCALE)) for adc in adcs]
|
th = "<tr><th>" + "</th><th>".join(heads) + "</th></tr>"
|
tr = "<tr><td>" + "</td><td>".join(vals) + "</td></tr>"
|
table = "<table><caption>ADC voltages</caption>%s</table>" % (th+tr)
|
esp.put_http_text(TEST_PAGE % table)
|
elif DATA_FNAME in r:
|
print(" [data/csv]")
|
esp.put_http_file(DIRECTORY+DATA_FNAME, "text/csv", DISABLE_CACHE)
|
else:
|
print(" [index]")
|
esp.put_http_file(DIRECTORY+INDEX_FNAME)
|
time.sleep(0.01)
|
#EOF
|
# <FILESEP>
|
# coding=utf-8
|
import os
|
import sys
|
import webbrowser
|
from platform import system
|
from traceback import print_exc
|
from typing import Any
|
from typing import Callable
|
from typing import List
|
from typing import Tuple
|
def clear_screen():
|
if system() == "Linux":
|
os.system("clear")
|
if system() == "Windows":
|
os.system("cls")
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.