Post a new topicPost a reply Page 1 of 1   [ 6 posts ]
Author Message
 Post subject: Error in standard routine?
PostPosted: Sun May 16, 2010 7:51 am 

Joined: Sat May 08, 2010 11:49 am
Posts: 26
Location: Bombay
I think there's some error in the "sprintf" routine or is it something else. I have written the following code and also have provided a typical output below. The problem is that, when I simply print values using Xbee.println() or for that sake even USB.println() the values are printed without any fuss. But, when I make a string "GPSData" and "sendData" using sprintf , It doesn't print the values.

Am I doing some silly mistake?


-----------------CODE--------------------------------
char* getSensorValues()
{
int GPS_Error=1;
int GPS_Tries=0;
const char* sep ="@@";

char sendData[100];

// open the uart
GPS.begin();

// Inits the GPS module
GPS.init();
//** Obtain GPS stuffs **
XBee.println("Checking GPS...");
while((!GPS.check()) && (GPS_Tries<35)){
XBee.println(GPS_Tries);
delay(1000);
GPS_Tries+=1;
}

if(GPS_Tries<35)
{
XBee.println("GPS Check Successful :-)");
GPS_Error=0;
}
else
XBee.println("GPS Check Not Successful :-(");

if(!GPS_Error)
{
if(GPS.flag != GPS_TIMEOUT) GPS.getLatitude();
if(GPS.flag != GPS_TIMEOUT) GPS.getLongitude();
if(GPS.flag != GPS_TIMEOUT) GPS.getAltitude();
if(GPS.flag != GPS_TIMEOUT) GPS.getDate();
if(GPS.flag != GPS_TIMEOUT) GPS.getTime();
}

XBee.print("Lat: ");
XBee.println(GPS.latitude);
XBee.print("Lon: ");
XBee.println(GPS.longitude);
XBee.print("Alt: ");
XBee.println(GPS.altitude);
XBee.print("date: ");
XBee.println(GPS.dateGPS);
XBee.print("time: ");
XBee.println(GPS.timeGPS);


// Printing a message, remember to open 'Serial Monitor' to be able to see this message

XBee.println("Online...sending Temperature after 2s");
delay(2000);
int temperature_value=RTC.getTemperature();
XBee.println(temperature_value);


XBee.println("Online...sending Humidity after 2s");
delay(2000);
float humidity_value=SensorGas.readValue(SENS_HUMIDITY);
XBee.println(humidity_value);


XBee.println("Online...sending atmospheric pressure after 40ms");
SensorGas.setSensorMode(SENS_ON,SENS_PRESSURE);
delay(40);
float atp_value=SensorGas.readValue(SENS_PRESSURE);
SensorGas.setSensorMode(SENS_OFF, SENS_PRESSURE);
XBee.println(atp_value);


// Printing a message, remember to open 'Serial Monitor' to be able to see this message
XBee.println("Online...sending CO2 sensor data after 10 seconds");
SensorGas.configureSensor(SENS_CO2, 10);
SensorGas.setSensorMode(SENS_ON, SENS_CO2);
delay(10000);
float CO2_value = SensorGas.readValue(SENS_CO2);
SensorGas.setSensorMode(SENS_OFF, SENS_CO2);
XBee.println(CO2_value);

XBee.println("Online...sending Oxygen sensor data after 10 seconds");
SensorGas.configureSensor(SENS_O2, 50);
SensorGas.setSensorMode(SENS_ON, SENS_O2);
delay(10000);
float O2_value = SensorGas.readValue(SENS_O2);
SensorGas.setSensorMode(SENS_OFF,SENS_O2);
XBee.println(O2_value);

int sizeGPS = strlen(GPS.latitude) +strlen(GPS.longitude) + strlen(GPS.altitude) + strlen(GPS.dateGPS) + strlen(GPS.timeGPS);
char* GPSData = (char*)calloc(1,sizeGPS + 16); //16 bytes for separator

if(!GPS_Error)
sprintf(GPSData,"%s%s%s%s%s%s%s%s%s",GPS.latitude,sep,GPS.longitude,sep,GPS.altitude,sep,GPS.dateGPS,sep,GPS.timeGPS);
sprintf(sendData,"%d%s%d%s%.2f%s%.2f%s%.2f%s%.2f%s%s",GPS_Error,sep,temperature_value,sep,humidity_value,sep,atp_value,sep,CO2_value,sep,O2_value,sep,GPSData);

GPS.close();
XBee.print("Msg from getValueSensors: ");
XBee.println(sendData);

free(GPSData);
return sendData;

}


