Updated Guide: Weewx, Nginx, Belchertown, MQTT/WS

Install Prerequisites for Belchertown

This is a four part process, they are split up below.

Install mosquitto package

This is where things begin to get a little funky, primarily if you use Let’s Encrypt or any SSL Certificate services. We’ll touch on that in a short while, as we’ll be going back to Nginx for that. We’ll be making Nginx handle the SSL stuff, and making it act as a layer between mosquitto and the client – skipping the need for mosquitto to have SSL certs loaded.

  1. apt install mosquitto mosquitto-clients
  2. nano /etc/mosquitto/mosquitto.conf

Remove all data from mosquitto.conf excluding include_dir /etc/mosquitto/conf.d – apt updates or system updates can sometimes obliterate the mosquitto.conf config file, which means your additions won’t persist… and hours of ire will ensue.

  1. nano /etc/mosquitto/conf.d/weewx.conf
  2. Paste the following into the weewx.conf file…
pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_type all
websockets_log_level 1023
connection_messages true
log_dest file /var/log/mosquitto/mosquitto.log


allow_anonymous true
password_file /etc/mosquitto/passwd
acl_file /etc/mosquitto/acl

listener 9001
protocol websockets

listener 1883
protocol mqtt

log_type error
log_type warning
log_type notice
log_type information
  1. CTRL O
  2. CTRL X
  3. service mosquitto restart

Now to generate some username and password details for /etc/mosquitto/passwd and access limits in /etc/mosquitto/acl!

mosquitto_passwd -c /etc/mosquitto/passwd -c /etc/mosquitto/passwd UserNameHere – You’re best to keep it simple, and set the usernamehere segment to weewx (it’s what I set).

The username and password you create will be required for the Weewx daemon!! As always, create a secure password, but do jot it down so that you can modify weewx.conf accordingly.

Once completed, we need to generate a quick access control list for both backend and frontend.

nano /etc/mosquitto/acl

Paste the following details into the acl file…

topic read $SYS/#

topic read weather/#

user weewx
topic weather/#

CTRL O and CTRL X to save and exit.

Install Weewx MQTT extension

User Iain MacDonnell on Github.com has provided the Weewx MQTT extension here. Condensed instructions are below that work on Debian 10…

  1. pip3 install paho-mqtt
  2. pip3 install ujson (python-cjson is not supported on Python3 currently)
  3. wget -O weewx-mqtt.zip https://github.com/matthewwall/weewx-mqtt/archive/master.zip
  4. In terminal: /home/weewx/bin/wee_extension --install weewx-mqtt.zip OR cd /home/weewx/bin then wee_extension --install weewx-mqtt.zip
  5. Modify /home/weewx/weewx.conf file, add the following to the [StdRESTful] section if it is missing:
    [[MQTT]]
        server_url = mqtt://username:password@192.168.1.123:1883/
        aggregation = aggregate
        topic = weather
        unit_system = US
        binding = loop,archive

Please note that you will need to replace username:password with the username and password for the mosquitto package that you installed above. Change the above to something like weewx for the username, and the password can be anything really (needs to be hard to guess) – just needs to match what you’ve set for mosquitto (in the next segment). The IP address and port of 192.168.1.123:1883 is by pure example. I use statically assigned LAN IP’s on my internal network – and generally do not bind to localhost/127.0.0.1.

  1. Next, go to nearly the bottom of the weewx.conf file and ensure , user.mqtt.MQTT was included to the restful_services segment under [Engine] [[Services]].
  2. Once all changes have been made, save the changes, and restart weewx.

Now onto Nginx…