Converting float to send

I'm trying to transmit via 433 communication. The piece below works fine. I receive "Hello World!" correctly. But I want to send a floating point number and I just can't figure out how to do it. I've tried converting to a string and a character array but nothing works.
I'm using Radiohead.h
Thanks.

void loop()
{
    const char *msg = "Hello World!";
    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    delay(200);
}

So post the code that did not work.

If there was an error, please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

To convert a float to a string did you try the dtostrf() function?

float x = 1.73;
driver.send((uint8_t *) &x, sizeof(float));

It is almost always better to send integer values.

Thanks. I changed 'sizeof float' to 'size of int', because at this point I don't need the decimals anymore, and it seems to work. But now I have a problem reading it on the receive side. DOH! :woozy_face:

void loop()
{
    uint8_t buf[12];
    uint8_t buflen = sizeof(buf);
    if (driver.recv(buf, &buflen)) // Non-blocking
    {
      int i;
      // Message with a good checksum received, dump it.
      Serial.print("Message: ");
      Serial.println((char*)buf);         
    }
}

As long as you also changed the data type itself to int, that's OK. You can't just truncate two bytes off of a float and expect to just have the decimals cut off.

Post the full sender and receiver sketch.

I'm assuming that your problem is that the receiver side is only outputting ascii characters. This works in your first post because you were only sending ascii "Hello World!". The float is being sent in binary using jremington's example.

To send a float in ascii, you can use the "dtostrf" function;

float floatVar = 3.14;
char float_to_Char[5];
dtostrf(floatVar, 3, 2, float_to_Char);

Serial.println(float_to_Char);

driver.send((uint8_t *)float_to_Char, strlen(float_to_Char));

If you want to send multiple things, you can concatenate floats, ints, and char arrays etc. into a bigger char array (using sprintf), and then send that;

int intVar = 123;
char charArray[] = "Hello World";
float floatVar = 3.14;

char array_to_print[100]; 

//convert float to char
char float_to_Char[5];
dtostrf(floatVar, 3, 2, float_to_Char);

sprintf(array_to_print, "<%s,%s,%d>\n", float_to_Char,charArray,intVar);


Serial.println(array_to_print);

driver.send((uint8_t *)array_to_print, strlen(array_to_print));

This should output as;

<3.14,Hello World,123>

You can then copy this string into a char array (on the receiver side) and parse everything into variables using the Serial Input Basics tutorial; Serial Input Basics - updated - #3 by Robin2

A more efficient way of sending multiple variables like this is using a struct (sends in binary). I'm not familiar with this particular communication method you're using, but here's an example of sending and receiving structs, which might transfer easily to your application; Use I2C for communication between Arduinos

Here I publish several float, actually doubles, items to the MQTT broker from a ESP32,

      String sTopic = "";
      sTopic.reserve( 35 );
      sTopic.concat( String(px_eData.SunRiseHr) + "," );
      sTopic.concat( String(px_eData.SunRiseMin) + "," );
      sTopic.concat( String(px_eData.SunSetHr) + "," );
      sTopic.concat( String(px_eData.SunSetMin) + "," );
      sTopic.concat( String(px_eData.DawnHr) + "," );
      sTopic.concat( String(px_eData.DawnMin) + "," );
      sTopic.concat( String(px_eData.TransitHr) + "," );
      sTopic.concat( String(px_eData.TransitMin) );
      xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY );
      MQTTclient.publish( topicSRSSDDT, sTopic.c_str() );
      xSemaphoreGive( sema_MQTT_KeepAlive );

Note the MQTTclient.publish( topicSRSSDDT, sTopic.c_str() ); that converts the String to a string.

    xEventGroupWaitBits (eg, evtDoBME, pdTRUE, pdTRUE, portMAX_DELAY );
    xSemaphoreTake ( sema_eData, portMAX_DELAY );
    x_eData.Temperature  = bme.readTemperature();
    x_eData.Temperature  = ( x_eData.Temperature * 1.8f ) + 32.0f; // (Celsius x 1.8) + 32
    x_eData.Pressure     = bme.readPressure();
    x_eData.Pressure     = x_eData.Pressure / 133.3223684f; //converts to mmHg
    x_eData.Humidity     = bme.readHumidity();
    x_eData.IAQ          = fCalulate_IAQ_Index( bme.readGas(), x_eData.Humidity );
    bmeInfo.concat( String(x_eData.Temperature, 2) );
    bmeInfo.concat( "," );
    bmeInfo.concat( String(x_eData.Pressure, 2) );
    bmeInfo.concat( "," );
    bmeInfo.concat( String(x_eData.Humidity, 2) );
    bmeInfo.concat( "," );
    bmeInfo.concat( String(x_eData.IAQ, 2) );
    xSemaphoreGive ( sema_eData );
    xSemaphoreTake( sema_MQTT_KeepAlive, portMAX_DELAY );
    if ( MQTTclient.connected() )
    {
      MQTTclient.publish( topicInsideInfo, bmeInfo.c_str() );
    }

That won't work at all.

As I said, it is better to send integers anyway, because one most often sends integer sensor data. There is also a problem that float variables can have different representations on different computers.

Receive an integer:

    int data=0;
    uint8_t datalen = sizeof(int);
    if (driver.recv((uint8_t *) &data, &datalen)) // Non-blocking
    {
      Serial.println(data);         
    }

That did it. jremington, you're the man!!! I already changed the variable to an integer before this as shown:

int iT1
float T1 = 123.45
iT1 = T1;
  rf_driver.send((uint8_t *) &iT1, sizeof(int));
  rf_driver.waitPacketSent();
  delay(200);

On the receive side:

int iTr;
void loop() {
   // Set buffer to size of expected message
    uint8_t buflen = sizeof(int);
    
    // Check if received packet is correct size
    if (rf_driver.recv((uint8_t *) &iTr, &buflen))
    {
      // Message received with valid checksum      
      Serial.println("Message Received: ");
      Serial.println(iTr); 
    }
}

The integer represents temperature and I'm going to display the integer onto an lcd display, so I will need to convert iTr to a string at some point. But I could figure that out on my own.Thank you, thank you, thank you. I promise I'll try to figure out this pointer business so I don't have to ask for help again.

Dave