Skip to main content

USB to MQTT Gateway

Whatever hardware you chose as your platform, you will need to install a USB to MQTT gateway. This will allow you to connect any USB device to the MQTT broker.

A USB to MQTT gateway has been developed for this purpose, leveraging Qts library to communicate with USB ports.

Install on Ubuntu

It is possible to cross compile to other CPU architectures, but the easiest way is to compile directly on the target platform.

If you install on the Jetson nano, it's recommented to power it through the power jack since the CPU only utilises half the amount of cores if powered via USB.

  1. First set up a github token for your account. Github Account Tokens

  2. Clone the repo

    git clone https://[your_username]:[pass_token]@github.com/Knightec-Internal-Product-Team/UsbMqttGateway.git
  3. Find the scripts folder

    cd UsbMqttGateway/scripts
  4. Run install.sh with sudo

    sudo ./install.sh

    Or if you want to run it unattended, you can use -y or -n parameters to tell it to overwrite the default config file or not, if it exists. Run with -h to read what the commands do.

    sudo ./install.sh -y
    sudo ./install.sh -n
    sudo ./install.sh -h
  5. Change the config file to use the correct settings

    sudo nano /etc/usbmqttgateway/usbmqttgateway.conf

    And restart the service to apply the new config.

    sudo systemctl restart usbmqttgateway.service
  6. Check that the service is running

    systemctl status usbmqttgateway.service

It will say something about if the service is running or not, and what the current configuration settings are.

Troubleshooting If it says something like MQTT error [5]: CONNACK return code when you check the service status it probably means the user or password is wrong for the MQTT Broker you're trying to connect to.

Manual compile and install (Optional) If you are interested in the details of how everything is installed, inspect the install script with

sudo nano install.sh

It runs the same commands as you would do manually.

Uninstall

  1. Find the scripts folder

    cd UsbMqttGateway/scripts
  2. Run uninstall.sh with sudo

    sudo ./uninstall.sh   

    Or if you want to run it unattended, you can use -y or -n parameters to tell it if you want to keep config files in the default config folder or not. Run with -h to read what the commands do.

    sudo ./uninstall.sh -y
    sudo ./uninstall.sh -n
    sudo ./uninstall.sh -h

Note: This will not install the dependencies installed before, since other applications may be depending on them.

Manual uninstall (Optional) If you are interested in the details of how everything is uninstalled, inspect the install script with

sudo nano uninstall.sh

It runs the same commands as you would do manually.

Setup MQTT Broker

Mosquitto is a popular open-source MQTT broker. Here’s a step-by-step guide to installing and running Mosquitto on a Linux system:

Install MQTT Broker (Linux)

Autostart USB to MQTT Gateway on Logon

The install script should already have configured the gateway to auto start. But if it doesn't work for some reason, you can follow these instructions. To check if the service is started, run:

sudo systemctl status usbmqttgateway.service

To make your compiled program start on reboot on a Linux system, you can use several methods. One common and reliable way is to create a systemd service. Here’s a step-by-step guide to setting up a systemd service for your program:

  1. Enable the Service: Enable the service to start on boot.

    sudo systemctl enable usbmqttgateway.service
  2. Start the Service: Start the service immediately.

    sudo systemctl start usbmqttgateway.service
  3. Check the Service Status: Verify that your service is running correctly.

    sudo systemctl status usbmqttgateway.service

This setup will ensure that your program starts automatically on system reboot and will be managed by systemd.