icon

UNIHIKER - MQTT real world tutorial

0 200 Medium

The web is full of fantastic UNIHIKER examples and tutorials, but many are designed more for controlled environments than for real-world applications. This project aims to bridge that gap by guiding you through using UNIHIKER with MQTT to set up and configure a IoT smart home environment. You’ll learn practical techniques that bring IoT closer to everyday use, showing you how to turn ideas into hands-on solutions for a connected home.

 

The great thing about this project is that it requires no coding or development skills! All you need to do, is some basic configuration, and I’ll guide you through each step in detail.

STEP 1
PROJECT OBJECTIVE

The goal of this project is to set up UNIHIKER as an MQTT Broker using Mosquitto, allowing seamless communication and control of smart devices in a home environment. We’ll be working with devices like the "Shelly Duo - RGBW" smart bulb and the "Shelly Plug S Gen3" smart plug, which can be operated directly from your mobile device (Android or iPhone) through the "IoT MQTT Panel" app — a free, user-friendly option for mobile MQTT control.

 

While Shelly devices offer a range of advanced features, I will keep it straightforward: we’ll focus on turning devices on and off remotely via the mobile app. By the end of this project, you’ll have a robust foundation for setting up and managing IoT devices in a smart home, all powered by MQTT communication with UNIHIKER as the hub.

HARDWARE LIST
1 UNIHIKER
1 Shelly Duo - RGBW
1 Shelly Plug S Gen3 White
1 Mobile/Tablet (Android or iOS)
STEP 2
SOFTWARE LIST
STEP 3
IMPORTANT PREREQUISITE

All devices used  must be on the same WLAN (UNIHIKER, Shelly devices and your mobile)! In addition, Internet connection is required to install Mosquitto on UNIHIKER and to update Shelly devices with latest Firmware.

 

However, this guide will not go into this important point in depth. You can do this without any additional help!

STEP 4
INSTALL MOSQUITTO ON UNIHIKER

UNIHIKER includes a basic built-in IoT service, but it lacks the advanced features required for this project. To enable the necessary functionality, we’ll use Eclipse Mosquitto as our MQTT broker. Before installing Mosquitto, make sure to disable the existing SioT service to avoid any conflicts.

 

Disable SIoT on UNIHIKER

 

You can disable the SIoT service via different options. One option is done directly via the UNIHIKER touchscreen and the other via the web interface.

 

 

SSH connection and installation

 

To install and configure mosquitto, you need to connect via SSH into UNIHIKER from a local device. If you haven't changed something, the user is "root" and the password is "dfrobot".

CODE
# establish an SSH connection (from local device to UNIHIKER)
$ ssh [email protected]

As soon the SSH connection is successfully done, you can install the required Mosquitto packages on UNIHIKER via the package manager "apt".

CODE
# update repositories and install latest packages
$ apt update && apt upgrade -y

# install required package
$ apt install -y mosquitto

# install optional packages (optional)
$ apt install -y vim tree mosquitto-clients

# start mosquitto service after boot
$ systemctl enable mosquitto.service

# get service status (optional)
$ systemctl status mosquitto.service

Done! Please don't close the SSH connection.

STEP 5
CONFIGURE MOSQUITTO ON UNIHIKER

If the installation was successful, Mosquitto should already be running, but without any protection. Let's change that right now!

CODE
# stop mosquitto
$ systemctl stop mosquitto.service

# create empty mosquitto configuration file
$ touch /etc/mosquitto/conf.d/secure.conf

# create empty mosquitto passwd file
$ touch /etc/mosquitto/mqttpasswd

# create empty mosquitto acl file
$ touch /etc/mosquitto/user.acl

# verify mosquitto folders and files (optional)
$ tree /etc/mosquitto/
/etc/mosquitto/
├── ca_certificates
│  └── README
├── certs
│  └── README
├── conf.d
│  ├── README
│  └── secure.conf
├── mosquitto.conf
├── mqttpasswd
└── user.acl

Now use your favorite text editor and add following content into the files. The example will make use of "vim".

 

secure.conf

CODE
# edit file secure.conf
$ vim /etc/mosquitto/conf.d/secure.conf
CODE
listener 1883

allow_anonymous false
password_file /etc/mosquitto/mqttpasswd
acl_file /etc/mosquitto/user.acl

listener 1883 - specifies that Mosquitto will listen for MQTT connections on port 1883 (standard port for MQTT communication).

 

allow_anonymous false - Setting allow_anonymous to false requires all clients to authenticate before connecting. This enhances security by preventing unauthorized access.

 

password_file /etc/mosquitto/mqttpasswd - This directive points Mosquitto to a password file, where we’ll store usernames and passwords for clients allowed to connect.

 

acl_file /etc/mosquitto/user.acl - The acl_file specifies an Access Control List (ACL) that defines permissions for each user, such as which topics they can publish or subscribe to.

 

