Presto wifi

I’ve just had my first go at using the Presto and don’t seem to be able to connect to wifi.
I created the “secrets.py” file and filled it with:

WIFI_SSID = "<MY_SSID>"
WIFI_PASSWORD = "<MY_PASSWORD>"

Then used:

from presto import Presto
presto = Presto()
wifi = presto.connect()
print(wifi)

Can’t see where this could be going wrong looking at the available examples; is wifi functionality ready to be used yet?

I got my PRESTO working like this:

# Test WIFI network connection
# Tony Goodhew 27 Dec 2024
import time
import network
import rp2
import requests
rp2.country("GB")
from secrets import WIFI_SSID, WIFI_PASSWORD

wlan = network.WLAN(network.STA_IF)
wlan.active(True)

wlan.connect(WIFI_SSID, WIFI_PASSWORD)
max_wait = 30
while max_wait > 0:
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    max_wait -= 1
    print("Waiting for Wi-Fi connection...")
    time.sleep(1)

if wlan.status() != 3:
    raise RuntimeError("Network connection failed")
else:
    print("Connected to Wi-Fi network.")
    print(wlan.ifconfig())

#response = requests.get("https://text.npr.org/")
#response = requests.get("http://wttr.in/cambridge?format=3")
#response = requests.get("http://ipecho.net/plain")
response = requests.get("https://earthquake.usgs.gov/fdsnws/event/1/query?format=text&limit=10")
#response = requests.get("http://artscene.textfiles.com/asciiart/unicorn")
for x in response.content.splitlines():
    xs = str(x)
    print(str(xs))
response.close()

Basically the Pico2 W uf2 appears to be under all Phil’s super bits and bobs add-ons. I’ve not had time to process data from the web and output it to the big screen yet.

The WIFI is working OK so I wanted to move on and use the display screen with Picographics and unpack the data being downloaded.

If I used the raw downloaded binary list (array?) b’string of characters’ I get an error but if I copy the same data and put it in the same variable I do not get an error.

>>> 

MPY: soft reboot
MicroPython feature/presto-wireless, presto v0.0.5 on 2024-12-18; Presto with RP2350

Type "help()" for more information.

>>> %Run -c $EDITOR_CONTENT

