← index #556Issue #16239
Off-topic · high · value 1.102
QUERY · ISSUE

Raspberry Pi Pico W - umqtt clean_session=False -[Errno 12] ENOMEM

openby Zuroyopened 2022-10-25updated 2023-07-22

Hello,
I'm using a raspberry pi pico w to connect to a mqtt server. The clean sesssion connection works pretty well but when I try to use client.connect(False) it returns the error: OSError: [Errno 12] ENOMEM

Here a snippet of the code:

`
from umqtt.robust import MQTTClient
def mqtt_connect():

client = MQTTClient(client_id, mqtt_server, port, username, pwd,  keepalive=3600)
client.set_callback(on_message)
client.on_message = on_message
while (client.connect(False) != 0):
    print('Waiting for MQTT connection...')
    sleep(1)
print('Connected to %s MQTT Broker'%(mqtt_server))
client.subscribe(topic_sub)
print("Connected to %s, subscribed to %s topic" % (mqtt_server, topic_sub))

return client

`
and the Error:

Waiting for MQTT connection...
Waiting for MQTT connection...
Waiting for MQTT connection...
Waiting for MQTT connection...
Waiting for MQTT connection...
Traceback (most recent call last):
File "<stdin>", line 189, in <module>
File "<stdin>", line 71, in mqtt_connect
File "/lib/umqtt/simple.py", line 66, in connect
OSError: [Errno 12] ENOMEM

Can you help me please?

Thanks

1 comment
kdesimini · 2023-07-22

Also seeing this issue when trying to connect on the pico.

CANDIDATE · ISSUE

Maximum recursion depth

closedby Vishal-Birajdaropened 2024-11-14updated 2024-11-19
bug

Port, board and/or hardware

Pi Pico w

MicroPython version

MicroPython v1.22.2 on 2024-02-22; Raspberry Pi Pico W with RP2040

Expected behaviour

No response

maximum recursion depth exceeded occurdue to mqtt connection problem and reconnection problem. after hours code stuck automatically and mqtt not reconnected

import network
import json
from umqtt.robust import MQTTClient
import machine
import ssl
import time
import shared
from ota import OTAUpdater
def mqtt_connect():
    try:
        print("Attempting to connect to MQTT broker...")
        shared.client = MQTTClient(config['device_info']['device_id'],config['mqtt']['broker'],user=config['mqtt'] 
  ['user'],password=config['mqtt']['password'],keepalive=90,ssl=True)
        shared.client.set_callback(x.message_callback)
        shared.client.set_last_will(shared.topic_hb,"-1",False,qos=0)
        shared.client.connect()
        print('Connected to %s MQTT Broker' % config['mqtt']['broker'])
        shared.client.subscribe(shared.topic_cmd+"/D")
        shared.client.subscribe(shared.topic_cmd+"/FD")
        shared.client.subscribe(shared.topic_cmd+"/R1")
        shared.client.subscribe(shared.topic_cmd+"/R2")
        shared.client.subscribe(shared.topic_cmd+"/R3")
        shared.client.subscribe(shared.topic_cmd+"/R4")
        shared.client.subscribe(shared.topic_cmd+"/RR")
        shared.client.subscribe(shared.topic_cmd+"/MR")
        shared.pix[3] = shared.GREEN
        shared.pix.write()
        #return shared.client  
    except Exception as e:
        mqtt_connect()
        time.sleep(0.2)
        print('Failed to connect to MQTT broker. Reconnecting...',e)
        shared.event('\n Failed to connect to MQTT broker. Reconnecting...')
        shared.pix[3] = shared.RED
        shared.pix.write()
        time.sleep(0.3)
        machine.reset()
#p1 = Connection()       
def check_updates():
    try:
        firmware_url = "https://github.com/Vishal-Birajdar/Micropython-/"
        files = ["shared.py", "esecurity.py", "config.json"]
        ota_updater = OTAUpdater(config['wifi']['ssid'],config['wifi']['password'] , firmware_url, files)
        ota_updater.download_and_install_update_if_available()
    except:
        shared.event("Error in updating device \n")
        machine.reset()
 
key_data = read_certificate('privkey4.pem')
cert_data = read_certificate('cert4.pem')
ca_data = read_certificate('chain4.pem')
ssl_params = {
    "certfile": cert_data,
    "keyfile": key_data,
    "cadata": ca_data,
    "server_hostname": config['mqtt']['broker'],
    "cert_reqs": ssl.CERT_REQUIRED
}
#check_updates()
#p1.mqtt_connect()
try:
    wifi_connect()
    mqtt_connect()
except Exception as e:
    time.sleep(0.3)
    mqtt_connect()
    print('Failed to connect to MQTT broker. Reconnecting...',e)
    shared.event('\n Failed to connect to MQTT broker. Reconnecting...')
    machine.reset()

while True:
    try:
        shared.client.publish(shared.topic_hb,'1')
        currentMillis = time.ticks_ms()
        for i, s_pin in enumerate(sensor_pin):
            shared.buffer["S"+str(i+1)] = str(s_pin.value())+str(i+1)
            if s_pin.value()==1:shared.active_sensor[i]=1
            else:shared.active_sensor[i]=0
        shared.act_sensor_cnt=shared.active_sensor.count(1)
        print("active sensor",shared.act_sensor_cnt)
        print("sensor buffer",shared.active_sensor)
        time.sleep(0.1)
        x.main()
        if time.ticks_diff(currentMillis,previousMillis)>=interval:
            previousMillis = currentMillis
            if not wlan.isconnected():
                shared.pix[1] = shared.YELLOW
                shared.pix.write()
                wifi_connect()
                #time.sleep(.4)
    except Exception as e:
        print("Error with ",e)
        shared.event("\n Error in the main flie in loop")
        machine.reset()

I am not using any recursion or thread in my code but running a code after some time occuremaximum recursion depth error .

Additional Information

I am working on Iot project there is three .py file one is for main logic and other is varibale declared which used in all three file. and third file is main.py here i impleted wifi,mqtt and in loop I check the 4 gpio input (for any detection) and then call the main logic function which is define in another file,and checking the wifi connection status continously.

after running a code 10-15 min working ok then i got two error

  1. maximum recursion depth
  2. mqtt connection lost automatically

Code of Conduct

Yes, I agree

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