MQTT Broker - client disconnected, not authoris - mqtt

I created a mosquitto broker on an Ubuntu Linux machine.
when i'm connecting from MQTT.fx software, it looks ok, im able to connect.
but when i'm turning on a device that connect to the broker i'm getting this message on the broker log file :
> 1652968173: New connection from 84.110.147.52:22942 on port 1883.
> 1652968173: Sending CONNACK to M_B2:CC:4C:42:A6:FE (0, 5) 1652968173:
> Client M_B2:CC:4C:42:A6:FE disconnected, not authorised.
the configuration file has this in it:
listener 1883
password_file /home/passwordfile.txt
log_dest file /home/mosquitto_logs.txt
log_type all
the passwordfile.txt is ok since i'm able to connect via MQTT.fx
any idea what can be the issue?
is there more relevant information i can share?
Thank you

Related

Looking for a working example of a non-secure bridge between a local Mosquitto instance and the Watson IoT platform

I'm trying to establish a non-secure bridge from Mosquitto running on Home Assistant (home-assistant.io) to the MQTT broker on the Watson IoT platform. Why non-secure? To take out additional complexity as I can't connect. Once this step is successful I'll move on to secure the connection.
Firstly I have setup the Watson IoT platform and created a gateway device type ('ha-iotp-gateway') and device ('ha'). No other changes to the IoT platform.
I then created a bridge config file to connect Mosquitto with the Watson IoT platform broker by adopting the conf shared in this post and now have the following:
connection bridge-to-watsoniot
address xxxxxx.messaging.internetofthings.ibmcloud.com:1883
cleansession true
try_private false
bridge_attempt_unsubscribe false
notifications false
notification_topic iot-2/type/ha-iotp-gateway/id/ha/evt/status/fmt/raw
remote_username use-token-auth
remote_password <token>
remote_clientid g:xxxxxx:ha-iotp-gateway:ha
topic iot-2/type/+/id/+/cmd/+/fmt/+ in iot-2/type/+/id/+/cmd/+/fmt/+
topic iot-2/type/+/id/+/evt/+/fmt/+ out iot-2/type/+/id/+/evt/+/fmt/+
connection_messages true
The Mosquitto log file shows a connection attempt
1606909705: Connecting bridge bridge-to-watsoniot (xxxxxx.messaging.internetofthings.ibmcloud.com:1883)
1606909706: Socket error on client local.g:xxxxxx:ha-iotp-gateway:ha, disconnecting.
1606909707: New connection from 172.30.32.1 on port 1883.
[INFO] found homeassistant on local database
1606909707: New client connected from 172.30.32.1 as auto-BD9CD1CE-BE73-6CBA-196D-C02E83268AEB (p2, c1, k60, u'homeassistant').
1606909712: Connecting bridge bridge-to-watsoniot (xxxxxx.messaging.internetofthings.ibmcloud.com:1883)
1606909723: Connection Refused: not authorised
1606909723: Socket error on client local.g:xxxxxx:ha-iotp-gateway:ha, disconnecting.
The gateway device on the Watson IoT platform (created earlier) shows connected and the connection log shows
Closed connection. The operation is not authorized.
Has anyone been successful in establishing a non-secure connection to Watson IoT? I'm not sure whether the IoT platform is correctly configured or my bridge config has issues - or both.

Mosquitto server hosted on Digital ocean not making remote connections with android and Eclipse Paho Javascript Clients

I hosted Mosquitto MQTT broker on Ubuntu in Digital Ocean Cloud droplet. It's domain name is instrux.live and IP of the domain is 192.34.63.138. I publish and subscribe messages from two terminal windows and it worked fine.
When I tried to connect it with the android using Paho MQTT android client it could not connect. I also checked its connection with the MyMQTT android application and it did not connect. After making sure the android code has not any error, I again checked its connection with the Eclipse Paho Javascript client on eclipse.org website and it did not connect. Now, I have realized there might be something missing or wrong in my broker configuration which is not allowing it to get connected with other clients remotely. I have also opened firewall for ports 1883 for Android client and 8083 for the Javascript client and still it is not connecting.Here is mosquitto configuration file:
allow_anonymous true
password_file /etc/mosquitto/passwd
listener 1883
protocol mqtt
listener 8083
protocol websockets
Since the error is "Connection Refused" rather than a timeout I suspect that you have not configured the firewall to allow inbound traffic on port 8083.
You may need to do this both on the machine it's self and in Digital Ocean's console.

How to get client IP using Mosquitto MQTT

