Send multiple variables float

Hello everyone

I’m trying to use two 433 Mhz RF modules, to allow communication between Arduino UNO and Arduino MEGA.
I would like to send at least 4 data, variables of type float.

Try to use these two sketches, work, but with these sketches can only send variables of type integer.

That trick I use to be able to send data of type float? (Even with only one decimal place)

how can I convert data from float to integer, send, and then convert them back to a float?

I also read the dtostrf function () but I do not know how to use it to concatenate multiple variables of type float

Help me, I’m stuck =( =(

Thank you!

Transimetter

#include <VirtualWire.h>
 

int ledPin = 13;

int Sensor1Data;// The variable were the data from each sensor
int Sensor2Data;// will be stored 
int Sensor3Data;
int Sensor4Data;

char Sensor1CharMsg[21];// The string that we are going to send trought rf 
 
void setup() {
 
 // LED 
 pinMode(ledPin,OUTPUT);
  
  
 // VirtualWire setup
 vw_setup(2000); // Bits per sec
 vw_set_tx_pin(52);// Set the Tx pin. Default is 12
 
}
 
void loop() {
   
  
  Sensor1Data = 152;
  Sensor2Data = 258;
  Sensor3Data = 325;
  Sensor4Data = 412;
   
  sprintf(Sensor1CharMsg, "%d,%d,%d,%d,", Sensor1Data, Sensor2Data, Sensor3Data, Sensor4Data);
  
 // Turn on a light to show transmitting
 vw_send((uint8_t *)Sensor1CharMsg, strlen(Sensor1CharMsg));
 vw_wait_tx(); // Wait until the whole message is gone
 // Turn off a light after transmission
 delay(40);
 
}

RECEIVER

#include <VirtualWire.h>
  
// Sensors 
int Sensor1Data;
int Sensor2Data;
int SensorData3;
int SensorData4;
  
char StringReceived[22]; 
  
  
  
void setup() {
  Serial.begin(9600);
    // VirtualWire 
    // Initialise the IO and ISR
    // Required for DR3100
  
    // Bits per sec
    vw_setup(2000);
    vw_set_rx_pin(8);  
      
    // Start the receiver PLL running
    vw_rx_start();       
  
} // END void setup
  
void loop(){
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
      
//Taking the data from the control base
    if (vw_get_message(buf, &buflen)) 
    {
 int i;
        // Message with a good checksum received, dump it. 
        for (i = 0; i < buflen; i++)
 {            
          // Fill Sensor1CharMsg Char array with corresponding 
          // chars from buffer.   
          StringReceived[i] = char(buf[i]);
 }
  
      sscanf(StringReceived, "%d,%d,%d,%d",&Sensor1Data, &Sensor2Data,&SensorData3,&SensorData4); // Converts a string to an array
          
      Serial.println(Sensor1Data);
      Serial.println(Sensor2Data);
      Serial.println(SensorData3);
      Serial.println(SensorData4);
      
         
    }
  
 memset( StringReceived, 0, sizeof( StringReceived));// This line is for reset the StringReceived
}

there are in general 2 ways

  1. convert the float to text and back
  2. send it in binary form

For the second you can use an UNION. this maps different types on the same memory.

// make a union of a float and a byte array
union 
{
    float f;
    byte b[4];
} _float;


// sending the float in bytes

_float.f = 3.14159265;
for (int i=0; i< 4; i++) send (_float.b[i]);

receiving is done similar , but you must watch the order of bytes of course.

more playing with floats see - http://playground.arduino.cc//Main/IEEE754tools -

and how do you convert float to text?

use the dtostrf is not recommended?

dtostrf() is a fine function, you can use it.

using dtostrf to transport a float between two computers has two drawbacks 1) the conversion forth and back can/will loose precission 2) the number of chars transported depends on the value - typically between 3 and 10 (more?) characters (incl sign and . )

A binary transport will always 1) be exact (no conversion) 2) fixed transport time of 4 bytes (equal to 4 chars)