# Value NaN on Node-red

**URL:** <https://discourse.nodered.org/t/value-nan-on-node-red/46665>\
**Category:** General\
**Created:** [3 June 2021 20:04 UTC](https://discourse.nodered.org/t/value-nan-on-node-red/46665 "2021-06-03T20:04:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex95](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@Alex95](https://discourse.nodered.org/u/Alex95)\
**Post date:** [3 June 2021 20:04 UTC](https://discourse.nodered.org/t/value-nan-on-node-red/46665/1 "2021-06-03T20:04:45Z")

</div>

Good evening to all. Before I tell you my problem I will tell you some details. Initially I have two Arduino which communicate with each other via LoRa due to the fact that I have connected them to two Dragino. So I have two codes, one for the LoRa transmitter and one for the LoRa receiver. One Dragino has connected on it two sensors, a heat-humidity-one and a water level. So I have a total of three data. I send these data to Node red serially. For some reason, however, while I receive prices, I also receive the NaN price. Do you know how I can fix it so that I do not receive the value ΝaΝ?

Also i will send you 2 codes of Arduino

Client code:

#include "DHT.h"  
#define DHTPIN 4  
#define DHTTYPE DHT22  
#include \<SPI.h\>  
#include \<RH\_RF95.h\>  
RH\_RF95 rf95;  
DHT dht(DHTPIN, DHTTYPE);  
const int waterSensor = A0;  
int waterValue = 0;  
int cmwaterValue = 0;

void setup() {  
Serial.begin(115200);  
dht.begin();  
if (!rf95.init()) //  
Serial.println("init failed");  
rf95.setFrequency(868.0);  
}  
void loop() {

float h = dht.readHumidity();  
float t = dht.readTemperature();  
float f = dht.readTemperature(true);  
//float a = dht.readHumidity(true);  
int waterValue = analogRead(waterSensor);  
cmwaterValue = map(waterValue, 0, 1023, 0, 40);  
[//Serial.println](https://Serial.println)(mmwaterValue);  
if (isnan(h) || isnan(t)) {  
Serial.println("Failed to read from DHT sensor!");  
return;  
}  
Serial.print("Humidity: ");  
Serial.print(h);  
Serial.print(" %\t");  
Serial.print("Temperature: ");  
Serial.print(t);  
Serial.println(" \*C ");  
Serial.print("this is water depth: ");  
Serial.print(cmwaterValue);  
Serial.println("cm"); // σε cm  
String data = String(h) + "-" + String(t)+ "-" +String(cmwaterValue);  
//String json = "{"humidity": " + String(h) + ", "temperature": " + String(t)+ ", "water": " +String(mmwaterValue) + "}";  
[//Serial.println](https://Serial.println)(json);  
int dataLength = data.length(); dataLength++;  
uint8\_t total[dataLength];  
data.toCharArray(total, dataLength);  
Serial.println(data);  
rf95.send(total, dataLength);  
rf95.waitPacketSent();  
uint8\_t buf[RH\_RF95\_MAX\_MESSAGE\_LEN];  
uint8\_t len = sizeof(buf);

if (rf95.waitAvailableTimeout(500))  
{

```
if (rf95.recv(buf, &len))
{
  //digitalWrite(led, HIGH);
  Serial.print("got reply: ");
  Serial.println((char*)buf);
  Serial.print("RSSI: ");
  Serial.println(rf95.lastRssi(), DEC);

```

delay(10000);  
}  
}  
}

Server code:

#include \<SPI.h\>  
#include \<RH\_RF95.h\>  
#include \<Wire.h\>

RH\_RF95 rf95;  
unsigned long int millisBefore;  
int turn = 0;  
int h, t, cmwaterValue;

void setup()  
{

Serial.begin(115200);  
while (!Serial) ;  
if (!rf95.init())  
Serial.println("init failed");  
rf95.setFrequency(868.0);

}  
void loop()  
{

uint8\_t buf[RH\_RF95\_MAX\_MESSAGE\_LEN];  
uint8\_t len = sizeof(buf);  
if (rf95.waitAvailableTimeout(500))  
{  
if (rf95.recv(buf, &len))  
{  
Serial.println("Received at Server: ");  
Serial.println((char\*)buf);  
String dataTotal = (char\*)buf;  
Serial.println(dataTotal);  
explode(dataTotal);  
}  
else  
{  
Serial.println("recv failed");  
}  
}

}  
void explode(String req) {  
char str[20];  
req.toCharArray(str, 20);  
char \* pch;  
pch = strtok (str, "-");

while (pch != NULL)  
{  
String sementara = pch;  
turn++;  
if (turn == 1) {  
Serial.println(h);  
h = sementara.toFloat();  
}  
if (turn == 2) {  
Serial.println(t);  
t = sementara.toFloat();  
}  
if (turn == 3) {  
Serial.println(cmwaterValue);  
cmwaterValue = sementara.toFloat();  
}  
pch = strtok (NULL, "-");  
delay(100);  
}  
[//Serial.println](https://Serial.println)("humidity: ");  
[//Serial.println](https://Serial.println)(h);  
//delay(5000);  
[//Serial.println](https://Serial.println)("temperature: ");  
[//Serial.println](https://Serial.println)(t);  
//delay(5000);  
[//Serial.println](https://Serial.println)("this is the water level: ");  
[//Serial.println](https://Serial.println)(cmwaterValue);  
//delay(5000);  
[//Serial.println](https://Serial.println)("RSSI: ");  
[//Serial.println](https://Serial.println)(rf95.lastRssi(), DEC);  
//delay(5000);  
String json = String(h) + "," + String(t) + "," + String(cmwaterValue) + "," + String(rf95.lastRssi(),DEC) + "\n";  
Serial.println(json);  
delay(5000);  
turn = 0;

}

Here is output:

 ![node-red forum](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/8/d8abfa1ba1cf518e7cd9149003e25df0fd86e410.png)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [4 June 2021 21:16 UTC](https://discourse.nodered.org/t/value-nan-on-node-red/46665/2 "2021-06-04T21:16:22Z")

</div>

Hi, you need to wrap your code in back-ticks so that it can be read properly I'm afraid.

Have you determined what the arduino code is actually sending when node-red sees the value as NaN? It is possible that a value is sometimes being sent that JavaScript can't interpret as a number, perhaps it contains a spurious space or line feed or something else entirely.

---

<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:** [3 August 2021 21:17 UTC](https://discourse.nodered.org/t/value-nan-on-node-red/46665/3 "2021-08-03T21:17:00Z")

</div>

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