<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Certificate based MQTT (AWS IoT) trouble with changed umqtt]]></title><description><![CDATA[<p dir="auto">Hi,<br />
unfortunately the micropython certificate handling for MQTT has been changed last year. <a href="https://repost.aws/questions/QUnMyMEwxNT_e4TSDhDtjrbg/help-connecting-raspberry-pi-pico-w-to-aws-iot-core-using-micropython#ANKne8WcOFTraNmzE15drwkg" target="_blank" rel="noopener noreferrer nofollow ugc">good description</a><br />
Here is the new way to do it:</p>
<pre><code> # Create an SSL context
    context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
    context.verify_mode = ssl.CERT_REQUIRED

    # Load certificates
    context.load_verify_locations(cafile='/flash/certificate/ca.der')
    context.load_cert_chain('/flash/certificate/cert.der', '/flash/certificate/key.der')
    mqtt = MQTTClient(client_id=client, server=endpoint, port=8883, keepalive=1200, ssl=context)
    print("Connecting to AWS IoT...")
    mqtt.connect()
    print("Done")
</code></pre>
<p dir="auto">While the code and certificates work fine on ESP32-S3 Micropython they result in the following error on uiflow2</p>
<pre><code>Traceback (most recent call last):
  File "&lt;stdin&gt;", line 74, in &lt;module&gt;
  File "&lt;stdin&gt;", line 52, in mqtt_connect
  File "/flash/libs/umqttn/simple.py", line 69, in connect
  File "ssl.py", line 1, in wrap_socket
ValueError: 
The certificate validity has expired
</code></pre>
<p dir="auto">I triple checked the certificate validity..<br />
Next suspect would be an incorrect system time. Therfore I set the time as follows before attempting to initialize a mqtt connection:</p>
<pre><code>sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
if sta_if.isconnected():
    sta_if.disconnect()
sta_if.connect(wifi_ssid, wifi_password)
while not sta_if.isconnected():
    utime.sleep(1)
    print("Connecting...")
print("Connected:", sta_if.ifconfig())


print(f"time before NTP settime {time.localtime()}")
ntptime.settime() 
print(f"time after NTP settime {time.localtime()}")
import machine
rtc = machine.RTC()
# Set manually to a known correct UTC date/time: (year, month, day, weekday, hour, minute, second, microsecond)
rtc.datetime((2025, 3, 14, 1, 14, 0, 0, 0))
print("Manually set time:", machine.RTC().datetime())
</code></pre>
<p dir="auto">Still I get the same error:</p>
<pre><code>Connecting...
Connecting...
Connecting...
Connecting...
Connected: ('192.168.5.0', '255.255.252.0', '192.168.4.1', '192.168.4.1')
time before NTP settime (2025, 3, 14, 12, 8, 44, 5, 72)
time after NTP settime (2025, 3, 14, 12, 8, 45, 5, 72)
Manually set time: (2025, 3, 14, 5, 14, 0, 0, 67)
Connecting to AWS IoT...
MQTT Connection failed: 
The certificate validity has expired

Traceback (most recent call last):
  File "&lt;stdin&gt;", line 79, in &lt;module&gt;
  File "&lt;stdin&gt;", line 57, in mqtt_connect
  File "/flash/libs/umqttn/simple.py", line 69, in connect
  File "ssl.py", line 1, in wrap_socket
ValueError: 
The certificate validity has expired
</code></pre>
<p dir="auto">I used the micropython/umqtt.simple/umqtt/simple.py implementation installed as module umqtt<strong>n</strong> in the /flash/libs folder. It differs from the m5stack implementation (which is currently broken for certificate based MQTT due to the described changes in Micropython).</p>
<p dir="auto">At this point I wonder why the same code (umqtt.simple, demo-code, exactly the same certificate files) work on an UMFeatherS2 MicroPython v1.24.1  but fail on MicroPython v1.24.0-dirty on 2025-03-06; M5STACK CoreS3 with ESP32S3.</p>
<p dir="auto">I would happy for any hint on how to solve this.</p>
]]></description><link>https://community.m5stack.com/topic/7394/certificate-based-mqtt-aws-iot-trouble-with-changed-umqtt</link><generator>RSS for Node</generator><lastBuildDate>Thu, 30 Apr 2026 06:38:20 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/7394.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 14 Mar 2025 13:24:58 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Certificate based MQTT (AWS IoT) trouble with changed umqtt on Fri, 14 Mar 2025 13:52:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sistar_hh" aria-label="Profile: sistar_hh">@<bdi>sistar_hh</bdi></a> said in <a href="/post/28512">Certificate based MQTT (AWS IoT) trouble with changed umqtt</a>:</p>
<blockquote>
<p dir="auto">Hi,<br />
unfortunately the micropython certificate handling for MQTT has been changed last year. good description<br />
Here is the new way to do it:</p>
<h1>Create an SSL context</h1>
<pre><code>context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.verify_mode = ssl.CERT_REQUIRED

