Author Topic: Beta release of the MQTT bindings  (Read 2699 times)

rtrbt

  • Tinkerforge Staff
  • Administrator
  • Full Member
  • *****
  • Posts: 110
    • View Profile
Beta release of the MQTT bindings
« on: January 24, 2019, 16:22:24 »
Hi,

As of today, a beta version of the MQTT bindings 2.0 is available. Please tinker around with them and post any bugs, suggestions and other feedback here.

The MQTT bindings are now generated like other programming language bindings. All Bricks and Bricklets are supported. The bindings map directly to the Python bindings, which is why they are not backwards-compatible to the old MQTT proxy. The MQTT proxy is now discontinued.

The current version of the bindings is attached to this post, including examples.

The bindings depend on Python >= 2.7.9 or >= 3.4 and the Paho library (>= 1.3.1) available here.

The documentation can be found here

Have a lot of fun!

Erik

Edit:
Version 2.0.1
- Fix handling of JSON errors for Python 2
« Last Edit: January 25, 2019, 12:34:26 by rtrbt »

rtrbt

  • Tinkerforge Staff
  • Administrator
  • Full Member
  • *****
  • Posts: 110
    • View Profile
Re: Beta release of the MQTT bindings
« Reply #1 on: February 26, 2019, 14:34:46 »
The MQTT bindings have left the beta phase and are now available on the Tinkerforge home page: https://www.tinkerforge.com/de/doc/Downloads.html

The bindings now support the --init-file parameter, which allows loading initial configuration from a file.

riro

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Beta release of the MQTT bindings
« Reply #2 on: March 04, 2019, 18:30:00 »
Hello Forum,

just unzipped the new zip file, copied it to /usr/local/bin, made it runable via chmod +x.

Now, running it:

Code: [Select]
sudo /usr/local/bin/tinkerforge_mqtt

Traceback (most recent call last):
  File "/usr/local/bin/tinkerforge_mqtt", line 6649, in <module>
    main()
  File "/usr/local/bin/tinkerforge_mqtt", line 6646, in main
    bindings.run(initial_config)
  File "/usr/local/bin/tinkerforge_mqtt", line 5981, in run
    for topic, payload in initial_config.items():
AttributeError: 'NoneType' object has no attribute 'items'
Exception in thread Disconnect-Prober (most likely raised during interpreter shutdown):
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
  File "/usr/lib/python2.7/threading.py", line 754, in run
  File "/usr/local/bin/tinkerforge_mqtt", line 1179, in disconnect_probe_loop
<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'Empty'


resulted in the messsage above.

Could anyone give me a hint, how to use this MQTT Binding?
What has to be done, for example, to make this Binding to publish the values from the Outdoor Weather Station.

From the example file:

Quote
# Change XYZ to the UID of your Outdoor Weather Bricklet

setup:
   # Enable station data callbacks
   publish '{"enable_callback": true}' to tinkerforge/request/outdoor_weather_bricklet/XYZ/set_station_callback_configuration
   
   # Enable sensor data callbacks
   publish '{"enable_callback": true}' to tinkerforge/request/outdoor_weather_bricklet/XYZ/set_sensor_callback_configuration
   
   # Handle incoming station data callbacks
   subscribe to tinkerforge/callback/outdoor_weather_bricklet/XYZ/station_data
   publish '{"register": true}' to tinkerforge/register/outdoor_weather_bricklet/XYZ/station_data # Register station_data callback
   
   # Handle incoming sensor data callbacks
   subscribe to tinkerforge/callback/outdoor_weather_bricklet/XYZ/sensor_data
   publish '{"register": true}' to tinkerforge/register/outdoor_weather_bricklet/XYZ/sensor_data # Register sensor_data callback


I dont know, or i'm blind, how to put the right command.
I'm using a MQTT Broker on another machine. To view all the MQTT-chatter i use MQTT.fx and the new MQTT Explorer from https://github.com/thomasnordquist/MQTT-Explorer

Thank you!  :D

rtrbt

  • Tinkerforge Staff
  • Administrator
  • Full Member
  • *****
  • Posts: 110
    • View Profile
Re: Beta release of the MQTT bindings
« Reply #3 on: March 04, 2019, 20:06:36 »
Hi,

