0

Below is the micropython code that is supposed to use the api.sunrise-sunset to gather the sunrise & sunset times. The program worked about 2-3 times, and now without making any changes it provides the following error:

>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
  File "urequests.py", line 180, in get
  File "urequests.py", line 76, in request
OSError: -6
>>> 

This error persists across all other programs I try to run with an api call. I have tried different libraries such as usocket and other methods of accessing the api. I am new to api's. This code is a part of a larger project that will be developed after this issue is resolved.

I am using MicroPython v1.19.1-1014-gbde222ce8 on 2023-04-11; Raspberry Pi Pico W with RP2040. The IDE is Thonny, Version thonny-4.0.2. I am running this on a windows 10 device and it is intended to be running on my Raspberry Pi Pico W.

I tried using chatgpt to figure out the error code. It suggested checking if the website was down, which it was not. I tried writing other programs that accessed different api's, and I got the same error code. I was able to access the websites just fine through a browser. I pulled up thonny on a different computer, my laptop, and I got the exact same error with the same program. I tried using an older version of thonny and got the same error. Again, the error is OSError: -6.

import urequests
import json

# URL for the sunrise/sunset API endpoint
url = "https://api.sunrise-sunset.org/json?lat=37.7749&lng=-122.4194&formatted=0"

# Make an HTTP GET request to the API endpoint
response = urequests.get(url)

# Parse the JSON response
data = json.loads(response.text)

# Extract the sunrise and sunset times from the JSON data
sunrise = data["results"]["sunrise"]
sunset = data["results"]["sunset"]

# Print the sunrise and sunset times to the screen
print("Sunrise: {}".format(sunrise))
print("Sunset: {}".format(sunset))
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

I had the same issue and then connected my Pico W to wifi explicitly.

Just call the connect() function before your API code.

ssid = '<wifi-network-name>'
password = ''

def connect():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    wlan.connect(ssid)
    while wlan.isconnected() == False:
        print('Waiting for connection..')
        sleep(1)
    # print(wlan.ifconfig())
    ip = wlan.ifconfig()[0]
    print(f'Connected on {ip}')
    return ip

connect ()
tan
  • 1
  • 1