You may want to skip this part if you want to control your power strip from Telegram only.
Open up the python program and find the portion that says MQTT definitions.
I think the settings you’ll find there speak for themselves.
You’ll have to set the details of your MQTT broker, including its URL, the port number and whether it requires TLS or not.
The mqtt_name is the prefix for the topics to subscribe to. I use the name of the computer, which is Carl, one of my minions. You can include the username and password here too. However I prefer to put them in a separate hidden file in my home directory, in which case you can leave the username and password in the program file untouched. The username and password file is defined by the mqtt_auth variable.
If your MQTT broker doesn’t use TLS you must empty the variable mqtt_cert.
In case you follow my advice to put the username and password in a separate file you must create a file with the name ~/.mqttpwd and add one line to it like this:
-u username -P password
Perhaps it’s a good idea to chmod this file to prevent others from reading your secrets, chmod 600 ~/.mqttpwd .
When all that’s done you can (re)start the power strip service again with the command sudo systemctl restart powerstrip.service, after which your powerstrip can be controlled over MQTT.
In case the connection to your MQTT broker fails the file /var/log/syslog may give some indication why.
Now you can control the power strip with MQTT.
You may want to install the program mosquitto-client on your computer for that.
This can be on any computer running Linux, not just the power strip controller itself.
However I don’t like the complexity of the command line parameters of this client. Too much to remember and type for every single command you want to give. Especially if your broker requires TLS encryption.
I’ve created two aliases, one for the mosquitto_sub and one for mosquitto_pub command, which makes working with them a whole lot simpler.
Add the following two lines to your ~/.bash_aliases file. You’ll have to change the hostname, the port number to match those of your broker. And if your broker doesn’t require TLS encryption you may omit the everything after the port number.
alias iot_sub='mosquitto_sub -h YOURBROKER $(cat ~/.mqttpwd) -p 8883 --capath /etc/ssl/certs/' alias iot_pub='mosquitto_pub -h YOURBROKER $(cat ~/.mqttpwd) -p 8883 --capath /etc/ssl/certs/'
And don’t forget to create a file called .mqttpwd on that same computer, containing the user name and password for your broker, just like it’s done on the power strip controller itself.
You may want to logout and login again for the aliases to take effect.
Now a line like this:
mosquitto_pub -h YOURBROKER $(cat ~/.mqttpwd) -p 8883 --capath /etc/ssl/certs/ -t carl/control -m “on 4”
can be substituted by:
iot_pub -t carl/control -m “on 4”
Quite an improvement if you ask me. And an extra bonus is that the user name and password for your broker can’t be stolen from your .bash_history file anymore.