I'm trying to parse many variables into a URL so that I can update a website through a simple HTTP API with sensor data. After talking to a buddy of mine, he suggested going with the code below.

When I put this in, the Arduino Yun boots up. I see the L13 red LED turn on, but then it hoses. When I comment out the sprintf(); line, everything works fine — except no URL is parsed.

I've tried to debug to the best of my ability and am looking for thoughts on what may be happening.

I'm not sure how much code to post without posting the whole 130 line sketch, so I'm just dumping the parts from the loop that is trying to piece together the URL.

  // Get update time
  Process date;
  date.runShellCommand("date --utc \"+%Y-%m-%d%%20%H:%M:%S\"");
  String utcDateString = "";
  while (date.available()) {
    char d = date.read();
    utcDateString += d;

      char url[512];
      float humid, baro, windspeed, raingauge;
      int winddir;
      Serial.print("humid: ");
        Serial.print("baro: ");
        Serial.print("windspeed: ");
        Serial.print("raingauge: ");
        Serial.print("winddir: ");
//         utcDateString,Thermistor(analogRead(0)),humid,baro,winddir,windspeed,raingauge);
        Serial.print("url: ");

double Thermistor(int RawADC) {
  double Temp;
  Temp = log(11100.0/((1024.0/RawADC-1)));  // Was 10000... Manually dialed down to increase accuracy. 9400 is a good value. Changing again to match wunderground
  Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
  Temp = Temp - 273.15;            // Convert Kelvin to Celcius
  Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
  return Temp;

Here's the output from those Serial.print() calls:

humid: 0.00
baro: 0.00
windspeed: 0.00
raingauge: 0.00
winddir: 0

I'm not sure how else to debug this because when I uncomment sprintf(); the Yun locks up and I have to press the 32U4 Reset button and power cycle the Arduino to get it to work again.

2 Answers 2


This variable:

String utcDateString = "";

Is a C++ String, not a character array like sprintf() is expecting. To convert this string to a character array such that sprintf is expecting, you must use .c_str() in your sprintf, i.e.:

sprintf(foo,"This is a format for %s", utcDateString.c_str());

Doing this should prevent the sprintf line from causing a memory fault.

  • Well spotted. I was surprised to see that the Arduino's String class actually supports c_str() now. I'm fairly sure it didn't support it in the past, and it doesn't seem to be documented. Feb 28, 2014 at 16:42
  • Yep, that seems to have cleared it up
    – Pat
    Feb 28, 2014 at 16:43

Looks like %f isn't wired up for performance reasons. You can convert floats to strings first using dtostrf().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.