← index #754Issue #218
Off-topic · high · value 1.085
QUERY · ISSUE

umqtt.simple got OSError: -1 after publishing 65535 packets message on esp32

openby mzhboyopened 2023-10-21updated 2023-10-21

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()
2 comments
mzhboy · 2023-10-21

also tried reconnect after exception, didn't work

    try :
        mqClient.publish(mq_topic, mq_message, qos=1)
    except OSError as err:
        print('error: when publish,', err)
        try :
            mqClient.connect()
        except Exception as err2 :
            print('error: when reconnect,', err2)            
    
mzhboy · 2023-10-21

how ever mqtt_as has no issue

"pkcnt": 147481,

CANDIDATE · ISSUE

OSError: [Errno 103] ECONNABORTED

closedby Javier96Barreraopened 2017-10-11updated 2017-10-13

Hi, I'm quite new to micropython. I have flashed the 1.9.2 version on my Lolin v3 (Nodemcu 12e).
I load the code using ampy command, then open terminal with Putty, and reset the board. i get this error while trying to use the sentence "connect()":

Traceback (most recent call last):
File "main.py", line 44, in <module>
File "umqtt/simple.py", line 56, in connect
OSError: [Errno 103] ECONNABORTED

MicroPython v1.9.2-8-gbf8f45cf on 2017-08-23; ESP module with ESP8266

import time
import machine
import network
import gc
import dht
from umqtt.robust import MQTTClient

#DHT PIN
d = dht.DHT11(machine.Pin(14))
led = machine.Pin(16, machine.Pin.OUT)
gc.enable()


def do_connect():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network...')
        wlan.connect('SSID', 'PASSWORD')
        while not wlan.isconnected():
            pass
    print('network config:', wlan.ifconfig())


for i in range(20):
    do_connect()
    thingspeakChannelId = "MyChannelID"  # Thingspeak Channel ID
    thingspeakChannelWriteapi = "MyWriteKey"  # Write API Key
    myMqttClient = "esp8266_mp"  
    thingspeakIoUrl = "mqtt.thingspeak.com"
    publishPeriodInSec = 15
    print("Control 1")
    c = MQTTClient(myMqttClient, thingspeakIoUrl, 1883)
    print("Control 2")
    d.measure()
    temp = d.temperature()
    humidity = d.humidity()
    print("Control 3")
    credentials = "channels/{:s}/publish/{:s}".format(thingspeakChannelId, thingspeakChannelWriteapi)
    print("Control 4")
    payload = "field1={:.1f}&field2={:.1f}\n".format(temp, humidity)
    print("Control 5")
    c.connect() #HERE
    print("Control 6")
    c.publish(credentials, payload)
    print("Control 7")
    for i in range(5):
        led.low()
        time.sleep(0.05)
        led.high()
        time.sleep(0.05)
    print("Sending data...", gc.mem_free())
    time.sleep(publishPeriodInSec)

    c.disconnect()
machine.reset()

Thanks for your time, if there is any problem regarding the information or post structure, tell me and I will try to fix it.

2 comments
dpgeorge · 2017-10-13

This github issue tracker is mainly for bug reports and code improvements. The report here is more of a problem using the libraries, and help in this area is best asked at https://forum.micropython.org. So please try to ask your question there (or maybe someone had the same issue and already fixed it).

Otherwise, the general thing to try is to verify you have internet connectivity, eg:

>>> import socket
>>> socket.getaddrinfo('micropython.org', 80)
Javier96Barrera · 2017-10-13

Thank you for the reply, I will move this question right now. PS: I checked and yes, i got internet connectivity. Reply: [(2, 1, 0, '', ('176.58.119.26', 80))]

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied