jpmens/mqtt-launcher

Execute bash scripts

Closed this issue · 8 comments

I have an implementation of mqtt in a Raspberry and a number of sensors that publish to topics. Each sensor writes to individual topic. I would like to call some bash scripts in case of a sensor is enabled. For example if topic /home/burglar/master_bedroom_window is enabled, then execute script /home/atuxnull/scripts/a.sh
if topic /home/burglar/gront_door is enabled, then execute script /home/atuxnull/scripts/b.sh and the list goes on.
Pretty basic question here but how do i install the mqtt-launchet and how do i make it each one of the scenario's call particular scripts?

Here is an example of the output in MQTT cli when there is a change in one of the doors/windows
/home/burglar/master_bedroom_window 0
/home/burglar/master_bedroom_window 1
/home/burglar/gront_door 1
/home/burglar/gront_door 0

0 means the door/windows is closed and 1 it is open. in case it is 0 run script_0.sh, in case it turns to 1 then execute script_1.sh.
May i have some help on this please?

Show me what you've tried so far (and please remember to paste code within code fences here)

Thanks a lot for the reply. here is the code fpr devices with some different name that mentioned before.

pi@rpiB:~/mqtt-launcher $ more launcher.conf

logfile         = 'logfile'
mqtt_broker     = 'localhost'       # default: 'localhost'. If using TLS, this must be set to the domain name signed by your TLS certificate.
mqtt_port       = 1883              # default: 1883
mqtt_clientid   = 'mqtt-launcher-1'
mqtt_username   = None
mqtt_password   = None
mqtt_tls        = None              # default: No TLS
# mqtt_username = 'jane'
# mqtt_password = 'secret'

topiclist = {

    # topic                     payload value       program & arguments
    "open/close"          :   {
                                '0' :   [ '/device/alarm/sensor/livingroom', '/home/pi/scrpts/scriptlivingroom_0.sh' ],
                                '1' :   [ '/device/alarm/sensor/livingroom', '/home/pi/scrpts/scriptlivingroom_1.sh'],
                                '0' :   [ '/device/alarm/sensor/hallway', '/home/pi/scrpts/scripthallway_0.sh' ],
                                '1' :   [ '/device/alarm/sensor/hallway', '/home/pi/scrpts/scripthallway_1.sh'],
                              },

}
pi@rpiB:~/mqtt-launcher $

the /device/alarm/sensor/livingroom has a value of 0 or 1. the same goes for /device/alarm/sensor/hallway. 0 is closed and it needs to execute script that ends with 0 and 1 is open door and it needs to execute the script that ends with 1.
thanks in advance

Look at your configuration.
Look under where it says # topic
Look under where it says program & arguments

And compare with the example in the README.

Hint: you've put your topic as program.

i edited the conf and now it has the following format:

pi@rpiB:~/mqtt-launcher $ more launcher.conf

logfile         = 'logfile'
mqtt_broker     = 'localhost'       # default: 'localhost'. If using TLS, this must be set to the domain name signed by your TLS certificate.
mqtt_port       = 1883              # default: 1883
mqtt_clientid   = 'mqtt-launcher-1'
mqtt_username   = None
mqtt_password   = None
mqtt_tls        = None              # default: No TLS
# mqtt_username = 'jane'
# mqtt_password = 'secret'

topiclist = {

    # topic                     payload value       program & arguments
    "/device/alarm/sensor/livingroom"          :   {
                                '0' :   [ 'close', '/home/pi/scrpts/scriptlivingroom_0.sh' ],
                                '1' :   [ 'open', '/home/pi/scrpts/scriptlivingroom_1.sh'],
                              },
    "/device/alarm/sensor/hallway"          :   {
                                '0' :   [ 'close', '/home/pi/scrpts/scripthallway_0.sh' ],
                                '1' :   [ 'open', '/home/pi/scrpts/scripthallway_1.sh']
}
pi@rpiB:~/mqtt-launcher $

When i run the script, it fails.

pi@rpiB:~/mqtt-launcher $ ./mqtt-launcher.py
Cannot load configuration from file launcher.conf: invalid syntax (launcher.conf, line 23)
pi@rpiB:~/mqtt-launcher $ 

Did you read the error message? Did you count the braces, both the opening and the closing ones?

after correction

`pi@rpiB:~/mqtt-launcher $ more launcher.conf

logfile         = 'logfile'
mqtt_broker     = 'localhost'       # default: 'localhost'. If using TLS, this must be set to the domain name signed by your TLS certificate.
mqtt_port       = 1883              # default: 1883
mqtt_clientid   = 'mqtt-launcher-1'
mqtt_username   = None
mqtt_password   = None
mqtt_tls        = None              # default: No TLS
# mqtt_username = 'jane'
# mqtt_password = 'secret'

topiclist = {

    # topic                         payload value       program & arguments
"/device/alarm/sensor/livingroom"          :   {
                                               '0' :   [ 'close', '/home/pi/scrpts/scriptlivingroom_0.sh' ],
                                               '1' :   [ 'open',  '/home/pi/scrpts/scriptlivingroom_1.sh'],
                              },
"/device/alarm/sensor/hallway"          :   {
                                               '0' :   [ 'close', '/home/pi/scrpts/scripthallway_0.sh' ],
                                               '1' :   [ 'open',  '/home/pi/scrpts/scripthallway_1.sh']
                                             },
}

pi@rpiB:~/mqtt-launcher $ `

and the script

pi@rpiB:~/mqtt-launcher $ ./mqtt-launcher.py
Traceback (most recent call last):
  File "./mqtt-launcher.py", line 152, in <module>
    mqttc.connect(cf.get('mqtt_broker', 'localhost'), int(cf.get('mqtt_port', '1883')), 60)
  File "/home/pi/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 839, in connect
    return self.reconnect()
  File "/home/pi/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 962, in reconnect
    sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
  File "/usr/lib/python2.7/socket.py", line 575, in create_connection
    raise err
socket.error: [Errno 111] Connection refused
pi@rpiB:~/mqtt-launcher $

what am i doing wrong???

Did you read the error message? It says socket.error: [Errno 111] Connection refused which means the program cannot connect to the host/TCP port you've specified.