MQTTブローカー(Mosquitto)をRaspberryにインストールし、クライアント(Paho Client Python)をインストールしますが、クライアントを起動しようとしましたが、私のファイルをコンパイルするときにこのエラーが発生します。コンパイル時のエラーMQTT 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)