Go Down

Topic: Sending float over serial (Read 286 times) previous topic - next topic

Workaholic

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.

Robin2

Have a look at the examples in Serial Input Basics - 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
Two or three hours spent thinking and reading documentation solves most programming problems.

GolamMostafa

#2
Mar 18, 2019, 05:06 pm Last Edit: Mar 20, 2019, 01:48 am by GolamMostafa
@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.


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.  

freaklabs

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.
FreakLabs
Wireless Technology for the environment, wildlife, and agriculture
https://freaklabs.org
https://hackerfarm.jp
Twitter: @freaklabs

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

GolamMostafa

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.   

Workaholic

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)?

GolamMostafa

#7
Mar 25, 2019, 08:35 am Last Edit: Mar 25, 2019, 08:43 am by GolamMostafa
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.


Code: [Select]
#include<SoftwareSerial.h>
SoftwareSerial SUART(4, 5); //SRX=GPIO4/D2, STX=GPIO5/D1 for NodeMCU 1.0
SUART.begin(9600);

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

Workaholic

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.

Go Up