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