ayatec logo
Home Store Blog Contact
unicontrol user guide by ayatec
Home Store Blog Contact

Topics

  1. Welcome
  2. Getting started
    1. What is unicontrol
    2. How it works
    3. Connectivity
    4. Integration options
    5. Default hardware
    6. Hardware extensions
    7. Special sensors
    8. RGB driver
    9. Audio driver
    10. Network API - HTTP
    11. Network API - MQTT
    12. Network security
    13. Downloads
  3. Web Interface
    1. Processes
      1. Process
      2. Name
      3. Main state
      4. Initial state
      5. Display
      6. Constraints
        1. Process timer
        2. Variable contraints
        3. Force output
      7. Input
        1. Input source
        2. Channel
        3. Control period
        4. Value
        5. Publish
        6. Frequency
        7. Subtopic
      8. Events
        1. On/Off event
        2. Condition
        3. Idle/running time
        4. Delay on/off
        5. Fade in/out
        6. High/low input
        7. Mid point/Tolerance
        8. Out on when
      9. Output
        1. Primary output
        2. Action
        3. Type
        4. Invert
        5. Duty cycle
        6. Mem value
        7. High/low output
        8. Folder/Track
        9. RGB mode
        10. RGB color
        11. RGB brightness
        12. RGB speed
        13. Publish
        14. Subtopic
        15. Secondary outputs
      10. Cycle
        1. Cycles
        2. High/Low phase
      11. On/Off button
    2. Peripheral
      1. Hardware
      2. Type
      3. Calibration
      4. Mapping (edit)
      5. IR remote (edit)
      6. Impulse_counter (edit)
      7. RGB driver (edit)
      8. DS18B20 (edit)
    3. Device
    4. Network
    5. System
    6. License
  4. Tutorials
    1. First boot
    2. First process
    3. Connect to MQTT
    4. Process sequencing

    5. DIY Project: Bath Fan

In this article

  • Introduction
advert
unicontrol > Tutorials > Connecting to MQTT

Connecting to MQTT

Mosquitto

Once you have set up your first process , you can extend it by adding remote access and monitoring capabilities. A great way how to achieve this is by using the MQTT protocol, which is lightweight and well-suited for this purpose.

