Disappointing distance for AP network

 Don't think I can reach the boardwalk garden about 20m away with AP wifi. Went outside with iPhone and pressed html buttons in Chrome from code below. Works ok down the stairs but not where I park car. So I'm going to journal what I tried below and then go onto other options..


Code used:

#Got this from https://www.perplexity.ai/search/i-m-using-the-python-requests-X_2r2DgcS1OF1_mnigU7qg
#Idea is to send a text file generated by pico server. It logs x.log and produces reports x.txt.
import network
import socket
num = 4956
# Set up access point
ap = network.WLAN(network.AP_IF)
ap.active(False)  # Deactivate first
#ap.config(essid=ssid, password=password)
ap.config(essid='NAME',password='PASSWORD')
print("Configured SSID:", ap.config('essid'))
ap.active(True)

#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)
    
    # Attempt to read the my-log.txt file
    try:
        with open('simpleled2.html', 'r') as file:     #with open('my-log.txt', 'r') as file:
            log_data = file.read()
    except OSError:
        log_data = "Error: my-log.txt file not found."
     
    #log_data = "Now is the time for all.."
    # Send a proper HTTP response with the text data. After content-type-->Content-Disposition: attachment; filename="my-log.txt"
    #..and Content-Type: text/plain
    num=num+1
    nums=str(num)
    #**TEMP** txt = "I like bananas"  x = txt.replace("bananas", "apples")print(x)
    x = log_data.replace("**TEMP**",nums)




    response = """\
HTTP/1.1 200 OK
Content-Type: text/html

Content-Length: {}

{}

""".format(len(x), x)  #len(log_data), log_data

    cl.send(response)
    cl.close()
---------------------------
Plug in Pi PicoW. Thonny app sees all the files inside it. Compile and run the above micropython file called PerplexText4.py. Came from previous versions that used different html files or none in case of sending my-log.txt file. 
Here's the used HTML file,simpleled2.html. Below:

<!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 ON"/>
<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>
-------
It put two buttons on Chrome, once logged into NAME, Password network, that clicked by finger on IPhone screen, sent request to PicoW with appropriate path eg /ledon. This was ignored but a random number was increment and displayed on iPhone screen where the temperature should have been. This was done in th e.py code using 'replace'. All worked until iPhone more than about 10m away. No good for logging. 
Options: esp32 with ESP-Now. Better distance. Been done but my previous version was a bit clumsy and I didn't like the Arduino C++ environment much. 
Going to pursue another option called Captive AP Portal with micropython on picoW. Uses the phew library and looks like what I'd want. Only trouble is it would have to be used with an iPhone being walked around near sensors. No big problem as I've done that. Just not sire how an iPhone would capture files from logged info on outside picow. Could always get cheap Android phone, I suppose. Or build a pico receiver box.  


Comments

Popular posts from this blog

Reading LittleFS file into buffer for sending

ESP32 buttons and bouncing