---------Test OUTPUT--------------------------------
Checking GPS...
0
1
2
3
4
5
6
7
8
9
10
11
12
13
GPS Check Successful :-)
Lat: 1729.6429
Lon: 07808.4917
Alt: 596.1
date: 160510
time: 052042.000
Online...sending Temperature after 2s
39
Online...sending Humidity after 2s
1.0677418708
Online...sending atmospheric pressure after 40ms
2.1193547248
Online...sending CO2 sensor data after 10 seconds
1.6838708877
Online...sending Oxygen sensor data after 10 seconds
0.2193548440
Msg from getValueSensors: 0@@39@@ // ERROR??? Where are other values


Top
 Profile  
 
 Post subject: Re: Error in standard routine?
PostPosted: Sun May 16, 2010 8:43 am 

Joined: Sat May 08, 2010 11:49 am
Posts: 26
Location: Bombay
Kindly ignore the code and output of the previous post and consider the following
[Its the updated one, with minor changes] :

The issue is with the floating type.

-------------CODE-------------------------------------
char* getSensorValues()
{
int GPS_Error=1;
int GPS_Tries=0;

char sendData[100];

// open the uart
GPS.begin();

// Inits the GPS module
GPS.init();
//** Obtain GPS stuffs **
XBee.println("Checking GPS...");
while((!GPS.check()) && (GPS_Tries<35)){
XBee.println(GPS_Tries);
delay(1000);
GPS_Tries+=1;
}

if(GPS_Tries<35)
{
XBee.println("GPS Check Successful :-)");
GPS_Error=0;
}
else
XBee.println("GPS Check Not Successful :-(");

if(!GPS_Error)
{
if(GPS.flag != GPS_TIMEOUT) GPS.getLatitude();
if(GPS.flag != GPS_TIMEOUT) GPS.getLongitude();
if(GPS.flag != GPS_TIMEOUT) GPS.getAltitude();
if(GPS.flag != GPS_TIMEOUT) GPS.getDate();
if(GPS.flag != GPS_TIMEOUT) GPS.getTime();
}

XBee.print("Lat: ");
XBee.println(GPS.latitude);
XBee.print("Lon: ");
XBee.println(GPS.longitude);
XBee.print("Alt: ");
XBee.println(GPS.altitude);
XBee.print("date: ");
XBee.println(GPS.dateGPS);
XBee.print("time: ");
XBee.println(GPS.timeGPS);


// Printing a message, remember to open 'Serial Monitor' to be able to see this message

XBee.println("Online...sending Temperature after 2s");
delay(2000);
int temperature_value=RTC.getTemperature();
XBee.println(temperature_value);


XBee.println("Online...sending Humidity after 2s");
delay(2000);
float humidity_value=SensorGas.readValue(SENS_HUMIDITY);
XBee.println(humidity_value);


XBee.println("Online...sending atmospheric pressure after 40ms");
SensorGas.setSensorMode(SENS_ON,SENS_PRESSURE);
delay(40);
float atp_value=SensorGas.readValue(SENS_PRESSURE);
SensorGas.setSensorMode(SENS_OFF, SENS_PRESSURE);
XBee.println(atp_value);


// Printing a message, remember to open 'Serial Monitor' to be able to see this message
XBee.println("Online...sending CO2 sensor data after 10 seconds");
SensorGas.configureSensor(SENS_CO2, 10);
SensorGas.setSensorMode(SENS_ON, SENS_CO2);
delay(10000);
float CO2_value = SensorGas.readValue(SENS_CO2);
SensorGas.setSensorMode(SENS_OFF, SENS_CO2);
XBee.println(CO2_value);

XBee.println("Online...sending Oxygen sensor data after 10 seconds");
SensorGas.configureSensor(SENS_O2, 50);
SensorGas.setSensorMode(SENS_ON, SENS_O2);
delay(10000);
float O2_value = SensorGas.readValue(SENS_O2);
SensorGas.setSensorMode(SENS_OFF,SENS_O2);
XBee.println(O2_value);

int sizeGPS = strlen(GPS.latitude) +strlen(GPS.longitude) + strlen(GPS.altitude) + strlen(GPS.dateGPS) + strlen(GPS.timeGPS);
char* GPSData = (char*)calloc(1,sizeGPS + 16); //16 bytes for separator

if(!GPS_Error)
sprintf(GPSData,"%s%s%s%s%s%s%s%s%s",GPS.latitude,sep,GPS.longitude,sep,GPS.altitude,sep,GPS.dateGPS,sep,GPS.timeGPS);

XBee.print("GPSData is:");
XBee.println(GPSData);

sprintf(sendData,"%d%s%d%s%.2f%s%.2f%s%.2f%s%.2f%s%s",GPS_Error,sep,temperature_value,sep,humidity_value,sep,atp_value,sep,CO2_value,sep,O2_value,sep,GPSData);

GPS.close();
XBee.print("Msg from getValueSensors: ");
XBee.println(sendData);

free(GPSData);
return sendData;

}


