# Reading Arduino serial data into node red via usb

**URL:** https://discourse.nodered.org/t/reading-arduino-serial-data-into-node-red-via-usb/41410
**Category:** General
**Created:** [22 February 2021 09:27 UTC](https://discourse.nodered.org/t/reading-arduino-serial-data-into-node-red-via-usb/41410 "2021-02-22T09:27:58Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![AllenSlaghuis](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/allenslaghuis/32/37660_2.png) [@AllenSlaghuis](https://discourse.nodered.org/u/AllenSlaghuis)
#### Post date: [22 February 2021 09:27 UTC](https://discourse.nodered.org/t/reading-arduino-serial-data-into-node-red-via-usb/41410/1 "2021-02-22T09:27:58Z")

</div>

Good day All.  
I am new to the node red system and i use openhab. as fisialatition of my home automation. now i am trying to use nano's as sensor breakouts on my server to monitor my server room the serial link via usb works and i get the date to node red. but now i want to split the date out to different mqtt topics to my openhab .  
my problem comes in with the split out of data. this is what i get in the Serial monitor of my ide.

![Screenshot 2021-02-22 at 11.25.04](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/a/ca048db03df462ee4028dc47d90eee3262cda87c.png)

in node red it splits them in to strings. how do i get that in to the mqtt channels i want to use ?

---

<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: [22 February 2021 10:26 UTC](https://discourse.nodered.org/t/reading-arduino-serial-data-into-node-red-via-usb/41410/2 "2021-02-22T10:26:05Z")

</div>

If you have control of the s/w in the nano (as I presume you do) then a good solution may to send the data to node-red in the form of JSON, so the example you show would be a string containing something like  
{"h": 58, "t": 28.1, "Heat Index": 23.34, ...}  
Then in node-red you can feed that into a JSON node to convert it to a javascript object. Then the rest is easy.

---

<div class="post-metadata">

### Author: ![AllenSlaghuis](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/allenslaghuis/32/37660_2.png) [@AllenSlaghuis](https://discourse.nodered.org/u/AllenSlaghuis)
#### Post date: [22 February 2021 10:37 UTC](https://discourse.nodered.org/t/reading-arduino-serial-data-into-node-red-via-usb/41410/3 "2021-02-22T10:37:11Z")

</div>

Hi thanks for reply.  
this is out my nano : Must i change it to be a single line output

```auto
Serial.print("h: ");
Serial.print(h);
Serial.println(" % ");
Serial.print("t: ");
Serial.print(t);
Serial.println(" *c ");
Serial.print("Heat Index: ");
Serial.print(hi);
Serial.println(" *C ");
Serial.print("Dew ");
Serial.println(DP);
Serial.print("DOOR STAT : ");
Serial.println(door);

```

then on node red what should my serial in be set on

 ![Screenshot 2021-02-22 at 12.35.13](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/4/84aa5b29102cc59d0b72482c545488b84badc978.png)

---

<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: [22 February 2021 10:54 UTC](https://discourse.nodered.org/t/reading-arduino-serial-data-into-node-red-via-usb/41410/4 "2021-02-22T10:54:10Z")

</div>

I would do it as one json string probably. Alternatively if all you want to do is to send it straight to MQTT then send it as separate lines like  
{"topic": "h", "value": 58}  
{"topic": "t", "value": 28.1}  
and so on. Then in node red you will receive the messages one at a time and in a Change node can 'Set msg.topic To msg.payload.topic' and 'Move msg.payload.value To msg.payload' and send that direct to the MQTT node.

---

<div class="post-metadata">

### Author: ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)
#### Post date: [22 February 2021 12:33 UTC](https://discourse.nodered.org/t/reading-arduino-serial-data-into-node-red-via-usb/41410/5 "2021-02-22T12:33:13Z")

</div>

I would send the following

> Serial.print(h);  
> Serial.print(":");  
> Serial.print(t);  
> Serial.print(":");  
> Serial.print(hi);  
> Serial.print(":");  
> Serial.println(DP)  
> Serial.print(":");  
> Serial.println(door);

And then in a function node

> var output = msg.payload.split(":");
> 
> var h = parseFloat(output[0]);  
> var t = parseFloat(output[1]);  
> var heat = parseFloat(output[2]);  
> var dew = parseFloat(output[3]);  
> var door = parseFloat(output[3]);  
> var msg0 = {payload:h};  
> var msg1 = {payload:t};  
> var msg2 = {payload:heat};  
> var msg3 = {payload:dew};  
> var msg4 = {payload:door};
> 
> return [msg0 , msg1, msg2, msg3, msg4];

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/7/771c909edd956332b86aacf4400d10eb4eff8420.png)

The serial setting are the same as on you nano

---

<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: [22 February 2021 13:34 UTC](https://discourse.nodered.org/t/reading-arduino-serial-data-into-node-red-via-usb/41410/6 "2021-02-22T13:34:47Z")

</div>

Does that have an advantage over my suggestion which uses a single Change node instead of a function node with four outputs?

---

<div class="post-metadata">

### Author: ![AllenSlaghuis](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/allenslaghuis/32/37660_2.png) [@AllenSlaghuis](https://discourse.nodered.org/u/AllenSlaghuis)
#### Post date: [22 February 2021 13:39 UTC](https://discourse.nodered.org/t/reading-arduino-serial-data-into-node-red-via-usb/41410/7 "2021-02-22T13:39:00Z")

</div>

Thanks .  
I actual used both the only issue i picked up with the function now is that i lose one value .  
i lost the dew value. and cant seem to get it sorted. but both are winners. not sure what is the best way. but will run both as i have 2 setups like this and will monitor them.

---

<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: [22 February 2021 14:12 UTC](https://discourse.nodered.org/t/reading-arduino-serial-data-into-node-red-via-usb/41410/8 "2021-02-22T14:12:56Z")

</div>

> [@AllenSlaghuis](#):
>
> not sure what is the best way

Sending JSON (instead of string splitting) is the better solution. Every time, any day of the week.

---

<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: [23 April 2021 14:13 UTC](https://discourse.nodered.org/t/reading-arduino-serial-data-into-node-red-via-usb/41410/9 "2021-04-23T14:13:15Z")

</div>

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