atux_null
Posts: 127
Joined: Thu Jun 21, 2012 1:58 pm

MQTT send alert

Wed Oct 31, 2018 8:12 am

Hi. i running my custom made home alarma by making use MQTT broker in a RPI zero W and ESP8266 with different modules.
Each module publishes to a particular topic in MQTT broker and from an android app like mqtt dash i can see the status.
i would like to take it a step further and make the system to be able to send alerts, such as emails or even create a preformated text and post it in another server in my LAN.
Has anyone any ideas on how to do stuff like that and give some guidance please?

DirkS
Posts: 9692
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: MQTT send alert

Wed Oct 31, 2018 9:16 am

Many ways to do that...
If you want a flexible solution you could use e.g. https://github.com/jpmens/mqttwarn

atux_null
Posts: 127
Joined: Thu Jun 21, 2012 1:58 pm

Re: MQTT send alert

Mon Dec 31, 2018 12:21 pm

hi. i am coming back since i have given a try and i cannot make it work. Up to know i have a RPI that acts as broker. all i did is

Code: Select all

apt-get install mosquitto mosquitto-clients -y
pip install paho-mqtt
then i edited the /etc/mosquitto/mosquitto.conf and it has

Code: Select all

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

listener 1883
i have a number of ESP8266 with sensors that publish to the

Code: Select all

/device/alarm/sensor/
, one as

Code: Select all

hallway
, the other as

Code: Select all

master_bedroom
, next is

Code: Select all

bathroom
and the list continues.
it works fine for some time now and i see their status in an android application MQTT dashboard.
in the MQTT i see a 0 when door/window closes and 1 when it opens. i would like to execute a particular bash script when 0 and a different bash script when 1.

i am trying to play with https://github.com/jpmens/mqtt-launcher/issues/13 but it fails to work and i am stuck. here is the config of the launcher.conf

Code: Select all

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 yo
ur 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 here is the error when i execute it

Code: Select all

pi@rpiB:~/mqtt-launcher $ python 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 missing? May i have some help please?

User avatar
neilgl
Posts: 371
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: MQTT send alert

Mon Jan 07, 2019 11:28 am

As jpmens replied in his github issues page, the program cannot connect to the host/TCP port you've specified.
So logon to the pi where you are running mqtt and the python script and try this in a terminal:

Code: Select all

mosquitto_sub -v -t '#' -h localhost
That should show all the mqtt topics that are published to the mqtt server (mqtt broker)

User avatar
DougieLawson
Posts: 34561
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: MQTT send alert

Mon Jan 07, 2019 1:32 pm

It's also a good idea to check whether mosquitto is listening with sudo netstat -tlnp | grep 1883;. There was a bug a while ago where it wouldn't listen on port 1883 without a

Code: Select all

listener 1883
protocol mqtt
in your config file.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a hippy & doctor free zone.

mattmiller
Posts: 2018
Joined: Thu Feb 05, 2015 11:25 pm

Re: MQTT send alert

Mon Jan 07, 2019 3:56 pm

May i have some help please?
From what you previosuly posted, your broker is working fine on your Pi so lets try it out step by step and be explicit

e.g use the example launcher.conf first and see if you get any errors with that

So copy mqtt-launcher.py to /home/pi/mqtt-launcher.py

copy launcher.conf.example to /home/pi/launcher.conf

make sure you are in /home/pi

then run

Code: Select all

python mqtt-launcher.py 
On my system - it just sits there without errors (which I take to mean its working) - I use ctrl-c to exit

What happens on your system?

atux_null
Posts: 127
Joined: Thu Jun 21, 2012 1:58 pm

Re: MQTT send alert

Mon Jan 07, 2019 9:08 pm

hi. if i execute the command to manually run mosquitto to listen to all topics

Code: Select all

root@Rpi2: ~ $ mosquitto_sub -v -t '#' -h localhost
^C
root@Rpi2: ~ $
it works.

also

Code: Select all

root@Rpi2: ~ $ netstat -tlnp | grep 1883
tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN      310/mosquitto
tcp6       0      0 :::1883                 :::*                    LISTEN      310/mosquitto
root@Rpi2: ~ $
so what it wrong?

User avatar
DougieLawson
Posts: 34561
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: MQTT send alert

Mon Jan 07, 2019 9:13 pm

Leave mosquitto_sub running, then run mosquitto_pub -t 'test/topic' -h localhost -m "test message"

If that works try your publisher program again. If it doesn't work, we need to see the whole program not just snippets and not just examples of how it fails.
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a hippy & doctor free zone.

atux_null
Posts: 127
Joined: Thu Jun 21, 2012 1:58 pm

Re: MQTT send alert

Mon Jan 07, 2019 9:24 pm

it seems to work

Code: Select all

root@Rpi2: ~ $  mosquitto_sub -v -t '#' -h localhost
test/topic test message

so why the mqtt-launcher is not working?

User avatar
DougieLawson
Posts: 34561
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: MQTT send alert

Mon Jan 07, 2019 9:50 pm

I've just downloaded it with cd /tmp; wget https://github.com/jpmens/mqtt-launcher/archive/master.zip, unzipped it, edited the sample config (to add my broker's ip address) and renamed it as /tmp/mqtt-launcher-master/launcher.conf. Ran the launcher with ./mqtt_launcher.py

Code: Select all

dougie@apollo:~$ mosquitto_pub -t 'test/topic' -m "test" -h 192.168.3.14
dougie@apollo:~$ mosquitto_pub -t 'sys/file' -m "info"
dougie@apollo:~$ mosquitto_pub -t 'sys/file' -m "create"
dougie@apollo:~$ mosquitto_pub -t 'sys/file' -m "info"
dougie@apollo:~$ mosquitto_pub -t 'sys/file' -m "false"
dougie@apollo:~$ mosquitto_pub -t 'sys/file' -m "false"
dougie@apollo:~$ mosquitto_pub -t 'sys/file' -m "info"
dougie@apollo:~$

Code: Select all

dougie@apollo:~$ mosquitto_sub -t '#' -v -h 192.168.3.14
sys/file info
sys/file/report *****> Command '['/bin/ls', '-l', '/tmp/file.one']' returned non-zero exit status 2
sys/file create
sys/file/report (null)
sys/file info
sys/file/report -rw-r--r-- 1 pi pi 0 Jan  7 21:44 /tmp/file.one
sys/file false
sys/file/report (null)
sys/file false
sys/file/report (null)
sys/file info
sys/file/report *****> Command '['/bin/ls', '-l', '/tmp/file.one']' returned non-zero exit status 2
Note: Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

This is a hippy & doctor free zone.

atux_null
Posts: 127
Joined: Thu Jun 21, 2012 1:58 pm

Re: MQTT send alert

Tue Jan 08, 2019 6:43 am

i will try to reinstall the image and build it from scratch. I've tried everything, but still it resists to work. I imagine that something broke in my system. A fresh install will reveal if it's the case.

PhatFil
Posts: 846
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: MQTT send alert

Tue Jan 08, 2019 7:43 am

Node Red may be worth looking at it has nodes to both publish and subscribe to mqtt host/topics as well as nodes devoted to socail media twitter and email etc for external coms/alerts, I host node red alongside my mqtt broker on a pi-0-w as it also provides a gui dahboard interface too.

Return to “Automation, sensing and robotics”