Combine integer, float and character string whith snprintf

Post Reply
lpzb
Posts: 7
Joined: Tue Dec 17, 2019 10:49 am
Company: Tragsatec

Combine integer, float and character string whith snprintf

Post by lpzb » Fri Dec 27, 2019 8:05 am

I am trying to make a record with this format by means of snprintf function.

yymmdd hh:mm:dd,float_data,float_data

To achieve this I am using the following code line:

snprintf(dateBuffer, sizeof(dateBuffer) , "%02u%02u%02u %02u:%02u:%02u,%s,%s",
RTC.year,
RTC.month,
RTC.date,
RTC.hour,
RTC.minute,
RTC.second,
clevel,
cvolts);

Previusly I have shifted the float numbers to characters using the Utils.float2String function.

But I obtain this output:

090629 10:15:54,12.4709062

It seems, it's not possible to read the last float number, and instead of it appears the begining of the date.

The entire code is the next:

Code: Select all

char recordBuffer[11];
char dateBuffer[27];

char clevel[5];
char cvolts[5];

float fval;
float fvolts;

void setup()
{
  RTC.ON();
  USB.println("Begining");
  SD.ON();
  read4_20mA();
  battery();
  writeData();
  USB.println("End");

}

void loop(){}

void read4_20mA()
{
  //fval = 14.45877;
  fval = (3.22 + 7.25)-((10 - 0)/16)* 3.5 - ((5 * 0 - 10)/4);
  Utils.float2String(fval,clevel,2);
  USB.print("Level ");
  USB.println(clevel);
 
}

void battery(){
  
  Utils.float2String(PWR.getBatteryVolts(),cvolts,2);
  USB.print("Battery ");
  USB.println(cvolts);
  snprintf(recordBuffer, sizeof(recordBuffer), "%s,%s,", clevel,cvolts);
  USB.print("recordBuffer: ");
  USB.println(recordBuffer);
}

void writeData()
{
  RTC.setTime("09:06:29:02:10:15:54");
  snprintf(dateBuffer, sizeof(dateBuffer) , "%02u%02u%02u %02u:%02u:%02u,%s,%s", 
                            RTC.year, 
                            RTC.month, 
                            RTC.date,
                            RTC.hour,
                            RTC.minute,
                            RTC.second,
                            clevel, 
                            cvolts);                             
  USB.print("dateBuffer: ");
  USB.println(dateBuffer);
  
}

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: Combine integer, float and character string whith snprintf

Post by libelium-dev » Fri Dec 27, 2019 10:10 am

Hi,

It seems a memory problem, usually, they are related to the length of the variables.

Could you please copy here the complete output of the code?

Also, please try to convert from float to string using the dtostrf function and increase one byte the size of the char variables.

Code: Select all

  // define local buffer for float to string conversion
  char float_str[10];
  
  // use dtostrf() to convert from float to string: 
  // '1' refers to minimum width
  // '3' refers to number of decimals
  dtostrf( float_val, 1, 3, float_str);
Regards

lpzb
Posts: 7
Joined: Tue Dec 17, 2019 10:49 am
Company: Tragsatec

Re: Combine integer, float and character string whith snprintf

Post by lpzb » Fri Dec 27, 2019 10:27 am

Hello.
Here is the output of de code:

J#
Begining
Level 12.47
Battery 4.25
recordBuffer: 12.47,4.25
dateBuffer: 090629 10:15:54,12.4709062
End

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: Combine integer, float and character string whith snprintf

Post by libelium-dev » Fri Dec 27, 2019 10:50 am

Hi,

Increasing the clevel length to 6 the code should work.

Code: Select all

char clevel[6];
Also, we recommend increasing all variables to make sure the data fit.

Code: Select all

char recordBuffer[14];
char dateBuffer[30];

char clevel[6];
char cvolts[6];
Regards

lpzb
Posts: 7
Joined: Tue Dec 17, 2019 10:49 am
Company: Tragsatec

Re: Combine integer, float and character string whith snprintf

Post by lpzb » Fri Dec 27, 2019 4:18 pm

Thank you very much, now it works fine.

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: Combine integer, float and character string whith snprintf

Post by libelium-dev » Mon Dec 30, 2019 9:22 am

Glad to hear the problem was solved! :D

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest