|
9 | 9 |
|
10 | 10 | #define UPDATE_PERIOD_IN_MILLISECONDS 2000 |
11 | 11 |
|
| 12 | +#define TIMESTAMP_LENGTH 20 |
| 13 | +#define LOCATION_LENGTH 18 |
| 14 | +#define TEMPERATURE_LENGTH 21 |
| 15 | + |
12 | 16 | // For hardware serial 1 (recommended): |
13 | 17 | // GPS TX to Arduino Due Serial1 RX pin 19 |
14 | 18 | // GPS RX to Arduino Due Serial1 TX pin 18 |
@@ -75,7 +79,7 @@ void setupThermocouples(void) { |
75 | 79 | SPI.begin(CS1); |
76 | 80 | SPI.begin(CS2); |
77 | 81 |
|
78 | | - Serial.println('Thermocouples initialized.'); |
| 82 | + Serial.println("Thermocouples initialized."); |
79 | 83 | } |
80 | 84 |
|
81 | 85 | void setupXBee(void) { |
@@ -160,20 +164,29 @@ void loop() |
160 | 164 | if (millis() - timer > UPDATE_PERIOD_IN_MILLISECONDS) { |
161 | 165 | timer = millis(); // reset the timer |
162 | 166 |
|
163 | | - int chars_formatted = 0; |
164 | | - char timestamp[25]; |
165 | | - char location[25]; |
166 | | - char temperatures[25]; |
| 167 | + int timestamp_len = 0; |
| 168 | + int location_len = 0; |
| 169 | + int temperature_len = 0; |
| 170 | + int output_len = 0; |
| 171 | + |
| 172 | + char timestamp[TIMESTAMP_LENGTH + 1]; |
| 173 | + char location[LOCATION_LENGTH + 1]; |
| 174 | + char temperatures[TEMPERATURE_LENGTH + 1]; |
167 | 175 | char output[100]; |
168 | 176 |
|
169 | | - chars_formatted = sprintf(timestamp, "20%02d-%02d-%02dT%02d:%02d:%02dZ", GPS.year, GPS.month, GPS.day, GPS.hour, GPS.minute, GPS.seconds); |
| 177 | + timestamp_len = sprintf(timestamp, "20%02d-%02d-%02dT%02d:%02d:%02dZ", GPS.year, GPS.month, GPS.day, GPS.hour, GPS.minute, GPS.seconds); |
170 | 178 | if(GPS.fix) { |
171 | | - chars_formatted = sprintf(location, "%4f%c, %4f%c", GPS.latitude, GPS.lat, GPS.longitude, GPS.lon); |
| 179 | + location_len = sprintf(location, "%4f%c | %4f%c", GPS.latitude, GPS.lat, GPS.longitude, GPS.lon); |
| 180 | + } else { |
| 181 | + location_len = sprintf(location, "Location | unknown"); |
| 182 | + } |
| 183 | + temperature_len = sprintf(temperatures, "%03.2f | %03.2f | %03.2f", readThermocouple(CS0), readThermocouple(CS1), readThermocouple(CS2)); |
| 184 | + |
| 185 | + if(timestamp_len == TIMESTAMP_LENGTH && location_len == LOCATION_LENGTH && temperature_len == TEMPERATURE_LENGTH) { |
| 186 | + output_len = sprintf(output, "%s | %s | %s\r\n", timestamp, location, temperatures); |
172 | 187 | } else { |
173 | | - chars_formatted = sprintf(location, "Location unknown"); |
| 188 | + output_len = sprintf(output, "Something wrong with buffer lengths: %d, %d, %d\n", timestamp_len, location_len, temperature_len); |
174 | 189 | } |
175 | | - chars_formatted = sprintf(temperatures, "%03.2f, %03.2f, %03.2f", readThermocouple(CS0), readThermocouple(CS1), readThermocouple(CS2)); |
176 | | - chars_formatted = sprintf(output, "%s | %s | %s\n", timestamp, location, temperatures); |
177 | 190 | Serial.print(output); |
178 | 191 | Serial2.print(output); // send to XBee |
179 | 192 | } |
|
0 commit comments