-
Hi, I'm using an ESP32-WROOM32 module to fetch and display electricity prices on an ILI9341 display. https://www.elprisetjustnu.se/api/v1/prices/2025/10-09_SE3.json This setup has been working flawlessly for a long time, but after pulling the latest MicroPython codebase, fetching data from the API now fails due to SSL/TLS errors in for url, day in zip(self.get_url(), self.days):
print(f"Fetching JSON from: {url}")
response = urequests.get(url)
print("Response received") Using a manual socket test, it seems the error occurs at the line:
Debugging with Raw socket and ssl. def test(self, url=b"https://www.elprisetjustnu.se/api/v1/prices/2025/10-09_SE3.json", addr_family=0, use_stream=True):
# Split the given URL into components
proto, _, host, path = url.split(b"/", 3)
assert proto == b"https:"
# Lookup the server address
ai = socket.getaddrinfo(host, 443, addr_family, socket.SOCK_STREAM)
print("Address infos:", ai)
# Select the first address
ai = ai[0]
# Create and connect the socket
s = socket.socket(ai[0], ai[1], ai[2])
addr = ai[-1]
print("Connect address:", addr)
s.connect(addr)
# Upgrade to TLS
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
s = ctx.wrap_socket(s)
print(s)
# Send request and read response
request = b"GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % (path, host)
if use_stream:
s.write(request)
print(s.read(4096))
else:
s.send(request)
print(s.recv(4096))
# Close socket
s.close() The failures are:
The failure seems related to the SSL/TLS handshake or certificate verification layer. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
This's what I did:
import socket
import tls # use tls directly
import network
from time import sleep
from wifi import key # (SSID, PWD)
ssid, pwd = key # give correct cred for your wifi
# Set up WiFi
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(ssid, pwd)
while not sta_if.isconnected():
pass
NS = '1.1.1.1'
ip, nm, gw, _ = sta_if.ifconfig()
sta_if.ifconfig((ip, nm, gw, NS))
sleep(5)
print(sta_if.ifconfig()) # check proper DNS set
SRV = "www.elprisetjustnu.se"
ctx = tls.SSLContext(tls.PROTOCOL_TLS_CLIENT)
ctx.verify_mode = tls.CERT_NONE
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
addr = socket.getaddrinfo(SRV, 443)[0][-1]
s.connect(addr)
sx = ctx.wrap_socket(s, server_side=False)
# Send request and read response
path = "api/v1/prices/2025/10-09_SE3.json"
request = b"GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n" % (path, SRV)
sx.write(request)
print(s.read(4096))
# close socket
sx.close()
s.close() Then, I ran the above script, (test.py): $ mpr mount . run test.py
Local directory . is mounted at /remote
('192.168.4.85', '255.255.252.0', '192.168.4.1', '1.1.1.1')
b'\x17\x03\x03\x01a\x00\x00\x00\x00\x00\x00\x00\x01\xce\xfb\xf0\x1f\x13t\xe9M\x05$\x124~x\xbe\xcb\xfc\x93\xc1\x05K\xd2)\xe9i[x\xa9\x9f\xc7\xa6\x0c\x1cX\x97\xfb\x1f\\\xab\x81\x88s\x0e\xa4\x11y\xf7\xdd\xee\xf8\xdf\xc1:vc\x9e\xbb\x18\xbd\xff\x03\x0........ The mpr is my short name for mpremote. If you are interested to see more examples, please take a look at https://github.com/shariltumin/ssl-tls-examples-micropython |
Beta Was this translation helpful? Give feedback.
-
Thank you for your assistance @shariltumin. I have looked at the examples, but unfortunately, I’m still experiencing issues with server = "www.elprisetjustnu.se"
print(server)
ctx = tls.SSLContext(tls.PROTOCOL_TLS_CLIENT)
ctx.verify_mode = tls.CERT_NONE
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
addr = socket.getaddrinfo(server, 443)[0][-1]
s.connect(addr)
print(addr)
pytime.sleep(5)
gc.collect()
sx = ctx.wrap_socket(s, server_side=False)
Compiled with ESP-IDF 5.4 I have a lot of traffic tagged with; Could it be that I’m also affected by ##8940 ? |
Beta Was this translation helpful? Give feedback.
-
It wasn’t related to urequests, but rather an issue with ESP-IDF 5.4, which is addressed in #16650. |
Beta Was this translation helpful? Give feedback.
It wasn’t related to urequests, but rather an issue with ESP-IDF 5.4, which is addressed in #16650.