MQTT Client

Questions and comments specific to a particular plugin should go here.
eventspook
Posts: 23
Joined: Tue Mar 05, 2013 2:50 pm

Re: MQTT Client

Post by eventspook » Mon Feb 06, 2017 7:10 pm

This is a really useful plugin, thanks for putting the time into this!

Are there any plans to support binary content/image uploads? This would allow me to send a small camera thumbnail to my MQTT Dash client on my Android device.

It looks like there is native support for this within the paho framework, so making it part of the Send Message's GUI might be the easiest option?

Either way, thank you :)
Last edited by eventspook on Tue Feb 07, 2017 3:04 pm, edited 1 time in total.

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: MQTT Client

Post by krambriw » Tue Feb 07, 2017 4:38 am

Haven't thought about it but makes perfect sense, I'll look into it
Thanks for the suggestion
My latest plugins
http://plugins.krambriw.net/index.html

Two men in front of a huge excavator...
- that beast steels work from 100 men with shovels
- well, a 1000 men could do the same using teaspoons

krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: MQTT Client

Post by krambriw » Tue Feb 07, 2017 11:01 am

Pls try this one, it supports publishing binary files like pictures. A new action has been added for this purpose
__init__.py
(50.59 KiB) Downloaded 102 times
Be aware that you need an MQTT client that can handle and support subscriptions to binary messages (it will not work using the EG MQTT Client plugin)

Add a new macro/action and configure the action. There is a new field where you browse and select the file
Skärmklipp.JPG
My latest plugins
http://plugins.krambriw.net/index.html

Two men in front of a huge excavator...
- that beast steels work from 100 men with shovels
- well, a 1000 men could do the same using teaspoons

Zendio
Posts: 3
Joined: Tue Oct 15, 2013 9:45 am

Re: MQTT Client

Post by Zendio » Sat Apr 08, 2017 2:58 pm

Sadly I'm unable to use either the old plugin, or the one based on Paho.
I've tried various things, such as reinstalling Python 2.6 and 3.x, but to no avail.

Looking in the eventviewer it tells me there's an error with COMCTL32.dll.

Does anyone have a clue why this is playing up?

Thank you!
Zendio

Snowbird
Experienced User
Posts: 115
Joined: Fri Jul 03, 2009 10:04 am

Re: MQTT Client

Post by Snowbird » Mon May 29, 2017 9:48 am

Hello,

I was just trying to use this plugin, and I had no problem with it when used in a non SSL mode, it's working great ! :) thanks !!

On the other hand, when I try to use it in SSL mode, I get the following error :

Code: Select all

11:02:32      Error in Action: "eventghost/pc/batt > Percentage"
11:02:32      Traceback (most recent call last) (0.5.0-rc2):
11:02:32        File "C:\Program Files (x86)\EventGhost\eg\Classes\ActionBase.py", line 116, in CallWrapper
11:02:32          return self(*args)
11:02:32        File "C:\ProgramData\EventGhost\plugins\MQTT Client\__init__.py", line 1221, in __call__
11:02:32          bind_address=""
11:02:32        File "C:\ProgramData\EventGhost\plugins\MQTT Client\paho\mqtt\client.py", line 612, in connect
11:02:32          return self.reconnect()
11:02:32        File "C:\ProgramData\EventGhost\plugins\MQTT Client\paho\mqtt\client.py", line 757, in reconnect
11:02:32          ciphers=self._tls_ciphers)
11:02:32        File "ssl.pyc", line 933, in wrap_socket
11:02:32        File "ssl.pyc", line 601, in __init__
11:02:32        File "ssl.pyc", line 830, in do_handshake
11:02:32      SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)
I have entered the certificate path :
C:\Users\Me\Documents\EventGhost\Certificate\fullchain.pem

The server running the MQTT Broker also hosts OpenSSL version 1.0.1t. To double check, I have tried to connect to the broker with an Android app, providing the same SSL information and it works, I can subscribe and publish.

What could be the problem ?

