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.
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.Peter Bloomfield– Peter Bloomfield2014年02月28日 16:42:17 +00:00Commented Feb 28, 2014 at 16:42 -
Yep, that seems to have cleared it upPat– Pat2014年02月28日 16:43:28 +00:00Commented 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()
.