To test your MQTT, you can use a free public broker. For example, you can use this one (no registration is required, so you won't need a username or password):

  • Broker: mosquitto.org (MQTT 5)
  • Broker address: test.mosquitto.org
  • TCP Port: 1883
  • User: "" (broker requires empty user name for an anonymous user)
  • Password: n/a

Setting up unicontrol

Setting up unicontrol for an MQTT is a simple process that can be completed entirely within the Wireless page of the web interface. In the MQTT section you only need to configure:

  • Connection: Enabled
  • Broker address: test.mosquitto.org (either a domain name or an IP address of the broker can be provided since version 1.07)
  • User: ""
  • Topic Level 1: any unique identifier, for example johndoe5896
  • Topic Level 2: anything you like, for example home (leaving it empty is also acceptable)
  • Topic Level 3: anything you like, for example garagedoor (leaving it empty is also acceptable)
Instead of using the Topic levels exactly as listed above you may want to alter them slightly to make your combination unique and prevent conflicts with other users connected to the same broker.
For now you can ignore:
  • Subscription - enabled by default
  • Port - default value of 1883 is correct
  • Password - does not matter when connecting to an anonymous public broker
MQTT set-up
MQTT set-up

Click Save to write the changes to memory.

MQTT set-up is only refreshed on a new handshake with the broker. Therefore, any subsequent changes made during an active MQTT session will not take effect until the next session starts, so it's generally advisable to Reconnect every time you save your changes.

The device will be trying to connect to the broker permanently, as long as it is allowed and there are no roadblocks such as a missing Wi-Fi connection. If all the settings were provided correctly, the connection should be established within seconds. To check if the connection was successful, you may navigate to the System page. If the MQTT session is alive, the MQTT conn. field will show Yes and MQTT up will track how long the currently active session has lasted.

MQTT connection status
MQTT connection status

With the MQTT connection online, the device is ready to exchange messages but is not sending anything process-related yet. To allow the device to send process-related messages, return to the Processes page of the web interface, select my_test, and make the following changes:

  • Set Input / Publish and Output / Publish to Allowed to allow sending MQTT messages with the input's state. This will also unhide the following options.
  • Set Input / Frequency to 10 seconds so that the input state is refreshed by a new message more frequently.
  • Set Input / Subtopic and Output / Subtopic to Use name so that the process's name is embedded into the outbound message Topic for easier identification.
Allowing publishing of MQTT telemetry
Allowing publishing of MQTT telemetry

Mobile app set-up

For testing purposes, we can use the IoT MQTT Panel app, which is sufficiently easy to use and has some excellent features for creating and sharing customized dashboards. However, thanks to the MQTT protocols's inherent simplicity, any similar mobile app supporting it can be used with only minor adjustments to the steps outlined below. There is a plenty of them available for android devices (the choice is somewhat limited for iOS, but seems to be slowly catching up):

Sample of various MQTT-compatible android mobile apps
Sample of various MQTT-compatible android mobile apps

Once you have installed and opened the app, create a new connection and fill in the following details:

  • Connection name: any name you like, for example, mosquitto
  • Broker Web/IP address: test.mosquitto.org
  • Add Dashboard with any name, for example, test_ayatec

If the connection details are correct, the app will establish a connection, and you will see an orange tick indicating a success:

Add Connection dialog window Successful connection labeled with an orange tick
Successful connection labeled with an orange tick

After opening the fresh connection, you will see an empty dashboard with three dots in the upper-right corner. Click on them to show the dashboard menu, and then select Dashboard configurations:

Empty dashboard view Activated dashboard menu
Activated dashboard menu

In the Dashboard configurations, you can enter the Dashboard prefix topic as johndoe5896/home/garagedoor/ so that you don't have to include the full topic for each panel separately. Click Save to apply the change:

For more technical details on constructing MQTT Topics, you can refer to the MQTT API.
Dashboard configuration dialog window
Dashboard configuration dialog window

Receiving telemetry

To create your first panel for monitoring the device's telemetry, return to the empty dashboard and click on the Add Panel icon. From the subsequent selection dialog, choose the LED Indicator option.

Add Panel icon Choosing the new panel type to be added
Creating the first state panel

Consistently with the previous tutorial and the unicontrol MQTT API (you may check for additional details), in the LED Indicator set-up panel you need to provide:

  • Panel name: any name you like, for example, my_test_in
  • Topic: pub/my_test/input (the prefix johndoe5896/home/garagedoor/ will be automatically included)
  • Payload on: 1
  • Payload off: 0

Click Save to add the panel to your dashboard. Your first passive panel my_test_in is now monitoring the D0 input pin of the my_test process as defined in the previous tutorial:

New LED Indicator dialog window New panel on the dashboard
Creating the first state panel

To create a second panel for monitoring the process output, you can duplicate the first one instead of creating a new one from scratch. To do this, click on the Duplicate icon and select the first panel as the one to duplicate. This will create an identical copy on your dashboard:

Duplicate Panel icon Choosing the panel to duplicate Dashboard view with 2 identical panels
Adding the second state panel

Click on the three dots next to the duplicated panel and choose Edit panel to adjust it by changing:

  • Panel name: _in to _out
  • Topic: /input to /output
Modifying the second panel
Modifying the second panel

Now you have two panels on your dashboard: one for monitoring the D0 input pin and one for monitoring the D4 output pin (on board LED) of the my_test process. Both panels will react to changes on their respective pins, with the D0 panel (in) updating every 10 seconds and the D4 panel (out) updating in real-time.

Dashboard view with my_test_out off Dashboard view with my_test_out on
Dashboard view with changing my_test_out state

Sending commands

so far the app has only served as a passive data monitor, but you can use it to send commands as well by adding a new Button panel to your dashboard. Click the Add Panel icon and select the Button option.

Add Panel icon Choose the new panel to be added
Adding an active button

In line with the MQTT API and the set-up presented in the previous tutorial, you need to provide:

  • Panel name: any label you like, for example, my_test on
  • Topic: sub/my_test/outset (the prefix johndoe5896/home/garagedoor/ will be automatically included)
  • Payload: 1

Click Save to add an ON button to your dashboard:

New button dialog window Dashboard view
Adding a button

To create an OFF button, duplicate the first one and modify it via Edit panel by changing:

  • Panel name: on to off
  • Payload: 1 to 0

Click Save to add the OFF button to your dashboard.

Choose the panel to duplicate Dashboard view Modifying the second button
Adding the second button

Now you have both an ON and an OFF button on your dashboard, granting you a direct control over your ESP8266's on-board LED. Clicking on them alternately will blink the LED and update the my_test_out panel in real-time.

Dashboard view Dashboard view
Final dashboard view

Congratulations! In just a few simple steps, you have created a fully remote control for your ESP8266!

Further reading

Do not forget that the steps in this tutorial were performed using a public broker that is not suitable for real-world applications due to security concerns. To find out more about the topic, including more secure broker options, you may check our selection of useful resources to help you further progress on the quest to your personal, customized IoT application!

  • Steve's Internet Guide: an excellent source of information for MQTT beginners
  • Instructables.com: step-by-step guide on how to set up an MQTT broker at home using Raspberry PI
  • mariushosting.com: set up an MQTT broker easily on your Synology NAS
  • arubacloud.com: install and secure your MQTT broker on Ubuntu
  • emqx.com: check how simple it is to integrate MQTT messages with Node-RED
© 2024 ayatec.eu • Found a mistake?  Let us know!