Urequests- What am I doing wrong?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
lazurx
Posts: 4
Joined: Sun Aug 14, 2022 3:32 am

Urequests- What am I doing wrong?

Post by lazurx » Sun Aug 14, 2022 3:48 am

Hello,

This won't work from my ESP-01S
I know I'm missing something- is the request malformed somehow?

I connect to the wifi- it gets and IP......

This works (test):
r = urequests.get("http://ip-api.com/json/")
print (r.text)
{"status":"success","country":"United States","countryCode":"US","region":"MI","regionName":","city":"and","zip":"","lat":11.11"lon":-81.3734,"timezone":"America/Newwork","is}}


But when try to open this URL (https://python.faith/dax/api4.php?Devices=2Relay) :o it blows up: (I'm passing data like I do from my pico- that code @ bottom)
r=urequests.get("https://python.faith/dax/api4.php?Devices=2Relay" )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/lib/urequests.py", line 112, in get
File "/lib/urequests.py", line 60, in request
OSError: -40

But the actual page does respond to that URL in a browser:
// 20220813233731
// https://python.faith/dax/api4.php?Devices=2Relay

{
"Devices": "2Relay"
}

Its a very simple php page that looks for a get operation and then reply's back with json.

<?php
$Devices=$_GET['Devices'];
$APIOut = new stdClass();
$APIOut->Devices = $Devices;
$APIOutJSON = json_encode($APIOut);
echo $APIOutJSON;
?>



---------------------------------------------------------------------------------------------------------------
Code that works on my PICO (not ESP-01S): much more complex.... (Its a Get operation even though I named the php file posttest)
pushData="https://python.faith/dax/posttest.php?D ... _Humidity=" + str(dht11hum) + "&Sensor_Temp=" + str(tempfbmp280) + "&DateTime=12/27/2002&Sensor2_name=DHT22&Sensor2_Humidity=" + str(dht22hum) + "&Sensor2_Temp=" + str(dht22temp)
#print(pushData)
r = urequests.get(pushData)

DeaD_EyE
Posts: 19
Joined: Sun Jul 17, 2022 12:57 pm
Contact:

Re: Urequests- What am I doing wrong?

Post by DeaD_EyE » Sun Aug 14, 2022 11:40 am

I guess the error came from usocket.wrap_socket because of having less memory for the decryption. I have the same problem with my Server, which offers TSL1.3 and using Elliptic Curve. Furthermore, I can use other hosts with different encryption. Since I'm using the WROVER modules with internal SPIRAM (4 MiB) I never had this issue again.

If you want to keep your Device, then try to change your SSL Settings of your server to offer encryption settings, which the controller could handle.

My testcode:

Code: Select all

import gc
import micropython
import urequests


URL = micropython.const("https://python.faith/dax/api4.php?Devices=2Relay")


def get():
    gc.collect()
    print("Mem:", gc.mem_free())
    
    try:
        result = urequests.get(URL).json()
    except OSError:
        print("OSError")
        print("Mem:", gc.mem_free())
    else:
        print("Success")
        print("Mem:", gc.mem_free())
    
    gc.collect()
    
    return result
    

Result:

Code: Select all

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4396
ho 0 tail 12 room 4
load:0x40078000,len:13884
load:0x40080400,len:3340
entry 0x40080618
MicroPython v1.19.1-283-g6f4d424f4-dirty on 2022-08-14; ESP32 module (spiram) with ESP32
Type "help()" for more information.
>>> 
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== import gc
=== import micropython
=== import urequests
=== 
=== 
=== URL = micropython.const("https://python.faith/dax/api4.php?Devices=2Relay")
=== 
=== 
=== def get():
===     gc.collect()
===     print("Mem:", gc.mem_free())
===     
===     try:
===         result = urequests.get(URL).json()
===     except OSError:
===         print("OSError")
===         print("Mem:", gc.mem_free())
===     else:
===         print("Success")
===         print("Mem:", gc.mem_free())
===     
===     gc.collect()
===     
===     return result
===     
>>> get()
Mem: 4092144
Success
Mem: 4088496
{'Devices': '2Relay'}
>>> 4092144 - 4088496
3648
>>> 
I guess if I repeat the same on a WROOM Module without external SPIRAM, I'll get the same error.

lazurx
Posts: 4
Joined: Sun Aug 14, 2022 3:32 am

Re: Urequests- What am I doing wrong?

Post by lazurx » Sun Aug 14, 2022 12:11 pm

I'm still learning...


Why would it work on a pico but not an esp01?

DeaD_EyE
Posts: 19
Joined: Sun Jul 17, 2022 12:57 pm
Contact:

Re: Urequests- What am I doing wrong?

Post by DeaD_EyE » Sun Aug 14, 2022 1:23 pm

ESP01 is a ESP8266:
SRAM: < 36kB
Flash-Memory: 512kB or 1MB

Pico W (RP2040):
SRAM: 264 KB
Flash-Memory: 2MB

The Raspberry Pi Pico (W) has more SRAM available.
On the ESP8266 is only 36 KiB available.

The ESP32 is the next generation of the ESP8266 with more SRAM. Use your ESP01 for Applications, which don't require TLS (SSL).

lazurx
Posts: 4
Joined: Sun Aug 14, 2022 3:32 am

Re: Urequests- What am I doing wrong?

Post by lazurx » Sun Aug 14, 2022 5:06 pm

You know if one that works with a 2 relay board that you drop in an esp01/S?

lazurx
Posts: 4
Joined: Sun Aug 14, 2022 3:32 am

Re: Urequests- What am I doing wrong?

Post by lazurx » Mon Aug 15, 2022 2:26 am

Thank you,

Do you think this would be a viable replacement? (Drop in - same form factor).

https://www.aliexpress.com/item/3256803 ... JWWKzfLke9

TTGO T-01C3 ESP32-C3

it says it has 4mb of flash.....

DeaD_EyE
Posts: 19
Joined: Sun Jul 17, 2022 12:57 pm
Contact:

Re: Urequests- What am I doing wrong?

Post by DeaD_EyE » Mon Aug 15, 2022 9:26 am

I had to use a VPN to see the page.

Code: Select all

TTGO T-01C3 

Technical details:
    MCU         : ESP32-C3 
    CPU         : RISC-V 32-bit 
    Flash       : 4Mb 
    SRAM        : 400KB 
    ROM         : 384KB 
    SRAM in RTC : 8KB 
The 400KB SRAM should be enough.

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

Re: Urequests- What am I doing wrong?

Post by jimmo » Wed Aug 17, 2022 3:02 am

See https://github.com/micropython/micropython/issues/8940 for the ongoing effort to fix the underlying issue here with SSL on ESP32.

Post Reply