Arduino Forum

Using Arduino => Networking, Protocols, and Devices => Topic started by: Workaholic on Mar 18, 2019, 08:07 am

Title: Sending float over serial
Post by: Workaholic on Mar 18, 2019, 08:07 am
Hello guys. I am communicating between arduino and esp. There is a sensor connected to arduino whose float values need to be sent to esp. Kindly give me some suggestions as to how I can serial send these float values to esp. e.g. I need to send a variable "processed_data" to esp, and processed_data = 15.92, and it can change as any sensor values do.
Title: Re: Sending float over serial
Post by: Robin2 on Mar 18, 2019, 09:56 am
Have a look at the examples in Serial Input Basics (http://forum.arduino.cc/index.php?topic=396450.0) - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)
Code: [Select]
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker


...R
Title: Re: Sending float over serial
Post by: GolamMostafa on Mar 18, 2019, 05:06 pm
@OP

1.  Make a connection between UNO and ESP as per following diagram; where, Software UART Ports (SUART) instead of Hardware UART Ports (UART Port) have been used as the UART ports are kept  engaged with Serial Monitor/IDE for debugging/uploading.
(https://forum.arduino.cc/index.php?action=dlattach;topic=604285.0;attach=299865)

2.  A SUART Port is created by including the following lines in the sketch:
Code: [Select]
#include<SoftwareSerial.h>
SoftwareSerial SUART(2, 3); //SRX=DPin-2, STX=DPin-3 for UNO
SUART.begin(9600);


3.  Now decide which one of the following two ways you wish to choose to transfer your float number (say, 15.92) from UNO to ESP:
(1)  8-bit ASCII code for every symbol/character (1  5 . 9  2) of the float number.
(2)  8-bit binary code for each byte of the corresponding 4-byte (32-bit) value of the float number. (A float number can be converted into 32-bit data (4 separate bytes or 1-piece long) using union data structure.)

4.  Write sketch for Sender - UNO and upload.

5.  write sketch for Receiver - ESP and upload.  
Title: Re: Sending float over serial
Post by: freaklabs on Mar 20, 2019, 10:58 am
A float takes 4 bytes on an AVR so you can just send a 4-byte buffer via SPI to the ESP. The ESP doesn't care what the data is and will just send the buffer. On the receiving end, you will need to decode the 4-byte buffer as a float.
Title: Re: Sending float over serial
Post by: Robin2 on Mar 20, 2019, 02:37 pm
On the receiving end, you will need to decode the 4-byte buffer as a float.
Do you know if the ESP8266 interprets the bytes of a float in the same way that an Arduino does?

Sending data as text avoids that problem at the expense of some small loss of performance.

...R
Title: Re: Sending float over serial
Post by: GolamMostafa on Mar 20, 2019, 03:47 pm
Do you know if the ESP8266 interprets the bytes of a float in the same way that an Arduino does?
The UNO and ESP8266(NodeMCU 1.0(ESP-12)) follow the same order to recover float data from the binray32 formatted data and vice versa.   
Title: Re: Sending float over serial
Post by: Workaholic on Mar 25, 2019, 08:32 am
Well I am able to send the data between arduino mega and esp, but when i put my webserver coding into esp, it is not communicating with arduino at that time. Maybe because as GolamMostafa says hardware serial lines are busy. But my module is ESP-01, Should I do software serial instead of hardware. And which pins of ESP should I use, TX, RX or GPIO0, GPIO1 (available on ESP-01)?
Title: Re: Sending float over serial
Post by: GolamMostafa on Mar 25, 2019, 08:35 am
Well I am able to send the data between arduino mega and esp, but when i put my webserver coding into esp, it is not communicating with arduino at that time. Maybe because as GolamMostafa says hardware serial lines are busy. But my module is ESP-01, Should I do software serial instead of hardware. And which pins of ESP should I use, TX, RX or GPIO0, GPIO1 (available on ESP-01)?
Yes! You communicate between UNO and ESP using software UART Port (SUART Port) as per diagram of Post#2 repeated here.
(https://forum.arduino.cc/index.php?action=dlattach;topic=604285.0;attach=299865)

Code: [Select]
#include<SoftwareSerial.h>
SoftwareSerial SUART(4, 5); //SRX=GPIO4/D2, STX=GPIO5/D1 for NodeMCU 1.0
SUART.begin(9600);
Title: Re: Sending float over serial
Post by: Robin2 on Mar 25, 2019, 09:10 am
Well I am able to send the data between arduino mega and esp, but when i put my webserver coding into esp, it is not communicating with arduino at that time.
You have not posted the working programs or the non-working programs so how can we see what you might have done that is causing the problem.

...R
Title: Re: Sending float over serial
Post by: Workaholic on Mar 25, 2019, 12:13 pm
I do not know what is the problem. But when I am doing software serial, the values are not correct. When I do RX TX pins of ESP-01, I get correct values.