Jump to content

MQTT on pi problems


Pat28

Recommended Posts

Hi Everyone.

I am new to TF MQTT binding and i have got some problems.

Her is what i did:

My hardware setup:

·         * Raspberry pi 2B with Buster os with latest updates and upgrades

·         * Network connection with fixed IP 192.168.10.21 from PI to switch

·       *   Tinkerforge master brick with USB cable to PI and my air quality bricklet connected

·         * Synology DSM7.1 (IP 192.168.10.5) with mosquitto package installed used as MQTT broker and Openhab 3.3 software

 

Install brick viewer on pi :

sudo apt install brickv

See bricklet UID via brickv software on PI: can see that it is working without problems

 

Install mqtt api binding on raspberry pi:

sudo apt install tinkerforge-mqtt

 

Copy the tinkerforge-mqtt fle to the /usr/local/bin folder on pi

Create a init-file in the same /usr/local/bin folder to put the mqtt commands

For example tf_mqtt.txt file with MQTT commands for air quality bricklet found in the Tinkerforge MQTT documentation examples webpage:

https://www.tinkerforge.com/en/doc/Software/Bricklets/AirQuality_Bricklet_MQTT.html#air-quality-bricklet-mqtt-examples

My content tf_mqtt.txt file:

subscribe to tinkerforge/callback/air_quality_bricklet/XYZ/all_values

publish '{"register": true}' to tinkerforge/register/air_quality_bricklet/XYZ/all_values

publish '{"period": 1000, "value_has_to_change": false}' to tinkerforge/request/air_quality_bricklet/X3s/set_all_values_callback_configuration

replace XYZ with your air quality bricklet UID value from the brick viewer.

 

In Pi terminal run :

python3 /usr/local/bin/tinkerforge_mqtt --broker-host 192.168.10.5 --init-file /usr/local/bin/tf_mqtt.txt

Where the broker host is the IP of my Synology MQTT broker on same network.

and i receive this error message:

pi@raspberrypi:~ $ python3 /usr/local/bin/tinkerforge_mqtt --broker-host 192.168.10.5 --init-file /usr/local/bin/tf_mqtt.txt
Could not read init file: Expecting value: line 1 column 1 (char 0)

what did i do wrong/forgot.

Thanks for helping me out.

Patrick

 

 

Link to comment
Share on other sites

The problem is that the MQTT examples are only descriptions what to do with your specific MQTT client, they are not written in the syntax used by init files. Basically you have to translate the content of tf_mqtt.txt to a valid init file as described here:https://www.tinkerforge.com/en/doc/Software/API_Bindings_MQTT.html#loading-initial-messages-from-a-file

In your case this would look like this:

{
    "pre_connect": {
        "tinkerforge/register/air_quality_bricklet/XYZ/all_values": {"register": true}
    },
    "post_connect": {
        "tinkerforge/request/air_quality_bricklet/X3s/set_all_values_callback_configuration": {"period": 1000, "value_has_to_change": false}
    }
}

You still have to replace XYZ with your bricklet's UID here.

 

By the way:

On 7/23/2022 at 10:49 AM, Pat28 said:

Copy the tinkerforge-mqtt fle to the /usr/local/bin folder on pi

This should not be necessary if you've installed the bindings via apt.

Link to comment
Share on other sites

Thanks very much rtrbt,

I used your code, run the command and using mqtt explorer i can see the air quality values:

image.png.157b1ee91604b26baa9bf90196b8a3f3.png

But i have a new problem.

What are the topic names of the air quality bricklet?

I tried to load the iaq_index, iaq_index_accuracy, temperature, humidity and air_pressure as topic into openhab3 and i only see a value= null, not the 60, low, 2598, 5071 and 100674 that i see in the value frame of the mqtt explorer.

Also if you have two or more air quality bricklets on the same broker, can you give every value a specific topic name so you know from witch bricklet for instance the temperature or humidity is coming from ?

Again thanks for help me,

Patrick

 

 

Link to comment
Share on other sites

50 minutes ago, Pat28 said:

What are the topic names of the air quality bricklet?

The topics are not expanded for single values. So you have to use (for example) a JSON PATH transformation to extract the values out of the .../all_values Topic

