Pico wireless pack fetching data from web?

Whew! I’m glad it works. I guess… if at first you don’t succeed (to make an HTTP request) try, try again?

That’s something I should have built into the example in the first place- so many little failure cases to catch and handle. I’m glad network programming is only a small part of what I do!

Edit: full diff of my code above versus the example:

diff --git a/micropython/examples/pico_wireless/cheerlights.py b/micropython/examples/pico_wireless/cheerlights.py
index 449bda1..b49482c 100644
--- a/micropython/examples/pico_wireless/cheerlights.py
+++ b/micropython/examples/pico_wireless/cheerlights.py
@@ -2,7 +2,7 @@ import time
 import picowireless
 from micropython import const
 
-WIFI_SSID = "your SSID here!"
+WIFI_SSID = "Your SSID here!"
 WIFI_PASS = "Your PSK here!"
 
 CLOUDFLARE_DNS = (1, 1, 1, 1)
@@ -31,7 +31,7 @@ def connect(host_address, port, client_sock, timeout=1000):
     return False
 
 
-def http_request(client_sock, host_address, port, request_host, request_path, handler):
+def http_request(client_sock, host_address, port, request_host, request_path, handler, timeout=5000):
     print("Connecting to {1}.{2}.{3}.{4}:{0}...".format(port, *host_address))
     if not connect(host_address, port, client_sock):
         print("Connection failed!")
@@ -46,7 +46,14 @@ Connection: close
 
     picowireless.send_data(client_sock, http_request)
 
+    t_start = time.time()
+
     while True:
+        if time.time() - t_start > timeout:
+            picowireless.client_stop(client_sock)
+            print("HTTP request to {}:{} timed out...".format(host_address, port))
+            return False
+
         avail_length = picowireless.avail_data(client_sock)
         if avail_length > 0:
             break

Edit2: PR to update example so future users don’t have the same problem - Retry failed HTTP connections in cheerlights.py by Gadgetoid · Pull Request #166 · pimoroni/pimoroni-pico · GitHub

Hi all. Sorry to raise a post from the dead but I just signed up to help future googlers. I too copied the example from github and I too had an issue where the request was hanging in the while loop. Irrespective of the timeout. It never worked.

It turned out the cause was that when you copy and paste from the github UI, it sometimes strips out newlines. And in this case, it stripped out this newline: pimoroni-pico/cheerlights.py at ff8917cbe0a898d2c118906eab31c7e77eb60bc2 · pimoroni/pimoroni-pico · GitHub

In this case it was vitally important, because it meant the library never actually sends a valid HTTP request!

I’ve worked with github all my life and never had this before. So I wonder if there’s a weird non-standard end of line character on that line, which makes the HTML version not work.

Of course if you view the raw it works.

The only way I figured this out is by diffing my code against the code above from @gadgetoid. So thanks for that.

1 Like

Thanks, Phil!
The same copy-paste-GitHub UI bug caught me too. There’s a few hours I’ll never get back…