Jump to content

Recommended Posts

Geschrieben

Hi,

 

in the process od switching from MQTT proxy 1.0 to 2.0.

 

Question: how to subscribe to topic changes?

 

Started as follows:

Raspberry Pi 4 with Master Brick and RGB LED 2.0 Bricklet

Installed mosquitto

Installed the MQTT bindings in /usr/local/bin.

Started the script: tinkerforge_mqtt --debug

Log:

2019-09-06 10:57:03,889 <DEBUG> MQTT bindings: Configuring connection to MQTT broker at localhost:1883
2019-09-06 10:57:03,890 <DEBUG> MQTT bindings: Connected to MQTT broker at localhost:1883
2019-09-06 10:57:03,892 <DEBUG> paho.mqtt.client: Sending CONNECT (u0, p0, wr0, wq0, wf1, c1, k60) client_id=
2019-09-06 10:57:03,893 <DEBUG> paho.mqtt.client: Received CONNACK (0, 0)
2019-09-06 10:57:03,893 <DEBUG> MQTT bindings: Connected to mqtt broker.
2019-09-06 10:57:03,894 <DEBUG> paho.mqtt.client: Sending SUBSCRIBE (d0, m1) [('tinkerforge/request/#', 0)]
2019-09-06 10:57:03,894 <DEBUG> paho.mqtt.client: Sending SUBSCRIBE (d0, m2) [('tinkerforge/register/#', 0)]
2019-09-06 10:57:03,895 <DEBUG> paho.mqtt.client: Sending PUBLISH (d0, q0, r0, m3), 'tinkerforge/callback/bindings/restart', ... (4 bytes)
2019-09-06 10:57:03,895 <DEBUG> paho.mqtt.client: Sending SUBSCRIBE (d0, m4) [('tinkerforge/callback/bindings/restart', 0)]
2019-09-06 10:57:03,896 <DEBUG> MQTT bindings: Connecting to brickd at localhost:4223
2019-09-06 10:57:03,897 <DEBUG> paho.mqtt.client: Received SUBACK
2019-09-06 10:57:03,899 <DEBUG> paho.mqtt.client: Received SUBACK

 

Published from terminal to set the color of a RGB LED V2 bricklet:

mosquitto_pub -t tinkerforge/request/rgb_led_v2_bricklet/Jng/set_rgb_value -m '{"r": 0, "g": 0, "b": 200}'

 

Log:

2019-09-06 10:57:51,513 <DEBUG> paho.mqtt.client: Received PUBLISH (d0, q0, r0, m0), 'tinkerforge/request/rgb_led_v2_bricklet/Jng/set_rgb_value', ...  (29 bytes)
2019-09-06 10:57:51,514 <DEBUG> MQTT bindings:

2019-09-06 10:57:51,515 <DEBUG> MQTT bindings: Calling function set_rgb_value for device Jng of type rgb_led_v2_bricklet.
2019-09-06 10:57:51,516 <ERROR> MQTT bindings: ubyte format requires 0 <= number <= 255 (call of set_rgb_value of rgb_led_v2_bricklet Jng)
2019-09-06 10:57:51,517 <DEBUG> MQTT bindings: Calling function set_rgb_value for device Jng of type rgb_led_v2_bricklet succedded.
2019-09-06 10:57:51,517 <DEBUG> MQTT bindings: Publishing response to tinkerforge/response/rgb_led_v2_bricklet/Jng/set_rgb_value
2019-09-06 10:57:51,518 <DEBUG> paho.mqtt.client: Sending PUBLISH (d0, q0, r0, m5), 'tinkerforge/response/rgb_led_v2_bricklet/Jng/set_rgb_value', ... (2 bytes)
2019-09-06 10:57:51,518 <DEBUG> MQTT bindings:

2019-09-06 10:58:51,597 <DEBUG> paho.mqtt.client: Sending PINGREQ
2019-09-06 10:58:51,598 <DEBUG> paho.mqtt.client: Received PINGRESP

 

The color of the RGB LED 2.0 Bricklet changed.

Note: on the brick viewer had to refresh tab RGB LED 2.0 to get the new values.

 

Subscribing to get_rgb_value from terminal:

mosquitto_sub -v -t tinkerforge/request/rgb_led_v2_bricklet/Jng/get_rgb_value

 

Changed the color via Brick Viewer to RED (255,0,0).

Nothing happens on the terminal.

 

Changed the command to:

mosquitto_sub -t tinkerforge/request/rgb_led_v2_bricklet/Jng/get_rgb_value

Nothing happens on the terminal.

 

Question:

How to subscribe to value changes?

Would like to use this in Node-RED.

 

 

Geschrieben

You have to subscribe to the

[...]/response/[...]

topic, instead of the

[...]/request/[...]

topic. So in your case

