4

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];
 url[0]='0円';
 float humid, baro, windspeed, raingauge;
 int winddir;
 Serial.print("humid: ");
 Serial.println(humid);
 Serial.print("baro: ");
 Serial.println(baro);
 Serial.print("windspeed: ");
 Serial.println(windspeed);
 Serial.print("raingauge: ");
 Serial.println(raingauge);
 Serial.print("winddir: ");
 Serial.println(winddir);
 //sprintf(url,"http://weatherstation.wunderground.com/weatherstation/updateweatherstation.php?ID=123456&PASSWORD=xxxxxx&dateutc=%s&temp=%2.2f&humidity=%2.2f&baromin=%f&winddir=%d&windspeedmph=%3.2f&rainin=%3.2f&softwaretype=Custom%%20Arduino&action=updateraw",
// utcDateString,Thermistor(analogRead(0)),humid,baro,winddir,windspeed,raingauge);
 Serial.print("url: ");
 Serial.println(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
url:

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.

asked Feb 27, 2014 at 23:29

2 Answers 2

7

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.

answered Feb 28, 2014 at 16:14
2
  • 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. Commented Feb 28, 2014 at 16:42
  • Yep, that seems to have cleared it up Commented Feb 28, 2014 at 16:43
3

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

sachleen
7,5655 gold badges40 silver badges57 bronze badges
answered Feb 28, 2014 at 0:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.