To receive Float data from Arduino Mega to Node MCU

I am receiving data as "4" instead of "4.51".

Here is the code for NodeMCU:

#include <SoftwareSerial.h>
SoftwareSerial s(D6, D5);

float receivedData; // Define an integer variable to store received data

void setup() {
  Serial.begin(9600); // Initialize the Serial monitor
  s.begin(9600);     // Initialize SoftwareSerial
  while (!s) {
    ;
  }
}

void loop() {
  // Read data from SoftwareSerial
  if (s.available()) {
    char receivedChar = s.read(); // Read a character from Arduino Mega
    
    if (receivedChar == '\n') {  // End of transmission, process received data
    
      Serial.print("Data Received = ");
      Serial.println(receivedData);
      delay(1000);
      
    } else {
      receivedData = (receivedChar);
    }
  }
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

1 Like

Also, please post the code for the Mega.

Copying a char into a float is usually need going to do what you expect it to do.

You need to fill a char array with the received characters and once you have detected a '\n' convert it to a flat using e.g. atof().

If you read through Serial Input Basics - updated, you might get ideas.

try use receivedData = s.parseFloat();

#include <SoftwareSerial.h>
SoftwareSerial s(D6, D5);

float receivedData; // Define an integer variable to store received data

void setup() {
  Serial.begin(9600); // Initialize the Serial monitor
  s.begin(9600);     // Initialize SoftwareSerial
  while (!s) {
    ;
  }
}

void loop() {
  // Read data from SoftwareSerial
  if (s.available()) {
      receivedData = s.parseFloat();
      Serial.println(receivedData);
      delay(1000);
  }
}

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