# Extract numbers from serial input

**URL:** <https://discourse.nodered.org/t/extract-numbers-from-serial-input/92343>\
**Category:** General\
**Created:** [12 October 2024 13:18 UTC](https://discourse.nodered.org/t/extract-numbers-from-serial-input/92343 "2024-10-12T13:18:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![BubbleBobble](https://avatars.discourse-cdn.com/v4/letter/b/a183cd/32.png) [@BubbleBobble](https://discourse.nodered.org/u/BubbleBobble)\
**Post date:** [12 October 2024 13:18 UTC](https://discourse.nodered.org/t/extract-numbers-from-serial-input/92343/1 "2024-10-12T13:18:47Z")

</div>

The task is probably simple to do, but after trying all examples i can find on the topic, i'm still struggling. So i'm not posting any of my non working flow 🙂

I read a serial input, and want to extract only the numbers so i can use the values further in my flow for example in a chart or gauge. Preferably by using existing nodes, split, change etc.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/d/2d7925894fe0d57f610e7fa1fdef25f92f5e14ba.png)

```auto
[{"id":"0221fe0db6b96fa3","type":"serial in","z":"4656a7ccbca6a363","name":"","serial":"28e2a41a4b0a9950","x":990,"y":280,"wires":[["b9ab1baf53647282"]]},{"id":"28e2a41a4b0a9950","type":"serial-port","name":"","serialport":"COM8","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"10000"}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 October 2024 13:29 UTC](https://discourse.nodered.org/t/extract-numbers-from-serial-input/92343/2 "2024-10-12T13:29:08Z")

</div>

You have shared a single Serial In node

No users actually have access to your device so that is pretty pointless. In cases like this, it is helpful to provide dummy data via inject nodes instead.

Questions:

1. What generates the serial data and do you have control over that (i.e. could you change it to a sensible/parse-able format)?
2. Is there a protocol sheet you can share?

---

<div class="post-metadata">

**Author:** ![BubbleBobble](https://avatars.discourse-cdn.com/v4/letter/b/a183cd/32.png) [@BubbleBobble](https://discourse.nodered.org/u/BubbleBobble)\
**Post date:** [12 October 2024 15:11 UTC](https://discourse.nodered.org/t/extract-numbers-from-serial-input/92343/3 "2024-10-12T15:11:49Z")

</div>

I was hoping that the debug info could help, but I see that this was to little info.

I read a senserion SHT sensor with a Arduino UNO. This is the serial print i use in the Arduino:

Serial.print("SHT85nr5:\n");  
Serial.print(" RH: ");  
Serial.print(sht.getHumidity(), 2);  
Serial.print("\n");  
Serial.print(" T: ");  
Serial.print(sht.getTemperature(), 2);  
Serial.print("\n");

I guess to simulate one value with inject would be something like this?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/3/73f1078cbe91109008c2e94d56f49eb8f7e0a161.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8f626c99aaa7ec84a6dab1634935091d614cc49e.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [12 October 2024 15:28 UTC](https://discourse.nodered.org/t/extract-numbers-from-serial-input/92343/4 "2024-10-12T15:28:13Z")

</div>

You would be better of having the arduino send a properly formatted JSON  
This may help [How to do serial communication between two boards? | ArduinoJson 6](https://arduinojson.org/v6/how-to/do-serial-communication-between-two-boards/)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 October 2024 15:30 UTC](https://discourse.nodered.org/t/extract-numbers-from-serial-input/92343/5 "2024-10-12T15:30:57Z")

</div>

> [@BubbleBobble](#):
>
> ```auto
> Serial.print("SHT85nr5:\n");
> Serial.print(" RH: ");
> Serial.print(sht.getHumidity(), 2);
> Serial.print("\n");
> Serial.print(" T: ");
> Serial.print(sht.getTemperature(), 2);
> Serial.print("\n");
> 
> ```

It would be much better to send these in one go in a JSON formatted string. Build a string that looks something like

```auto
{"rh": 65, "t": 30}

```

Then when node red receives that, feed it through a JSON node, which will convert it to a javascript object, which you can do what you want with.

---

<div class="post-metadata">

**Author:** ![BubbleBobble](https://avatars.discourse-cdn.com/v4/letter/b/a183cd/32.png) [@BubbleBobble](https://discourse.nodered.org/u/BubbleBobble)\
**Post date:** [12 October 2024 17:04 UTC](https://discourse.nodered.org/t/extract-numbers-from-serial-input/92343/6 "2024-10-12T17:04:51Z")

</div>

Thanks,  
I think my solution is not exactly as smooth as the intention with suggestions here, but this is what I could manage out from the tips.

Arduino UNO:  
Serial.print(sht.getHumidity(), 2);  
Serial.print(",");  
Serial.print(sht.getTemperature(), 2);  
Serial.print("\n");

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/c/6c18d49107a3c42940ad2a3b65dc065a46a30472.png)

```auto
[{"id":"4656a7ccbca6a363","type":"tab","label":"Flow 11","disabled":false,"info":"","env":[]},{"id":"0268d882db65fd61","type":"serial in","z":"4656a7ccbca6a363","name":"","serial":"28e2a41a4b0a9950","x":450,"y":120,"wires":[["664986f8eb4d35a7"]]},{"id":"664986f8eb4d35a7","type":"csv","z":"4656a7ccbca6a363","name":"","spec":"rfc","sep":",","hdrin":"","hdrout":"all","multi":"one","ret":"\\r\\n","temp":"RH,T","skip":"0","strings":false,"include_empty_strings":false,"include_null_values":false,"x":590,"y":160,"wires":[["3da911677b658c1c","a762704eb1091d95"]]},{"id":"8710a581bf3258ad","type":"debug","z":"4656a7ccbca6a363","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1300,"y":120,"wires":[]},{"id":"3da911677b658c1c","type":"split","z":"4656a7ccbca6a363","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","property":"payload","x":730,"y":160,"wires":[["c6761f0d01bd6b32"]]},{"id":"af4e227fad55949e","type":"switch","z":"4656a7ccbca6a363","name":"","property":"parts.key","propertyType":"msg","rules":[{"t":"eq","v":"RH","vt":"str"},{"t":"eq","v":"T","vt":"str"}],"checkall":"true","repair":true,"outputs":2,"x":970,"y":160,"wires":[["bbf51e9a1c1448ad"],["3e02199ec808e680"]]},{"id":"b387856fbee04b83","type":"debug","z":"4656a7ccbca6a363","name":"debug 11","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1300,"y":200,"wires":[]},{"id":"c6761f0d01bd6b32","type":"json","z":"4656a7ccbca6a363","name":"","property":"payload","action":"obj","pretty":false,"x":850,"y":160,"wires":[["af4e227fad55949e"]]},{"id":"8e73c6b50161d62a","type":"ui_gauge","z":"4656a7ccbca6a363","name":"","group":"da8d7d36988223dc","order":5,"width":0,"height":0,"gtype":"gage","title":"Temperature","label":"C","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","diff":false,"className":"","x":1310,"y":80,"wires":[]},{"id":"c3f3b26a425e39f9","type":"ui_gauge","z":"4656a7ccbca6a363","name":"","group":"da8d7d36988223dc","order":6,"width":0,"height":0,"gtype":"gage","title":"Humidity","label":"%","format":"{{value}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","diff":false,"className":"","x":1300,"y":240,"wires":[]},{"id":"bbf51e9a1c1448ad","type":"change","z":"4656a7ccbca6a363","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"Temperature","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1130,"y":120,"wires":[["8710a581bf3258ad","8e73c6b50161d62a","4742f328a4b8e494"]]},{"id":"3e02199ec808e680","type":"change","z":"4656a7ccbca6a363","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"Humidity","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1130,"y":200,"wires":[["b387856fbee04b83","c3f3b26a425e39f9","4742f328a4b8e494"]]},{"id":"4742f328a4b8e494","type":"ui_chart","z":"4656a7ccbca6a363","name":"","group":"da8d7d36988223dc","order":6,"width":0,"height":0,"label":"chart","chartType":"line","legend":"true","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":1290,"y":160,"wires":[[]]},{"id":"954246b950b69c22","type":"inject","z":"4656a7ccbca6a363","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"33.33,44.44","payloadType":"str","x":430,"y":200,"wires":[["664986f8eb4d35a7"]]},{"id":"a762704eb1091d95","type":"file","z":"4656a7ccbca6a363","name":"test.txt","filename":"C:\\pellesTestMappe\\test.txt","filenameType":"str","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":730,"y":200,"wires":[[]]},{"id":"28e2a41a4b0a9950","type":"serial-port","name":"","serialport":"COM8","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","waitfor":"","dtr":"none","rts":"none","cts":"none","dsr":"none","newline":"\\n","bin":"false","out":"char","addchar":"","responsetimeout":"10000"},{"id":"da8d7d36988223dc","type":"ui_group","name":"Graf","tab":"5935c569c1b3150f","order":5,"disp":true,"width":24,"collapse":false,"className":""},{"id":"5935c569c1b3150f","type":"ui_tab","name":"ch1-4","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [12 October 2024 20:48 UTC](https://discourse.nodered.org/t/extract-numbers-from-serial-input/92343/7 "2024-10-12T20:48:22Z")

</div>

> [@BubbleBobble](#):
>
> but this is what I could manage out from the tips.

What are you now getting from the COM node? If you are generating JSON as suggested you should not need the CSV, Split, or Switch nodes.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 January 2025 20:49 UTC](https://discourse.nodered.org/t/extract-numbers-from-serial-input/92343/8 "2025-01-10T20:49:14Z")

</div>

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