digitalstrom_mqtt_gateway_in_a_docker_container

This is an old revision of the document!


DigitalSTROM MQTTG Gateway in a Docker container

this is WIP this is how i set up Chriss Gross' MQTT bridge to run inside a node docker container. i have also slightly modified his script to use authentication and to allow setting the qos in the config file as well:

docker-compose.yml
# create ./app directory with uid and gid 1000 first
services:
  node:
    build: ./build
    restart: always
    user: 1000:1000
    working_dir: /app
    environment:
      - NODE_ENV=production
    volumes:
      - ./app:/app
mkdir ./app
chown 1000.1000 app
mkdir ./build
build/startup.sh
#!/bin/bash
USERNAME="myuser"
PASSWORD="mysecret"
MQTT_SERVER="mqtt.psuter.ch"
 
if [ -z "$(ls -A /app)" ]; then
    echo "installing dss mqtt bridge (see https://github.com/cgHome/mqtt-dss-bridge)"
    cd /app
    npm install --save mqtt-dss-bridge
    echo "IMPORTANT: configure the bridge by editing /app/node_modules/mqtt-dss-bridge/config.js"
    echo "installation completed"
fi
echo "Starting DSS to MQTT bridge"
# start the bridge in the background 
cd /app
node /app/node_modules/mqtt-dss-bridge/index.js 2>&1 &
PID=$!
echo "DSS Bridge started with PID $PID"
# start a loop to monitor the status every second
errcntr=0
lastUpdate=999
while true; do 
    lastUpdate=$(( \
            $(date +%s)\
            -\
            $(date -d "$(\
                mosquitto_sub -h $MQTT_SERVER -u $USERNAME -P $PASSWORD -t dss/state/lastDiscovered -C 1 | \
                sed -E -e 's/"([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}:[0-9]{2}:[0-9]{2}).*$/\1 \2Z/'\
            )" +%s)\
        ))
    if [ 10 -lt $lastUpdate ]; then 
            if [ $errcntr -lt 10 ]; then
                let errcntr++
                echo "DSS Bridge state is too old (last updated $lastUpdate seconds ago, max 10 allowed). This was failure $errcntr out of 10 accepted failures" 
            else 
                echo "DSS bridge state is still too old ($lastUpdate seconds) after 10 retries. killing mqtt-dss-bridge (pid $PID) and exit with error status 1"
                kill $PID
                exit 1
            fi
	fi
    sleep 1
done

obviously you need to adjust the three variables at the top of the script to match your setup

chmod 755 build/startup.sh
build/Dockerfile
FROM node:22-bookworm
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/Zurich
# mqtt client 
RUN apt-get update && apt-get install -y mosquitto-clients
#RUN npm install --save mqtt-dss-bridge
COPY startup.sh /startup.sh
CMD /startup.sh
nano ./app/node_modules/mqtt-dss-bridge/config.js
  • digitalstrom_mqtt_gateway_in_a_docker_container.1720250782.txt.gz
  • Last modified: 06.07.2024 09:26
  • by Pascal Suter