prjbrook

minresponse.py for nensensor() network with PI Pico

Sep 17th, 2024
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. minresponse3.py
  2.  
  3. #min network web AP server from perplexity.
  4. import network
  5. import socket
  6. import machine
  7.  
  8. import do_csv, do_log
  9. adcpin = 4
  10. sensor = machine.ADC(adcpin)
  11.  
  12. # Set up access point
  13. ap = network.WLAN(network.AP_IF)
  14. te =""
  15. templ = "template1.html"   #update when form changes
  16. led = machine.Pin("LED", machine.Pin.OUT)
  17. led.on()
  18.  
  19. ap.config(essid='NAME', password='PASSWORD')
  20. ap.active(True)
  21.  
  22. while not ap.isconnected():
  23.     pass
  24.  
  25. print('AP Mode Is Active, You can Now Connect')
  26. print('IP Address To Connect to::', ap.ifconfig()[0])
  27.  
  28. # Set up socket
  29. addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
  30. s = socket.socket()
  31. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)   #promising fix?
  32.  
  33. s.bind(addr)
  34. s.listen(1)
  35.  
  36. print('Listening on', addr)
  37.  
  38. while True:
  39.     cl, addr = s.accept()
  40.     print('Got a connection from', addr)
  41.     request = cl.recv(1024)
  42.     request_str = request.decode('utf-8')
  43.     lines = request_str.split('\r\n')
  44.     if lines==['']:
  45.         print("lines have only one value")
  46.     else:
  47.         method, path, protocol = lines[0].split(' ')
  48.         print('The method,path,protocol= ',method,path,protocol)
  49.     if path=='/':
  50.         print("Very short path",path)
  51.     try:
  52.         with open(templ, 'r') as file:  #('simpleled2.html', 'r')
  53.             te = file.read()
  54.     except OSError:
  55.         te = "Error: template0.html file not found."
  56.        
  57.     response = """\
  58. HTTP/1.1 200 OK
  59. Content-Type: text/html
  60. Content-Length: {}
  61.  
  62. {}
  63.  
  64. """.format(len(te), te)  #(len(x), x)
  65.    
  66. #-------------------do tasks based in path params-------------
  67.     if "toggle" in path:
  68.         led.toggle()
  69.     if "exttemp" in path:
  70.         response = response.replace("***", "123456789")
  71.     if "inttemp" in path:
  72.         volt = (3.3/65535) * sensor.read_u16()
  73.         temperature = 27 - (volt - 0.706)/0.001721
  74.         response = response.replace("***", str(round(temperature,1)))
  75.     if "csv" in path:
  76.         print("csv in path ...")
  77.         response = do_csv.do_csv()
  78.     if "log" in path:
  79.         print("log in path ...")
  80.         response = do_log.do_log()
  81.    
  82.     cl.send(response)
  83.     cl.close()
  84. ================================next, do_csv.py========
  85. #do_text.py
  86.  
  87. def do_csv():
  88.     # Attempt to read the my-log.txt file
  89.     try:
  90.         with open('log.csv', 'r') as file:  #('simpleled2.html', 'r')
  91.             log_data = file.read()
  92.     except OSError:
  93.         log_data = "Error: my-log.txt file not found."
  94.        
  95.     html3 = """\
  96. HTTP/1.1 200 OK
  97. Content-Type: text/csv
  98. Content-Disposition: attachment; filename="log.csv"
  99. Content-Length: {}
  100.  
  101. {}
  102.  
  103. """.format(len(log_data), log_data)  #(len(x), x)
  104.  
  105.    
  106.    
  107.     return html3
  108.  ================================next, do_log.py==========
  109.  
  110. #do_text.py
  111.  
  112. def do_log():
  113.     # Attempt to read the my-log.txt file
  114.     try:
  115.         with open('my-log.txt', 'r') as file:  #('simpleled2.html', 'r')
  116.             log_data = file.read()
  117.     except OSError:
  118.         log_data = "Error: my-log.txt file not found."
  119.        
  120.     html3 = """\
  121. HTTP/1.1 200 OK
  122. Content-Type: text/plain
  123. Content-Disposition: attachment; filename="my-log.txt"
  124. Content-Length: {}
  125.  
  126. {}
  127.  
  128. """.format(len(log_data), log_data)  #(len(x), x)
  129.  
  130.    
  131.    
  132.     return html3
  133.  
  134. #     response = """\
  135. # HTTP/1.1 200 OK
  136. # Content-Type: text/csv
  137. # Content-Disposition: attachment; filename="log.csv"
  138. # Content-Length: {}
  139. #
  140. # {}
  141. #
  142. # """.format(len(log_data), log_data)
  143.  
  144. =============next template1.html============
  145.  
  146. <html>
  147. <body>
  148.  
  149. <h2>Pi PicoW tasks</h2>
  150.  
  151. <p>Pick a task from the list</p>
  152.  
  153. <form action="/choice">
  154.   <label for="tasks">Choose a task:</label>
  155.   <select id="tasks" name="tasks" size="6">
  156.     <option value="toggle">Toggle</option>
  157.     <option value="exttemp">Exttemp</option>
  158.     <option value="inttemp">Inttemp</option>
  159.     <option value="csv">Csv</option>
  160.     <option value="log">Log</option>
  161.     <option value="other">Other</option>
  162.   </select><br><br>
  163.   <input type="submit">
  164. </form>
  165. <p> Information : *** </p>
  166.  
  167. </body>
  168. </html>
  169.  
  170.  
  171.  
Advertisement
Add Comment
Please, Sign In to add comment