Micropython requests via Thonny

 Pi Pico sets up local AP network and sends some led buttons yo browser request on 192.168.4.1

Then clicking the buttons causes Pico to receive extra information of URL like /ledon and /ledoff so that the pico can interrogate this response code and change leds or do anything else. Would really like this to initiate an sending of a data-logging file in text that sends lots of sensor data back to browser to be handled by other software like Excel.




#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


file = open("simpleled.html")

html = file.read()

file.close()

def web_page():

  html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1"></head>

            <body><h1>Hello 33v21World</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

    """

    # Just making our internet connection

    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()

      response=html #new

      conn.send(response)

      conn.close()


ap_mode('NAME',

        'PASSWORD')

----------------------------------end of Thonny program--------------------WiFiAP2.py----------

Now comes the HTML used above....


<!DOCTYPE html>

<html>

<head>

    <title>Raspberry Pi Pico Simple LED</title>

</head>

<body>

<p>Click the buttons to control the LED</p>

<input type="button" onclick="document.location='/ledon'" value="TURN LED ONnnnn"/>

<p></p>

<input type="button" onclick="document.location='/ledoff'" value="TURN LED OFF"/>

<p></p>

<p>The LED is **ledState**</p>

</body>

</html>

----------------------------Now comes the responses that the pic got from PC browser, Chrome. Buttons were pressed.

MPY: soft reboot
AP Mode Is Active, You can Now Connect
IP Address To Connect to:: 192.168.4.1
Got a connection from ('192.168.4.16', 65223)
Content received by Pico = b'GET /ledoff HTTP/1.1\r\nHost: 192.168.4.1\r\nConnection: keep-alive\r\nCache-Control: max-age=0\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.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'
Got a connection from ('192.168.4.16', 65224)
Content received by Pico = b'GET /favicon.ico HTTP/1.1\r\nHost: 192.168.4.1\r\nConnection: keep-alive\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36\r\nAccept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8\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'
Got a connection from ('192.168.4.16', 65225)
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/127.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'
Got a connection from ('192.168.4.16', 65226)
Content received by Pico = b'GET /favicon.ico HTTP/1.1\r\nHost: 192.168.4.1\r\nConnection: keep-alive\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36\r\nAccept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8\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'
Got a connection from ('192.168.4.16', 65227)
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/127.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'
Got a connection from ('192.168.4.16', 65228)
Content received by Pico = b'GET /favicon.ico HTTP/1.1\r\nHost: 192.168.4.1\r\nConnection: keep-alive\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36\r\nAccept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8\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'

Comments

Popular posts from this blog

Reading LittleFS file into buffer for sending

ESP32 buttons and bouncing