Page 1 of 1

urequests

Posted: Thu Oct 06, 2022 8:50 am
by priis
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!?

Re: urequests

Posted: Thu Oct 06, 2022 8:54 am
by jimmo
As before, please open new topics at GitHub Discussions

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

Re: urequests

Posted: Thu Oct 06, 2022 10:27 am
by priis
Where can I find a more recent version with double precision?

Re: urequests

Posted: Thu Oct 06, 2022 2:14 pm
by scruss
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)