Introduction
MQTT is a lightweight publish/subscribe messaging protocol. It is useful for use with low power sensors, but is applicable to many scenarios. This guide cover how to install and test mosquitto broker on OpenWrt router. The scheme proposed is as follow.
The standard router is connected to the internet, one of its wifi radio on 2.4ghz provide the SSID that the OpenWrt Router is connected to.
Why this scheme?
-Standart router do not allow to install custom softwares like Mosquitto broker.
-Any devices connected to the Standard router WIFI can use the broker installed on the openwrt router, additionally any devices connected to the openwrt router can use the broker.
####### Installing mosquitto in openwrt Glinet Ar150 #####
1-Make sure Glinet router is connected to the internet
2-open ssh connection with the router and type the following commands
opkg update
opkg install mosquitto mosquitto-client libmosquitto
#### testing
/etc/init.d/mosquitto enable
/etc/init.d/mosquitto restart
mosquitto_sub -h localhost -t test
mosquitto_pub -h localhost -t test -m “hello world”
#### adding user & password####
mosquitto_passwd -c /etc/mosquitto/passwd donald //donal is the user , you will be prompted to enter password.
Password–> duck
Re Enter password–> duck
#### then open mosquitto conf file ### you need to install nano on your router to be able edit the file. As alternative one can use WinScp to connect and edit files on the router.
nano /etc/mosquitto/mosquitto.conf
/ / edit the following lines:
allow_anonymous false // tell mosquito to block not authenticated connections
password_file /etc/mosquitto/passwd // tell mosquitto where the password files is located
/etc/init.d/mosquitto restart // restart mosquitto
mosquitto_sub -h localhost -t test // try subscribe a topic without authentication
Connection Refused: not authorised. // this is the response you will get
### try to subscribe with the credential entered before :
mosquitto_sub -h localhost -t test -u “donald” -P “duck”
mosquitto_pub -h localhost -t “test” -m “hello world” -u “donald” -P “duck”
#### One more last change###
nano /etc/mosquitto/mosquitto.conf //open mosquitto config and change the standard port
port 1886 / Why? Because in my case I have other mosquitto broker running on my local network and already using the 1883 port, if that is not you case you might leave at default the standard port 1883.
/etc/init.d/mosquitto restart // type on SSH this and restart mosquitto
## now let’s try again with different port:
mosquitto_sub -h localhost -p 1886 -t test -u “donald” -P “trump”
##in a second SSH window publish something with topic “test”
mosquitto_pub -h localhost -p 1886 -t “test” -m “hello world” -u “donald” -P “duck”
Testing the Mqtt broker on Node-red
###CONFIG FILES GLinet###
nano /etc/config firewall //Firewall file add this at the end :
config rule
option target ‘ACCEPT’
option src ‘wan’
option proto ‘tcp udp’
option dest_port ‘1886’
option name ‘mosquitto’
#### it was also required to set static IP to the wwan config### → nano /etc/config/network
config interface ‘wwan’
option _orig_ifname ‘radio0.network2’
option _orig_bridge ‘false’
option proto ‘static’
option ipaddr ‘192.168.2.11’
option netmask ‘255.255.255.0’
option gateway ‘192.168.2.1’
option broadcast ‘255.255.255.255’
Leave A Comment