urequests.get() wont work anymore.

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
slawekw79
Posts: 2
Joined: Tue Apr 19, 2022 6:42 pm

urequests.get() wont work anymore.

Post by slawekw79 » Tue Apr 19, 2022 6:47 pm

Hi all,
I was running code on my NodeMCU esp8266 for switching light in my fish tank according to sunset and sunrise. Today code frozen on line 8

Code: Select all

r = urequests.get(url)
, and I spend few hours to figure out but it seems I can't find solution. Does anyone see the solution?
errors:

Code: Select all

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

Code: Select all

import network
ssid = 'SSID'
password = 'PASS'
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
  pass
main.py

Code: Select all

import urequests
import ujson, ntptime, utime
import ssd1306
from machine import Pin, PWM, SoftI2C
from utime import time, sleep

url = 'https://api.sunrise-sunset.org/json?lat=50.147240&lng=18.838700&formatted=0'
r = urequests.get(url)
timezone_hour = 2 # timezone offset (hours)

Blue = PWM(Pin(14), 1000)
Red = PWM(Pin(12), 1000)
White_1 = PWM(Pin(13), 1000)

i2c = SoftI2C(scl=Pin(5), sda=Pin(4))

while True:
    ntptime.settime()
    now = utime.localtime() 
    day = now[0],now[1],now[2]
    hours = now[3]+timezone_hour,now[4]
    
    data = ujson.loads(r.content)
    sunrise = data['results']['sunrise']
    sunset = data['results']['sunset']
    sunrise_time = int(sunrise[11:13])+timezone_hour, int(sunrise[14:16])
    sunset_time = int(sunset[11:13])+timezone_hour, int(sunset[14:16])
    hours_string = str(hours)
    sunrise_time_string = str(sunrise_time)
    sunset_time_string = str(sunset_time)
    
    oled_width = 128
    oled_height = 64
    oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

    if hours > sunrise_time and hours < sunset_time:
        Blue.duty(1024)
        Red.duty(1024)
        White_1.duty(1024)
        
        oled.fill(0)
        oled.text(hours_string, 0, 0)
        oled.text(sunrise_time_string, 0, 10)
        oled.text(sunset_time_string, 0, 20)
        oled.text("sunrise", 0, 30)
        oled.show()
        print(hours_string)
            
    else:
        Blue.duty(5)
        Red.duty(0)
        White_1.duty(0)
        
        oled.fill(0)
        oled.text(hours_string, 0, 0)
        oled.text(sunrise_time_string, 0, 10)
        oled.text(sunset_time_string, 0, 20)
        oled.text("sunset", 0, 30)
        oled.show()

    sleep(60)

    if str(sunrise[0:10]) != str(day):
        r = requests.get(url)

tepalia02
Posts: 99
Joined: Mon Mar 21, 2022 5:13 am

Re: urequests.get() wont work anymore.

Post by tepalia02 » Wed Apr 20, 2022 9:37 am

Hi, you may get some ideas from this thread : viewtopic.php?t=5971

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

Re: urequests.get() wont work anymore.

Post by scruss » Wed Apr 20, 2022 4:30 pm

  • what version of MicroPython is on your ESP8266? There's been some fairly recent clean up work done on SSL/TLS, so it might be worth installing 1.18 from MicroPython - Python for microcontrollers. My recently-purchased Wemos D1s came with the rather old MicroPython v1.13.
  • RAM might be a challenge, as urequests is large and the ESP8266 has very little memory. Maybe gc.collect() might clear up enough memory for this to work.
There's been a lot of discussion on this topic before: see OSError: -40 · Issue #400 · micropython/micropython-lib, which suggests looking at Issue connecting sockets to some websites - MicroPython Forum. urequests may not be able to connect to all SSL websites, so maybe the sunset/sunrise website changed their encryption method to something that won't work.

Nice application for MicroPython, though!

slawekw79
Posts: 2
Joined: Tue Apr 19, 2022 6:42 pm

Re: urequests.get() wont work anymore.

Post by slawekw79 » Thu Apr 21, 2022 2:05 pm

I'm running 1.18. It seems to have real RAM problems.
I just did some changes in my code, add gc.collect() to the boot.py and change

Code: Select all

r = urequests.get(url)
to

Code: Select all

r = prequests.request('GET', url)
And also put new requests library, it cleared one error but still have
OSError: -40
I think I will run this code on Rpi and connect via MQTT to the ESP.

Post Reply