Install NanoMQ MQTT Broker on Raspberry Pi

install-nanomq-mqtt-broker-on-raspberry-pi
Spread the love

Prerequisites for Installing a NanoMQ MQTT Broker on Raspberry Pi.

  1. First, choose your raspberry. Here, we use Raspberry Pi 3b+ as an example.
  2. Install the OS. You can install Raspberry Pi OS.
  3. Connect Raspberry Pi via SSH.

Install and Configure NanoMQ MQTT Broker

Install NanoMQ

Raspberry has an arm64 CPU. So we choose this package.

$ wget https://github.com/nanomq/nanomq/releases/download/0.21.8/nanomq-0.21.8-linux-arm64-full.deb

Then install this package via apt.

$ sudo apt install ./nanomq-0.21.8-linux-arm64-full.deb

We now finished the installation.

$ nanomq --version

Usage: nanomq { start | stop | restart | reload } [–help]

NanoMQ Messaging Engine for Edge Computing & Messaging bus v0.21.8-8 Copyright 2023 EMQ Edge Computing Team.

Configuration

You can find these configuration files in the etc directory. Peripheral functions such as authentication can use separate configuration files (specified by the include method). The main configuration files include:

Configuration                      File Description

etc/nanomq.conf                    Main NanoMQ configuration file

etc/nanomq_pwd.conf                NanoMQ username and password auth config

etc/nanomq_acl.conf                NanoMQ ACL access control auth config

etc/nanomq_vsomeip_gateway.conf    NanoMQ SOME/IP gateway config (for nanomq_cli)

etc/nanomq_dds_gateway.conf        NanoMQ DDS gateway config (for nanomq_cli)

etc/nanomq_bridge.conf             NanoMQ bridge config (for nanomq_cli)

etc/nanomq_zmq_gateway.conf        NanoMQ ZeroMQ config (for nanomq_cli)

Here, we edit the configuration file of NanoMQ /etc/nanomq.conf and etc/nanomq_pwd.conf.

Edit the configuration file of NanoMQ /etc/nanomq.conf (Main NanoMQ configuration file). In this configuration,

$ sudo chmod 777 /etc/nanomq.conf
$ sudo apt install vim
$ vim /etc/nanomq.conf
auth {
  allow_anonymous = false
  no_match = deny
  deny_action = ignore
  cache {
    max_size = 1024
    duration = 1m
  }
  password = {include "/etc/nanomq_pwd.conf"}
  #acl = {include "/etc/nanomq_acl.conf"}
}

Edit the configuration file of NanoMQ etc/nanomq_pwd.conf(NanoMQ username and password auth config). In this configuration,

$ sudo chmod 777 etc/nanomq_pwd.conf
$ vim /etc/nanomq_pwd.conf
#Write "username":"password" in this way.
#admin: public
#client: public
rpi: rpi@123
Start NanoMQ
$ nanomq start --conf /etc/nanomq.conf

For more information about the configuration of NanoMQ, please refer to: NanoMQ Docs.

Upon completion, the EMQX open-source MQTT broker should be running on your Raspberry Pi as a service. Meaning, the broker will start on boot. In such cases systemd in Raspberry Pi helps to configure services which can be managed. To do so follow the following steps.

Create a Systemd service in Raspberry Pi

Follow the following steps:

1. Go to /etc/systemd/system directory.

$ cd /etc/systemd/system

2. Create a file named nanomq-service.service and include the following:

[Service]
User=<user e.g. pi>
WorkingDirectory=<directory_of_script e.g. /home/pi>
ExecStart=nanomq start --conf /etc/nanomq.conf
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target

3. Reload the service files to include the new service.

sudo systemctl daemon-reload

4. Start your service

sudo systemctl start nanomq-service.service

5. To check the status of your service.

sudo systemctl status nanomq-service.service

6. To enable your service on every reboot.

sudo systemctl enable nanomq-service.service

Testing NanoMQ MQTT Broker on Raspberry Pi

Open two additional terminals.

In terminal 1:

$ nanomq_cli sub -p 1883 -t topic1

In terminal 2:

$ nanomq_cli pub -p 1883 -t topic1 -m "Hello-NanoMQ"

If the nanomq_cli in terminal 1 gets the message “Hello-NanoMQ,” it shows that you have successfully deployed NanoMQ on Raspberry Pi.

Testing NanoMQ MQTT Broker using MQTTX on Windows

  1. Download and Install MQTTX Client Toolbox.
  2. Connect to NanoMQ MQTT Broker that you have deployed NanoMQ on Raspberry Pi.
  3. Subscribe to topic1
  4. Publish to topic1

Conclusion

Installing an MQTT NanoMQ broker on Raspberry Pi offers significant advantages, from local processing to scalability. NanoMQ’s lightweight design and ease of deployment make it an excellent choice for Raspberry Pi-based projects.

Also Read: What Is Raspberry Pi And Sense HAT


Spread the love

Be the first to comment

Leave a Reply

Your email address will not be published.


*