urequests

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
priis
Posts: 26
Joined: Tue Mar 31, 2015 9:52 pm

urequests

Post by priis » Thu Oct 06, 2022 8:50 am

Here is a minimal program (I use an esp32) to get some data from an url:

import network
import urequests as requests
ssid = "myWifi"
password = "myPassword"
from machine import Pin
blueled=Pin(2,Pin.OUT)

station = network.WLAN(network.STA_IF)

station.active(True)
station.connect(ssid, password)
i=0
while station.isconnected() == False:
utime.sleep_ms(10)
i=i+1
print(i)
blueled.value(not blueled.value())
pass

print("Connection successful")
res = requests.get(url = "http://worldtimeapi.org/api/timezone/Europe/Paris")
print(res.text)
for i in range(0,10):
blueled.value(not blueled.value())
utime.sleep_ms(50)

Sometimes the program works, sometime it says 'no module urequests', and sometimes it says 'OSError: -202. How can I make it always work.
I find it very strange that the urequests module sometimes is there and sometimes not!?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: urequests

Post by jimmo » Thu Oct 06, 2022 8:54 am

As before, please open new topics at GitHub Discussions

Note the double-precision firmware you installed is a very old version (1.12).

priis
Posts: 26
Joined: Tue Mar 31, 2015 9:52 pm

Re: urequests

Post by priis » Thu Oct 06, 2022 10:27 am

Where can I find a more recent version with double precision?

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: urequests

Post by scruss » Thu Oct 06, 2022 2:14 pm

I think you'd have to build it from source, which isn't particularly easy. Roberto hasn't touched these builds in a while.

Are you sure you need double precision? Remember, these are microcontrollers here. Everything is constrained.

To free up memory, call res.close() after you're done with a request. That firmware might support ntptime, which is an alternative way of getting time sync. Note that MicroPython isn't particularly aware of timezones.

Also, your code might mean something if you used code tags:

Code: Select all

from machine import Pin
import network
import urequests as requests
ssid = "myWifi"
password = "myPassword"
blueled = Pin(2, Pin.OUT)

station = network.WLAN(network.STA_IF)

station.active(True)
station.connect(ssid, password)
i = 0
while station.isconnected() == False:
    utime.sleep_ms(10)
    i = i+1
    print(i)
    blueled.value(not blueled.value())
    pass

print("Connection successful")
res = requests.get(url="http://worldtimeapi.org/api/timezone/Europe/Paris")
print(res.text)
for i in range(0, 10):
    blueled.value(not blueled.value())
    utime.sleep_ms(50)

Post Reply