esp32micropythonthonny

Cannot do a get request in micropython on esp32


I tried up to day to get requests working on micropython, tried installing libraries and everything. I use thonny IDE. what can i do?

Error:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "urequests.py", line 184, in post
  File "urequests.py", line 76, in request
OSError: -202

Code:

import urequests
response = urequests.get('http://pastebin.com/raw/4309d09')

Tried installing external libraries like 'micropython-urequests' etc


Solution

  • you did not connect your board to wifi before requesting data from internet!

    import network
    import urequests
    
    # Connect to network
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
     
    # Fill in your network name (SSID) and password here:
    ssid = 'MyWIFI'
    password = 'SuperSecretPassword'
    wlan.connect(ssid, password)
    while not wlan.isconnected():
        utime.sleep(1)  # sleep for a second
    print("Network connected")
    
    response = urequests.get('http://pastebin.com/raw/4309d09')
    # do whatever is needed with your response