Pi Pico AP network with Python

 Perplexity, AI app, created the following program:

----------------------------------------------

import network

import socket


# Set up access point

ap = network.WLAN(network.AP_IF)

ap.active(True)

ap.config(essid='PicoW_AP')


# Wait for connection

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

    print('Content received by Pico =', request)


    # Send a proper HTTP response

    response = """\

HTTP/1.1 200 OK

Content-Type: text/html

Content-Length: 44


<html><body><h1>Hello, World!</h1></body></html>

"""

    cl.send(response)

    cl.close()

----------------------------------
This was downloaded to PicoW via Thonny. Browser st for http://192.168.4.1/ can get the hoell world program, and now with the above the Python code below from the PC can get the htmle response too. Before the headers and msg preceeding the html code were missed out and Chrom understood the request but to the python request line below:
--------------------
import requests


response = requests.get('http://192.168.4.1')
#response.raise_for_status()
print(response.text)
-----------------------------
I got in IDLE:
--------------
Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.

= RESTART: C:/Users/Dell/AppData/Local/Programs/Python/Python310/myfiles/request2.py
<html><body><h1>Hello, World!</h1></body></h
---------------------------------------------
Note had to have network settings pointed to the internal NAME AP network. (Not sure why this NAME network persists. It came from previous micopython Pico program.)

Next steps: Get pico server to send CSV file when requested. 

Comments

Popular posts from this blog

Reading LittleFS file into buffer for sending

ESP32 buttons and bouncing