Value NaN on Node-red

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(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(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("humidity: ");
//Serial.println(h);
//delay(5000);
//Serial.println("temperature: ");
//Serial.println(t);
//delay(5000);
//Serial.println("this is the water level: ");
//Serial.println(cmwaterValue);
//delay(5000);
//Serial.println("RSSI: ");
//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:

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.

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