Documentation / LemmeKnow Server

Setting up Mosquitto

LemmeKnow Server doesn't carry messages itself. It publishes to a Mosquitto MQTT broker, and your phone subscribes to that same broker. This page covers what the server needs from Mosquitto. If you run the LemmeSee Hub, see MQTT and Mosquitto for the hub's setup instead.

Install Mosquitto

Mosquitto is free and available for Windows, macOS and Linux from the Eclipse Mosquitto project.

Windows

Install it with winget and start its service:

winget install EclipseFoundation.Mosquitto
net start mosquitto

Its settings live in mosquitto.conf in the Mosquitto folder, usually C:\Program Files\mosquitto.

Linux

Install it from your distribution. On Debian, Ubuntu, and Raspberry Pi OS:

sudo apt install mosquitto mosquitto-clients

The package runs Mosquitto as a service that starts at boot. Put your settings in a file of your own in /etc/mosquitto/conf.d/, such as /etc/mosquitto/conf.d/lemmeknow.conf; the main mosquitto.conf reads every file in that folder.

Out of the box Mosquitto listens on port 1883 and only accepts connections from the same PC. Your phone connects over the network, so you need to add a listener for it in mosquitto.conf and restart the service.

Option 1: an open broker on your home network

This is the simplest setup. Anyone who can reach the broker can connect without a username or password, so only use it on a network you trust.

listener 1883
allow_anonymous true

On the phone, enter the PC's address with port 1883, leave Username and Access Key empty, and leave TLS off.

There is no access control on an open broker: every connected phone receives every public and private message. See Public and private channels. Don't share an open broker between two servers either: each phone would receive both servers' messages and file them under one server.

Option 2: signed-in phones

To make phones sign in with an access key, use two listeners:

  • A listener on 127.0.0.1 with anonymous access. LemmeKnow Server and your scripts publish here. The helper connects without credentials, so scripts need this listener.
  • A listener for phones that requires sign-in and TLS. The app ALWAYS uses TLS for security when an access key is set.

Phones are checked by the LemmeKnow auth plugin for Mosquitto. When a phone connects, the plugin sends its username and access key to the sign-in check on 127.0.0.1:8765, which LemmeKnow Server provides whenever the LemmeSee Hub isn't running. Because that check only listens on the same PC, the broker must run on the same PC as the server for this option.

Windows

Step 1: get the auth plugin. Download the LemmeKnow auth plugin for Mosquitto from Downloads and put mosquitto_lk_auth.dll in the Mosquitto folder, usually C:\Program Files\mosquitto.

Step 2: make a certificate. The phone listener needs one, and a self-signed one is fine. Open a Command Prompt in C:\Program Files\mosquitto\certs (create the folder if it isn't there) and run:

openssl req -x509 -newkey rsa:2048 -nodes -days 3650 -keyout server.key -out server.crt -subj "/CN=lemmeknow"

openssl comes with Git for Windows, or install it with winget install ShiningLight.OpenSSL.Light. That installer doesn't add openssl to your PATH, so run it by its full path, "C:\Program Files\OpenSSL-Win64\bin\openssl.exe", in place of openssl above.

Step 3: add the listeners. Add this to C:\Program Files\mosquitto\mosquitto.conf:

per_listener_settings true

listener 1883 127.0.0.1
allow_anonymous true

listener 1884
allow_anonymous false
certfile C:\Program Files\mosquitto\certs\server.crt
keyfile C:\Program Files\mosquitto\certs\server.key
plugin C:\Program Files\mosquitto\mosquitto_lk_auth.dll
plugin_opt_auth_host 127.0.0.1
plugin_opt_auth_port 8765

Step 4: restart Mosquitto. From an administrator Command Prompt, run net stop mosquitto and then net start mosquitto.

Step 5: start the server and add users. In LemmeKnow Server's config.py, keep AUTH_ENABLED = True and set DB_PATH. Start the server, then create a user for each phone. See Users and authentication.

Linux

Step 1: get the auth plugin. Download the LemmeKnow auth plugin for Mosquitto from Downloads, unzip it, and install mosquitto_lk_auth.so where Mosquitto can read it:

sudo install -m 644 mosquitto_lk_auth.so /usr/local/lib/mosquitto_lk_auth.so

The plugin is built for 64-bit Intel and AMD PCs.

Step 2: make a certificate. The phone listener needs one, and a self-signed one is fine. Mosquitto runs as its own mosquitto user, so that user has to own the key:

cd /etc/mosquitto/certs
sudo openssl req -x509 -newkey rsa:2048 -nodes -days 3650 -keyout server.key -out server.crt -subj "/CN=lemmeknow"
sudo chown mosquitto: server.key

Step 3: add the listeners. Save this as /etc/mosquitto/conf.d/lemmeknow.conf:

per_listener_settings true

listener 1883 127.0.0.1
allow_anonymous true

listener 1884
allow_anonymous false
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key
plugin /usr/local/lib/mosquitto_lk_auth.so
plugin_opt_auth_host 127.0.0.1
plugin_opt_auth_port 8765

Step 4: restart Mosquitto.

sudo systemctl restart mosquitto

If it doesn't come back up, the reason is at the end of /var/log/mosquitto/mosquitto.log; read it with sudo tail /var/log/mosquitto/mosquitto.log.

Step 5: start the server and add users. In LemmeKnow Server's config.py, keep AUTH_ENABLED = True and set DB_PATH. Start the server, then create a user for each phone. See Users and authentication.

The auth plugin asks LemmeKnow Server for the server id on port 8765, so you don't need to set it. If the broker starts before the server, the plugin asks again until the server answers.

The app trusts the certificate it sees on the first connection and warns you with "Server key changed" if it ever differs.

Phones that sign in through LemmeKnow Server can read public messages only. Private messages and per-user grants need the LemmeSee Hub. See Users and authentication.

Logging

Windows

Mosquitto doesn't write a log file unless you ask it to. To keep one, add these lines to mosquitto.conf:

log_dest file C:\ProgramData\mosquitto\mosquitto.log
log_type error
log_type warning
log_type notice

This records connections, disconnections, and failed sign-ins, which is what you need when a phone can't connect. Avoid log_type all: it writes a line for every message delivered to every phone, which adds tens of megabytes a day, and Mosquitto never trims the file on Windows.

Create the C:\ProgramData\mosquitto folder first. Mosquitto creates the log readable only by the service itself, so to open it, grant yourself read access from an administrator Command Prompt after the first restart:

icacls C:\ProgramData\mosquitto\mosquitto.log /grant %USERNAME%:R

Linux

The package already logs to /var/log/mosquitto/mosquitto.log and rotates the file daily, keeping a week. The default detail records connections, disconnections, and failed sign-ins, which is what you need when a phone can't connect. Avoid adding log_type all: it writes a line for every message delivered to every phone.

The file is readable only by the mosquitto user, so read it with sudo:

sudo tail -f /var/log/mosquitto/mosquitto.log

Firewall

Allow incoming connections on the phone listener's port (1883 for Option 1, 1884 in the Option 2 example) in your PC's firewall.

Windows

Mosquitto runs as a Windows service, so Windows never asks you about it; add the rule yourself from an administrator PowerShell:

New-NetFirewallRule -DisplayName "LemmeKnow phone listener" -Direction Inbound -Protocol TCP -LocalPort 1884 -Action Allow

Linux

Ubuntu's firewall, ufw, is off unless you turned it on; sudo ufw status tells you. If it's active, allow the port:

sudo ufw allow 1884/tcp

Don't open port 8765 to the network; the sign-in check is only meant for the broker on the same PC.