51 minutes ago, Pat28 said:

Also if you have two or more air quality bricklets on the same broker, can you give every value a specific topic name so you know from witch bricklet for instance the temperature or humidity is coming from ?

The UID of the Bricklet is part of the topic name.

Link to comment
Share on other sites

Hi Rtrbt,

How do you do this Jason transformation or other operation?

Where can i found the doc or can you send me a detailed how to do this.

Or is their a easier way to get the topic data from the bricklets to import in Openhab 3?

I am missing the tinkerforge binding we had for version 2.5 and lost for 3.3.

It was so easy to get the data.

Thanks

Patrick

Link to comment
Share on other sites

Hi Rtrbt,

Thanks for the link.

But there is not much info how to do this json transformation.

Googled a lot, but did not find the solution. Here is what i tried without success?

I saw in the MQTT explorer is in the PUBLISH frame a line called TOPIC:

image.png.ea13041105175bf43e2d7f67f74b99fb.png

with tinkerforge/callback/air_quality_bricklet/X3s/all_values in it.

I suppose i have to fill this in the Openhab MQTT binding channel:

image.png.e10e431e6056a4c88596219bd3608888.png

In the item configuration i saw a JSONPath  in profile profile:

image.thumb.png.970d58ccefbf3cdffd73713d29dfc241.png

I tried to put $.device.status.temperature as in the example on the required example line(and many other expressions i found on google) but no success to get the temperature value( is always NULL !!!)

Could you please send me, in detail, how to get the, for example the temperature, as above example, value  in the OH3 item.

I am stuck getting the data in my OH3.

A step by step example would help me to fix/learn how to do it.

Thanks in advance,

Patrick

 

 

Link to comment
Share on other sites

Hi,

Made some progress.

change tf_mqtt.txt file to:

{
    "tinkerforge/register/air_quality_bricklet/X3s/all_values": {"register": true},
    "tinkerforge/request/air_quality_bricklet/X3s/set_all_values_callback_configuration": {"period": 5000, "value_has_to_change": false},

    "tinkerforge/register/air_quality_bricklet/X3s/iaq_index": {"register": true},
    "tinkerforge/request/air_quality_bricklet/X3s/set_iaq_index_callback_configuration": {"period": 5000, "value_has_to_change": false, "option": "off", "min": 0, "max": 0},

    "tinkerforge/register/air_quality_bricklet/X3s/temperature": {"register": true},
    "tinkerforge/request/air_quality_bricklet/X3s/set_temperature_callback_configuration": {"period": 5000, "value_has_to_change": false, "option": "off", "min": 0, "max": 0},
 
     "tinkerforge/register/air_quality_bricklet/X3s/humidity": {"register": true},
    "tinkerforge/request/air_quality_bricklet/X3s/set_humidity_callback_configuration": {"period": 5000, "value_has_to_change": false, "option": "off", "min": 0, "max": 0},
 
     "tinkerforge/register/air_quality_bricklet/X3s/air_pressure": {"register": true},
    "tinkerforge/request/air_quality_bricklet/X3s/set_air_pressure_callback_configuration": {"period": 5000, "value_has_to_change": false, "option": "off", "min": 0, "max": 0},
}

and see the individual values in MQTT explorer:

image.thumb.png.f885f8ee8745f2c31f74b75473d0c3cc.png

 

In Openhab 3.3.0 made a mqtt generic binding with this code:

UID: mqtt:topic:651a155cc0:b06c533a7f
label: MQTT PI21 Generic Thing
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:651a155cc0
channels:
   - id: PI21_IAQ_Index
    channelTypeUID: mqtt:string
    label: MQTT PI21 IAQ index
    description: ""
    configuration:
      stateTopic: tinkerforge/callback/air_quality_bricklet/X3s/iaq_index
  - id: PI21temperature
    channelTypeUID: mqtt:string
    label: MQTT PI21 temperature
    description: ""
    configuration:
      stateTopic: tinkerforge/callback/air_quality_bricklet/X3s/temperature
      unit: '"°C"'
  - id: PI21Allvlues
    channelTypeUID: mqtt:string
    label: MQTT PI21 All values
    description: null
    configuration:
      stateTopic: tinkerforge/callback/air_quality_bricklet/X3s/all_values
  - id: MQTTPI21humidity
    channelTypeUID: mqtt:string
    label: MQTT PI21 Humidity
    description: ""
    configuration:
      stateTopic: tinkerforge/callback/air_quality_bricklet/X3s/humidity
  - id: MQTTPI21Air_pressure
    channelTypeUID: mqtt:string
    label: MQTT PI21 Air Pressure
    description: ""
    configuration:
      stateTopic: tinkerforge/callback/air_quality_bricklet/X3s/air_pressure

