MQTT Broker on Orange Pi Zero (Armbian)

 Posted by:   Posted on:   Updated on:  2021-04-03T19:44:47Z

Install Armbian and configure it for a MQTT broker on Orange Pi Zero single board computer

Some time ago I was attempting to install the Mosquitto MQTT broker software on an old router with OpenWrt firmware. I no longer think that is the best option for a self-hosted MQTT broker with the advent of single board computers (SBC) like Raspberry Pi and Orange Pi. To my surprise, the Orange Pi Zero board (with quad core 1 GHz CPU and 256 MB RAM) requires less power to run than an old router (300 MHz single core CPU and 64 MB RAM).

With sufficient processing capabilities, the SBC can run more than the MQTT broker. I’m thinking of turning this Orange Pi board into a Home Automation Gateway which manages local devices and makes data available in a web interface. There is plenty of software support for what I want (I’m thinking of Node-RED with a web-based dashboard). Another good candidate is Raspberry Pi, but because it does not have wired network port (Zero version), I chose Orange Pi.

MQTT Broker on Orange Pi Zero (Armbian)

If you take a look at my old posts about Mosquitto on OpenWrt router, you will see I came with a complex configuration. That is exactly I want to get on Orange Pi too. The server should listen for connections on three ports: a port for unsecured connections used only by local network devices, a PSK secured port for some remote clients and a TLS secured port mainly for Internet access. The unsecured port and PSK secured port will be blocked by a firewall, thus not accessible from outside of LAN.

I got an Orange Pi Zero and a 32 GB SD Card. I also got some heatsinks advertised as Raspberry Pi compatible. I placed one on the AllWinner SoC since it usually gets to 50 degrees Celsius. Besides these, I also got a LM2596 voltage converter that I adjusted for precise 5 V output. I will use it to power the SBC from a 12 V supply. I have in mind getting a battery backup 12 V power supply, that’s why I want 12 V compatibility.

Orange Pi Zero SBC, the SD card and power module

Orange Pi Zero SBC, the SD card and power module

I opted to install Armbian on Orange Pi. There are actually two versions, one uses Ubuntu software repositories, while the other uses Debian repos. They are named after the Ubuntu/Debian version they are using. My choice was Armbian Buster, configured for Debian software.

After downloading and extracting firmware image (with 7-Zip) I get an IMG file. This can be burned to SD card using Rufus on Windows or with dd on Linux (after a quick format of the card). Obviously, a card reader is required for this step. Once it is done, the SD card may be inserted in the unpowered Orange Pi.

Write Armbian to SD card using Rufus

Write Armbian to SD card using Rufus

Further on, connect it with an Ethernet cable and apply power on USB port or using the power pins from the existing headers. By default, it will obtain the IP address from existing DHCP server, and we need to find it to be able to connect to the SBC. First option would be to log in to the router management page and look at DHCP clients list. All routers which provide DHCP server will display this information somewhere in the management interface. Second option is to use a network scanner. Even your phone connected to WiFi, with Ping Tools app should be able to detect LAN clients.

Find Orange Pi IP in router management page

Find Orange Pi IP in router management page

If you can’t find IP address, you’ll have to use the serial port. If it doesn’t appear as a client in LAN, make sure the green LED on Orange Pi is on as an indication it has successfully booted from SD. A serial port is available over the USB port (you can power Orange Pi from a USB port on your computer and have access to the serial port ttyGS0). There is also a 3-pin header serial port on the board, next to Ethernet connector (3.3 V voltage levels), which requires a USB-Serial adapter such as CH340 or FT232. Any of these may be used. If you are able to log in, the IP address will be displayed on the welcome screen.

For the rest of this guide, I will assume everything went right and you know the IP of Orange Pi. On both Windows Powershell and Linux terminal, you should be able to connect using ssh root@orange_pi_ip. Accept connection with yes and you should see the welcome screen after you input the root password which is 1234. As an alternative, one may use PuTTY to connect over SSH.

Unless you are using the serial port method with your own USB-Serial adapter, any other connection to Armbian terminal will be reset upon reboot. So, if you want to see bootloader output, you need a serial port adapter.

First SSH connection to Orange Pi Armbian

First SSH connection to Orange Pi Armbian

You can’t go further until you change this password to something better. Follow instructions and enter twice the new root password. Then you are asked to select default shell (I choose bash) and create your user account. Follow prompts and enter username, password and full name. Your account will be created and since it is sudo enabled this is what you should use from now on. Timezone will be automatically detected and locales will be generated. After this process is completed, I highly recommend disconnecting from this root session and reconnecting using your account. Type exit and you should be disconnected (or close terminal/PuTTY window).

Switched from root to user account

Switched from root to user account

On the new session, log in with your username and password. It is time to complete network configuration. Let’s run sudo armbian-config (enter your user password when prompted).

There are two approaches. You may leave default network configuration on Orange Pi and configure the router DHCP server to always assign the same IP to the SBC. Or, as I prefer, configure Orange Pi with fixed (static) IP in the same subnet as the router, but outside of the range of DHCP allowable addresses (i.e., I have my router set to assign IP addresses from 192.168.1.11 to 192.168.1.254 and I will set Orange Pi board to static 192.168.1.4 since this one cannot be already assigned by DHCP server). Go to Network, IP, eth0 and change to Static. Make required edits, then hit Tab and select OK.

Configure wired network witj static IP on Armbian

Configure wired network witj static IP on Armbian

After you hit Enter, you will lose SSH connection. You have to log in again on the new IP address. Disconnect and reconnect Ethernet cable if you can’t connect on the new IP, but avoid cutting power to Orange Pi. Launch armbian-config again. Go to Personal section and edit settings if needed (timezone could have been automatically detected, I choose to set the keyboard layout back to US and I changed hostname to something else). This is an optional step, but you can go to System and enable Avahi (multicast DNS). With this, if there is a running web interface on Armbian, it can be accessed from LAN at the address: hostname.local:port (where mDNS is supported).

Before installing software, I like to have DNS servers set to 8.8.8.8 and 8.8.4.4. To do this, we must edit a file with the following command:

sudo nano /etc/systemd/resolved.conf

Make sure to un-comment and edit the following lines:

[Resolve]
DNS=8.8.8.8 2001:4860:4860::8888
FallbackDNS=8.8.4.4 2001:4860:4860::8844
#Domains=
#LLMNR=yes
#MulticastDNS=yes

Press Ctrl+O, then Enter and Ctrl+X and reboot. Once you are able to reconnect, let’s update software and install Mosquitto broker.

sudo apt update && sudo apt upgrade
sudo apt install mosquitto mosquitto-clients

The server is up and running with the default configuration.

Mosquitto is running on Armbian

Mosquitto is running on Armbian

In the next post I will complete the configuration of Mosquitto. And by the way, I mentioned reboot during this guide. Do not cut power to Orange Pi to shut it down or reboot it. Use terminal commands sudo poweroff and sudo reboot. When the board's green LED is back on, reconnect over SSH.

No comments :

Post a Comment

Please read the comments policy before publishing your comment.