-----------------------------TYPICAL OUTPUT--------------------------------------
Checking GPS...
0
1
2
3
4
5
6
7
GPS Check Successful :-)
Lat: 1729.6382
Lon: 07808.4910
Alt: 599.8
date: 160510
time: 061226.887
Online...sending Temperature after 2s
48
Online...sending Humidity after 2s
0.9451612472
Online...sending atmospheric pressure after 40ms
2.0838711261
Online...sending CO2 sensor data after 10 seconds
1.5225806236
Online...sending Oxygen sensor data after 10 seconds
0.2096774101
GPSData is:1729.6382@@07808.4910@@599.8@@160510@@061226.887
Msg from getValueSensors: 0@@48@@?@@?@@?@@?@@1729.6382@@07808.4910@@599.8@@160510@@061226.887 //ERROR!!!


Top
 Profile  
 
 Post subject: Re: Error in standard routine?
PostPosted: Mon May 17, 2010 10:36 am 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7813
Hi,

'sprintf' function has several problems due to the AVR compiler. You have to consider:

1 - 'sprintf' can be only used once inside a function. If you need to use it several times, you should declare an auxiliary function.

2 - Don't declare GPSData using 'calloc', declare it using static memory. 'sprintf' does not handle well the variables declared as dynamic memory

Regards


Top
 OnlineProfile  
 
 Post subject: Re: Error in standard routine?
PostPosted: Mon May 17, 2010 10:59 am 

Joined: Sat May 08, 2010 11:49 am
Posts: 26
Location: Bombay
But the issue is only with the floating type.

Also, could you cite an example , on how to use sprintf multiple times using auxiliary function.

Regards,
RKM


Top
 Profile  
 
 Post subject: Re: Error in standard routine?
PostPosted: Mon May 17, 2010 11:04 am 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7813
Hi,

Yes, you are right, the 'FLOAT' type can not be used with 'sprintf'. You should convert the number to string type or other before using 'sprintf'.

Code:
void loop(){
  char data[100];
  char data2[100];
 
  sprintf(data,"Test example: %d %c", 8, '\n');
  anotherSprintf();
}

void anotherSprintf(){

  sprintf(data2, "Test example 2: %d %c", 7, '\n');
}


Regards


Top
 OnlineProfile  
 
 Post subject: Re: Error in standard routine?
PostPosted: Mon May 17, 2010 12:15 pm 

Joined: Sat May 08, 2010 11:49 am
Posts: 26
Location: Bombay
Quote:
1 - 'sprintf' can be only used once inside a function. If you need to use it several times, you should declare an auxiliary function.

2 - Don't declare GPSData using 'calloc', declare it using static memory. 'sprintf' does not handle well the variables declared as dynamic memory


I don't quite understand, why sprintf only once.

Could I get the waspmote ide code, so that I can look into the matter.
Because, these 2 issues seems to be totally indigestable.[atleast for now]

I had sent a formal request for the waspmote ide source. But , no response yet. You might like to look at this link:

http://www.libelium.com/forum/viewtopic.php?f=14&t=86


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 6 posts ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Libelium theme based on 610nm Style by Daniel St. Jules of http://www.gamexe.net


© Libelium Comunicaciones Distribuidas S.L. | Terms of use