MQTT Client

Questions and comments specific to a particular plugin should go here.
User avatar
kgschlosser
Site Admin
Posts: 4209
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: MQTT Client

Post by kgschlosser » Tue Aug 01, 2017 8:07 am

no worries.

a container data type list, tuple, dict, str, unicode can be tested in a Boolean True/False manner to see if there is data in the container or not.

example:

Code: Select all


test = 'test string'

if test:
    print 'there is data'

test = ''

if not test:
    print 'there is no data'

If you like the work I have been doing then feel free to Image

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

Re: MQTT Client

Post by Foune » Tue Aug 01, 2017 11:41 am

I think it won't work. The current code actually fills "result" when parsing my MQTT message, so your condition "if result:" will be true. The error comes at "str(result[4])" because there are 3 items in my message : I changed "str(result[4])" to "str(result[0])" (in case res_key must provide the domoticz ID, I'm not sure), the MQTT plugin crashes one step further. I still ignore why this code is triggered during message sending, the message is correctly delivered even when it crashes.

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

Re: MQTT Client

Post by kgschlosser » Tue Aug 01, 2017 12:00 pm

I am not sure if the creator of this plugin is still offering support for it. but he would know. hopefully he will chime in

but You are correct in your assessment of if it works or not. I wasn't thinking
I am not sure if the exception you are getting is an IndexError but I am going to base this fix on that. if it is a different error you can replace the IndexError with the exception name you are getting

Code: Select all

            def on_message(client, userdata, msg):
                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
                try:
                    res_key = msg.topic + ', ' + str(result[4])
                    res_base = str(msg.qos)+" "+str(result)
                    ProcessEvent2(msg, False, 0.0, res_key, result)
                except IndexError:
                    pass
                return
If you like the work I have been doing then feel free to Image

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

Re: MQTT Client

Post by krambriw » Tue Aug 01, 2017 1:20 pm

Hello friends,
I just saw this and for sure, this is something I can support,

When you are publishing a message to a MQTT broker and subscribe to the same topic, you will for sure receive your own message, maybe this is what you have done? This is not wrong in principle but in this case, what seems missing, is support for the message structure that is received.

Could you somehow "debug" so that we can have a view of how the message is structured when it arrives from the broker? You could insert a "print event" statement like in this example and then put a response here in the forum showing examples of the event structure

Best regards, Walter

Code: Select all

        def on_message(client, userdata, msg):
            if msg.topic.find('domoticz') != -1:
                event = literal_eval(msg.payload)
                print event
                result = []
/code]

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

Re: MQTT Client

Post by Foune » Tue Aug 01, 2017 2:29 pm

This is a clever idea as I spent a bit more time on the MQTT plugin and could'nt find any link between the message sending and this code. Only in message reception.

BUT

I didn't subscribe to the topic domoticz/in , and I know I've never received any domoticz/in MQTT message in Eg.

Nevertheless

Yesterday I tried to subscribe to domoticz/in in Eg, for testing purpose. With no luck, I could'nt receive any message (I had already tried some month ago).

So, something looks strange with Eg and domoticz/in . The domoticz/in MQTT messages I started to generate yesterday are perfect clones of the Tasmota devices I use everyday. I could check that they are identical in the domoticz log.
Example :

Code: Select all

Topic: domoticz/in, Message: {"idx":14,"nvalue":1,"svalue":""}
Anyway, I'll look at this tonight.

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

Re: MQTT Client

Post by krambriw » Tue Aug 01, 2017 5:56 pm

Please also provide the python script command you use to publish.

ToxMox
Posts: 14
Joined: Fri Aug 04, 2017 7:39 pm

Re: MQTT Client

Post by ToxMox » Wed Aug 09, 2017 2:09 pm

I just started using this plugin today (great plugin btw!) with beebotte.com as the mqtt broker. Currently my payloads are coming in as JSON. For example u"{"data":"cbs", "ispublic":true}.
For now I guess I will parse eg.event.payload using python's JSON parser but I think it would be great to have a JSON parsing built into the plugin to grab the separate items into payload segments.

My python parsing code btw:

Code: Select all

import json
print(eg.event.payload)
parsed_json = json.loads(eg.event.payload)
print(parsed_json['data'])

ToxMox
Posts: 14
Joined: Fri Aug 04, 2017 7:39 pm

Re: MQTT Client

Post by ToxMox » Thu Aug 10, 2017 3:19 pm

I'm currently using 1.1.0 using non ssl connection to my mqtt broker but I'd prefer to connect ssl/tls which isn't working for me in 1.1.0 because I have a single cert file.

I removed all mqtt plugin actions and installed 1.2.0 but when I add the plugin EG just hangs not responding.

This is on Windows 10 x64 Creator's Edition and EG version 0.4.1.r1722.

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

