Reading serial from arduino on nodemcu and "capturing" variable

Hi there,
i am strugling for past few days and really dont know what am I missing.
Doing this as hobby so pls don't judge.

My set up Arduino reads sensor (light) sends data to NodeMcu who sends it to localhost.

The thing i am struggling is i cant extract variable coming from Arduino ..
the code:

how output should work:

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() { // run over and over
if (Serial.available()) {
Serial.write(Serial.read());
}
}

OUTPUT

520.00
710.00
1020.00
640.00

Now if i try to Serial.read() and store it as variable i get gibberish and values which i cant use. Any attempt to mess with 1st code i get this result

void loop() { // run over and over
if (Serial.available()) {			
float arrivingdatabyte = Serial.read();
delay(1000);
Serial.println(arrivingdatabyte);  
}
}

OUTPUT
55.00
13.00
11.00
44.00
19.00

so pls can someone help me what am I doing wrong. Or @ least point me to right direction.
Many thanks.

Please read the online documentation on this site, for the read() method of the Serial class.

Data is sent over a serial link one character at a time so when you receive a character and print it, which is what you are doing, you see a print of a representation of the value one character at a time, not the actual value

If you want to turn what you receive into a value that you can use then you need to read each of the characters and convert the whole thing.

Start by looking at the Serial.parseFloat() function Serial.parseFloat() - Arduino Reference

However, I would question whether you need to use a float when an integer would probably suffice

I would also question whether you need the Arduino when the NodeMcu could read the sensor itself

thanks for yours reply...

i'm talking in n00b perspective, that i'm baffled by it, that this:

Serial.write(Serial.read())

can give you on serial monitor the value u seek, but Good forbid you try to "catch" it and reused in code .. it goes completely beserk

like i said .. tyvm

It is not giving you the value, rather it is printing each character of the value in turn

Have you tried Serial.parseInt() ?

@tandrli

Try the following (tested) setup and sketches:
uartnanoUno

NANO Simulator and Sender

#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11);

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);

}

void loop()
{
  SUART.print(520.00, 2);
  SUART.write(0x0A);
  //---------------------
  SUART.print(710.00, 2);
  SUART.write(0x0A);
  //---------------------
  SUART.print(1020.00, 2);
  SUART.write(0x0A);
  //---------------------
  SUART.print(640.00, 2);
  SUART.write(0x0A);
  //---------------------
  delay(1000);
}

UNO Receiver

#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11);
char myData[20];
byte counter = 4;

void setup()
{
  Serial.begin(9600);
  SUART.begin(9600);
}

void loop()
{
  do
  {
    byte n = SUART.available();
    if (n != 0)
    {
      byte m = SUART.readBytesUntil('\n', myData, 10);
      myData[m] = '\0';
      float x = atof(myData);
      Serial.println(x, 2);
    }
    counter--;
  }
  while (counter != 0);
  counter = 4;
}

Output: on SM2 (Serial Monitor 2)

520.00
710.00
1020.00
640.00

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