-
Notifications
You must be signed in to change notification settings - Fork 0
/
balloon.ino
534 lines (404 loc) · 12.8 KB
/
balloon.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
Columbia Space Initiative High-Altitude Balloons Code
*/
#include <SPI.h> // Reading time into SD card with RTC real time clock
#include <SD.h>
#include <Wire.h> // needed for all I2c protocols
#include <RTClib.h>
#include "DFRobot_OxygenSensor.h" //oxygen sensor lib
#include <Adafruit_LSM9DS1.h> //gyro / accelerator sensor I2C
#include <Adafruit_Sensor.h> // not used in this code, but required!
#include <Adafruit_SGP30.h> //SGP sensor
#include "Adafruit_BME680.h" //humidity sensor
#include "Adafruit_SI1145.h" //UV light sensor
#include <OneWire.h>
#include <DallasTemperature.h> //extern temp probe
////////////////////////////////////////////////////////////////////////////////////////////////////
#define Oxygen_IICAddress ADDRESS_3 //default slave address for I2C is ADDRESS_3 0x73
#define COLLECT_NUMBER 10 // collect number, the collection range is 1-100.
#define ALTITUDE 300.0 // change to match correct initial altitude!!!! 984 in ft
#define ONE_WIRE_BUS 47 //extern temp probe port
//initializing sensor objects
DFRobot_OxygenSensor oxygen;
Adafruit_LSM9DS1 lsm = Adafruit_LSM9DS1();
Adafruit_SGP30 sgp;
RTC_DS3231 rtc; //creating realtime clock and file objects
File dataFile;
Adafruit_BME680 bme; // I2C
Adafruit_SI1145 uv = Adafruit_SI1145();
OneWire oneWire(ONE_WIRE_BUS); //for extern temp sensor
DallasTemperature EOsensor(&oneWire); // Pass our oneWire reference to Dallas Temperature.
const int chipSelect = 53; // SD card module CS pin
////////////////////////////////////////////////////////////////////////////////////////////////////
#include <math.h>
#include <stdio.h>
#include "definitions.h"
#include "tone_generator.h"
#include <Adafruit_GPS.h>
#define GPSSerial Serial3
Adafruit_GPS GPS(&GPSSerial);
bool toneValue = _2400; // Define toneValue in the main sketch
#define _FLAG 0x7e
#define _CTRL_ID 0x03
#define _PID 0xf0
#define _DT_EXP ','
#define _DT_STATUS '>'
#define _DT_POS '!'
// == APRS information ==
const char *mycall = "KE2CZX";
char myssid = 11;
// destination, APRS with SSID 0 is usually ok (Trackuino)
const char *dest = "APRS";
// The recommended digi path for a balloon is WIDE2-1 or pathless. (Trackuino); http://wa8lmf.net/DigiPaths/
const char *digi = "WIDE2";
char digissid = 1;
const char *mystatus = "CSI HAB";
char lat[9] = "0000.00S";
char lon[10] = "00000.00E";
const char sym_ovl = 'H';
const char sym_tab = 'a';
char bit_stuff = 0;
unsigned short crc = 0xffff;
// == Transmission function definitions ==
void send_char_NRZI(unsigned char in_byte, bool enBitStuff);
void send_string_len(const char *in_string, int len);
void calc_crc(bool in_bit);
void send_crc(void);
void send_packet();
void send_flag(unsigned char flag_len);
void send_header();
void send_payload();
void set_io(void);
void print_code_version(void);
void print_debug();
////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
print_code_version();
EOsensor.begin();
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
} else {
Serial.println("RTC OK");
}
while (!oxygen.begin(Oxygen_IICAddress)) {
Serial.println("I2c device number error (oxygen) !");
delay(1000);
}
Serial.println("Oxygen OK");
//gyro accel. setup
if (!lsm.begin()) {
Serial.println("LSM9DS1 9DOF failed to initialize");
}
Serial.println("LSM9DS1 9DOF OK");
lsm.setupAccel(lsm.LSM9DS1_ACCELRANGE_2G, lsm.LSM9DS1_ACCELDATARATE_10HZ);
lsm.setupMag(lsm.LSM9DS1_MAGGAIN_4GAUSS);
lsm.setupGyro(lsm.LSM9DS1_GYROSCALE_245DPS);
// SGP sensor initialization
if (!sgp.begin()) {
Serial.println("SGP30 failed to initialize");
} else {
Serial.println("SGP30 OK");
}
// Humidity sensor setup
if (!bme.begin()) {
Serial.println("Could not find a valid BME680 sensor, check wiring!");
} else {
Serial.println("BME680 OK");
}
// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320*C for 150 ms
// UV sensor setup
if (!uv.begin()) {
Serial.println("Si1145 (UV sensor) failed to initialize");
} else {
Serial.println("Si1145 (UV sensor) OK");
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
if (!SD.begin(chipSelect)) {
Serial.println("SD card initialization failed!");
return;
}
dataFile = SD.open("data.csv", FILE_WRITE);
if (dataFile) {
dataFile.println("Internal Temperature (Celcius),External Temperature (Celcius),Pressure (Pa), Humidity (%),Oxygen Concentration (% vol),X-axis Acceleration (m/s^2),Y-axis Acceleration (m/s^2),Z-axis Acceleration (m/s^2),X-axis Mag. Flux (uT),Y-axis Mag. Flux (uT),Z-axis Mag. Flux (uT),X-axis Angular Velocity (rad/s),Y-axis Angular Velocity (rad/s),Z-axis Angular Velocity (rad/s), equivalent CO2 conc. (ppm), total volatile organic compounds conc. (ppb), UVindex, Year,Month,Day,Hour,Minute,Second,Lat,Lon, GPS Altitude");
dataFile.close();
} else {
Serial.println("Error opening data.csv");
}
Serial.println("Finished initializing sensors");
jacobSetup(); // GPS setup
}
uint32_t timer = millis();
uint32_t logTimer = millis();
void loop() {
// Reading from GPS
char c = GPS.read();
// constantly check if there is new GPS data
if (GPS.newNMEAreceived()) {
// Serial.println("new NMEA recieved");
GPS.parse(GPS.lastNMEA()); // Parse the received NMEA sentence; removed error checking
}
// Log data every SENSORLOG_FREQ_SEC seconds
if (millis() - logTimer > 1000 * SENSORLOG_FREQ_SEC) {
logTimer = millis();
dataFile = SD.open("data.csv", FILE_WRITE);
if (dataFile) {
writeOutData();
dataFile.close();
} else {
Serial.println("Error opening data.csv");
}
}
// Make an APRS transmission every SENSORLOG_FREQ_SEC seconds
if (millis() - timer > 1000 * TRANSMIT_FREQ_SEC) {
timer = millis();
delay(75);
digitalWrite(PTT_PIN, HIGH);
delay(100);
send_packet();
delay(75);
digitalWrite(PTT_PIN, LOW);
delay(75);
if (GPS.fix) { // Check if GPS fix is available
update_lat_lon(); // update variables
Serial.println(lon);
Serial.println(lat);
} else {
Serial.println("No GPS fix...");
}
}
}
void writeOutData() {
EOsensor.requestTemperatures();
float ExterntempC = EOsensor.getTempCByIndex(0);
Serial.print("Writing to SD Card...");
DateTime now = rtc.now();
float T = bme.readTemperature();
float P = bme.readPressure();
dataFile.print(T, DEC);
dataFile.print(',');
dataFile.print(ExterntempC, DEC);
dataFile.print(',');
dataFile.print(P, DEC);
dataFile.print(',');
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320*C for 150 ms
dataFile.print(bme.humidity, DEC);
dataFile.print(',');
float oxygenData = oxygen.getOxygenData(COLLECT_NUMBER);
dataFile.print(oxygenData, DEC);
dataFile.print(',');
lsm.read(); //gyro and acceleration readings
sensors_event_t accel, m, g, temp;
lsm.getEvent(&accel, &m, &g, &temp);
dataFile.print(accel.acceleration.x, DEC);
dataFile.print(',');
dataFile.print(accel.acceleration.y, DEC);
dataFile.print(',');
dataFile.print(accel.acceleration.z, DEC);
dataFile.print(',');
dataFile.print(m.magnetic.x, DEC);
dataFile.print(',');
dataFile.print(m.magnetic.y, DEC);
dataFile.print(',');
dataFile.print(m.magnetic.z, DEC);
dataFile.print(',');
dataFile.print(g.gyro.x, DEC);
dataFile.print(',');
dataFile.print(g.gyro.y, DEC);
dataFile.print(',');
dataFile.print(g.gyro.z, DEC);
dataFile.print(',');
sgp.IAQmeasure(); // gas sensor
uint16_t eCO2 = sgp.eCO2;
uint16_t TVOC = sgp.TVOC;
dataFile.print(eCO2, DEC);
dataFile.print(',');
dataFile.print(TVOC, DEC);
dataFile.print(',');
float UVindex = uv.readUV();
UVindex /= 100;
dataFile.print(UVindex, DEC);
dataFile.print(',');
dataFile.print(now.year(), DEC);
dataFile.print(',');
dataFile.print(now.month(), DEC);
dataFile.print(',');
dataFile.print(now.day(), DEC);
dataFile.print(',');
dataFile.print(now.hour() + 1, DEC);
dataFile.print(',');
dataFile.print(now.minute(), DEC);
dataFile.print(',');
dataFile.print(now.second(), DEC);
dataFile.print(',');
dataFile.print(lat);
dataFile.print(',');
dataFile.print(lon);
dataFile.print(',');
dataFile.print(GPS.altitude);
dataFile.println("");
Serial.println(" Finished writing data to SD card");
}
void calc_crc(bool in_bit) {
unsigned short xor_in;
xor_in = crc ^ in_bit;
crc >>= 1;
if (xor_in & 0x01)
crc ^= 0x8408;
}
void send_crc(void) {
unsigned char crc_lo = crc ^ 0xff;
unsigned char crc_hi = (crc >> 8) ^ 0xff;
send_char_NRZI(crc_lo, HIGH);
send_char_NRZI(crc_hi, HIGH);
}
void send_header() {
char temp;
temp = strlen(dest);
for (int j = 0; j < temp; j++)
send_char_NRZI(dest[j] << 1, HIGH);
if (temp < 6) {
for (int j = 0; j < (6 - temp); j++)
send_char_NRZI(' ' << 1, HIGH);
}
send_char_NRZI('0' << 1, HIGH);
/********* SOURCE *********/
temp = strlen(mycall);
for (int j = 0; j < temp; j++)
send_char_NRZI(mycall[j] << 1, HIGH);
if (temp < 6) {
for (int j = 0; j < (6 - temp); j++)
send_char_NRZI(' ' << 1, HIGH);
}
send_char_NRZI((myssid + '0') << 1, HIGH);
/********* DIGI ***********/
temp = strlen(digi);
for (int j = 0; j < temp; j++)
send_char_NRZI(digi[j] << 1, HIGH);
if (temp < 6) {
for (int j = 0; j < (6 - temp); j++)
send_char_NRZI(' ' << 1, HIGH);
}
send_char_NRZI(((digissid + '0') << 1) + 1, HIGH);
/***** CTRL FLD & PID *****/
send_char_NRZI(_CTRL_ID, HIGH);
send_char_NRZI(_PID, HIGH);
}
void send_payload() {
send_char_NRZI(_DT_POS, HIGH);
send_string_len(lat, strlen(lat));
send_char_NRZI(sym_ovl, HIGH);
send_string_len(lon, strlen(lon));
send_char_NRZI(sym_tab, HIGH);
send_char_NRZI(' ', HIGH);
send_string_len(mystatus, strlen(mystatus));
}
void send_char_NRZI(unsigned char in_byte, bool enBitStuff) {
bool bits;
for (int i = 0; i < 8; i++) {
bits = in_byte & 0x01;
calc_crc(bits);
if (bits) {
set_toneValue(toneValue);
bit_stuff++;
if ((enBitStuff) && (bit_stuff == 5)) {
toneValue ^= 1;
set_toneValue(toneValue);
bit_stuff = 0;
}
} else {
toneValue ^= 1;
set_toneValue(toneValue);
bit_stuff = 0;
}
in_byte >>= 1;
}
}
void send_string_len(const char *in_string, int len) {
for (int j = 0; j < len; j++)
send_char_NRZI(in_string[j], HIGH);
}
void send_flag(unsigned char flag_len) {
for (int j = 0; j < flag_len; j++)
send_char_NRZI(_FLAG, LOW);
}
void send_packet() {
print_debug();
send_flag(100);
crc = 0xffff;
send_header();
send_payload();
send_crc();
send_flag(3);
}
void print_code_version(void) {
Serial.println(" ");
Serial.println(" ");
Serial.print("Sketch: ");
Serial.println(__FILE__);
Serial.print("Uploaded: ");
Serial.println(__DATE__);
Serial.println(" ");
}
void print_debug() {
/****** MYCALL ********/
Serial.print(mycall);
Serial.print('-');
Serial.print(myssid, DEC);
Serial.print('>');
/******** DEST ********/
Serial.print(dest);
Serial.print(',');
/******** DIGI ********/
Serial.print(digi);
Serial.print('-');
Serial.print(digissid, DEC);
Serial.print(':');
/******* PAYLOAD ******/
Serial.print(_DT_POS);
Serial.print(lat);
Serial.print(sym_ovl);
Serial.print(lon);
Serial.print(sym_tab);
Serial.print(' ');
Serial.print(mystatus);
Serial.println(' ');
}
void update_lat_lon() {
Serial.println("Updating lat and lon variables");
float longitude = GPS.longitude;
float latitude = GPS.latitude;
char lonn[8];
char latt[7];
dtostrf(longitude, 6, 2, lonn);
dtostrf(latitude, 6, 2, latt);
snprintf(lon, sizeof(lon), "0%s%c", lonn, GPS.lon);
snprintf(lat, sizeof(lat), "%s%c", latt, GPS.lat);
}
void set_io(void) {
pinMode(PTT_PIN, OUTPUT);
pinMode(OUT_PIN, OUTPUT);
}
void jacobSetup() {
set_io();
Serial.print("Setting up GPS...");
GPS.begin(9600);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY); // RMC (lat, long, speed) and GGA data (# of sattelites, current altitude)
//GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // Set update rate to 1Hz (once per second)
GPS.sendCommand("$PMTK220,4000");
GPS.sendCommand(PGCMD_ANTENNA);
GPS.println(PMTK_Q_RELEASE);
Serial.println(" Finished setting up GPS.\nGoing to loop\n");
}