Function Node for Beginner

Hi Guys
I'm a total beginner to Node-Red with no programming experience.....I am looking to connect a DHT11 Temp Humidity Sensor with an Uno (Done) and represent on Two Graphs Node Red /ui. I am stuck with the Java code required in the function node to take the input "Temperature: *.*C or Humidity **% and only output the actual value to the respective graphic node.
Would any be able top point me in the right direction?
Thank You
Philip

Feed the message containing the values into a debug node and show it what it says.
By the way it is javascript not Java. Completely different but named so as to cause maximum confusion.

1 Like

You need to decide first how the arduino will "talk" to NodeRed. MQTT? http endpoint? Other methods?
Then on nodered, you need to put the "listen" part in place.
You may not need to write any function. Just using the available nodes could do the trick. But, 1st things 1st, what is going out and how, from the Uno?

This is what the debug node is showing in the "debug" pallett. I am inputting Uno serial (9600) into Pi using USB ......just passing msg.payload into debug node....what I want to do is split the temp & humidity into separate strands and attach a graph node for each?

That sounds interesting! Is there an example of this I could take a look at?

First you would need to connect the uno to Ethernet (or wifi). You can search for "arduino ethernet shield".

Then for exemple, using MQTT, this guide could help: http://www.steves-internet-guide.com/using-arduino-pubsub-mqtt-client/

You would also need to run an MQTT broker (eg mosquitto) for NodeRed and the Uno to exchange messages.

Clearly, there are much more "components" (hard and soft) to use than just the Uno board but, it will give you so much more flexibility.

I will need to source the parts for this but Thanks. Can you advise the syntax required in a function node to take input string from UNO and SELECT the "Temperature: 22.00C" message only .....then remove just select the value and output that to debug node? If I could get that done...would be a start? Thanks

Hi Philip,

Although the best approach would be greengolfers recommendations, for now .. as is ..
you can experiment with the javascript's split() command and maybe slice()

msg.payload = msg.payload.split(': ')
return msg;

I’d suggest you also look at getting a WeMos D1 Mini and flashing it with ESPEasy. The Wemos has wifi and espeasy makes it easy to select the device and send the readings Via MQTT.

Oh and get some BMP/E280 to use instead of the dht11’s which are not very accurate

Here is an exemple without using the function node:

With the code:

[{"id":"faf583b.7c0878","type":"inject","z":"33b1225f.3b50ee","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Temperature: 22.00°C","payloadType":"str","x":270,"y":2280,"wires":[["e7c6bd7f.044be"]]},{"id":"e7c6bd7f.044be","type":"change","z":"33b1225f.3b50ee","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.$substringBefore(\"°C\").$substringAfter(\"Temperature: \")\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":2280,"wires":[["f029984a.1f3e08"]]},{"id":"f029984a.1f3e08","type":"debug","z":"33b1225f.3b50ee","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":2280,"wires":[]}]

I understand that....and thank you!.....but I'm taking realtime data from a dht11 sensor as my flow input.....all I want to do for now is separate the Temperature value and the Humidity Value in payload so I can graph each on a Ui?

What about:

Code:

[{"id":"faf583b.7c0878","type":"inject","z":"33b1225f.3b50ee","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Temperature: 22.00°C","payloadType":"str","x":180,"y":2220,"wires":[["5dee92ea.fdae9c"]]},{"id":"e7c6bd7f.044be","type":"change","z":"33b1225f.3b50ee","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.$substringBefore(\"°C\").$substringAfter(\"Temperature: \")\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":2220,"wires":[["f029984a.1f3e08"]]},{"id":"f029984a.1f3e08","type":"debug","z":"33b1225f.3b50ee","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":2260,"wires":[]},{"id":"1c812656.a7cb7a","type":"inject","z":"33b1225f.3b50ee","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Humidity: 75%","payloadType":"str","x":180,"y":2300,"wires":[["5dee92ea.fdae9c"]]},{"id":"5dee92ea.fdae9c","type":"switch","z":"33b1225f.3b50ee","name":"","property":"payload","propertyType":"msg","rules":[{"t":"regex","v":"Temperature","vt":"str","case":false},{"t":"regex","v":"Humidity","vt":"str","case":false}],"checkall":"true","repair":false,"outputs":2,"x":370,"y":2260,"wires":[["e7c6bd7f.044be"],["22418e6e.e66db2"]]},{"id":"22418e6e.e66db2","type":"change","z":"33b1225f.3b50ee","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.$substringBefore(\"%\").$substringAfter(\"Humidity: \")\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":2300,"wires":[["f029984a.1f3e08"]]}]

For the UI part, I don't know. Not using it. Sorry.

let me try that. Thanks

Using msg.payload.split.....my payload has been separated......how now do I select only Temp Value and connect that into graph node?

You may want to learn as well how to share flows and debug output... Have a look at How to share code or flow json

If you have control of the output, I would strongly recommend you output JSON. That way, the properties and values are very very easy to access (no string splitting etc)..

Side note...
Philip. Can I ask. Why do you keep taking photographs of your flows/debug output & not simple screenshots? Is the node-red installation not accessible on your PC browser where you make these posts? A sniping tool or greenshot or shared or similar is far better in both file size and clarity.

Thanks Steve.......side note first......I'm running all from a Pi......and communicating here through Laptop.....not sure how to snip on Linux?

If you have any examples of using JSON for this task....I would very much appreciate?
regards
Philip

There is a good introductory video on working with JSON
Working With JSON Data in Node Red

If you can modify your Arduino code so it outputs JSON format. For example :

"{"Temperature": 22.40, "Humidity": 60.50}"

a JSON or function node can easily parse that JSON string which makes life much easier accessing individual values.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.