mqttpasswd

CODE
# edit file mqttpasswd
$ vim /etc/mosquitto/mqttpasswd
CODE
admin:test123
shelly_bulb:test456
shelly_plug:456test
mobile:test789

The users and passwords are defined in plain text in this step. The passwords will be hashed later.

 

admin - Will be used for "MQTT Explorer"

 

shelly_bulb - Will be used for "Shelly Duo - RGBW"

 

shelly_plug - Will be used for "Shelly Plug S Gen3 White"

 

mobile - Will be used for "IoT MQTT Panel"

 

user.acl

 

At this point we have a “chicken/egg” problem. The following configuration requires information that we previously need from the Shelly devices.

 

You have to go to the Shelly plug web interfaces and note this information. In the example I show my information and configuration, please adapt "shellyplusplugs-e86beaeab358" to your environment!

 

 

Note: Your MQTT prefix will be different!

CODE
# edit file user.acl
$ vim /etc/mosquitto/user.acl
CODE
user admin
topic readwrite #
topic read $SYS/#

user shelly_bulb
topic readwrite shellies/#

user shelly_plug
topic readwrite shellyplusplugs-e86beaeab358/#

user mobile
topic read shellies/+/light/+/power
topic write shellies/+/color/+/command
topic write shellyplusplugs-e86beaeab358/rpc

pattern write $SYS/broker/connection/%c/state

In the configuration we have defined few users (admin, shelly_bulb, shelly_plug and mobile) with topics they can subscribe (read) and publish (write). To make it a bit simpler, we are using MQTT Wildcards (like + and #). The single-level wildcard is represented by the plus symbol. The multi-level wildcard covers multiple topic levels. It is represented by the hash symbol.

STEP 6
FINALIZE MOSQUITTO CONFIGURATION ON UNIHIKER

Now we convert the plain text passwords into hashed values. Execute this command only one time! It does not detect whether passwords are already hashed, so using it on a password file that already contains hashed passwords will generate new hashes based on the old hashes and render the password file unusable.

CODE
# convert plain text to use hashed passwords
$ mosquitto_passwd -U /etc/mosquitto/mqttpasswd

# verify file (optional)
$ cat /etc/mosquitto/mqttpasswd

Mostly done! Let's finish the configuration and start mosquitto.

CODE
# change file permissions
$ chmod 0640 /etc/mosquitto/mqttpasswd
$ chmod 0640 /etc/mosquitto/user.acl

# start mosquitto service
$ systemctl start mosquitto.service

# get service status (optional)
$ systemctl status mosquitto.service
STEP 7
CONNECT SHELLY DEVICES

If the MQTT broker is running on the UNIHIKER, we can start to connect the Shelly devices. The easiest way to do this step is via the respective web interfaces. You can access these web interfaces in the browser with the URL http://[IP address]. Here are the examples of my configuration.

 

Shelly Duo - RGBW

 

 

Note: The IP address used in the picture will be different for you! Reboot the device after saving the configuration.

 

Shelly Plug S Gen3 White

 

 

Note: The IP address used in the picture will be different for you! Reboot the device after saving the configuration.

 

MQTT Explorer

 

For testing and debugging you can install "MQTT Explorer" on your computer. Use the connection credentials of the user “admin” here!

 

 

After a few seconds you should see information about the broker itself and the respective topics.

 

 

Note: If you cannot see anything, check your network and Mosquitto configurations and if Mosquitto is running.

STEP 8
CREATE MOBILE DASHBOARD

Now we use the free version of “IoT MQTT Panel” to control the Shelly devices and to display information. Please install the app and create a connection to the broker (UNIHIKER) on the same WiFi.

 

 

Note: The IP address used in the picture will be different for you!

 

Now create a new Dashboard with the connection and start to create the panels.

 

Plug Power Switch

 

 

The plug uses RPC topic and JSON payload for ON/OFF.

CODE
# power on
{
  "method": "Switch.Set",
  "params": {
    "id": 0,
    "on": true
  }
}

#power off
{
  "method": "Switch.Set",
  "params": {
    "id": 0,
    "on": false
  }
}

Bulb Power Switch

 

 

Bulb Watt Arc

 

 

Final Dashboard

 

STEP 9
Annotation

MQTT is widely used for IoT because of its lightweight messaging protocol, ideal for remote connections where bandwidth may be limited. UNIHIKER is a great device to act as the MQTT Broker, centralizing the control and communication of IoT devices in your smart home setup.

 

Consider expanding beyond Shelly devices to integrate a variety of smart home products, such as temperature sensors, motion detectors, or smart door locks. Many IoT devices support MQTT and can be added to the same network, enhancing your smart home’s capabilities.

License
All Rights
Reserved
licensBg
0