MPY: soft reboot
malloc self
set fb pointers
m_new_class(ST7701...
launch core1
launched core1
core1 returned
Waiting for Wi-Fi connection...
Waiting for Wi-Fi connection...
Waiting for Wi-Fi connection...
Waiting for Wi-Fi connection...
Waiting for Wi-Fi connection...
Waiting for Wi-Fi connection...
Waiting for Wi-Fi connection...
Waiting for Wi-Fi connection...
Waiting for Wi-Fi connection...
Connected to Wi-Fi network.
('192.168.0.234', '255.255.255.0', '192.168.0.1', '194.168.4.100')
b'#EventID|Time|Latitude|Longitude|Depth/km|Author|Catalog|Contributor|ContributorID|MagType|Magnitude|MagAuthor|EventLocationName'
b'ak024gok29z8|2024-12-28T13:40:41.644|61.1041|-152.2603|36.6|ak|ak|ak|ak024gok29z8|ml|0.6|ak|60 km W of Tyonek, Alaska'
b'nn00890729|2024-12-28T13:39:57.703|36.8836|-116.3368|13|nn|nn|nn|nn00890729|ml|-0.5|nn|37 km E of Beatty, Nevada'
b'ak024gojy0nb|2024-12-28T13:20:54.471|61.2621|-152.0829|43.8|ak|ak|ak|ak024gojy0nb|ml|0.7|ak|55 km WNW of Tyonek, Alaska'
b'nn00890726|2024-12-28T13:10:14.574|39.1804|-119.0367|5.6|nn|nn|nn|nn00890726|ml|1.4|nn|24 km NNE of Yerington, Nevada'
b'ci40829359|2024-12-28T13:09:51.570|33.6586667|-116.6923333|15.64|ci|ci|ci|ci40829359|ml|0.63|ci|9 km SSE of Idyllwild, CA'
b'nn00890723|2024-12-28T13:03:02.356|39.1732|-119.0198|5.9|nn|nn|nn|nn00890723|ml|1.7|nn|24 km NNE of Yerington, Nevada'
b'nn00890720|2024-12-28T12:59:27.883|37.3603|-117.2082|0|nn|nn|nn|nn00890720|ml|1.1|nn|38 km S of Goldfield, Nevada'
b'ci40829351|2024-12-28T12:50:52.020|33.4151667|-116.6093333|7.77|ci|ci|ci|ci40829351|ml|0.39|ci|15 km N of Warner Springs, CA'
b'ci40829343|2024-12-28T12:36:31.910|35.0888333|-119.0911667|12.5|ci|ci|ci|ci40829343|ml|1.46|ci|22 km NW of Grapevine, CA'
b'nn00890718|2024-12-28T12:36:07.331|37.3406|-117.2023|4|nn|nn|nn|nn00890718|ml|1.3|nn|40 km S of Goldfield, Nevada'

b'ak024gok29z8|2024-12-28T13:40:41.644|61.1041|-152.2603|36.6|ak|ak|ak|ak024gok29z8|ml|0.6|ak|60 km W of Tyonek, Alaska'
b'ak024gojy0nb|2024-12-28T13:20:54.471|61.2621|-152.0829|43.8|ak|ak|ak|ak024gojy0nb|ml|0.7|ak|55 km WNW of Tyonek, Alaska'

(['ak024gojy0nb', '2024-12-28T13:20:54.471', '61.2621', '-152.0829', '43.8', 'ak', 'ak', 'ak', 'ak024gojy0nb', 'ml', '0.7', 'ak', '55 km WNW of Tyonek, Alaska'], '2024-12-28', '13:20:54')
>>> %Run -c $EDITOR_CONTENT

MPY: soft reboot
signal core1
core1 returned
malloc self
set fb pointers
m_new_class(ST7701...
launch core1
launched core1
core1 returned
Connected to Wi-Fi network.
('192.168.0.234', '255.255.255.0', '192.168.0.1', '194.168.4.100')
b'#EventID|Time|Latitude|Longitude|Depth/km|Author|Catalog|Contributor|ContributorID|MagType|Magnitude|MagAuthor|EventLocationName'
b'ak024gok29z8|2024-12-28T13:40:41.644|61.1041|-152.2603|36.6|ak|ak|ak|ak024gok29z8|ml|0.6|ak|60 km W of Tyonek, Alaska'
b'nn00890729|2024-12-28T13:39:57.703|36.8836|-116.3368|13|nn|nn|nn|nn00890729|ml|-0.5|nn|37 km E of Beatty, Nevada'
b'ak024gojy0nb|2024-12-28T13:20:54.471|61.2621|-152.0829|43.8|ak|ak|ak|ak024gojy0nb|ml|0.7|ak|55 km WNW of Tyonek, Alaska'
b'nn00890726|2024-12-28T13:10:14.574|39.1804|-119.0367|5.6|nn|nn|nn|nn00890726|ml|1.4|nn|24 km NNE of Yerington, Nevada'
b'ci40829359|2024-12-28T13:09:51.570|33.6586667|-116.6923333|15.64|ci|ci|ci|ci40829359|ml|0.63|ci|9 km SSE of Idyllwild, CA'
b'nn00890723|2024-12-28T13:03:02.356|39.1732|-119.0198|5.9|nn|nn|nn|nn00890723|ml|1.7|nn|24 km NNE of Yerington, Nevada'
b'nn00890720|2024-12-28T12:59:27.883|37.3603|-117.2082|0|nn|nn|nn|nn00890720|ml|1.1|nn|38 km S of Goldfield, Nevada'
b'ci40829351|2024-12-28T12:50:52.020|33.4151667|-116.6093333|7.77|ci|ci|ci|ci40829351|ml|0.39|ci|15 km N of Warner Springs, CA'
b'ci40829343|2024-12-28T12:36:31.910|35.0888333|-119.0911667|12.5|ci|ci|ci|ci40829343|ml|1.46|ci|22 km NW of Grapevine, CA'
b'nn00890718|2024-12-28T12:36:07.331|37.3406|-117.2023|4|nn|nn|nn|nn00890718|ml|1.3|nn|40 km S of Goldfield, Nevada'

b'ak024gok29z8|2024-12-28T13:40:41.644|61.1041|-152.2603|36.6|ak|ak|ak|ak024gok29z8|ml|0.6|ak|60 km W of Tyonek, Alaska'
b'ak024gok29z8|2024-12-28T13:40:41.644|61.1041|-152.2603|36.6|ak|ak|ak|ak024gok29z8|ml|0.6|ak|60 km W of Tyonek, Alaska'
Traceback (most recent call last):
  File "<stdin>", line 111, in <module>
  File "<stdin>", line 93, in splitup
TypeError: can't convert str to int
>>> 

Here is the code:

# Test WIFI network DEMO on PRESTO
# Tony Goodhew 27 Dec 2024
import time
from random import randint
from presto import Presto
import network
import rp2
import requests
rp2.country("GB")
from secrets import WIFI_SSID, WIFI_PASSWORD

# Setup for the Presto display
presto = Presto(full_res=True,ambient_light=True)
display = presto.display

WIDTH, HEIGHT = display.get_bounds()

# Couple of colours for use later
BLUE = display.create_pen(28, 181, 202)
WHITE = display.create_pen(255, 255, 255)
RED = display.create_pen(230, 60, 45)
ORANGE = display.create_pen(245, 165, 4)
GREEN = display.create_pen(0,255,0)
PINK = display.create_pen(250, 125, 180)
MAGENTA = display.create_pen(255,0,255)
BLACK = display.create_pen(0, 0, 0)

COLOURS = [BLUE, RED, ORANGE, GREEN, PINK, MAGENTA]
def clean():
    display.set_pen(BLACK)
    display.clear()
    presto.update()

display.set_font("bitmap8")
clean()
display.set_pen(RED)
display.text("Quake",70,200,460,8)
presto.update()
time.sleep(2)

touch = presto.touch

clean()
display.set_pen(RED)

wlan = network.WLAN(network.STA_IF)
wlan.active(True)

wlan.connect(WIFI_SSID, WIFI_PASSWORD)
max_wait = 30
while max_wait > 0:
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    max_wait -= 1
    print("Waiting for Wi-Fi connection...")
    display.set_pen(RED)
    display.text("Waiting for Wi-Fi connection...",10,200,460,5)
    presto.update()
    time.sleep(1)

if wlan.status() != 3:
    raise RuntimeError("Network connection failed")
else:
    print("Connected to Wi-Fi network.")
    print(wlan.ifconfig())

clean()
display.set_pen(GREEN)
display.text("Connected",10,200,460,5)
presto.update()
time.sleep(1)
clean()
#response = requests.get("https://text.npr.org/")
#response = requests.get("http://wttr.in/cambridge?format=3")
#response = requests.get("http://ipecho.net/plain")
response = requests.get("https://earthquake.usgs.gov/fdsnws/event/1/query?format=text&limit=10")
#response = requests.get("http://artscene.textfiles.com/asciiart/unicorn")
lines = []
for x in response.content.splitlines():
    xs = str(x)
    print(str(xs))
    lines.append(str(x))
response.close()
names = ['EventID', 'Time', 'Latitude', 'Longitude', 'Depth/km', 'Author', 'Catalog', 'Contributor',
         'ContributorID', 'MagType', 'Magnitude', 'MagAuthor', 'EventLocationName', 'Date', 'Time']

def splitup(s):
    string = ""
    items = []

    for p in range(len(s)):
        c = s[p]
        if chr(c) != "|":
            string = string + chr(c)
        else:
            items.append(string)
            string = ""
    items.append(string)        

    # Extract date and time from second item
    dt = items[1]
    date = dt[0:10]
    time = dt[11:19]
    return(items,date,time)

test = lines[1]
print()
print(test)
#test = b'ak024gojy0nb|2024-12-28T13:20:54.471|61.2621|-152.0829|43.8|ak|ak|ak|ak024gojy0nb|ml|0.7|ak|55 km WNW of Tyonek, Alaska'
print(test)
k = splitup(test)
print()
print(k)

I do not understand why the unpasted version does not work.

Has anyone any ideas?

Hi Tony, just a thought: have you tried decoding the bytes object you get back, to convert it into a python string?

Something like:
response = response.decode()

Thanks. I’ve found a solution and you can find the working project here:
PRESTO WiFi demo - Discussion / Projects - Pimoroni Buccaneers

Just a late reaction to the part of establishing the WiFi connection. I just received my Presto a week ago. I used the modules builtin the original firmware of the Presto.
In the file secrets.py I filled in the SSID and PASSWORD needed for connecting.

The code I used to get WiFi connected is as follows:

from presto import Presto
presto = Presto()

ip = None
connected = False

presto.wifi._wait_for_connection()

wifi_connected = presto.wifi.isconnected()

if wifi_connected:
    ip = presto.wifi.ipv4()
    secr = presto.wifi._secrets()
    print(f"wifi connected = {wifi_connected}")
    print(f"ssid_connected = \"{secr[0]}\"")
    print(f"ip = {ip}")
else:
    print("wifi not connected")

The output was as follows:

MPY: soft reboot
wifi connected = True
ssid_connected = "<SSID of my WiFi>"
ip = 192.168.1.<xxx>

Regards,
Paulsk

I put my info in the secrets.py file but the world clock would not connect unti I ran the script in Presto wifi - #2 by Tonygo2 . After that it worked fine.

If I had to guess, I think it had to do with the NTP clock, it would not connect.

Hi, I’ve tried the above script and get the following “BadStatusLine” error:

>>> %Run -c $EDITOR_CONTENT

MPY: soft reboot
Waiting for Wi-Fi connection...
Waiting for Wi-Fi connection...
Waiting for Wi-Fi connection...
Connected to Wi-Fi network.
('192.168.3.87', '255.255.255.0', '192.168.3.1', '192.168.3.1')
Traceback (most recent call last):
  File "<stdin>", line 31, in <module>
  File "requests/__init__.py", line 205, in get
  File "requests/__init__.py", line 153, in request
ValueError: HTTP error: BadStatusLine:
[]
>>> 

Other networked examples such as word_clock.py fail at the call to ntptime.settime()

When I add timeout=5 to requests.get, I receive the following:

>>> %Run -c $EDITOR_CONTENT

MPY: soft reboot
Connected to Wi-Fi network.
('192.168.3.87', '255.255.255.0', '192.168.3.1', '192.168.3.1')
Traceback (most recent call last):
  File "<stdin>", line 31, in <module>
  File "requests/__init__.py", line 205, in get
  File "requests/__init__.py", line 148, in request
OSError: -110
>>> 

My guess is that my wifi is not working correctly. I tried clicking on the traceback which seemed like blue underlined links but they didn’t open up the source code. I’d like to find the source code to the network module so I can add some debugging code. Is the source code on the Presto somewhere? Thanks!