# Load certificates
context.load_verify_locations(cafile='/flash/certificate/ca.der')
context.load_cert_chain('/flash/certificate/cert.der', '/flash/certificate/key.der')
mqtt = MQTTClient(client_id=client, server=endpoint, port=8883, keepalive=1200, ssl=context)
print("Connecting to AWS IoT...")
mqtt.connect()
print("Done")
</code></pre>
<p dir="auto">While the code and certificates work fine on ESP32-S3 Micropython they result in the following error on uiflow2</p>
<p dir="auto">Traceback (most recent call last):<br />
File "&lt;stdin&gt;", line 74, in &lt;module&gt;<br />
File "&lt;stdin&gt;", line 52, in mqtt_connect<br />
File "/flash/libs/umqttn/simple.py", line 69, in connect<br />
File "<a href="http://ssl.py" target="_blank" rel="noopener noreferrer nofollow ugc">ssl.py</a>", line 1, in wrap_socket<br />
ValueError:<br />
The certificate validity has expired<br />
I triple checked the certificate validity..<br />
Next suspect would be an incorrect system time. Therfore I set the time as follows before attempting to initialize a mqtt connection:</p>
<p dir="auto">sta_if = network.WLAN(network.STA_IF)<br />
sta_if.active(True)<br />
if sta_if.isconnected():<br />
sta_if.disconnect()<br />
sta_if.connect(wifi_ssid, wifi_password)<br />
while not sta_if.isconnected():<br />
utime.sleep(1)<br />
print("Connecting...")<br />
print("Connected:", sta_if.ifconfig())</p>
<p dir="auto">print(f"time before NTP settime {time.localtime()}")<br />
ntptime.settime()<br />
print(f"time after NTP settime {time.localtime()}")<br />
import machine<br />
rtc = machine.RTC()</p>
<h1>Set manually to a known correct UTC date/time: (year, month, day, weekday, hour, minute, second, microsecond)</h1>
<p dir="auto">rtc.datetime((2025, 3, 14, 1, 14, 0, 0, 0))<br />
print("Manually set time:", machine.RTC().datetime())<br />
Still I get the same error:</p>
<p dir="auto">Connecting...<br />
Connecting...<br />
Connecting...<br />
Connecting...<br />
Connected: ('192.168.5.0', '255.255.252.0', '192.168.4.1', '192.168.4.1')<br />
time before NTP settime (2025, 3, 14, 12, 8, 44, 5, 72)<br />
time after NTP settime (2025, 3, 14, 12, 8, 45, 5, 72)<br />
Manually set time: (2025, 3, 14, 5, 14, 0, 0, 67)<br />
Connecting to AWS IoT...<br />
MQTT Connection failed:<br />
The certificate validity has expired</p>
<p dir="auto">Traceback (most recent call last):<br />
File "&lt;stdin&gt;", line 79, in &lt;module&gt;<br />
File "&lt;stdin&gt;", line 57, in mqtt_connect<br />
File "/flash/libs/umqttn/simple.py", line 69, in connect<br />
File "<a href="http://ssl.py" target="_blank" rel="noopener noreferrer nofollow ugc">ssl.py</a>", line 1, in wrap_socket<br />
ValueError:<br />
The certificate validity has expired<br />
I used the micropython/umqtt.simple/umqtt/simple.py implementation installed as module umqttn in the /flash/libs folder. It differs from the m5stack implementation (which is currently broken for certificate based MQTT due to the described changes in Micropython).</p>
<p dir="auto">At this point I wonder why the same code (umqtt.simple, demo-code, exactly the same certificate files) work on an UMFeatherS2 MicroPython v1.24.1 but fail on MicroPython v1.24.0-dirty on 2025-03-06; M5STACK CoreS3 with ESP32S3.</p>
<p dir="auto">I would happy for any hint on how to solve this.</p>
</blockquote>
<p dir="auto"><strong>Aha!</strong> When trying the following change:<br />
context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)<br />
context.verify_mode = <strong>ssl.CERT_OPTIONAL</strong>  # Instead of CERT_REQUIRED<br />
I successfully connected!</p>
]]></description><link>https://community.m5stack.com/post/28514</link><guid isPermaLink="true">https://community.m5stack.com/post/28514</guid><dc:creator><![CDATA[sistar_hh]]></dc:creator><pubDate>Fri, 14 Mar 2025 13:52:59 GMT</pubDate></item></channel></rss>