Mosquitto Mqtt|Publish and Subscribe examples-Part 2

 The Eclipse moquitto provides the open source message Broker which supports the Mqtt Protocol versions 5.0,3.11,3,1.The Mqtt protocol is a very lightweight and can be operated on microcontroller based systems like Raspberry Pi to computer PC servers. The Mqtt clients can be operated from the small microcontroller based sensor which are constrained.

The Mosquitto Brokers can be installed on your systems by visiting the Eclipse mosquitto Download page.Here the setup file suitable to various os can be downloaded and installed. IF want to How to setup the Mosquiito Broker on WIndows PC then click here.

Once the Broker is setup on the system. We can initiate testing of the Mosquitto Broker by using mosquitto Publish and Subscribe Commands. To know some details about the Mqtt Publish and Subscribe operation details visit the blog Publish and Subscribe-Mqtt.


Remote Mqtt Communication

When we run Mqtt Broker on our windows PC and want communication with remote clients then we need to add some commands in the configuration file otherwise connections from the remote clients will get rejected.

In order to do so, create new configuration file called test.conf file in the same folder where the Mosquitto Broker application file is located

Add the following statements in the file.

listener  1883 

allow_anonymous true

listener shows that the Broker is going to listen on port 1883.

setting  allow_anonymous true allows remote connections with Broker without any username or password.

Now we can run  the following command from the command prompt terminal

mosquitto -c test.conf -v 

where c stands for specifying file name with  path. Here it is  test.conf

>cd c:\mosquitto

c:\mosquitto>mosquitto -c test.conf -v

1658559064: mosquitto version 2.0.14 starting

1658559064: Config loaded from test.conf.

1658559064: Opening ipv6 listen socket on port 1883.

1658559064: Opening ipv4 listen socket on port 1883.

1658559064: mosquitto version 2.0.14 running

With this command the mosquitto Broker starts running on public ip of the PC. But you get windows defender firewall  popup(as shown below) asking whether to block the process or allow.Click on Allow access button to allow remote connections. 



Now the mosquitto Broker is available for connection on the public ip of your computer.

The public of your pc can be found by running command ipconfig for the command prompt of window based pc.






Publish message using host address as ip

Publish the message from client using host address as 192.168.xx.xxx
Run the following command from new command prompt terminal.

mosquitto_pub -h 192.168.xx.xxx   -t test -m hello  -i clientPub -r -d

note that we are using hostname(-h) as public ip of the pc.

c:\mosquitto>mosquitto_pub -h 192.168.xx.xxx   -t test -m hello  -i clientPub -r -d
Client clientPub sending CONNECT
Client clientPub received CONNACK (0)
Client clientPub sending PUBLISH (d0, q0, r1, m1, 'test', ... (5 bytes))
Client clientPub sending DISCONNECT


Log for mosquitto Broker Terminal.

1658560865: New connection from 192.168.xx.xxx:52xxx on port 1883.

1658560865: New client connected from 192.168.xx.xxx:52xxx as clientPub (p2, c1, k60).

1658560865: No will message specified.

1658560865: Sending CONNACK to clientPub (0, 0)

1658560865: Received PUBLISH from clientPub (d0, q0, r1, m0, 'test', ... (5 bytes))

1658560865: Received DISCONNECT from clientPub

1658560865: Client clientPub disconnected.

Note here, the new client has got connected on ip address 192.168.xx.xxx and published message is received by the Broker.


Subscribe to the Broker using host address as ip

Run the following command from the new command prompt terminal.

mosquitto_sub -h 192.168.xx.xxx  -t test   -i clientSub -d

Note that the host address set is  192.168.xx.xxx.

log

c:\mosquitto>mosquitto_sub -h 192.168.xx.xxx -t test   -i clientSub -d

Client clientSub sending CONNECT

Client clientSub received CONNACK (0)

Client clientSub sending SUBSCRIBE (Mid: 1, Topic: test, QoS: 0, Options: 0x00)

Client clientSub received SUBACK

Subscribed (mid: 1): 0

The client has subscribed to the mosquitto Broker over  192.168.xx.xxx ip.


Log for mosquitto Broker

c:\mosquitto>mosquitto -c test.conf -v

1658568051: mosquitto version 2.0.14 starting

1658568051: Config loaded from test.conf.

1658568051: Opening ipv6 listen socket on port 1883.

1658568051: Opening ipv4 listen socket on port 1883.

1658568051: mosquitto version 2.0.14 running

1658568137: New connection from 192.168.xx.xxx:63xxx on port 1883.

1658568137: New client connected from 192.168.xx.xxx:63xxx as clientSub (p2, c1, k60).

1658568137: No will message specified.

1658568137: Sending CONNACK to clientSub (0, 0)

1658568137: Received SUBSCRIBE from clientSub

1658568137:     test (QoS 0)

1658568137: clientSub 0 test

1658568137: Sending SUBACK to clientSub

The Broker has received the connection request from the clientSub over ip  192.168.xx.xxx over the topic test.

Now Publish message from clientPub.using command 

 mosquitto_pub -h 192.168.xx.xxx   -t test -m hello  -i clientPub 


Log for the Broker Terminal

1658568627: New connection from 192.168.xx.xxx:63xxx on port 1883.

1658568627: New client connected from 192.168.xx.xxx:63xxx as clientPub (p2, c1, k60).

1658568627: No will message specified.

1658568627: Sending CONNACK to clientPub (0, 0)

1658568627: Received PUBLISH from clientPub (d0, q0, r1, m0, 'test', ... (5 bytes))

1658568627: Sending PUBLISH to clientSub (d0, q0, r0, m0, 'test', ... (5 bytes))

1658568627: Received DISCONNECT from clientPub

1658568627: Client clientPub disconnected.

Note that the Broker has received the message over ip 192.168.xx.xxx  and same message is delivered to the Subscriber client.


Log from Subscriber terminal

Client clientSub sending PINGREQ

Client clientSub received PINGRESP

Client clientSub received PUBLISH (d0, q0, r0, m0, 'test', ... (5 bytes))

hello

Client clientSub sending PINGREQ

The clientSub received the message hello over ip topic test.


Related Blogs:

 Install Mosquitto Mqtt Broker on Windows based PC


Related videos:




Post a Comment

Previous Post Next Post