Re: MQTT Client

Post by krambriw » Thu Aug 10, 2017 4:58 pm

And you did remember to copy the paho folder to lib27/site-packages ??? (If you did not delete the old one manually, it is still there and a newer version is included in the 1.2.0 plugin)

I use EG version 0.5.0 rc1 but I think it should work ok also in older versions. As example, this is my setting connecting to mycloudmqtt using username, password and a single certificate

BR Walter
Skärmklipp.PNG
Skärmklipp.PNG (17.38 KiB) Viewed 2426 times

ToxMox
Posts: 14
Joined: Fri Aug 04, 2017 7:39 pm

Re: MQTT Client

Post by ToxMox » Thu Aug 10, 2017 5:03 pm

krambriw wrote:And you did remember to copy the paho folder to lib27/site-packages ??? (If you did not delete the old one manually, it is still there and a newer version is included in the 1.2.0 plugin)

I use EG version 0.5.0 rc1 but I think it should work ok also in older versions. As example, this is my setting connecting to mycloudmqtt using username, password and a single certificate

BR Walter
Skärmklipp.PNG
Well I started with 1.2.0 then downgraded to 1.1.0. I'll play around some more and maybe move to the latest RC. I didn't mention that in my last post but yeah I started with 1.2.0 which froze EG so I downloaded 1.1.0 which worked but then I realized I need the improved cert stuff so tried 1.2.0 again.

ToxMox
Posts: 14
Joined: Fri Aug 04, 2017 7:39 pm

Re: MQTT Client

Post by ToxMox » Thu Aug 10, 2017 5:57 pm

krambriw wrote:And you did remember to copy the paho folder to lib27/site-packages ??? (If you did not delete the old one manually, it is still there and a newer version is included in the 1.2.0 plugin)

I use EG version 0.5.0 rc1 but I think it should work ok also in older versions. As example, this is my setting connecting to mycloudmqtt using username, password and a single certificate

BR Walter
Skärmklipp.PNG
So I just tried this:

1) Completely uninstall EG
2) Remove EG folders from Program Files (x86) and ProgramData
3) Installed RC1
4) Installed 1.2.0
5) Copied Paho folder to lib27/site-packages (BTW I didn't see anywhere instructions to do this until you asked in your post)
6) Add plugin to EG
7) Try to add a client action and get the following errors in my console:
http://i.imgur.com/chm8Hm6.png

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

Re: MQTT Client

Post by krambriw » Fri Aug 11, 2017 11:41 am

I suspect I made a bug

I was not able to reproduce the error but I think I found what could cause the problem. I have modified the plugin (new version 1.2.1)

User avatar
yokel22
Experienced User
Posts: 251
Joined: Thu Feb 05, 2015 5:56 pm
Location: U.S. - Kansas city

Re: MQTT Client

Post by yokel22 » Sun Aug 13, 2017 5:58 pm

Krambriw, do you mind sharing your mqtt client script your using on the raspberry pi?

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

Re: MQTT Client

Post by krambriw » Sun Aug 13, 2017 6:28 pm

Here is a simple python script that gets the weather at my location, connects to a mqtt broker and publish it. The example connects to a local mqtt broker in the rpi on port 1883 but you can change as you like

1) Create a python file and copy & paste the script below, save and transfer the python file to /home/pi
2) You need also to copy pywapi.py to the rpi /home/pi (you can find it in my plugin SunTracker)
3) You need also to copy mosquitto.py to the rpi /home/pi (you can find it here http://krambriw.net/Release/MQTT%20Clie ... 20version/ )

You can then run the script in a command prompt under Linux in a rpi, type python name_of_script.py

Best regards, Walter

Code: Select all

import pywapi
import mosquitto
import string

def send_mqtt_message(topic, msg):
    client = mosquitto.Mosquitto("Weather")
    client.connect(mqtt_host, port)
    for i in range(5):
        result, mid = client.publish(topic, msg, 0)
        if result == 0:
            break
    client.disconnect()
    del client


def CheckWeatherCondition():
    weather_data = None
    currCondition = "Undefined"
    try:
        weather_data = pywapi.get_weather_from_weather_com('SWXX0256')
        print weather_data
        currCondition = (weather_data['current_conditions']['text'])
    except:
        pass
    return currCondition


mqtt_host = '127.0.0.1'
port = 1883
topic_out = '/weather'
weather = CheckWeatherCondition()
send_mqtt_message(topic_out, str(weather))


User avatar
yokel22
Experienced User
Posts: 251
Joined: Thu Feb 05, 2015 5:56 pm
Location: U.S. - Kansas city

Re: MQTT Client

Post by yokel22 » Sun Aug 13, 2017 6:34 pm

Thanks, that saved me a bunch of time.

Post Reply