Skip to main content
Arduino

Return to Question

Tweeted twitter.com/StackArduino/status/755395437767655428

sprintf() to assemble String and float pieces locks up Arduino Yun

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.

 // 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.prints();Serial.print() calls:

sprintf() locks up Arduino Yun

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.

 // 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.prints();

sprintf() to assemble String and float pieces locks up

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.

 // 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:

added 255 characters in body
Source Link
Pat
  • 217
  • 1
  • 5
  • 10
 // 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;
}
 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;
}
 // 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;
}
Source Link
Pat
  • 217
  • 1
  • 5
  • 10

sprintf() locks up Arduino Yun

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.

 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.prints();

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.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /