-
Notifications
You must be signed in to change notification settings - Fork 4
Enabling MQTT
$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>
$MQTT_control = "tcp"; // Control topic identifier for host and script control ie control/<MQTT_control>
$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
$HASSOnline = "hass/status"; // Topic Home Assistant publishes to, to announce its online
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.
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
[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/pi/mqtt_sub.py > null
[Install]
WantedBy=multi-user.target
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 retain 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 a cron job, log in 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
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
- Home
- Installation & Configuration
- Pi Setup Walk-through
- Additional Project Information