Stream analytics

Show us how you have configured the serial node to give that output. Also I think you had better show us your latest Arduino code.

Yes of course. Serial node has the following form:

And the code of server lora has the following form:


#include <Adafruit_BusIO_Register.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_I2CRegister.h>
#include <Adafruit_SPIDevice.h>

#include <SPI.h>
#include <RH_RF95.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMonoBold12pt7b.h>

RH_RF95 rf95;
int led = 13;
unsigned long int millisBefore;
int turn = 0;
int h, t, mmwaterValue;

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup()
{
  pinMode(led, OUTPUT);
  Serial.begin(115200);
  while (!Serial) ; // Wait for serial port to be available
  if (!rf95.init())
    Serial.println("init failed");
  rf95.setFrequency(868.0);
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }
  // Clear the buffer
  display.clearDisplay();
  printText();
  delay(1500);
}
void loop()
{
  // Now wait for a reply
  uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);
  if (rf95.waitAvailableTimeout(500))
  {
    if (rf95.recv(buf, &len))
    {
      
      String dataTotal = (char*)buf;
  
      explode(dataTotal);
    }
    else
    {
      Serial.println("recv failed");
    }
  }
  printText();
  display.display();
  delay(300);
  display.clearDisplay();
  
}
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(mmwaterValue);
      mmwaterValue = sementara.toFloat();
    }
    pch = strtok (NULL, "-");
    delay(100);
  }
   //Serial.println(h);
   //delay(5000); // vazoyme to delay etsi wste i node red na ksexwrisei ta dedomena apo to Arduino 
   //Serial.println(t);
   //delay(5000);  //vazoyme to delay etsi wste i node red na ksexwrisei ta dedomena apo to Arduino
   //Serial.println(mmwaterValue);
   //delay(5000); //vazoyme to delay etsi wste i node red na ksexwrisei ta dedomena apo to Arduino
   String json = "{\"humidity\": " + String(h) + ", \"temperature\": " + String(t)+ ", \"water\": " +String(mmwaterValue) +" }";
   Serial.println(json);
  turn = 0;
  
}

void printText() {
  display.setFont(&FreeMonoBold12pt7b);
  display.setTextColor(WHITE);        // Draw white text
  display.setCursor(0, 28);            // Start at top-left corner
  display.print(t);
  display.drawCircle(34, 8, 3, WHITE);
  display.setCursor(40, 27);
  display.print("C");
  display.setCursor(80, 28);
  display.print(h);
  display.print("%");
}

I have edited your post to add tripple backticks round the code so we can see what is really there.

You said you had configured the node to split on } but you have it splitting on \n

Can you try changing this...

to this...

String json = "{\"humidity\": " + h + ", \"temperature\": " + t + ", \"water\": " + mmwaterValue +" }\n";`
Serial.print(json);

i changed it and but i have this error:

Odd, according to String documentation, concatenating ints was OK.

Please try one more time...

String json = "{\"humidity\": " + String(h) + ", \"temperature\": " + String(t)+ ", \"water\": " +String(mmwaterValue) +" }\n";`
Serial.print(json);

And set the serial node config to split on \n

i haven't error in Arduini code but the debug is still the same..

and the seiral node:

node

Try splitting on a gap of 100ms rather than a character and see what you get.

Unfortunately the same..

com5 node

very very strange.

Would you indulge me with these tests?...

TEST 1...

String json = "1,2,3\n";
Serial.print(json);

TEST 2...

String json = String(h) + "," + String(t) + "," + String(mmwaterValue) + "\n";
Serial.print(json);

TEST 3...

String json = "\"humidity\": 1, \"temperature\": 2, \"water\": 3 \n";
Serial.print(json);

TEST 5...

String json = "{\"humidity\": 1, \"temperature\": 2, \"water\": 3 }\n";
Serial.print(json);

yes i will try and i will let you know. By the way in serial mode what have i do? to put split input "after a timeout of 100 ms" or "on the character \n?"

It should work with \n but try both if necessary.

So here are outputs..

TEST 1:

TEST 2:

TEST 3:

TEST 4:

It's really stranger..pff..

Yes indeed.

Oh, well, at least you have a solution you can use.


So use TEST2 version, feed the COM port to a function node with this code...

var pl = msg.payload.trim();
if(!pl) return null;
var parts = pl.trim().split(",");
if(parts.length < 3) return null;

msg.payload = {
    humidity: pl[0],
    temperature: pl[1],
    water: pl[2]
}
return msg;

--- alternatively ---


If you want 3 individual values with a topic (for MQTT) then ...

var pl = msg.payload.trim();
if(!pl) return null;
var parts = pl.trim().split(",");
if(parts.length < 3) return null;
var msg1 = {
    topic: "humidity",
    payload: pl[0]
}
var msg2 = {
    topic: "temperature",
    payload: pl[1]
}
var msg3 = {
    topic: "water",
    payload: pl[2]
}
return [[msg1, msg2, msg3]];

not bad!!! it's very close to be correct. Here is the result:

1st code (with no MQTT):

2nd code:

Geberally it's correct, i think that with this way, node red, "understands" which value is which. Now the only problem is the results. Actually, in my room humidity is 62 and temperature is 21.

Oops.

Try this...

var pl = msg.payload.trim();
if(!pl) return null;
var parts = pl.split(",");
if(parts.length < 3) return null;

msg.payload = {
    humidity: Number(parts[0]),
    temperature: Number(parts[1]),
    water: Number(parts[2])
}
return msg;

image

image

demo flow...

[{"id":"dfb8f894.a94758","type":"function","z":"8c0d4c46.2bfb5","name":"","func":"var pl = msg.payload.trim();\nif(!pl) return null;\nvar parts = pl.split(\",\");\nif(parts.length < 3) return null;\n\nmsg.payload = {\n    humidity: Number(parts[0]),\n    temperature: Number(parts[1]),\n    water: Number(parts[2])\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":732,"y":1072,"wires":[["7e7a56b6.ef8648"]]},{"id":"4a2a8e76.9a358","type":"inject","z":"8c0d4c46.2bfb5","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"60,22,0","payloadType":"str","x":546,"y":1072,"wires":[["dfb8f894.a94758"]]},{"id":"7e7a56b6.ef8648","type":"debug","z":"8c0d4c46.2bfb5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":918,"y":1072,"wires":[]}]

Alternative version (separate messages - for MQTT)

image

var pl = msg.payload.trim();
if(!pl) return null;
var parts = pl.trim().split(",");
if(parts.length < 3) return null;
var msg1 = {
    topic: "humidity",
    payload: Number(parts[0])
}
var msg2 = {
    topic: "temperature",
    payload: Number(parts[1])
}
var msg3 = {
    topic: "water",
    payload: Number(parts[2])
}
return [[msg1, msg2, msg3]];

It works!!! Thank you very very much!!! honestly!!! Tommorow, i might ask you another question, about connection node-red with power bi.

Again, thank you very very much!!!!

You're welcome. Glad it is working for you.

If you do have new (different) questions, please start a new thread instead of asking here.

PS, you should mark one of the posts in this thread as the solution.

ok i will do it!! thank you very much again.