Thanks for any help you could provide.

Snowbird
Experienced User
Posts: 115
Joined: Fri Jul 03, 2009 10:04 am

Re: MQTT Client

Post by Snowbird » Tue May 30, 2017 9:05 am

Hi,

In my previous message I forgot to mention that I was using LetsEncrypt to generate the certificates.

I have found some information, I've checked the log on my MQTT broker, and it looks like it's a TLS problem :

Code: Select all

1496075089: New connection from xxx.xxx.xxx.xxx on port 8883.
1496075089: OpenSSL Error: error:14094418:SSL routines:SSL3_READ_BYTES:tlsv1 alert unknown ca
1496075089: OpenSSL Error: error:140940E5:SSL routines:SSL3_READ_BYTES:ssl handshake failure
1496075089: Socket error on client <unknown>, disconnecting.
it looks like it's a CA (Certificate Authority) problem.

I've found other useful information on these websites :
https://mosquitto.org/2015/12/using-let ... mosquitto/
https://github.com/emqtt/emqttd/issues/562

I'm still investigating why I can't connect with this plugin, I've tried the Eclipse PAHO client and it works... If anyone reading this has any knowledge regarding this certificate business i'll be more than happy to hear from you :) there must be something I'm doing wrong...

Foune
Experienced User
Posts: 87
Joined: Sun Jul 03, 2011 7:08 pm

Re: MQTT Client

Post by Foune » Mon Jun 05, 2017 3:25 pm

#EDIT : all right, the problem under is solved, I made some cleaning in the MQTT subscriptions, all is working fine again. Now I have to find how to manage different subscriptions in e.g as they are not nicely formated at first sight :

From domoticz/out :
MQTT.domoticz/out/Temp/34 [255, 12, 'Temp', '82034', 34, 'HTPC CPU', 0, 'LaCrosse TX3', '33.0', 1]

From sonoff/stat :
MQTT.stat/sonoff/POWER.ON u"ON"

I don't see client ID, I can't set prefixes, I think there's something wrong in my setup.

-------------------------------------

Previous message :

I just flashed an sonoff module using Tasmota project. This is very powerful, as it makes any sonoff talking to domoticz using MQTT, and to any other app using some other MQTT topics (for instance publications to stat/sonoff/POWER for status messages).
Since I tried to add new subscriptions to Eg, I get exceptions anytime the sonoff sends domoticz a message. I removed all MQTT subscriptions from eg except domoticz/out, and restarted eg several times : eg now fires exceptions if I trigger the push button on the sonoff. If I trigger the sonoff from domoticz, everything is fine. If I do the same from the push button : exceptions

Code: Select all

17:20:32   Exception in thread co:
17:20:32   Traceback (most recent call last):
17:20:32     File "threading.pyc", line 532, in __bootstrap_inner
17:20:32     File "C:\ProgramData\EventGhost\plugins\MQTT Client\__init__.py", line 439, in run
17:20:32       lrsp = mqttc.loop(10.0, 1)
17:20:32     File "C:\ProgramData\EventGhost\plugins\MQTT Client\paho\mqtt\client.py", line 824, in loop
17:20:32       rc = self.loop_read(max_packets)
17:20:32     File "C:\ProgramData\EventGhost\plugins\MQTT Client\paho\mqtt\client.py", line 1086, in loop_read
17:20:32       rc = self._packet_read()
17:20:32     File "C:\ProgramData\EventGhost\plugins\MQTT Client\paho\mqtt\client.py", line 1488, in _packet_read
17:20:32       rc = self._packet_handle()
17:20:32     File "C:\ProgramData\EventGhost\plugins\MQTT Client\paho\mqtt\client.py", line 1956, in _packet_handle
17:20:32       return self._handle_publish()
17:20:32     File "C:\ProgramData\EventGhost\plugins\MQTT Client\paho\mqtt\client.py", line 2131, in _handle_publish
17:20:32       self._handle_on_message(message)
17:20:32     File "C:\ProgramData\EventGhost\plugins\MQTT Client\paho\mqtt\client.py", line 2287, in _handle_on_message
17:20:32       self.on_message(self, self._userdata, message)
17:20:32     File "C:\ProgramData\EventGhost\plugins\MQTT Client\__init__.py", line 323, in on_message
17:20:32       res_key = msg.topic + ', ' + str(result[4])
17:20:32   IndexError: list index out of range

