Lora THings Plus Explorable print float error

See my original post for reference. https://forum.arduino.cc/t/string-print … ple/880775

I still have not been able to solve this (using arduino IDE 1.8.12 and sparkfun apollo 3 boards version 2.1.1) . The file i am testing it on is under file->examples-> strings-> string constructors

I tried modifying compiler flags, which did not work. I tried using dtostrf with the following code

#include “api/deprecated-avr-comp/avr/dtostrf.h”

float f = 3.14159;

char floatString[10];

dtostrf(f,4,2,floatString);

char derpy[50];

sprintf(derpy, “myFloat in string is = %s\n”, floatString);

Serial.println(derpy);

Which still printed out myFloat in string is = %4.2f

I tried using the PrintEx Library v1.2, however that does not work i get a huge compilation error out of the box with the default example Printex-> basic usage in the attached file.

I am at a loss of ideas I need to print floats to a terminal as well as put a float to a character array with this board to send over the lora radio with the function?

radio.transmit((const char*) &txPacket[0]);

printex error.txt (10.1 KB)

this is a known issue with MBED. It is using a limited / minimal version of (s)(n) printf() that does NOT support float. https://github.com/sparkfun/Arduino_Apollo3/issues/278. Even with a workaround indicated in the post, you can still not format to 4.2f

I figured out a workaround for it.

Hi,

I have the same problem, want to send string with double or float but doesn’t work with the Sparkfun Explorable Apollo3 on Arduino IDE 2.

also made the modification of the printf suggested on github but the problem with the String in “radio.transmit()” remains.

it only sends the format (e.g. %4.2f) of a float or double variable.

Not clear for me if this is a limitation of the radiolib library or the Apollo3 micro…

conveniently the “Sparkfun Sending Sensor Data over LoRa” tutorial uses only integer variables…

quite disappointing that in this years this wasn’t addressed, also for a so pricey board.

anyway @acw210ee what was your workaround?

Regards

Dan

now I created my workaround, just a function which splits a float into two integer and then I concatenate them in a new string.

Probably isn’t perfect but works. in case someone could make it better and integrate in the library.

I emitted all the other code parts.

//functions to split floats into two integer
String first_p (float myFloat){
  int firstPart = (int)myFloat;
  String str_first = String(firstPart);
  return str_first ;
}

String second_p (float myFloat){
  int a = (int)myFloat;
  int secondPart = (myFloat -a)*100 ;
  secondPart=secondPart+0.6; //in case it needs rounding, probably not the best solution
  String str_second = String(secondPart);
  //in case the second part has a leading 0
  if (secondPart < 10){  
    str_second ="0"+String(secondPart);
    }
  return str_second ;
  }

//in void loop() {

  ...
  String str_first = first_p(float_val);
  String str_second = second_p(float_val); 
  String new_val = String(str_first+ "."+ str_second);

  String myData = "Val= " + new_val;
  Serial.println(myData);
  int state = radio.transmit(myData);