Arduino sending float numbers to NodeMCU (ESP8266)

I want to send from an Arduino float data to NodeMCU (ESP8266), without lose the decimals.
The basic circuit is connected this way:

Arduino | NodeMCU


3 (Rx) | Tx


2 (Tx) | Rx


GND | GND


Vcc(usb) | Vcc(usb)


I'm receiving data in a float format but they do not have the decimal values. While if I try to transmit int values, this works with no errors.

Here's my code:

Arduino

#include<SoftwareSerial.h> //Included SoftwareSerial Library
//Started SoftwareSerial at RX and TX pin of ESP8266/NodeMCU
//arduino sending

//int a;
float f;

SoftwareSerial s(3,2);    //Rx, Tx

void setup() {
  //Serial S Begin at 9600 Baud
  //Serial.begin(9600);
  s.begin(9600);
}

void loop() {

  //a = 10+random(1,5);
  f = 1.5+random(1,5);

  //s.write(a);
  s.write(f);
  delay(1000);
  //Serial.println(f, 3);
  //Serial.println(a);
}

NodeMCU (ESP8266)

//int data; //Initialized variable to store recieved data
float data1;
//nodemcu receiving


void setup() {
  //Serial Begin at 9600 Baud 
  Serial.begin(9600);
}

void loop() {

  data1 = Serial.read();
  delay(1000);
  Serial.println(data1);
  
}

How can I solve this? Do you have similar simple example to show with SotwareSerial.h library? Thank You

Pleasr read this from Arduuino reference. Then You will know.

Serial.read()
Description
Reads incoming serial data.
Serial.read() inherits from the Stream utility class.
Syntax
Serial.read()
Parameters
Serial: serial port object. See the list of available serial ports for each board on the Serial main page.
Returns
The first byte of incoming serial data available (or -1 if no data is available). Data type: int.

Serial.read() returns a byte:

data1 = Serial.read() ;

but you want 4 bytes. A float is represented as 4 bytes.

I'm sorry but I'm not an expert. So I understood that with Serial.read I can only read 1byte, but my question remains, how can I send and read the float (4bytes)?
I do not have idea, i wouldn't had asked here.

You need to read 4 bytes, glue them together and do some kind if type casting, cheating the compiler to read those 4 bytes as a float. Easy to do in assembler but harder to make it pass the C-compiler.

You can use SerialTransfer.h to automatically packetize and parse your data for inter-Arduino communication without the headace (including floats!). The library is installable through the Arduino IDE and includes many examples.

Here are the library's features:

This library:

  • can be downloaded via the Arduino IDE's Libraries Manager (search "SerialTransfer.h")
  • works with "software-serial" libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 255 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  float myFloat = 5.2;
  myTransfer.txObj(myFloat, sizeof(myFloat));
  myTransfer.sendData(sizeof(myFloat));
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    float myFloat;
    
    Serial.print("New Data: ");
    myTransfer.rxObj(myFloat, sizeof(myFloat));
    Serial.println(myFloat);
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

Riemanndiy:
I'm sorry but I'm not an expert. So I understood that with Serial.read I can only read 1byte, but my question remains, how can I send and read the float (4bytes)?
I do not have idea, i wouldn't had asked here.

A simple answer might be to just send/receive the numeric value as a string of characters that represents the numeric value, then convert the string of characters back into the numeric value after it has been received.

Thank You so much @Power_Broker, stunning library! this works perfect, I would add a random component to the variable myFloat to simulate a sensor.

float myFloat = 5.2 + random(1,5);

I'm giving you a star on github

I am having issues sending data between Arduino and NodeMCU.

I am using the suggested "SerialTransfer.h" library. In the example sketch "softwareserial_rx/tx_multi_byte" a sruct with multiple fields is sent. When i try this script the char is transferred but the float is depicted as 0.00. When i add an additional integer the integer is also not received correctly. I tried sending only int or float or char and i works without any problem. I really dont understand what is happening since this library is mentioned in almost every post regarding serial communication between two devices and i cant get it to work properly. Would really appreciate some help. Since i am new to this Forum i am not sure if posting the used example code would be of any help in this scenario, if it does, please let me know.

anthero:
this library is mentioned in almost every post regarding serial communication between two devices

:wink:

anthero:
I am having issues sending data between Arduino and NodeMCU.

Please post all your sketches in tags. It's impossible to help without being able to see the code.