I would like to log the IP address of clients using Mosquitto MQTT. I am using Paho - Python on the server side and Arduino - PubSubClient on the client side.
In PHP, it is quite simple, I just use $_SERVER['REMOTE_ADDR']. Is there something similar for Mosquitto?
Mosquitto records when new clients connect if logging is enabled:
Oct 13 15:09:32 bagend mosquitto[1361]: 1476367772: New connection from 127.0.0.1 on port 1883.
Oct 13 15:09:32 bagend mosquitto[1361]: 1476367772: New client connected from 127.0.0.1 as mosqsub/18943-bagend (c1, k60).
This shows a new client connecting from localhost and includes the client id mosqsub/18943-bagend
There is no way to get it from another MQTT client as every client has no knowledge of any other clients connected to the broker.
EDIT: The mosquitto.conf man page implies you may be able to log to a MQTT topic using the following in the config file:
log_dest topic
This logs to a topic of $SYS/broker/log/<log level>
so you get something like this when subscribing with mosquitto_sub -v -t '$SYS/broker/log/#'
$SYS/broker/log/N 1476378785: New connection from 127.0.0.1 on port 1885.
$SYS/broker/log/N 1476378785: New client connected from 127.0.0.1 as mosqpub/4654-tiefighter (c1, k60).
$SYS/broker/log/N 1476378785: Client mosqpub/4654-tiefighter disconnected.
I just set the IP as the client ID. Be careful with this though. If anyone has taken up this client ID already, you will get disconnected with reason code 1.
Alternatively, you can use the user data field.

Setting up Paho Javascript Client (MQTT) with mosquitto on AWS EC2 Ubuntu + Ruby on Rails

I have been trying to setup a MQTT broker on my AWS EC2 server, using the port 1883. So far it works with the ruby-mqtt gem, but I am having trouble with setting it up with Paho Javascript Client for the website.
What I have done so far:
Mosquitto
Installed mosquitto on my AWS EC2 instance, and it is running and listening on port 1883. I subscribed to a topic locally using the command
mosquitto_sub -h localhost -p 1883 -v -t 'topic1'
AWS EC2 Security Group
Allow traffic over port 1883 (under tcp protocol)
Ruby on Rails
Installed ruby-mqtt gem, and tested the mqtt to be working by running the code below in rails console (development environment)
MQTT::Client.connect(ip_address_or_domain_name) do |c|
c.publish('topic1', 'message to topic 1')
end
The message appears in the terminal where mosquitto_sub was running.
Nginx
All this was done without any configuration on Nginx configuration files.
Paho Client
So I fired up a local rails server on my local computer, and run the example javascript snippet on one of my html view.
// Create a client instance
client = new Paho.MQTT.Client("mqtt.hostname.com", Number(1883), "", "clientId")
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
client.connect({onSuccess:onConnect});
// called when the client connects
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("onConnect");
client.subscribe("topic1");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "topic1";
client.send(message);
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
// called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
}
But I fail to connect. The error I am getting in the chrome developer console is:
WebSocket connection to 'ws://mqtt.example.com:1883/' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET
Im not sure what is wrong here. Greatly appreciate any help! Thanks in advance!
So the problem is Paho Javascript Client states that the parameter for client object has to be
the address of the messaging server, as a fully qualified WebSocket URI, as a DNS name or dotted decimal IP address.
So making it listen to port 1883, which is the standard port for mqtt, will not work.
ruby-mqtt works as it is because it parameter is treated as a mqtt uri
In other words, Paho connects via ws://host while ruby-mqtt connects via mqtt://host. The latter connects to port 1883 with the correct protocol (not sure if this is the right word here) for the
correct port.
So Paho has to connect to another port where the websocket protocol can be used.
This is my solution.
Mosquitto
Version need to be at least 1.4 where websocket is supported. I add the last 3 lines to the default mosquitto.conf file.
# /etc/mosquitto/mosquitto.conf
pid_file /var/run/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
port 1883
listener 1884
protocol websockets
This opens 2 ports for mosquitto to subscribe to over 2 different protocols respectively.
AWS Security Group
Allow traffic over port 1884 (under tcp protocol)
Paho Client
mqtt.hostname.com
change just the line where the client object is initialized to
client = new Paho.MQTT.Client("mqtt.hostname.com", Number(1884), "", "clientId")

Paho MQTT connecting to MQXR broker

I have a mqtt java code (paho) written which works fine when it attempts to connect to Mosquitto broker. It is able to establish, publish all messages to a topic. But with MQXR (broker included with MQ), I get
SEVERE: Could not connect to MQTT broker, requests may be lost.
Connection lost (32109) - java.io.EOFException
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:138)
at java.lang.Thread.run(Thread.java:736)
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:250)
at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:56)
at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:100)
... 1 more
The QoS is set as 2. Not able to find why it fails in MQXR? Any thoughts is greatly appreciated.
Thanks
There are two possibilities for losing connection:
1. Some MQTT Brokers will not support QOS-2 Implementations like Azure IoT-Hub. Verify that your broker supports QOS-2.
2. What Port are you connecting to?. If its SSL/TLS encrypted, then you cannot connect with unencrypted connect process. You have to enable SSL connection using bouncycastle during your connect process.

Resources