1 | #include "Arduino.h"
|
2 | #include "Adafruit_GPS.h"
|
3 | #include "SoftwareSerial.h"
|
4 | #include "temperature.h"
|
5 | #include "ph.h"
|
6 | #include "SD.h"
|
7 |
|
8 | SoftwareSerial mySerial_gps(6, 5); //Simulierte Serielle
|
9 |
|
10 | Adafruit_GPS GPS(&mySerial_gps);
|
11 |
|
12 | // Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
|
13 | // Set to 'true' if you want to debug and listen to the raw GPS sentences.
|
14 | #define GPSECHO false
|
15 |
|
16 | // this keeps track of whether we're using the interrupt
|
17 | // off by default!
|
18 | boolean usingInterrupt = false;
|
19 | void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
|
20 |
|
21 | float temp;
|
22 |
|
23 |
|
24 | //SD
|
25 | //=====================================================================================
|
26 | #define ECHO_TO_SERIAL 1 // echo data to serial port
|
27 | #define WAIT_TO_START 0 // Wait for serial input in setup()
|
28 | #define redLEDpin 7
|
29 | #define greenLEDpin 8
|
30 | const int chipSelect = 10;
|
31 | File logfile;
|
32 | //=====================================================================================
|
33 |
|
34 | void error(char *str)
|
35 | {
|
36 | //Fehlertext-Ausgabe
|
37 | Serial.print("Fehler: ");
|
38 | Serial.println(str);
|
39 | digitalWrite(redLEDpin, HIGH); //Rote LED leuchtet permanent
|
40 | while(1); //bleibt hier hängen im Falle eines Fehlers
|
41 | }
|
42 |
|
43 |
|
44 | void setup()
|
45 | {
|
46 |
|
47 | Serial.begin(115200);
|
48 | Serial.println("GPS Ausgabe starten...");
|
49 | pinMode(tempVcc, OUTPUT);
|
50 | pinMode(redLEDpin, OUTPUT);
|
51 | pinMode(greenLEDpin, OUTPUT);
|
52 | // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
|
53 | GPS.begin(9600);
|
54 |
|
55 | GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
|
56 |
|
57 | // Set the update rate
|
58 | GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
|
59 |
|
60 | // Request updates on antenna status, comment out to keep quiet
|
61 | // GPS.sendCommand(PGCMD_ANTENNA);
|
62 |
|
63 | useInterrupt(true); // Jede 1 millisekunden - GPS read
|
64 |
|
65 |
|
66 | //SD
|
67 | //=====================================================================================
|
68 | //Check ob Speicherkarte vorhanden ist
|
69 | if (!SD.begin(chipSelect)) {
|
70 | error("Zugriff auf SD-Speicherkarte nicht moeglich");
|
71 | }
|
72 |
|
73 | Serial.println("SD-Speicherkarte initialisiert.");
|
74 |
|
75 | //Logging-File erstellen
|
76 | char filename[] = "LOGGER00.CSV";
|
77 | for (uint8_t i = 0; i < 100; i++) {
|
78 | filename[6] = i/10 + '0';
|
79 | filename[7] = i%10 + '0';
|
80 | if (! SD.exists(filename)) {
|
81 | // only open a new file if it doesn't exist
|
82 | logfile = SD.open(filename, FILE_WRITE);
|
83 | break; // leave the loop!
|
84 | }
|
85 | }
|
86 |
|
87 | if (! logfile) {
|
88 | error("Datei konnte nicht erstellt werden.");
|
89 | }
|
90 | //=====================================================================================
|
91 |
|
92 |
|
93 | delay(1000);
|
94 |
|
95 | }
|
96 |
|
97 |
|
98 | // Interrupt is called once a millisecond, looks for any new GPS data, and stores it
|
99 | SIGNAL(TIMER0_COMPA_vect) {
|
100 | char c = GPS.read();
|
101 | // if you want to debug, this is a good time to do it!
|
102 | #ifdef UDR0
|
103 | if (GPSECHO)
|
104 | if (c) UDR0 = c;
|
105 | // writing direct to UDR0 is much much faster than Serial.print
|
106 | // but only one character can be written at a time.
|
107 | #endif
|
108 | }
|
109 |
|
110 | void useInterrupt(boolean v) {
|
111 | if (v) {
|
112 | // Timer0 is already used for millis() - we'll just interrupt somewhere
|
113 | // in the middle and call the "Compare A" function above
|
114 | OCR0A = 0xAF;
|
115 | TIMSK0 |= _BV(OCIE0A);
|
116 | usingInterrupt = true;
|
117 | } else {
|
118 | // do not call the interrupt function COMPA anymore
|
119 | TIMSK0 &= ~_BV(OCIE0A);
|
120 | usingInterrupt = false;
|
121 | }
|
122 | }
|
123 |
|
124 | uint32_t timer = millis();
|
125 | void loop() // run over and over again
|
126 | {
|
127 | temp = read_temp();
|
128 | ph = read_ph();
|
129 |
|
130 | // in case you are not using the interrupt above, you'll
|
131 | // need to 'hand query' the GPS, not suggested :(
|
132 | if (! usingInterrupt) {
|
133 | // read data from the GPS in the 'main loop'
|
134 | char c = GPS.read();
|
135 | // if you want to debug, this is a good time to do it!
|
136 | if (GPSECHO)
|
137 | if (c) Serial.print(c);
|
138 | }
|
139 |
|
140 |
|
141 | // if a sentence is received, we can check the checksum, parse it...
|
142 | if (GPS.newNMEAreceived()) {
|
143 |
|
144 | if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
|
145 | return; // we can fail to parse a sentence in which case we should just wait for another
|
146 | }
|
147 |
|
148 | // if millis() or timer wraps around, we'll just reset it
|
149 | if (timer > millis()) timer = millis();
|
150 |
|
151 | //approximately every 2 seconds or so, print out the current stats
|
152 | if (millis() - timer > 2000) {
|
153 | timer = millis(); // reset the timer
|
154 | delay(1000);
|
155 | Serial.print("\nZeit: ");
|
156 | Serial.print(GPS.hour+1, DEC); Serial.print(':'); // +1 wegen GMT+1
|
157 | Serial.print(GPS.minute, DEC); Serial.print(':');
|
158 | Serial.print(GPS.seconds, DEC); Serial.print('.');
|
159 | Serial.println(GPS.milliseconds);
|
160 | Serial.print("Datum: ");
|
161 | Serial.print(GPS.day, DEC); Serial.print('/');
|
162 | Serial.print(GPS.month, DEC); Serial.print("/20");
|
163 | Serial.println(GPS.year, DEC);
|
164 | Serial.print("Fix: "); Serial.print((int)GPS.fix);
|
165 | Serial.print(" Quali: "); Serial.println((int)GPS.fixquality);
|
166 | Serial.println("Temperatur: ");
|
167 | Serial.println(temp);
|
168 | Serial.println("pH-Wert: ");
|
169 | Serial.println(ph);
|
170 | if (GPS.fix) {
|
171 | Serial.print("Position: ");
|
172 | Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
|
173 | Serial.print(", ");
|
174 | Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
|
175 | Serial.print("Position (in degrees, works with Google Maps): ");
|
176 | Serial.print(GPS.latitudeDegrees, 4);
|
177 | Serial.print(", ");
|
178 | Serial.println(GPS.longitudeDegrees, 4);
|
179 |
|
180 | Serial.print("Geschwindigkeit (Knoten): "); Serial.println(GPS.speed);
|
181 | Serial.print("Angle: "); Serial.println(GPS.angle);
|
182 | Serial.print("Altitude: "); Serial.println(GPS.altitude);
|
183 | Serial.print("Satelliten: "); Serial.println((int)GPS.satellites);
|
184 | }
|
185 | }
|
186 | }
|