umqtt.simple got OSError: -1 after publishing 65535 packets message on esp32
platform: MicroPython v1.21.0 on 2023-10-06; Generic ESP32 module
broker: mosquitto
description:
got OSError: -1 after sending 65535 packets
Result:
pkcnt 65000
Traceback (most recent call last):
File "<stdin>", line 47, in <module>
File "<stdin>", line 40, in publish_test
File "umqtt/simple.py", line 144, in publish
File "umqtt/simple.py", line 184, in wait_msg
OSError: -1
code:
from umqtt.simple import MQTTClient
import time
import ujson as json
from machine import unique_id
import sys, os
def str_current_time():
tm_ns = time.time_ns()
tm_rem_ms = (tm_ns % 1_000_000_000) // 1_000_000
tm = time.localtime(tm_ns//1_000_000_000)
str_tm = str(tm[3])+':'+str(tm[4])+':'+str(tm[5])+'.'+ str(tm_rem_ms)
return str_tm
id = unique_id() #machine.unique_id()
id_str = '{:02x}{:02x}{:02x}{:02x}'.format(id[0], id[1], id[2], id[3])
mq_id = id_str
mq_machine = os.uname().machine.split()[0]
mq_server = '192.168.12.25'
mq_user='esp32-srv'
mq_pass='123456'
mq_topic= b'esp/test'
mq_message = {'id':mq_id, 'machine': mq_machine, 'time': str_current_time(), 'hello': 'hello'}
mqClient = MQTTClient(mq_id, mq_server, port=1883, user=mq_user, password=mq_pass, keepalive=60*60*12)
mqClient.connect(clean_session=True) # only support clean_sesson=True
mqClient.publish(b'esp/hello', json.dumps(mq_message), qos=1)
pkcnt = 0x00_00_00_01
pkcnt_max = 999_999
def publish_test():
global pkcnt, pkcnt_max
pkcnt += 1
if pkcnt >= pkcnt_max :
pkcnt = 1
tm = time.time()
message = {'id':mq_id, 'machine': mq_machine, 'time': tm, 'current': 1, 'ctrl': 1, 'pkcnt':pkcnt}
mq_message = json.dumps(message)
mqClient.publish(mq_topic, mq_message, qos=1)
if pkcnt % 1000 == 0:
print('pkcnt',pkcnt, 'time', tm)
if __name__ == '__main__' :
print('test start')
while True :
publish_test()
Unable to Publish Message to AWS IoT Cloud
MCU: ESP32-S3
Version: GENERIC_S3-20220618-v1.19.1
With umqtt.robust2 unable to publish messages to AWS IoT Cloud and there is no error too. While, with umqtt.robust it works properly for below script with ESP32-C3 for version v1.17 and message is received successfully in AWS.
Have used micropython version - v.1.19 and his error is common with both ESP32-C3 and ESP32-S3.
And, below is my code snippet:
#AWS MQTT client cert example for esp8266 or esp32 running MicroPython 1.9
from umqtt.robust2 import MQTTClient
import time
import network
import machine
from machine import Pin, ADC
from time import sleep
import usocket as socket
import ujson
#Certs for ESP8266
CERT_FILE = "/flash/ESP32-Core.cert.pem"
KEY_FILE = "/flash/ESP32-Core.private.key"
#if you change the ClientId make sure update AWS policy
MQTT_CLIENT_ID = "basicPubSub"
MQTT_PORT = 8883
MQTT_KEEPALIVE_INTERVAL = 45
THING_NAME = "ESP32-Core1"
SHADOW_UPDATE_TOPIC = "/sdk/test/Python"
RESPONSE_RECEIVED = False
#if you change the topic make sure update AWS policy
MQTT_TOPIC = "esp8266-topic"
#Change the following three settings to match your environment
MQTT_HOST = "*******************-ats.iot.us-east-1.amazonaws.com"
WIFI_SSID = "*************"
WIFI_PW = "************"
mqtt_client = None
def sub_cb(topic, msg, retained, duplicate):
print((topic, msg, retained, duplicate))
def connect_mqtt():
try:
with open(KEY_FILE, "r") as f:
key = f.read()
print("MQTT received KEY")
with open(CERT_FILE, "r") as f:
cert = f.read()
print("MQTT received CERTIFICATE")
mqtt_client = MQTTClient(client_id=MQTT_CLIENT_ID, server=MQTT_HOST, port=MQTT_PORT, keepalive=5000, ssl=True, ssl_params={"cert":cert, "key":key, "server_side":False})
mqtt_client.connect()
mqtt_client.set_callback(sub_cb)
print("MQTT is connected")
a = {"message": "Hello from IoT console"}
try:
mqtt_client.publish(SHADOW_UPDATE_TOPIC, ujson.dumps(a))
print("Publish Message")
except Exception as e:
print('Cannot Publish' + str(e))
except Exception as e:
print('Cannot connect to MQTT ' + str(e))
raise
def connect_wifi(ssid, pw):
wlan = network.WLAN(network.STA_IF)
if(wlan.isconnected()):
wlan.disconnect()
nets = wlan.scan()
if not wlan.isconnected():
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PW)
while not wlan.isconnected():
pass
print("Connected as:", wlan.ifconfig())
try:
print("Connecting to WIFI...")
connect_wifi(WIFI_SSID, WIFI_PW)
print("Connecting to MQTT...")
connect_mqtt()
print("OK")
except Exception as e:
print(str(e))
And, below is the output of the script:
>>> %Run -c $EDITOR_CONTENT
Connecting to WIFI...
Connected as: ('192.***.*.*', '255.255.255.0', '192.***.*.*', '192.***.*.*')
Connecting to MQTT...
MQTT received KEY
MQTT received CERTIFICATE
MQTT is connected
Publish Message
OK
The publish event is successful, however, still don't see the message in AWS. Please advise, what is wrong here? Why the publish event is successful when the message is not sent?
Below is the snippet of the ESP32-S3 filesystem:
<img width="164" alt="image" src="https://user-images.githubusercontent.com/112946408/200180620-b7591706-686a-4536-b614-aa6435cdbaf9.png">
also tried reconnect after exception, didn't work
how ever mqtt_as has no issue