Getting better at html forms for Ben wbserver

 bensens2.py with associated bensensor3.html good start for coming BEN network.

Took bits from kevsrobots cyberdog and bits from w3schools to give interaction below:



bensense2.py is:

#Worked ok Tue Aug  6 11:04:01 NZST 2024. Can't get other mode to work  Will play with this one.
import network
import time
import socket
from machine import Pin
# led = Pin(25, Pin.OUT)
# led.off()
# led.on()

led = machine.Pin("LED", machine.Pin.OUT)
#led.off()
led.on()
# def web_page():
#   html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1"></head>
#             <body><h1>Hello 33w21World</h1></body></html>
#          """
#   return html
#todo make this HTML into separate file
# if you do not see the network you may have to power cycle
# unplug your pico w for 10 seconds and plug it in again
def ap_mode(ssid, password):
    """
        Description: This is a function to activate AP mode
        Parameters:
        ssid[str]: The name of your internet connection
        password[str]: Password for your internet connection
        Returns: Nada
    """
    
#     file = open("simpleled.html")
#     html = file.read()
#     file.close()
    
    # Just making our internet connection
    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.bind(('', 80))
    s.listen(5)
    while True:
      conn, addr = s.accept()
      print('Got a connection from %s' % str(addr))
      request = conn.recv(1024)
      print('Content received by Pico = %s' % str(request))
      #response = web_page()
      #new---
      #print("RRRRR" + request.method)
      request = str(request)
      
      #led_on = request.find('/light/on')
      #led_off = request.find('/light/off')
      led_on = request.find(' /ledon')  #the space is important
      led_off = request.find(' /ledoff')
      print( 'led on = ' + str(led_on))
      print( 'led off = ' + str(led_off))
      #---finish new---got -1 for off and 308 for on--
      #new new--
#todo sort out indenting. Better to do  != -1 
      #if led_on > 0:
      if led_on != -1:
            print("led on")
            led.value(1)
            stateis = "LED is ON"
      if led_off != -1:
        print("led off")
        led.value(0)
        stateis = "LED is OFF"
        
      file = open("bensensor3.html")
      #file = open("index.html")
      html = file.read()
      file.close()
      html2 = html.replace('**ledState**', stateis)
      
      print(stateis)
      response=html2 #new
      conn.send(response)
      conn.close()
      #s.close() #Try this !!
ap_mode('NAME',
        'PASSWORD')
#todo put -->html = html.replace('**ledState**', led_state_text) before line 79
---------------------------------
bensensor3.html is this below:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ben Sensor</title>
</head>
<body>
    <h1>bensensor0</h1>
    Led status is **ledState**
    {{text}}

    <p>Choose an LED pattern:</p>
    <form action="/" method="POST">
        <label>Enter the text to scroll</label>
        <input type="text" name="text" />
        <button type="submit" value="Login">Submit</button>
    </form>
    
    <form action="/action_page.php">
  <p>Please select your favorite Web language:</p>
  <input type="radio" id="html" name="fav_language" value="HTML">
  <label for="html">HTML</label><br>
  <input type="radio" id="css" name="fav_language" value="CSS">
  <label for="css">CSS</label><br>
  <input type="radio" id="javascript" name="fav_language" value="JavaScript">
  <label for="javascript">JavaScript</label>

  <br>  

  <p>Please select your age:</p>
  <input type="radio" id="age1" name="age" value="30">
  <label for="age1">0 - 30</label><br>
  <input type="radio" id="age2" name="age" value="60">
  <label for="age2">31 - 60</label><br>  
  <input type="radio" id="age3" name="age" value="100">
  <label for="age3">61 - 100</label><br><br>
  <input type="submit" value="Submit">
</form>
    <p>Click the buttons to control the LED</p>
<input type="button" onclick="document.location='/ledon'" value="TURN LED ON1"/>
<p></p>
<input type="button" onclick="document.location='/ledoff'" value="TURN LED OFF"/>
<p></p>
<p>The LED is **ledState**</p>
<p>The internal Pico temperature is **TEMP**</p>
</body>
</html>
-------------------now look at what pico received, new POST stuff. It's all there.
led off
LED is OFF
Got a connection from ('192.168.4.16', 55085)
Content received by Pico = b'GET /ledon HTTP/1.1\r\nHost: 192.168.4.1\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\r\nReferer: http://192.168.4.1/ledoff\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.9,en-NZ;q=0.8\r\nsec-gpc: 1\r\n\r\n'
led on = 5
led off = -1
led on
LED is ON
Got a connection from ('192.168.4.16', 55086)
Content received by Pico = b'GET /action_page.php?fav_language=HTML&age=30 HTTP/1.1\r\nHost: 192.168.4.1\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\r\nReferer: http://192.168.4.1/ledon\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.9,en-NZ;q=0.8\r\nsec-gpc: 1\r\n\r\n'
led on = -1
led off = -1
LED is ON
Got a connection from ('192.168.4.16', 55087)
Content received by Pico = b'GET /ledoff HTTP/1.1\r\nHost: 192.168.4.1\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\r\nReferer: http://192.168.4.1/action_page.php?fav_language=HTML&age=30\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.9,en-NZ;q=0.8\r\nsec-gpc: 1\r\n\r\n'
led on = -1
led off = 5
led off
LED is OFF
Got a connection from ('192.168.4.16', 55764)
Content received by Pico = b'GET /ledon HTTP/1.1\r\nHost: 192.168.4.1\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\r\nReferer: http://192.168.4.1/ledoff\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.9,en-NZ;q=0.8\r\nsec-gpc: 1\r\n\r\n'
led on = 5
led off = -1
led on
LED is ON
Got a connection from ('192.168.4.16', 55765)
Content received by Pico = b'GET /action_page.php? HTTP/1.1\r\nHost: 192.168.4.1\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\r\nReferer: http://192.168.4.1/ledon\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.9,en-NZ;q=0.8\r\nsec-gpc: 1\r\n\r\n'
led on = -1
led off = -1
LED is ON
Got a connection from ('192.168.4.16', 55766)
Content received by Pico = b'GET /action_page.php?fav_language=JavaScript&age=100 HTTP/1.1\r\nHost: 192.168.4.1\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\r\nReferer: http://192.168.4.1/action_page.php?\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.9,en-NZ;q=0.8\r\nsec-gpc: 1\r\n\r\n'
led on = -1
led off = -1
LED is ON
Got a connection from ('192.168.4.16', 55767)
Content received by Pico = b'GET /ledoff HTTP/1.1\r\nHost: 192.168.4.1\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\r\nReferer: http://192.168.4.1/action_page.php?fav_language=JavaScript&age=100\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.9,en-NZ;q=0.8\r\nsec-gpc: 1\r\n\r\n'
led on = -1
led off = 5
led off
LED is OFF
Got a connection from ('192.168.4.16', 55768)
Content received by Pico = b''
led on = -1
led off = -1
LED is OFF

NB Got GET /action_page.php? HTTP/1.1 when no buttons pushed. 
Got    Content received by Pico = b'GET /action_page.php?fav_language=JavaScript&age=100 when two radio buttons pushed in bensensor3.html 

Next 1) change names of buttons, choices etc
2) Parse response lines so server route software can choose what to do depending on elements in form being activated.
Pleased with progress.

Comments

Popular posts from this blog

Reading LittleFS file into buffer for sending

ESP32 buttons and bouncing