This was a problem on my end. Please try the attached version. To connect to a MQTT broker on another machine, you can run the bindings like this:
Code: [Select]
sudo /usr/local/bin/tinkerforge_mqtt --broker-host <IP>
MQTT-Explorer seems to subscribe automatically to all topics, so to see the sensor data, you only have to publish the messages listed in the example.
« Last Edit: March 04, 2019, 20:10:07 by rtrbt »

riro

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Beta release of the MQTT bindings
« Reply #4 on: March 04, 2019, 22:33:52 »
Hey,

installed the new version. Thank you for your answer. But no luck.

Perhaps:

Code: [Select]
WARNING:MQTT bindings:Another MQTT bindings instance started on this broker with the same global prefix. This is not recommended as both bindings instances will receive requests and send responses.

Idk what other instance is running here.  :-[

I send the (json formatted) payload from the example file to the broker. I get a reaction like "callback/binding/restart" ... strange ...

Perhaps i should remove the broker and install again.
Are there any special requirements for the broker?


rtrbt

  • Tinkerforge Staff
  • Administrator
  • Full Member
  • *****
  • Posts: 110
    • View Profile
Re: Beta release of the MQTT bindings
« Reply #5 on: March 05, 2019, 09:26:24 »
The message published to "callback/bindings/restart" is sent by the bindings to notify other bindings to print the warning you see. As your broker is running on another machine, my first guess is, that there is a timing problem here: Maybe the bindings see their own restart message. But I need to take another look. For now you can ignore the warning if you are sure, there is no other instance running.

Your broker seems to run okay, you don't need to reinstall.

For testing purposes you can try to enumerate the attached devices:
publish
Code: [Select]
{"register": true} to
Code: [Select]
tinkerforge/register/ip_connection/enumerateand then publish an empty message to
Code: [Select]
tinkerforge/request/ip_connection/enumerateYou should then see responses for all found devices under the topic
Code: [Select]
tinkerforge/callback/ip_connection/enumerate
« Last Edit: March 05, 2019, 09:30:08 by rtrbt »

riro

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Beta release of the MQTT bindings
« Reply #6 on: March 05, 2019, 09:58:19 »
The broker is running on the same machine. So no external host ips are needed.

Ok, your given way seems to work/there is  a reaction:

Output from MQTT Explorer:
Code: [Select]
{"connected_uid": "6evjRp", "uid": "Es8", "device_identifier": "outdoor_weather_bricklet", "hardware_version": [1, 0, 0], "enumeration_type": "available", "position": "a", "firmware_version": [2, 0, 2], "_display_name": "Outdoor Weather Bricklet"}

Output from Console:
while running tinkerforge_mqtt and posting mqtt messages:
Code: [Select]
ERROR:MQTT bindings:253 ('6kL62k', '0', '0', (2, 0, 0), (2, 4, 10), 13, 0)
ERROR:MQTT bindings:253 ('eVC', '6kL62k', 'a', (1, 0, 0), (2, 0, 2), 221, 0)
ERROR:MQTT bindings:253 ('eny', '6kL62k', 'b', (1, 1, 0), (2, 0, 3), 21, 0)
ERROR:MQTT bindings:253 ('eXk', '6kL62k', 'c', (1, 2, 0), (2, 0, 6), 212, 0)
ERROR:MQTT bindings:253 ('eE3', '6kL62k', 'd', (1, 1, 0), (2, 0, 2), 27, 0)
ERROR:MQTT bindings:253 ('6evjRp', '6kL62k', '1', (2, 0, 0), (2, 4, 10), 13, 0)
ERROR:MQTT bindings:253 ('EQL', '6evjRp', 'b', (1, 1, 0), (2, 0, 4), 216, 0)
ERROR:MQTT bindings:253 ('yLJ', '6evjRp', 'd', (2, 0, 0), (2, 0, 2), 259, 0)
ERROR:MQTT bindings:253 ('Es8', '6evjRp', 'a', (1, 0, 0), (2, 0, 2), 288, 0)
ERROR:MQTT bindings:253 ('6kL62k', '0', '0', (2, 0, 0), (2, 4, 10), 13, 0)
ERROR:MQTT bindings:253 ('eVC', '6kL62k', 'a', (1, 0, 0), (2, 0, 2), 221, 0)
ERROR:MQTT bindings:253 ('eny', '6kL62k', 'b', (1, 1, 0), (2, 0, 3), 21, 0)
ERROR:MQTT bindings:253 ('eXk', '6kL62k', 'c', (1, 2, 0), (2, 0, 6), 212, 0)
ERROR:MQTT bindings:253 ('eE3', '6kL62k', 'd', (1, 1, 0), (2, 0, 2), 27, 0)
ERROR:MQTT bindings:253 ('6evjRp', '6kL62k', '1', (2, 0, 0), (2, 4, 10), 13, 0)
ERROR:MQTT bindings:253 ('EQL', '6evjRp', 'b', (1, 1, 0), (2, 0, 4), 216, 0)
ERROR:MQTT bindings:253 ('yLJ', '6evjRp', 'd', (2, 0, 0), (2, 0, 2), 259, 0)
ERROR:MQTT bindings:253 ('Es8', '6evjRp', 'a', (1, 0, 0), (2, 0, 2), 288, 0)

Output from the console

Code: [Select]

./tinkerforge enumerate

uid=6kL62k
connected-uid=0
position=0
hardware-version=2,0,0
firmware-version=2,4,10
device-identifier=master-brick
enumeration-type=available

uid=eVC
connected-uid=6kL62k
position=a
hardware-version=1,0,0
firmware-version=2,0,2
device-identifier=barometer-bricklet
enumeration-type=available

uid=eny
connected-uid=6kL62k
position=b
hardware-version=1,1,0
firmware-version=2,0,3
device-identifier=ambient-light-bricklet
enumeration-type=available

uid=eXk
connected-uid=6kL62k
position=c
hardware-version=1,2,0
firmware-version=2,0,6
device-identifier=lcd-20x4-bricklet
enumeration-type=available

uid=eE3
connected-uid=6kL62k
position=d
hardware-version=1,1,0
firmware-version=2,0,2
device-identifier=humidity-bricklet
enumeration-type=available

uid=6evjRp
connected-uid=6kL62k
position=1
hardware-version=2,0,0
firmware-version=2,4,10
device-identifier=master-brick
enumeration-type=available

uid=EQL
connected-uid=6evjRp
position=b
hardware-version=1,1,0
firmware-version=2,0,4
device-identifier=temperature-bricklet
enumeration-type=available

uid=yLJ
connected-uid=6evjRp
position=d
hardware-version=2,0,0
firmware-version=2,0,2
device-identifier=ambient-light-v2-bricklet
enumeration-type=available

uid=Es8
connected-uid=6evjRp
position=a
hardware-version=1,0,0
firmware-version=2,0,2
device-identifier=outdoor-weather-bricklet
enumeration-type=available

 :o

Are there any updates needed? The brickdaemon ist the latest ... Any librarys?


rtrbt

  • Tinkerforge Staff
  • Administrator
  • Full Member
  • *****
  • Posts: 110
    • View Profile
Re: Beta release of the MQTT bindings
« Reply #7 on: March 05, 2019, 10:06:11 »
The error messages are left-over debug code, I've removed them, this change will be in the next "official" version (will be posted in a few hours).

The rest looks good to me. Are the callbacks of the outdoor weather station working too? One thing I've noticed in the documentation: The single quotes are only there to mark the payload, so if you use the MQTT-Explorer, you have to omit them.

rtrbt

  • Tinkerforge Staff
  • Administrator
  • Full Member
  • *****
  • Posts: 110
    • View Profile
Re: Beta release of the MQTT bindings
« Reply #8 on: March 05, 2019, 11:50:46 »
The official version 2.0.3 is now available here.

riro

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Beta release of the MQTT bindings
« Reply #9 on: March 05, 2019, 17:16:03 »
Hello!

thank you very much for your guidance.

Within MQTT.fx i can reveive messages from both - the station and the sensor.

The process for activating and subscribing is quite long ... and does it have te be done after every reboot ... for every sensor?

Are you doing this with MQTT.fx (no paste possible, so manual write for each payload  :-\) or are u using mosquitto_pub and sub in the commandline?

What is the best way to autostart the tinkerforge_mqtt system after a reboot?

rtrbt

  • Tinkerforge Staff
  • Administrator
  • Full Member
  • *****
  • Posts: 110
    • View Profile
Re: Beta release of the MQTT bindings
« Reply #10 on: March 05, 2019, 17:26:58 »
The process for activating and subscribing is quite long ... and does it have te be done after every reboot ... for every sensor?

You don't have to configure the callbacky by hand. Instead you can use an init file, as described here.

What is the best way to autostart the tinkerforge_mqtt system after a reboot?
You could write a systemd service file. This is explained here. You should definitely use init-files then, or else you would have to reconfigure everything after a reboot.

riro

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Beta release of the MQTT bindings
« Reply #11 on: March 05, 2019, 21:00:04 »
this is my init file:

Code: [Select]
{
 "tinkerforge/request/outdoor_weather_bricklet/Es8/set_station_callback_configuration": {"enable_callback": true},
 "tinkerforge/register/outdoor_weather_bricklet/Es8/station_data": {"register": true}
}

and running it this:

Code: [Select]
sudo /usr/local/bin/tinkerforge_mqtt --init-file stationmqttini.txt
But under the used topic, there are no incoming messages.

Since there is a init file, i tried to switch off the status led.
With the request (empty payload)

Code: [Select]
tinkerforge/request/outdoor_weather_bricklet/Es8/get_status_led_config
i wanted to examine the payload structure for  request/outdoor_weather_bricklet/<UID>/set_status_led_config

But i dont get a response message. Any idea? Because this payload could be added to the init file.

Could you please look at my init file and tell me what's wrong? :-)

What is your setup for autostart after a reboot?

riro

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Beta release of the MQTT bindings
« Reply #12 on: March 05, 2019, 21:09:40 »
Publishing {"config": 0} to tinkerforge/request/outdoor_weather_bricklet/Es8/set_status_led_config
... works ... and the led is off.  :D

rtrbt

  • Tinkerforge Staff
  • Administrator
  • Full Member
  • *****
  • Posts: 110
    • View Profile
Re: Beta release of the MQTT bindings
« Reply #13 on: March 06, 2019, 09:28:14 »
Your init file looks good, I've tested it here and got callback responses. You could run the bindings with the --debug parameter and check if there are the following lines in the output:
Code: [Select]
<DEBUG> MQTT bindings: Calling function set_station_callback_configuration for device Es8 of type outdoor_weather_bricklet.

<DEBUG> MQTT bindings: Calling function set_station_callback_configuration for device Es8 of type outdoor_weather_bricklet succedded.

<DEBUG> MQTT bindings: Registered callback station_data for device Es8 of type outdoor_weather_bricklet. Will publish messages to tinkerforge/callback/outdoor_weather_bricklet/Es8/station_data.

If these are printed, the bindings should publish a message under the callback topic when there is new data (can take about 45 seconds).

The get_status_led_config request works here, but maybe there are some hints in your debug output.

Bottesford

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Beta release of the MQTT bindings
« Reply #14 on: March 18, 2019, 20:48:02 »
Maybe this will help someone - I created a systemd unit file for auto starting my mqtt bindings for all my TF bricks.

Create a file:
nano /etc/systemd/system/tf_mqtt@.service

Code: [Select]
[Unit]
Description=Tinkerforge MQTT %i

[Service]
Type=simple
StandardOutput=file:/var/log/tinkerforge_mqtt/tf_mqtt_%i.log
ExecStart=/usr/bin/python3 /usr/local/bin/tinkerforge_mqtt --ipcon-host %i --broker-username mqtt --broker-password <password> --global-topic-prefix tf_mqtt_%i

[Install]
WantedBy=multi-user.target

To start I pass the ip address (I use fixed ips) of my brick into the command:

sudo systemctl start tf_mqtt@192.168.2.150
sudo systemctl start tf_mqtt@192.168.2.151
etc.

Then to start on boot:
sudo systemctl enable tf_mqtt@192.168.2.150

Now I get all my sensor data under a topic: tf_mqtt_192.168.2.150/#

I register my callbacks via a python app in AppDaemon within Home Assistant. This means I can keep all my sensor config (led status, multi touch sensitivity, etc.) together instead of in the init file.
« Last Edit: May 07, 2019, 15:37:30 by Bottesford »