Ben Boardwalk Project finished phase1
THis started to fail when new if--elif options added. REcast as simpler version. Don't use this version. Use next one called minresponse3.py. See above...
Ben Boardwalk Project more or less finished phase1. This is temperature and logging data is captured by PC HTML client form from Pi PicoW web server that's doing the logging and the sensing. Early days but structures seem fine and micropython code is relatively organised.
Here is pic of a client session. Just asked for logging data which goes into file dialog query.
The main file is the micropython file bensense12.py. It establishes the AP network, called NAME and awaits query from chrome html form called bensensor5.html. Mostly buttons that cause a cascade of if,elif statements to sort through all the options and return with information that gets printed on the form, like temperature or, new work for me, gets the browser to save a log file, not print it, to via a normal Windows dialog.
Here's the main program written in micropython using Thonny:
#Sun Sep 1 11:46:42 NZST 2024. Going to write route finder for all the paths from HTML form
#Mon Sep 2 12:20:06 NZST 2024. Tidied up all loose comments. Now ready for sensors.
#trying tidyup Tue Sep 10 11:06:12 NZST 2024
#Wed Sep 11 11:30:24 NZST 2024. INternal temp seems to be working with html form. ext temp stub done.
import network
import time
import socket
from machine import Pin
import parse_request1 #added this, based on perplexity
import parreqlite1
import do_ledon, do_ledoff, do_inttemp,do_exttemp, do_text, do_csv,do_other
led = machine.Pin("LED", machine.Pin.OUT)
led.on()
# adcpin = 4
# sensor = machine.ADC(adcpin) #for internal temp
# ts=type(sensor)
# print(ts)
def ap_mode(ssid, password):
file = open("bensensor5.html") #initialize html
html2 = file.read()
file.close()
stateis ='Starting up'
ap = network.WLAN(network.AP_IF)
ap.config(essid=ssid, password=password)
ap.active(True)
while ap.active() == False:
pass
print('AP Mode Is Active, You can Now Connect')
print('IP Address To Connect to:: ' + ap.ifconfig()[0])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #creating socket object
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #promising fix?
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
if len(request) > 1: #0:
meth,pa,prot=parreqlite1.parse_request1(request)
print("Pa0 =....",pa)
#print("Path0 =....",path)
path = pa # Parse query parameters
query_params = {}
if '?' in path:
path, query_string = path.split('?', 1)
params = query_string.split('&')
for param in params:
if '=' in param:
key, value = param.split('=')
query_params[key] = value
#x=query_params["sensor"]
#print("queryp isss..and path is, and query_params[sensor]",query_params, path,x)
print("Path =....",path)
print (query_params)
if path == '/ledon': #new
x,st=do_ledon.do_ledon()
led.value(1)
html2=st
elif path ==('/ledoff'):
x,st=do_ledoff.do_ledoff()
led.value(0)
html2=st
elif path == '/sensors':
k=query_params['sensor']
print ("k for query_params is ...",k)
if k == "External+temperature":
print("Got external temp ,...")
st = do_exttemp.do_exttemp()
html2 = st
elif k == "Internal+temperature":
print("Got internal temp xxxxx ,...")
st = do_inttemp.do_inttemp()
html2 = st
elif k == "SENSOR1":
print("Got sensor1 value ,...")
elif path == '/files':
k=query_params["file"]
if k== "log":
print("Got log...")
st = do_text.do_text()
html2 =st
elif k== "csv":
print("Got csv..." )
st = do_csv.do_csv()
html2 = st
elif k== "other":
print("Got other file..." )
st=do_other.do_other()
html2 =st
else:
print("Query problem..., path, len(request)", path, len(request))
response=html2 #new
conn.send(response)
conn.close()
ap_mode('NAME',
'PASSWORD')
#todo put -->html = html.replace('**ledState**', led_state_text) before line 79
Comments
Post a Comment