bensense boardwalk program with pi pico > better version. Works

 Pi pico boardwalk sensor using PicoW with AP network. Seems to work. Better version than bensensor() ones.

Here's screen shot when all working:


Below is a list of all the files copied into notepad.Also put into pastebin https://pastebin.com/itps554S

minresponse3.py


#min network web AP server from perplexity.
import network
import socket
import machine
import do_csv, do_log
adcpin = 4
sensor = machine.ADC(adcpin)
# Set up access point
ap = network.WLAN(network.AP_IF)
te =""
templ = "template1.html"   #update when form changes
led = machine.Pin("LED", machine.Pin.OUT)
led.on()
ap.config(essid='NAME', password='PASSWORD')
ap.active(True)
while not ap.isconnected():
    pass
print('AP Mode Is Active, You can Now Connect')
print('IP Address To Connect to::', ap.ifconfig()[0])
# Set up socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)   #promising fix?
s.bind(addr)
s.listen(1)
print('Listening on', addr)
while True:
    cl, addr = s.accept()
    print('Got a connection from', addr)
    request = cl.recv(1024)
    request_str = request.decode('utf-8')
    lines = request_str.split('\r\n')
    if lines==['']:
        print("lines have only one value")
    else:
        method, path, protocol = lines[0].split(' ')
        print('The method,path,protocol= ',method,path,protocol)
    if path=='/':
        print("Very short path",path)
    try:
        with open(templ, 'r') as file:  #('simpleled2.html', 'r')
            te = file.read()
    except OSError:
        te = "Error: template0.html file not found."
       
    response = """\
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: {}
{}
""".format(len(te), te)  #(len(x), x)
    
#-------------------do tasks based in path params-------------
    if "toggle" in path:
        led.toggle()
    if "exttemp" in path:
        response = response.replace("***", "123456789")
    if "inttemp" in path:
        volt = (3.3/65535) * sensor.read_u16()
        temperature = 27 - (volt - 0.706)/0.001721
        response = response.replace("***", str(round(temperature,1)))
    if "csv" in path:
        print("csv in path ...")
        response = do_csv.do_csv()
    if "log" in path:
        print("log in path ...")
        response = do_log.do_log()
    
    cl.send(response)
    cl.close()
================================next, do_csv.py========
#do_text.py
def do_csv():
    # Attempt to read the my-log.txt file
    try:
        with open('log.csv', 'r') as file:  #('simpleled2.html', 'r')
            log_data = file.read()
    except OSError:
        log_data = "Error: my-log.txt file not found."
        
    html3 = """\
HTTP/1.1 200 OK
Content-Type: text/csv
Content-Disposition: attachment; filename="log.csv"
Content-Length: {}
{}
""".format(len(log_data), log_data)  #(len(x), x)
    
   
    return html3
 ================================next, do_log.py==========
 
#do_text.py
def do_log():
    # Attempt to read the my-log.txt file
    try:
        with open('my-log.txt', 'r') as file:  #('simpleled2.html', 'r')
            log_data = file.read()
    except OSError:
        log_data = "Error: my-log.txt file not found."
        
    html3 = """\
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Disposition: attachment; filename="my-log.txt"
Content-Length: {}
{}
""".format(len(log_data), log_data)  #(len(x), x)
    
   
    return html3
  
#     response = """\
# HTTP/1.1 200 OK
# Content-Type: text/csv
# Content-Disposition: attachment; filename="log.csv"
# Content-Length: {}
# 
# {}
# 
# """.format(len(log_data), log_data)
=============next template1.html============
<html>
<body>
<h2>Pi PicoW tasks</h2>
<p>Pick a task from the list</p>
<form action="/choice">
  <label for="tasks">Choose a task:</label>
  <select id="tasks" name="tasks" size="6">
    <option value="toggle">Toggle</option>
    <option value="exttemp">Exttemp</option>
    <option value="inttemp">Inttemp</option>
    <option value="csv">Csv</option>
    <option value="log">Log</option>
    <option value="other">Other</option>
  </select><br><br>
  <input type="submit">
</form>
<p> Information : *** </p>
</body>
</html>

Next: schematic for pi pico w with above code on breadboard with SD card and Oled display. Get board made via EasyEDA.




Comments

Popular posts from this blog

RTC with ESP32

ESP32 buttons and bouncing

LittleFS