Domoticz/out when triggered from the sonoff button :

Code: Select all

{
  "Battery": 255,
  "RSSI": 12,
  "description": "",
  "dtype": "Light/Switch",
  "id": "00014193",
  "idx": 323,
  "name": "Relais sonoff2",
  "nvalue": 1,
  "stype": "Switch",
  "switchType": "On/Off",
  "unit": 1
}
When triggered from domoticz :

Code: Select all

{
  "Battery": 255,
  "RSSI": 12,
  "description": "",
  "dtype": "Light/Switch",
  "id": "00014193",
  "idx": 323,
  "name": "Relais sonoff2",
  "nvalue": 1,
  "stype": "Switch",
  "svalue1": "0",
  "switchType": "On/Off",
  "unit": 1
}
Before I started adding other subscriptions to mqtt, I had no errors. Now I removed all other subscriptions, restarted eg, I can't get thing work as before.
Is there a rational explanation?
Is there an old a bad remaining susbscription to my mosquito mqtt broker?

Snowbird
Experienced User
Posts: 115
Joined: Fri Jul 03, 2009 10:04 am

Re: MQTT Client

Post by Snowbird » Tue Jun 13, 2017 7:45 am

Hello,

I finally find out what was causing the SSL handshake failure !! :) it has been more than 2 weeks that i've been searching ! :)
Ok here's what you need to do in case this happens to someone else :

if you're running your own MQTT broker such as Mosquitto, on your own linux server such as Debian, and running your own VPN server such as OpenVPN, and finally you're using let'sencrypt certificates to secure a bit things, then all you need to do is to find the certificate file located on your linux server, it's here : /etc/ssl/certs then search for *.crt, there should be only one file with this extension, mine is named : ca-certificates.crt but it could have a different name on your server. All you have to do is copy this file anywhere on your the computer where your EG is installed, and in the MQTT plugin setup specify the certificate path, just provide the location of the certificate you've just copied, and that's it, no more errors, and the connection is now secured !

Hope this helps !

sintei
Posts: 26
Joined: Thu Oct 09, 2014 9:46 am

Re: MQTT Client

Post by sintei » Thu Jun 29, 2017 1:03 pm

For the thrills I just updated my EG to 5 without making a backup :roll:
My MQTT stopped working so I found the new MQTT plugin (PAHO) and installed it.
It now sees my broker etc.
However, how do you publish messages from EG now?
In my script I have this:

Code: Select all

eg.plugins.MQTTthreads.publishMQTT(u'hum to mqtt', u'192.168.x.xx', 1883, u'xx/xx/humidity/oregon1sensorhumidity/state', u'{eg.globals.oregon1humidity}', 0, True)
However, I get this warning:
eg.plugins.MQTTthreads.publishMQTT(u'hum to mqtt', u'192.168.x.xx', 1883, u'xx/xx/humidity/oregon1sensorhumidity/state', u'{eg.globals.oregon1humidity}', 0, True)
TypeError: __call__() takes exactly 14 arguments (8 given)

Is there a change in how to publish in a python script? I'm not using any credentials or cert at the moment.

Snowbird
Experienced User
Posts: 115
Joined: Fri Jul 03, 2009 10:04 am

Re: MQTT Client

Post by Snowbird » Thu Jun 29, 2017 1:23 pm

Indeed, you have the 6 last arguments that are missing, you should have something similar to this :

eg.plugins.MQTTthreads.publishMQTT(u'evenghtost', u'192.168.x.xx', 1883, u'eg/session', u'Session Locked', 0, True, '223671544216', False, u'username', u'password', False, u'')

