forked from urban-mobile-sensors/nUrveSensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnUrve_MEGA2560.ino
304 lines (267 loc) · 13 KB
/
nUrve_MEGA2560.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
// Basic Includes
#include "SoftwareSerial.h"
#include "Wire.h"
#include "SPI.h"
#include "Adafruit_Sensor.h" // Adafruit, standard library for all sensors
#include "SD.h" // We're using a special SD Library from Adafruit. More information can be found here: https://github.com/adafruit/SD
// Sensor Specific Includes
#include "Adafruit_HTU21DF.h" // Temperature and Humidity Sensor - HTU21D-F
#include "Adafruit_TSL2561_U.h" // Light Sensor - TSL2561
#include "Adafruit_ADXL345_U.h" // Accelerometer - ADXL345
#include "Adafruit_GPS.h" // GPS
//General vars
int IterationCounter = 0;
// DataLogger vars
const int chipSelect = 4;
File dataFile;
// GPS vars
#define GPSECHO false
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
Adafruit_GPS GPS(&Serial1);
// Sensor vars
Adafruit_HTU21DF htu = Adafruit_HTU21DF(); //HTUD21DF I2C Address: 0x40 (cannot be changed)
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345); //TSL2561 I2C Address: 0X39 (can be changed)
// Accelerometer vars
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345); // ADXL345 I2C Address: ????
// Define all the variables that we're going to need:
String GPS_Date = "0000-00-00";
String GPS_Time = "HH:MM:SS.MS";
float GPS_Lat = -9999;
float GPS_Lon = -9999;
float GPS_Altitude = -9999;
float GPS_Speed = -9999;
float GPS_Angle = -9999;
float GPS_Sats = -9999;
float GPS_Fix = -9999;
float GPS_Quality = -9999;
float AMB_Temp = -9999;
float AMB_Humd = -9999;
float AMB_Lux = -9999;
float AMB_Snd = -9999;
float RDQ_AcX = -9999;
float RDQ_AcY = -9999;
float RDQ_AcZ = -9999;
const int AMB_SND_sampleWindow = 500; // Sample window width in mS (50 mS = 20Hz)
unsigned int AMB_SND_sample;
void setup() {
// initialize serial and wait for the port to open:
Serial.begin(115200);
Serial.print("+-------------------------------------------------------------------------+\n");
Serial.print("| Urban Mobile Sensors, LLC - nUrve Sensor V1 |\n");
Serial.print("+-------------------------------------------------------------------------+\n");
Serial.print("| w: urbanmobilesensors.com | t: @urbansensors | e: [email protected] |\n");
Serial.print("+-------------------------------------------------------------------------+\n");
setupDataLogger(); // Set up Data Logger
setupGPS(); // Set up GPS
// setupComms(); // Set up Comms | PLACEHOLDER
setupAmbientTemp(); // Set up Ambient Sensor 1 - Temp & Humidity
setupAmbientLux(); // Set up Ambient Sensor 2 - Luminosity
setupAmbientSound(); // Set up Ambient Sensor 3 - Sound
setupRoadSensor(); // Set up Road Sensor
}
// ADDITIONAL CODE
// GPS
// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect) {
char c = GPS.read();
// if you want to debug, this is a good time to do it!
#ifdef UDR0
if (GPSECHO)
if (c) UDR0 = c;
// writing direct to UDR0 is much much faster than Serial.print
// but only one character can be written at a time.
#endif
}
void useInterrupt(boolean v) {
if (v) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
usingInterrupt = true;
} else {
// do not call the interrupt function COMPA anymore
TIMSK0 &= ~_BV(OCIE0A);
usingInterrupt = false;
}
}
uint32_t timer = millis();
// END OF GPS
// END OF ADDITIONAL CODE
void loop() {
IterationCounter ++;
Serial.print("+-------------------------------------------------------------------------+\n");
Serial.print("| - Start of Loop - |\n");
Serial.print("+-------------------------------------------------------------------------+\n");
Serial.print("| Iteration: #");Serial.println(IterationCounter);
Serial.print("+-------------------------------------------------------------------------+\n");
Serial.print("| Board Information: [TO DO]\n");
Serial.println("| ...: ");
Serial.print("+-------------------------------------------------------------------------+\n");
Serial.print("| Communication Data: [TO DO]\n");
Serial.print("| SSID: ");Serial.println("");
Serial.print("| RSSI: ");Serial.println("");
Serial.print("| Nets: ");Serial.println("");
// GPS CODE
if (! usingInterrupt) {
// read data from the GPS in the 'main loop'
char c = GPS.read();
// if you want to debug, this is a good time to do it!
if (GPSECHO)
if (c) Serial.print(c);
}
// if a sentence is received, we can check the checksum, parse it...
if (GPS.newNMEAreceived()) {
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
// if millis() or timer wraps around, we'll just reset it
if (timer > millis()) timer = millis();
// approximately every 2 seconds or so, print out the current stats
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
if (GPS.fix) {
// GPS_Date = Serial.println(GPS.year);
// GPS_Time = "HH:MM:SS.MS";
GPS_Lat = GPS.latitudeDegrees;
GPS_Lon = GPS.longitudeDegrees;
GPS_Speed = GPS.speed;
GPS_Angle = GPS.angle;
GPS_Altitude = GPS.altitude;
GPS_Sats = GPS.satellites;
}
}
// STILL TO DO HERE:
// 1) Get Date as a single properly formatted string YYYY-MM-DD
// 2) Get Time as a single properly formatted string HH:MM:SS.mmm
Serial.print("+-------------------------------------------------------------------------+\n");
Serial.print("| GPS Information:\n");
Serial.print("| Date: ");Serial.println(GPS_Date);
Serial.print("20");Serial.print(GPS.year, DEC);Serial.print(":");Serial.print(GPS.month, DEC);Serial.print(":");Serial.print(GPS.month, DEC);Serial.print(":");Serial.println(GPS.day, DEC);
Serial.print("| Time: ");Serial.println(GPS_Time);
Serial.print(GPS.hour, DEC); Serial.print(':');Serial.print(GPS.minute, DEC); Serial.print(':');Serial.print(GPS.seconds, DEC); Serial.print('.');Serial.println(GPS.milliseconds);
Serial.print("| Lat: ");Serial.println(GPS_Lat,6);
Serial.print("| Lon: ");Serial.println(GPS_Lon,6);
Serial.print("| Speed (knots): ");Serial.println(GPS_Speed,2);
Serial.print("| Altitude: ");Serial.println(GPS_Altitude,2);
Serial.print("| Angle: ");Serial.println(GPS_Angle,2);
Serial.print("| Sats: ");Serial.println(GPS_Sats,0);
Serial.print("| Fix: ");Serial.println(GPS.fix,2); // TO BE DONE
Serial.print("| Quality: ");Serial.println(GPS.fixquality,2); // TO BE DONE
// END OF GPS CODE
// =============================================================================================
// AMBIENT SENSORS
// =============================================================================================
// Ambient set temperature and humidity
AMB_Temp = htu.readTemperature();
AMB_Humd = htu.readHumidity();
// Ambient - Set Light Values
sensors_event_t event;
tsl.getEvent(&event);
if (event.light) {AMB_Lux = event.light;} else {AMB_Lux = -9998;}
// Ambient - Set Sound Values
unsigned long startMillis= millis(); // Start of sample window
unsigned int peakToPeak = 0; // peak-to-peak level
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
// collect data for 50 mS
while (millis() - startMillis < AMB_SND_sampleWindow)
{
AMB_SND_sample = analogRead(8);
if (AMB_SND_sample < 1024) // toss out spurious readings
{
if (AMB_SND_sample > signalMax)
{
signalMax = AMB_SND_sample; // save just the max levels
}
else if (AMB_SND_sample < signalMin)
{
signalMin = AMB_SND_sample; // save just the min levels
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
double volts = (peakToPeak * 3.3) / 1024; // convert to volts
AMB_Snd = volts;
// Output of data - For development purposes only
Serial.print("+-------------------------------------------------------------------------+\n");
Serial.print("| Ambient Information:\n");
Serial.print("| Temp: ");Serial.print(AMB_Temp,2);Serial.println("C");
Serial.print("| Humid: ");Serial.print(AMB_Humd,2);Serial.println("%");
Serial.print("| Lux: ");Serial.print(AMB_Lux,2);Serial.println("Lux");
Serial.print("| Sound: ");Serial.print(AMB_Snd);Serial.println("vlts");
// =============================================================================================
// ACCELEROMTER CODE
// =============================================================================================
accel.getEvent(&event);
RDQ_AcX = event.acceleration.x;
RDQ_AcY = event.acceleration.y;
RDQ_AcZ = event.acceleration.z;
// Display the results (acceleration is measured in m/s^2)
Serial.print("+-------------------------------------------------------------------------+\n");
Serial.print("| Accelerometer Information:\n");
Serial.print("| X: "); Serial.print(RDQ_AcX); Serial.print("m/s^2\n");
Serial.print("| Y: "); Serial.print(RDQ_AcY); Serial.print("m/s^2\n");
Serial.print("| Z: "); Serial.print(RDQ_AcZ); Serial.print("m/s^2\n");
// =============================================================================================
// OTHER SENSORS
// =============================================================================================
Serial.print("+-------------------------------------------------------------------------+\n");
Serial.print("| Other Information... For later |\n");
// =============================================================================================
// DATA LOGGING
// =============================================================================================
// make a string for assembling the data to log:
String dataString = "";
// Build the String
/*
*
for (int analogPin = 0; analogPin < 3; analogPin++) {
int sensor = analogRead(analogPin);
dataString += String(sensor);
if (analogPin < 2) {
dataString += ",";
}
}
*/
// We need to address issues with writing the header in each file.
if(IterationCounter == 1) {
dataFile.println("ID,DATESTAMP,TIMESTAMP,GPS_LAT,GPS_LON,GPS_Speed,GPS_Alt,GPS_Sats,GPS_Fix,GPS_Quality,AMB_Temp,AMB_Humd,AMB_Lux,AMB_Snd,RDQ_AcX,RDQ_AcY,RDQ_AcZ");
}
// LEFT HERE... I HAVE TO FIGURE OUT HOW TO FORMAT THE DATE AND TIME PROPERLY
// String timestamp_len = sprintf(timestamp, "20%02d-%02d-%02dT%02d:%02d:%02dZ", GPS.year, GPS.month, GPS.day, GPS.hour, GPS.minute, GPS.seconds);
//String FormattedDate = Serial.print("20");Serial.print(GPS.year, DEC);Serial.print(":");Serial.print(GPS.month, DEC);Serial.print(":");Serial.print(GPS.month, DEC);Serial.print(":");Serial.println(GPS.day, DEC);
// String FormattedTime = Serial.print(GPS.hour, DEC); Serial.print(':');Serial.print(GPS.minute, DEC); Serial.print(':');Serial.print(GPS.seconds, DEC); Serial.print('.');Serial.println(GPS.milliseconds);
// DATASTRING PRINTING
dataFile.print("ID: ");dataFile.print(IterationCounter);dataFile.print(",");
// START OF DATE STIME SNAFU
dataFile.print("20");dataFile.print(GPS.year, DEC);dataFile.print(":");dataFile.print(GPS.month, DEC);dataFile.print(":");dataFile.print(GPS.month, DEC);dataFile.print(":");dataFile.println(GPS.day, DEC);dataFile.print(",");
dataFile.print(GPS.hour, DEC); dataFile.print(':');dataFile.print(GPS.minute, DEC); dataFile.print(':');dataFile.print(GPS.seconds, DEC); dataFile.print('.');dataFile.println(GPS.milliseconds);dataFile.print(",");
// END OF DATE TIME SNAFU
dataFile.print(GPS_Lat,6);dataFile.print(",");
dataFile.print(GPS_Lon,6);dataFile.print(",");
dataFile.print(GPS_Speed,2);dataFile.print(",");
dataFile.print(GPS_Altitude,2);dataFile.print(",");
dataFile.print(GPS_Sats,2);dataFile.print(",");
dataFile.print(GPS_Fix,2);dataFile.print(",");
dataFile.print(GPS_Quality,2);dataFile.print(",");
dataFile.print(AMB_Temp,2);dataFile.print(",");
dataFile.print(AMB_Humd,2);dataFile.print(",");
dataFile.print(AMB_Lux,2);dataFile.print(",");
dataFile.print(AMB_Snd,2);dataFile.print(",");
dataFile.print(RDQ_AcX,4);dataFile.print(",");
dataFile.print(RDQ_AcY,4);dataFile.print(",");
dataFile.print(RDQ_AcZ,4);
dataFile.println("");
// END OF DATASTRING
// print to the serial port too:
Serial.print("+-------------------------------------------------------------------------+\n");
Serial.print("| Output to data logger\n");
Serial.print("| ");Serial.println(dataString);
dataFile.flush();
Serial.print("+-------------------------------------------------------------------------+\n");
Serial.print("| - End of Loop - |\n");
Serial.print("+-------------------------------------------------------------------------+\n");
delay(1000); // delay is measured in milliseconds - 1000 ms= 1 s
}