Skip to content

Enabling MQTT

root edited this page Sep 13, 2019 · 1 revision

Currently this code requires an authenticated connection to the MQTT server

config.inc.php Variables

$ENABLE_MQTT = 1;			// Enable MQTT State Publishing (1 = true, 0 = false)
$MQTTserver = "172.16.33.8"; 		// Change as necessary
$MQTTport = 1883;			// Change as necessary
$MQTTusername = "admin";		// Set your username
$MQTTpassword = "password";             // set your password
$MQTTsub_id = "tcp-subscriber"; 	// make sure this is unique for connecting to your server - you could use uniqid()
$MQTTpub_id = "tcp-publisher"; 		// make sure this is unique for connecting to your server - you could use uniqid()
$MQTT_prefix = "light";         	// Topic prefix for lights - ie light/<room-name>/<light-name>/<UniqueBulbID>
$ENABLE_HA_DISCO = 1;		        // Enable MQTT Publishing of Home Assistant Discovery Topics (1 = true, 0 = false)
$HASSTopic_id = "homeassistant";	// Topic prefix for Home Assistant Discovery Topics - this must match with HASS

Python Subscriber Script Generation

Once your lights are setup in TCP Connected bridge. You would run http://lighting.local/MQTTGenerator.php This will generate the python subscriber file mqtt_sub.py - To run it you need the python modules - paho.mqtt.client and requests.

The generator only ever needs to be run again if you change the names of lights or add/remove bulbs

You can trigger the script however you like. I generally create a service for scripts like this so they always run on restart. https://tecadmin.net/setup-autorun-python-script-using-systemd/ My service definition looks like this.

[Unit]
Description=MQTT Sub
After=multi-user.target
[email protected]

[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/pi/mqtt_sub.py
StandardInput=tty-force

[Install]
WantedBy=multi-user.target

Publishing External State Changes

If you are only going to control lights using MQTT any states changed by MQTT should be published. Additionally the state topic is set to retained so that if the web interface is restarted the state of the bulbs should be retained as well.

But if you might control the bulbs with the scheduler, the Web Interface or a phone app the states can get out of sync. As such there is a php page called mqttstate.php that will double check the current state of a bulb and update MQTT accordingly. I run it as a cron job every 5 minutes.

To create cron job login to your system. At the command line enter the following: sudo crontab -e - this will open the crontab editor.

Once in the crontab, add the following line: (Adjust the URI to match your install

*/5 * * * * lynx -dump https://lighting.local/mqttstate.php

Topics

There are 5 topics for each bulb On/Off, State, Brightness, Brightness State & Availability. They follow the format of. Note the light prefix can be changed in config.inc.php

On/Off State: light/<room-name>/<light-name>/<UniqueBulbID>/status
On/Off Command: light/<room-name>/<light-name>/<UniqueBulbID>/switch
Brightness State: light/<room-name>/<light-name>/<UniqueBulbID>/brightness
Brightness Command: light/<room-name>/<light-name>/<UniqueBulbID>/brightness/set
Availability: light/<room-name>/<light-name>/<UniqueBulbID>/LWT

You are now ready to move on to Scheduler Configuration