what version are you using ? you seem to have an old version that doesn't have authentication. Just try to add a MQTT Publish action in EG and "copy as python" and see what is the result, it should look something similar to what I have above.

sintei
Posts: 26
Joined: Thu Oct 09, 2014 9:46 am

Re: MQTT Client

Post by sintei » Thu Jun 29, 2017 1:41 pm

Snowbird wrote:Indeed, you have the 6 last arguments that are missing, you should have something similar to this :

eg.plugins.MQTTthreads.publishMQTT(u'evenghtost', u'192.168.x.xx', 1883, u'eg/session', u'Session Locked', 0, True, '223671544216', False, u'username', u'password', False, u'')

what version are you using ? you seem to have an old version that doesn't have authentication. Just try to add a MQTT Publish action in EG and "copy as python" and see what is the result, it should look something similar to what I have above.
I'm not using auth at the moment!
However, you pointed me in the right direction. It's been so long before I used EG so I totally forgot about "copy to python".
I managed to get it working now, thanks a bunch Snowbird!

Snowbird
Experienced User
Posts: 115
Joined: Fri Jul 03, 2009 10:04 am

Re: MQTT Client

Post by Snowbird » Thu Jun 29, 2017 1:57 pm

my pleasure ! ;)

Foune
Experienced User
Posts: 87
Joined: Sun Jul 03, 2011 7:08 pm

Re: MQTT Client

Post by Foune » Mon Jul 31, 2017 10:03 pm

Hello,

I've been using the PAHO version for 5 month with great satisfaction. Today I decided to publish MQTT messages to domoticz because it is much cleaner than the json method.
Something strange is happening. When sending a MQTT message, this code is launched :

Code: Select all

        def on_message(client, userdata, msg):
            if msg.topic.find('domoticz') != -1:
                event = literal_eval(msg.payload)
                result = []
                allowed = [
                    "Battery", 
                    "RSSI", 
                    "dtype", 
                    "id", 
                    "idx", 
                    "name", 
                    "nvalue", 
                    "stype", 
                    "svalue1", 
                    "svalue2", 
                    "switchType", 
                    "unit"
                ]
                for item in allowed:
                    try:
                        result.append(event[item])
                    except:
                        pass
                res_key = msg.topic + ', ' + str(result[4])
                res_base = str(msg.qos)+" "+str(result)
                ProcessEvent2(msg, False, 0.0, res_key, result)
                return
Unfortunately this crashes at this point : res_key = msg.topic + ', ' + str(result[4]) , I guess that result is not filled as expected
I can't explain why this method is triggered during a message sending. To stop the exception I just added this ugly patch :

Code: Select all

if msg.topic.find('/in') != -1:
                    return
Because the message is sent to the domoticz/in topic.
Now everything is working as expected.

User avatar
kgschlosser
Site Admin
Posts: 2891
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: MQTT Client

Post by kgschlosser » Tue Aug 01, 2017 4:20 am

Code: Select all

        def on_message(client, userdata, msg):
            if msg.topic.find('domoticz') != -1:
                event = literal_eval(msg.payload)
                result = []
                allowed = [
                    "Battery",
                    "RSSI",
                    "dtype",
                    "id",
                    "idx",
                    "name",
                    "nvalue",
                    "stype",
                    "svalue1",
                    "svalue2",
                    "switchType",
                    "unit"
                ]
                for item in allowed:
                    try:
                        result.append(event[item])
                    except:
                        pass
                if result:
                    res_key = msg.topic + ', ' + str(result[4])
                    res_base = str(msg.qos)+" "+str(result)
                    ProcessEvent2(msg, False, 0.0, res_key, result)
                return
If you like the work I have been doing then feel free to Image

Foune
Experienced User
Posts: 87
Joined: Sun Jul 03, 2011 7:08 pm

Re: MQTT Client

Post by Foune » Tue Aug 01, 2017 8:01 am

Thanks, will try asap.

Post Reply