mosquitto_sub  -t tinkerforge/response/rgb_led_v2_bricklet/Jng/get_rgb_value

If you then run

mosquitto_pub -t tinkerforge/request/rgb_led_v2_bricklet/Jng/get_rgb_value -m ''

in another shell, you will see the color.

 

For debugging purposes, you can start the bindings with

tinkerforge_mqtt --debug

The response paths are then printed to standard out. Also you can subscribe to

tinkerforge/#

to see all traffic.

Geschrieben

Thanks for help.

Using mosquito for this test is ok:

Terminal 1 subscribing to all topics:

mosquitto_sub  -t tinkerforge/#

Terminal 2 setting the color:

mosquitto_pub -t tinkerforge/request/rgb_led_v2_bricklet/Jng/set_rgb_value -m '{"r":0, "g":0, "b":123}'

Terminal 1 shows:

{"r":0, "g":0, "b":123}

 

Next step is to obtain the same in Node-RED.

Defined a simple flow.

mqtt in node with topic tinkerforge/# and broker localhost port 1883.

The mqtt in node outputs to a debug node.

 

When changing the color via terminal, then the debug node shows:

06/09/2019, 14:15:01node: DEBUG TF ALL
tinkerforge/request/rgb_led_v2_bricklet/Jng/set_rgb_value : msg.payload : string[23]
"{"r":0, "g":0, "b":123}"

 

Now, to test changing the color via mqtt out node (and debug node) using a function node:

//Example to set a green color = RGB (0, 255, 0)
  var r = 0;
  var g = 255;
  var b = 0;
  
  //Mosquitto Command:
  //mosquitto_pub -t tinkerforge/request/rgb_led_v2_bricklet/Jng/set_rgb_value -m '{"r":255, "g":0, "b":0}'
  
  //Translated to JavaScript Message with Topic and Payload
  var msgmqtt = {topic: "tinkerforge/request/rgb_led_v2_bricklet/Jng/set_rgb_value", payload:{"r":r,"g":g,"b":b} };

  return msgmqtt;  

The debug node shows:

06/09/2019, 14:16:30node: 65847a3b.80f134
tinkerforge/request/rgb_led_v2_bricklet/Jng/set_rgb_value : msg.payload : Object
{ r: 0, g: 255, b: 0 }

 

BUT the color is not changed.

 

Any hint what is missing in this setup?

Geschrieben

Can you post the output of

mosquitto_sub -t tinkerforge/#

and the debug output from the bindings for both cases? (Changing the color via the terminal and with a MQTT out node)

 

Also, the debug node outputs differ: If you use the terminal, the payload is a string, with the node, it is an object. Maybe you have to convert the JSON object to a string before publishing it?

Geschrieben

Yes, noted the difference in string and json from mqtt out.

To note is that this node-red example setup has been used without issues using mqtt proxy v1.0.

 

Tried to a JSON node, but again no change.

Tried a string, i.e. "R",... but also no change.

 

Output of mosquitto_sub -t tinkerforge/#

{"r":0, "g":0, "b":123}

Debug

pi@4dev:~ $ tinkerforge_mqtt --debug
2019-09-06 14:51:22,144 <DEBUG> MQTT bindings: Configuring connection to MQTT broker at localhost:1883
2019-09-06 14:51:22,145 <DEBUG> MQTT bindings: Connected to MQTT broker at localhost:1883
2019-09-06 14:51:22,147 <DEBUG> paho.mqtt.client: Sending CONNECT (u0, p0, wr0, wq0, wf1, c1, k60) client_id=
2019-09-06 14:51:22,148 <DEBUG> paho.mqtt.client: Received CONNACK (0, 0)
2019-09-06 14:51:22,148 <DEBUG> MQTT bindings: Connected to mqtt broker.
2019-09-06 14:51:22,148 <DEBUG> paho.mqtt.client: Sending SUBSCRIBE (d0, m1) [('tinkerforge/request/#', 0)]
2019-09-06 14:51:22,149 <DEBUG> paho.mqtt.client: Sending SUBSCRIBE (d0, m2) [('tinkerforge/register/#', 0)]
2019-09-06 14:51:22,149 <DEBUG> paho.mqtt.client: Sending PUBLISH (d0, q0, r0, m3), 'tinkerforge/callback/bindings/restart', ... (4 bytes)
2019-09-06 14:51:22,149 <DEBUG> paho.mqtt.client: Sending SUBSCRIBE (d0, m4) [('tinkerforge/callback/bindings/restart', 0)]
2019-09-06 14:51:22,150 <DEBUG> MQTT bindings: Connecting to brickd at localhost:4223
2019-09-06 14:51:22,152 <DEBUG> paho.mqtt.client: Received SUBACK
2019-09-06 14:51:22,153 <DEBUG> MQTT bindings: Connected to Brick Daemon: Connection established after request from user.
2019-09-06 14:51:22,153 <DEBUG> MQTT bindings: Connected to brickd at localhost:4223
2019-09-06 14:51:22,153 <DEBUG> paho.mqtt.client: Received SUBACK
2019-09-06 14:51:22,198 <DEBUG> paho.mqtt.client: Received SUBACK
2019-09-06 14:51:28,094 <DEBUG> paho.mqtt.client: Received PUBLISH (d0, q0, r0, m0), 'tinkerforge/request/rgb_led_v2_bricklet/Jng/set_rgb_value', ...  (23 bytes)
2019-09-06 14:51:28,094 <DEBUG> MQTT bindings:
2019-09-06 14:51:28,095 <DEBUG> MQTT bindings: Calling function set_rgb_value for device Jng of type rgb_led_v2_bricklet.
2019-09-06 14:51:28,096 <DEBUG> MQTT bindings: Calling function set_rgb_value for device Jng of type rgb_led_v2_bricklet succedded.

 

