我在我的Raspberry上安装了MQTT代理(Mosquitto),并安装了客户端(Paho Client Python)。我试图启动我的客户端,但我有这个错误时,运行我的文件。
File "client_mqtt.py", line 38, in <module>
client = mqtt.client()
AttributeError: 'module' object has no attribute 'client'
我的完整档案是
import paho.mqtt.client as mqtt
import sys
#definicoes:
Broker = "localhost"
username = "mosquitto"
password = "mqtt"
PortaBroker = 1883
KeepAliveBroker = 60
client_name = "Local"
topic_casa = "casa"
topic_mcu = "mcu"
#Callback - conexao ao broker realizada
def on_connect(client, userdata, flags, rc):
print("[STATUS] Conectado ao Broker. Resultado de conexao: "+str(rc))
client.subscribe(topic_mcu)
client.subscribe(topic_casa)
#Callback - mensagem recebida do broker
def on_message(client, userdata, msg):
MensagemRecebida = str(msg.payload)
print("[MSG RECEBIDA] Topico: "+msg.topic+" / Mensagem: "+MensagemRecebida)
def on_disconnect(client, userdata, rc):
if rc != 0:
print("Unexpected disconnection.")
#programa principal:
try:
print("[STATUS] Inicializando MQTT...")
#inicializa MQTT:
client = mqtt.Client()
client.username_pw_set(username, password)
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.connect(Broker, PortaBroker, KeepAliveBroker)
client.loop_forever()
except KeyboardInterrupt:
print "\nCtrl+C pressionado, encerrando aplicacao e saindo..."
sys.exit(0)
2条答案
按热度按时间nnt7mjpx1#
错误与您发布的代码不匹配。
该错误意味着您正在使用
mqtt.client()
而不是mqtt.Client()
5kgi1eie2#
我也有同样问题,
这是因为我当前的文件名是random.py。尝试重命名您的client_mqtt.py,也许它可以解决您的问题。