The following python code on my raspberrypi doesn't connect to my mqtt broker, it just hangs after printing Connecting...:
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("test")
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client(client_id="",clean_session=True,userdata=None,protocol=mqtt.MQTTv311,transport="tcp")
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(username="stackoverflow",password="stackoverflow")
print("Connecting...")
client.connect("learn.evermight.net", 9101, 10)
client.loop_forever()
What did I do wrong with my Python code?
Command Line Success
I confirmed that my mqtt does work because I can subscribe from terminal with this command:
mosquitto_sub -h learn.evermight.net -p 9101 -t "test" -u "stackoverflow" -P "stackoverflow" --capath /etc/ssl/certs/
And I will see messages in my terminal as soon as I run this command from another terminal
mosquitto_pub -h learn.evermight.net -p 9101 -t "test" -u "stackoverflow" -P "stackoverflow" -m "hello world" --capath /etc/ssl/certs/
What is wrong with my python code?
NodeJS Success
Additionally, the following NodeJS code also works for connecting and publishing to my MQTT server.
const mqtt = require('async-mqtt');
try{
const client = await mqtt.connectAsync("mqtts://learn.evermight.net",{
port:9101,
host:"mqtts://learn.evermight.net",
username:"stackoverflow",
password:"stackoverflow",
connectTimeout:5000,
protocolId:"MQIsdp",
protocolVersion:3,
encoding:"utf8",
keepalive: 60
});
await client.publish("test","hello world");
await client.end();
} catch(e) {
console.log(e);
}
Website JavaScript Success
And the following code can also connect to a websocket port via web browser javascript website, subscribe to the test topic and receive published messages (note that my websockets use port 9102)
import Paho from "paho-mqtt";
const client = new Paho.Client("learn.evermight.net",9102,"WebBrowser");
client.onConnectionLost = response=>console.log("lostMQTTConnection: " +(response.errorCode !== 0 ? response.errorMessage : "Unknown MQTT Error" ));
client.onMessageArrived = message=>console.log(message.payloadString);
client.connect({
onSuccess:_=>client.subscribe("test"),
useSSL:true,
userName:"stackoverflow",
password:"stackoverflow",
});
loop_forever()should runs code all the time until you kill it. - furas--capath /etc/ssl/certs/. When I run your code with my broker then it runs, and when I runmosquitto_pubwithout--capath /etc/ssl/certs/then it also runs but I getError: Connection refusedwhen I use--capath /etc/ssl/certs/. Probably in code you would have to use something similar to--capath /etc/ssl/certs/- furasmosquitto_sub/mosquitto_pubso I can connect. There are commandclient.tls_set()andclient.tls_set_context()and maybe they could help. But I never had to use it. I have own MTTQ in local network so I didn't bother to usecerts- furas