How can I combine data from several DS18B20 temp sensors?

537 views
Skip to first unread message

Dan

unread,
Feb 22, 2015, 5:34:17 PM2/22/15
to node...@googlegroups.com
Hi there,
Noob here.. :-)

I think this should be a simple flow, but I have troubles. Perhaps you can help? I guess it's a function issue, or something I do not understand with the DS18B20 module, perhaps related to timing...

Basically, I get input from 4 temperature sensors on a RPi (4 * ds18b20, using only 2 in this example here). 
I am using the module https://www.npmjs.com/package/node-red-contrib-ds18b20 for the data collection and build an HTTP request for data.sparkfun.com.
This works fine, as long as I just log to debug, or if I just send one temp to sparkfun.

My next step was to combine the data into one payload, based on the example frow from mharizanov (http://flows.nodered.org/flow/8ba7f90f3ea8d92b1e01)

The problem I encountered is that if I combine data coming from the DS10B20 input nodes, it does not work. If I do the same with a simple inject node, it works fine. Where could be the problem?

Here is a simple example where I try to combine two inputs. If I use the inject node, I get a msg object at the end of the flow as expected. If I wait for the 1 minute intervall, I do not get a msg object at the end of the flow. I get the payload from the DS10B20 in the msg.payload debug node, though..

[{"id":"ca465d63.35b9a","type":"ds18b20","name":"draussen","sensorid":"28-000005e5e8b0","timer":"1","x":332,"y":413,"z":"dafee8d6.250118","wires":[["a32d6178.5cd2a","12b8cecc.ed4731"]]},{"id":"9170c191.6e8f4","type":"ds18b20","name":"unterpc","sensorid":"28-000005e603ae","timer":"1","x":338,"y":507,"z":"dafee8d6.250118","wires":[["a32d6178.5cd2a","12b8cecc.ed4731"]]},{"id":"a32d6178.5cd2a","type":"function","name":"Wait for all tasks to finish","func":"context.data = context.data || new Object();\n\nswitch (msg.topic) {\n    case \"draussen\":\n        context.data.draussen = msg.payload;\n        msg = null;\n        break;\n    case \"unterpc\":\n        context.data.unterpc = msg.payload;\n        msg = null;\n        break;\n        \n    default:\n        msg = null;\n    \tbreak;\n\n}\n\nif(context.data.draussen != null && context.data.unterpc != null ) {\n\tmsg2 = new Object();\n    msg2 = context.data;\n    context.data=null;\n\treturn msg2;\n} else return msg;","outputs":1,"x":647,"y":621,"z":"dafee8d6.250118","wires":[["8793d1dd.786c3"]]},{"id":"bd264db9.42d9b","type":"function","name":"draussen","func":"msg.topic=\"draussen\";\nmsg.payload=\"draussen-payload\"\nreturn msg;","outputs":1,"x":335,"y":588,"z":"dafee8d6.250118","wires":[["a32d6178.5cd2a"]]},{"id":"be1374d1.41ec88","type":"function","name":"unterpc","func":"msg.topic=\"unterpc\";\nmsg.payload=\"unterpc-payload\"\nreturn msg;","outputs":1,"x":338,"y":683,"z":"dafee8d6.250118","wires":[["a32d6178.5cd2a"]]},{"id":"1233a591.edcc5a","type":"inject","name":"Start","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":165,"y":635,"z":"dafee8d6.250118","wires":[["bd264db9.42d9b","be1374d1.41ec88"]]},{"id":"12b8cecc.ed4731","type":"debug","name":"","active":true,"console":"false","complete":"false","x":607,"y":536,"z":"dafee8d6.250118","wires":[]},{"id":"8793d1dd.786c3","type":"debug","name":"","active":true,"console":"false","complete":"true","x":895,"y":620,"z":"dafee8d6.250118","wires":[]}]


A side question would be: how can I generate debug output within a function node? I'm afraid that I so far have not understood how to use Node.warn, Node.error,  Console.log... I have also not found an example used in a function node in node-red...
Remember, I am a beginner here..


Thanks for your help

Dan




Dan

unread,
Feb 22, 2015, 6:23:33 PM2/22/15
to node...@googlegroups.com
I've made a similar setup with MQTT and this works as expected.

[{"id":"a7c99df.f58366","type":"mqtt-broker","broker":"localhost","port":"1883","clientid":""},{"id":"f1cca903.0e3358","type":"ds18b20","name":"draussen","sensorid":"28-000005e5e8b0","timer":"2","x":206,"y":91,"z":"c0fd34.ff3f02d","wires":[["addc9110.52237","ce0962b2.31f6a"]]},{"id":"addc9110.52237","type":"debug","name":"draussen","active":true,"console":"false","complete":"payload","x":352,"y":142,"z":"c0fd34.ff3f02d","wires":[]},{"id":"1f0ddee7.e0f221","type":"ds18b20","name":"unterpc","sensorid":"28-000005e603ae","timer":"2","x":206,"y":215,"z":"c0fd34.ff3f02d","wires":[["156bae93.ea9451","e68ba0ed.19746"]]},{"id":"156bae93.ea9451","type":"debug","name":"unterpc","active":true,"console":"false","complete":"payload","x":353,"y":266,"z":"c0fd34.ff3f02d","wires":[]},{"id":"ce0962b2.31f6a","type":"mqtt out","name":"","topic":"temp/draussen","qos":"","retain":"","broker":"a7c99df.f58366","x":506,"y":92,"z":"c0fd34.ff3f02d","wires":[]},{"id":"97f0f16.f680f1","type":"mqtt in","name":"","topic":"temp/#","broker":"a7c99df.f58366","x":680,"y":89,"z":"c0fd34.ff3f02d","wires":[["741c7b3.f8be384","13b8c1db.ec473e"]]},{"id":"741c7b3.f8be384","type":"debug","name":"","active":false,"console":"false","complete":"true","x":850,"y":89,"z":"c0fd34.ff3f02d","wires":[]},{"id":"e68ba0ed.19746","type":"mqtt out","name":"","topic":"temp/unterpc","qos":"","retain":"","broker":"a7c99df.f58366","x":494,"y":215,"z":"c0fd34.ff3f02d","wires":[]},{"id":"13b8c1db.ec473e","type":"function","name":"Wait for all tasks to finish","func":"context.data = context.data || new Object();\n\nswitch (msg.topic) {\n    case \"temp/draussen\":\n        context.data.draussen = msg.payload;\n        msg = null;\n        break;\n    case \"temp/unterpc\":\n        context.data.unterpc = msg.payload;\n        msg = null;\n        break;\n        \n    default:\n        msg = null;\n    \tbreak;\n\n}\n\nif(context.data.draussen != null && context.data.unterpc != null ) {\n\tmsg2 = new Object();\n    msg2 = context.data;\n    context.data=null;\n\treturn msg2;\n} else return msg;","outputs":1,"x":890,"y":152,"z":"c0fd34.ff3f02d","wires":[["87d0d9a.f782f28","d923a185.26dc6","e15f1739.1ea0e8"]]},{"id":"87d0d9a.f782f28","type":"debug","name":"","active":true,"console":"false","complete":"true","x":1140,"y":152,"z":"c0fd34.ff3f02d","wires":[]},{"id":"d923a185.26dc6","type":"debug","name":"","active":true,"console":"false","complete":"draussen","x":1163,"y":188,"z":"c0fd34.ff3f02d","wires":[]},{"id":"e15f1739.1ea0e8","type":"debug","name":"","active":true,"console":"false","complete":"unterpc","x":1159,"y":227,"z":"c0fd34.ff3f02d","wires":[]}]

So I guess it's perhaps a problem related to the DS18B20 nodes and the context.data in the function node?

Dan

Nicholas O'Leary

unread,
Feb 22, 2015, 6:31:29 PM2/22/15
to node...@googlegroups.com
Hi Dan,

the 'Wait for all tasks to finish' function is looking at the msg.topic property of each message that arrives. If the topic is 'draussen', it stores the payload under context.data.draussen. If the topic is 'unterpc', it stores it under context.data.unterpc. once it has both values stored, it passes on the combined message.

So the requirement is for each msg arriving at that function to have its msg.topic property set to one of these values. The DS18b20 nodes do not set msg.topic, so when their messages arrive at the function block, they are ignored.

The fix is to add a 'Change' node between each ds18b20 node and the function node to set msg.topic to the appropriate value, eg: [{"id":"2089c754.df7638","type":"change","action":"replace","property":"topic","from":"","to":"draussen","reg":false,"name":"","x":642,"y":291,"z":"c7e190df.381e7","wires":[[]]}]

Nick

--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dan

unread,
Feb 22, 2015, 6:51:24 PM2/22/15
to node...@googlegroups.com
Hello Nick,

Great! That works. Many thanks.

I guess I misinterpreted the node gui (see screenshot) and did not check the complete msg coming from the node...


Could you eventually also point me to a description on how to debug within a function node?
I can't find a good description.

Thanks a lot

Regards
Dan

Nicholas O'Leary

unread,
Feb 22, 2015, 7:03:24 PM2/22/15
to node...@googlegroups.com

Looking at the history of the node, it looks like an earlier version did set msg.topic based on the name property. But that was removed in the runtime code, but not in the ui. Will raise an issue on its repository, unless the author sees this thread.

The only debug tools you have are console.log to print things to the log output where you are running node-red, or add a second output to the function from which you can wire up to a debug node and send whatever you want.

Improving the developer experience of the function node is something I'm interested in getting feedback on. We currently use the Eclipse Orion text editor, but we've been hitting some difficulties getting some of the basic features we want to work.. Auto complete, syntax validation etc. We're close to moving over to the ACE editor which seems to work out of the box much better and gives a better experience without relying on the things around it.

Although we've talked about it before on this list, we haven't yet exposed the node.log/warn/error functions, so you can ignore them for now.


Trevor Daniel

unread,
Feb 27, 2018, 3:26:59 AM2/27/18
to Node-RED
Many thanks!

This helped me as well!! 
Reply all
Reply to author
Forward
0 new messages