You are on page 1of 2

Steps to Install MQTT on windows machine and set password authentication

Step 1 : Run the (mosquitto-1.3.5-install-win32.exe) file.


Step 2 : After successful installation you can find the folder mosquitto on your installed folder.
Step 3 : Create Password for authentication.
a.
b.
c.
d.
e.
f.
g.

Open command prompt browse till mosquitto folder.


Run this command mosquitto_passwd -c /etc/mosquitto/passwd {Password}. This will add a user
to a new password file.
mosquitto_passwd -D /etc/mosquitto/passwd ral. This will Delete a user from a password file
Edit the conf file. Do the changes as below
allow_anonymous false.
user XXXX.
acl_file C:\pwfile.example.

Step 4 : After doing the changes, Open CMD and run the command (mosquitto.conf c mosquitto.conf).
Step 5 : Logger Enable. Uncomment the following
a.
b.
c.
d.
e.

log_type error.
log_type warning.
log_type notice.
log_type information.
connection_messages true

Testing :1.

we can write down java MATT client as bellow:-

public class MQTTReceiving implements MqttCallback {


public static void main(String[] args) {
new MQTTReceiving().doDemo();
}
MqttClient client;
public void doDemo() {
try {
client = new MqttClient("tcp://192.168.1.95:1883", "Sending");
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setPassword("ravi".toCharArray());
mqttConnectOptions.setUserName("ravi");
client.connect(mqttConnectOptions);
client.setCallback(this);
client.subscribe("MQTT");

MqttMessage message = new MqttMessage();


message.setPayload("A single message from my computer fff".getBytes());
client.publish("MQTT", message);
} catch (MqttException e) {
e.printStackTrace();
}
}
public void connectionLost(Throwable arg0) {
System.out.println("connectionLost");
}
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println("Topic:- " + topic + " Message:- " + message);
}
public void deliveryComplete(IMqttDeliveryToken arg0) {
System.out.println("deliveryComplete");
}
}

2. we can use MQTT FX client tool.

You might also like