The error i made before is that i used a number type instead of a string (see channelTypeUID: mqtt:string

I checked the "default" profile instead of the JSONPATH profile when linking the new item.

And i can see the temperature value in a string format/

image.thumb.png.8fd427ceba90bb83e03ede32d9e15c5e.png

Add the channels for  IAQ_index, humidity and air-pressure in the same way and it works fine.

I have other bricklets (motion detection, nfc and ambiant light) to try to get the mqtt values.

From the mqtt examples in the TF documentation, i see that not every bricklet has the same register and request as the air quality bricklet i use in my tf_mqtt.txt file to get the data. How do for example you can get the motion detection and nfc bricklet mqtt data in a mqtt.txt file?

Now find out how the convert the string data into a number in openhab. Will search the openhab forum.

Thanks for the help.

Patrick

Link to comment
Share on other sites

Hi,

That is exactly what i did for the ambiant light bricklet:

"tinkerforge/register/ambient_light_v3_bricklet/L2H/illuminance/get_illuminance": {"register": true},
 "tinkerforge/request/ambient_light_v3_bricklet/L2H/set_illuminance_callback_configuration": {"period": 1000, "value_has_to_change": false, "option": "off", "min": 0, "max": 0},

with success.

 

But the motion detection and nfc bricklets are not working. No data is shown in the mqtt explorer.

Here is what it tried without success with the info i found in the docs:

    "tinkerforge/register/nfc_bricklet/Y2U/reader_get_tag_id": {"register": true},
    "tinkerforge/request/nfc_bricklet/Y2U/reader_state_changed": {"period": 2000},

    "tinkerforge/register/motion_detector_v2_bricklet/MPc/get_motion_detected": {"register": true},
    "tinkerforge/request/motion_detector_v2_bricklet/MPc/motion_detected": {"period": 2000}

Could you send me a working code for those two not working bricklets?

Thanks

Patrick

Link to comment
Share on other sites

For the NFC Bricklet it's way easier to use the simple mode, as you don't have to implement a state machine to use it:

"tinkerforge/request/nfc_bricklet/Y2U/set_mode": {"mode: "simple"}

Unfortunately there is no callback for the simple mode, so you have to periodically send

{"index": 0}

to the topic

tinkerforge/request/nfc_bricklet/Y2U/simple_get_tag_id

 

 

3 hours ago, Pat28 said:

  "tinkerforge/register/motion_detector_v2_bricklet/MPc/get_motion_detected": {"register": true},
    "tinkerforge/request/motion_detector_v2_bricklet/MPc/motion_detected": {"period": 2000}

You can't set a period for the motion_detected callback. Also the registration topic is

"tinkerforge/register/motion_detector_v2_bricklet/MPc/motion_detected": {"register": true}

(as is documented)

Link to comment
Share on other sites

Hi,

I tried the NFC and motion bricklet , without success. 

I am not a programmer and try to learn by example with the help of documentation.

I was using the tinkerforge binding in OH 2 without any "coding", just configuration the things and items.

Very easy to use for a not pro.

But tinkerforge did not make the OH3 binding and told to use MQTT.

I try to do/learn this, but it is more difficult than configuring a OH binding.

Did not fully understand your previous post and the documentation is not very explaining.

After some trail and error i tried this:

   "tinkerforge/request/nfc_bricklet/Y2U/set_mode": {"mode": "simple"},
    "tinkerforge/request/nfc_bricklet/Y2U/simple_get_tag_id": {"index": 0, "tag_type": 2, "tag_id": 0, "last_seen": 0},
    
    "tinkerforge/register/motion_detector_v2_bricklet/MPc/motion_detected": {"register": true}

But did only got errors or null values.

Now i have bricks and bricklets i cannot use....

On every bricklet MQTT you have an example at the top off the document, but never in the api section. 

I would be easier for the non-pro's like me to have some results.

Thanks

Patrick

 

 

Link to comment
Share on other sites

Yeah, the MQTT bindings map 1:1 to the Bricklet API, so they are more complicated to use.

On 8/9/2022 at 8:59 AM, Pat28 said:

"tinkerforge/request/nfc_bricklet/Y2U/simple_get_tag_id": {"index": 0, "tag_type": 2, "tag_id": 0, "last_seen": 0},

tag_type, id and last_seen are the response values, you only have to pass the index.

For example if you put a tag on the bricklet and then send (Remember to switch to simple mode before doing this)

Quote

 "tinkerforge/request/nfc_bricklet/Y2U/simple_get_tag_id": {"index": 0}

you should get a response like this:

Quote

 "tinkerforge/response/nfc_bricklet/Y2U/simple_get_tag_id": {"tag_type": 2, "tag_id": "01:23:45:67", "last_seen": 123}

 

On 8/9/2022 at 8:59 AM, Pat28 said:

"tinkerforge/register/motion_detector_v2_bricklet/MPc/motion_detected": {"register": true}

This looks okay. What errors are you getting?

On 8/9/2022 at 8:59 AM, Pat28 said:

On every bricklet MQTT you have an example at the top off the document, but never in the api section. 

Those are examples showing how to use the API?

Link to comment
Share on other sites

Hi,

Sorry i did not reply to your last mail, but my HAT Zero is not detected anymore (is less than 2 years old).

I could not test your MQTT code.

I see the blue leds of the HAT zero and the bricklet, but no data.

Found a post ( HAT Zero and Thermal sensor are not responding ) and tried the tests.

Attached the brikd.log file from /var/log/brickd.log

brickd.log

Then did

sudo systemctl stop brickd

and then start a non-daemonized instance with debug logging:

sudo brickd --debug -all,+bricklet.c

and got this result:

pi@raspberrypi:~ $ sudo brickd --debug -all,+bricklet.c
2022-08-19 09:09:51.123255 <I> <main_linux.c:367> Brick Daemon 2.4.3 started (pid: 1220, daemonized: 0)
2022-08-19 09:09:51.138444 <D> <bricklet.c:237> No HAT product_id file in device tree, not using default HAT Brick config
2022-08-19 09:09:51.139510 <D> <bricklet.c:237> No HAT product_id file in device tree, not using default HAT Zero Brick config
2022-08-19 09:09:51.140378 <D> <bricklet.c:424> Found no supported HAT product_id in device tree, checking bricklet.* section in config file instead
2022-08-19 09:09:51.141187 <D> <bricklet.c:505> Found no bricklet.* section in config file

That seems not very good.

Then i send this command: brickd --check-config and got this results:

image.png.eba1ff7a412b45213b8fb113b962b24d.png

 

image.png.b5e050d5051dd34f98219f3d98cbf2ec.png

image.png.855beda42be3afde9fe266e42716ec31.png

I then formatted the SD card and loaded the latest PI image on the PI ZERO and nothing else than the brickd and brickv software.

No luck, still not HAT detection in brickv.

Tested on a pi4 , same result.

Any chances to get the HAT Zero back to life?

Thanks

Patrick

 

Link to comment
Share on other sites

Hi Borg and Rtrbt,

Enabling SPI and  editing of the  /etc/brickd.conf file did the job.

But the mqtt code for the motion detection only seems to work once with this result:

image.png.f2cc299b9232c501e59ce2a604ca26c0.png

For the NFC: nothing in the mqtt explorer.

I think i will stop trying to use tinkerforge mqtt to get the data to openhab.

To complicated.

Tinkerforge was a good hardware, easy to use with openhab 2 binding for non pro's of coding.

A pity it is not working with openhab 3 anymore.

You are losing potential users.....

Thanks for the help.

Patrick

 

Link to comment
Share on other sites

  • 1 month later...
  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...