#๐Ÿ”’ help with paho mqtt and EMQX

5 messages ยท Page 1 of 1 (latest)

loud path
#

Hi, I have this code:

import time

broker_address = ""
broker_port = 8883
username = ""
password = ""
topic = "energy_consumption"

def on_connect(self,client, userdata, flags, rc):
    if rc == 0:
        print("Connected to MQTT Broker!")
    else:
        print(f"Failed to connect, return code {rc}")

def on_message(client, userdata, message):
    print(f"Received message: {message.payload.decode()}")

client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2,"subscriper_raspberry")
client.tls_set(ca_certs='./../emqxsl-ca.crt')
client.username_pw_set(username, password)
client.on_connect = on_connect
client.subscribe(topic, qos=0)
client.on_message = on_message
try:
    client.connect(broker_address, broker_port)
    print("Connection attempt successful")
    client.loop_start()
except Exception as e:
    print(f"Error connecting to the broker: {e}")

while True:
    time.sleep(0.2)```
But, I don't get the message from the broker. I publish the data on the mqtt server with a raspberry and mqttx too. I'm using emqx mqtt server and obviously I have added the authentication and the authorization.
crystal pulsarBOT
#

@loud path

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

gilded tiger
#

Your code appears to have a few issues and areas that may require adjustments to work correctly with your EMQX MQTT server setup.

crystal pulsarBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.