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.