Node-RED MQTT out node

In the terminal no entry subscribing tinkerforge/#

In debug

pi@4dev:~ $ tinkerforge_mqtt --debug
2019-09-06 14:53:50,026 <DEBUG> MQTT bindings: Configuring connection to MQTT broker at localhost:1883
2019-09-06 14:53:50,026 <DEBUG> MQTT bindings: Connected to MQTT broker at localhost:1883
2019-09-06 14:53:50,028 <DEBUG> paho.mqtt.client: Sending CONNECT (u0, p0, wr0, wq0, wf1, c1, k60) client_id=
2019-09-06 14:53:50,029 <DEBUG> paho.mqtt.client: Received CONNACK (0, 0)
2019-09-06 14:53:50,029 <DEBUG> MQTT bindings: Connected to mqtt broker.
2019-09-06 14:53:50,030 <DEBUG> paho.mqtt.client: Sending SUBSCRIBE (d0, m1) [('tinkerforge/request/#', 0)]
2019-09-06 14:53:50,030 <DEBUG> paho.mqtt.client: Sending SUBSCRIBE (d0, m2) [('tinkerforge/register/#', 0)]
2019-09-06 14:53:50,030 <DEBUG> paho.mqtt.client: Sending PUBLISH (d0, q0, r0, m3), 'tinkerforge/callback/bindings/restart', ... (4 bytes)
2019-09-06 14:53:50,031 <DEBUG> paho.mqtt.client: Sending SUBSCRIBE (d0, m4) [('tinkerforge/callback/bindings/restart', 0)]
2019-09-06 14:53:50,031 <DEBUG> MQTT bindings: Connecting to brickd at localhost:4223
2019-09-06 14:53:50,032 <DEBUG> paho.mqtt.client: Received SUBACK
2019-09-06 14:53:50,033 <DEBUG> paho.mqtt.client: Received SUBACK
2019-09-06 14:53:50,034 <DEBUG> MQTT bindings: Connected to Brick Daemon: Connection established after request from user.
2019-09-06 14:53:50,034 <DEBUG> MQTT bindings: Connected to brickd at localhost:4223
2019-09-06 14:53:50,078 <DEBUG> paho.mqtt.client: Received SUBACK

 

Geschrieben

It seems like the MQTT out node sends either to the wrong topic or to the wrong broker. Can you try again, but with

mosquitto_sub -v -t '#'

instead of

mosquitto_sub -v -t 'tinkerforge/#'

This will print everything published to the broker.

Geschrieben

That was a great hint.

The log showed no topic, only payload, means error in node-red creating topic.

The log: first line using terminal, second via node-red.

tinkerforge/request/rgb_led_v2_bricklet/Jng/set_rgb_value {"r":255,"g":0,"b":0}
  {"r":255,"g":0,"b":0}

 

Changed the node-red function node creating topic with payload

//Example to set a green color = RGB (0, 255, 0)
var r = 0;
var g = 255;
var b = 0;
//Mosquitto Command:
//mosquitto_pub -t tinkerforge/request/rgb_led_v2_bricklet/Jng/set_rgb_value -m '{"r":0, "g":255, "b":0}'
//Translated to JavaScript Message with Topic and Payload
msg.topic = "tinkerforge/request/rgb_led_v2_bricklet/Jng/set_rgb_value";
msg.payload = {"r":r,"g":g,"b":b}; 
return msg;  

 

resulting in log:

tinkerforge/request/rgb_led_v2_bricklet/Jng/set_rgb_value {"r":0,"g":255,"b":0}

 

That was good learning = again thanks for help.

Join the conversation

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

Gast
Reply to this topic...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   Du kannst Bilder nicht direkt einfügen. Lade Bilder hoch oder lade sie von einer URL.

×
×
  • Neu erstellen...