paho.mqtt.python icon indicating copy to clipboard operation
paho.mqtt.python copied to clipboard

AttributeError: 'Client' object has no attribute 'connected_flag'

Open derAlff opened this issue 2 years ago • 1 comments

Hello,

I have an issue by running the following code on a Raspberry Pi:

import paho.mqtt.client as mqtt


BROKER_URL = "my.mqtt.broker"
BROKER_PORT = 1883
BROKER_USERNAME = "test"
BROKER_PASSWORD = "test"

def on_connect(client, userdata, flags, rc):
    if rc == 0:
        client.connected_flag=True
        print("Connected successfully")
    else:
        client.bad_connection_flag=True
        print("Connection failed with code {rc}")

client = mqtt.Client("tempsensor")
client.username_pw_set(username=BROKER_USERNAME, password=BROKER_PASSWORD)
client.on_connect = on_connect
client.connect(BROKER_URL, BROKER_PORT, 60)
#client.connect("10.83.134.97", 1883, 60)

client.loop_start()
print("Connecting to broker ", BROKER_URL)

while not client.connected_flag: #wait in loop
    print("In wait loop")
    time.sleep(0.1)

print("CODE HERE")

client.loop_stop()

If I run this code, I receive the following issue.

Connecting to broker  my.mqtt.broker
Traceback (most recent call last):
  File "/home/pi/src/test_mqtt/test_mqtt.py", line 26, in <module>
    while not client.connected_flag: #wait in loop
AttributeError: 'Client' object has no attribute 'connected_flag'

Running Python 3.9.2

Can anybody help me?

derAlff avatar Mar 17 '22 10:03 derAlff

i have searched through paho mqtt module and i haven't found this flag.

You can use global varible as a flage in your case

import time
import paho.mqtt.client as mqtt    #import client library

connected_flag = False #create flag

def on_connect(client, userdata, flags, rc):
    global connected_flag #use global variable
    if rc==0:
        connected_flag=True #set flag
        print("connected ok")
    else:
        print("Bad connection Returned code=",rc)
        client.loop_stop()

client = mqtt.Client("python1")             #create new instance 
client.on_connect=on_connect  #bind call back function
client.loop_start()  #Start loop 
client.connect("127.0.0.1")               #connect to broker
# Other code here
while not connected_flag:
    print("waiting for connection")
    time.sleep(0.3)
print("connected")
# client.publish("test/topic","Hello World")  #publish message
client.loop_stop()    #Stop loop 
client.disconnect()   #disconnect from broker

mohamedt-ea avatar Jul 20 '22 15:07 mohamedt-ea

As far as I can tell this is caused by the code update identified in the change log as "Add on_pre_connect() callback". I went into the change log and compare file and commented out the associated code, and now my python runs just fine...

madmacks59 avatar Jul 14 '23 17:07 madmacks59

I'm with @mohamedt-ea on this; I've looked back through the library history and cannot see a connected_flag; you could solve the issue with a one line change:

import paho.mqtt.client as mqtt
import time


BROKER_URL = "mosquitto"
BROKER_PORT = 1883
BROKER_USERNAME = "test"
BROKER_PASSWORD = "test"

def on_connect(client, userdata, flags, rc):
    if rc == 0:
        client.connected_flag=True
        print("Connected successfully")
    else:
        client.bad_connection_flag=True
        print("Connection failed with code {rc}")

client = mqtt.Client("tempsensor")
client.connected_flag=False   # add this
#client.username_pw_set(username=BROKER_USERNAME, password=BROKER_PASSWORD)
client.on_connect = on_connect
client.connect(BROKER_URL, BROKER_PORT, 60)
#client.connect("10.83.134.97", 1883, 60)

client.loop_start()
print("Connecting to broker ", BROKER_URL)

while not client.connected_flag: #wait in loop
    print("In wait loop")
    time.sleep(0.1)

print("CODE HERE")

client.loop_stop()

Note that there are various issues where users are adding connected_flag in a range of ways (e.g. #499, #501, #716).

As such without more information on why OP believes this should work I don't think this is answerable. Given the age of the question I'm going to close the issue (but please feel free to open it with more info).

Note: This is part of an exercise to clean up old issues so that the project can move forwards. Due to the number of issues being worked through mistakes will be made; please feel free to reopen this issue (or comment) if you believe it's been closed in error.

MattBrittan avatar Jan 08 '24 09:01 MattBrittan