[Flightcomputer] HoHoHo 1

This was the flightcomputer (Arduino C++ based) used for This Project. It gets serial data from TWO GPS devices, the atmel processor of the arduino parses it to a NTX2 […]

This was the flightcomputer (Arduino C++ based) used for This Project.

It gets serial data from TWO GPS devices, the atmel processor of the arduino parses it to a NTX2 443mhz radio output, and at the right time sends some text messages on touchdown. The nice thing is that it uses the info from two GPS devices, which means you need two serial inputs. In this case i used a Libelium Vinotech sirfIII GPS and a Falcom FSA03 (which sucks). But they performed equally well. Their localization is subsequently parsed through the radio. Oh yeah, it also logs ALL the info from the GPS devices to a SD card!

Parts List

  • Arduino or equivalent
  • Radio transmitter
  • Radio receiver
  • Sony Ericsson with serial interface
  • GPS
  • another GPS
  • Temp Sensor/Hum. Sensor
  • SD card

Code

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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
 
// Project Hollands Hoogte © July 2010
// By Tim Zaman
// Help from Terry Baume (& ntx2 by jharrison)
 
// Digital Pins
#define GPS_PIN_1               9       // GPS serial pin RX
#define GPS_PIN_2               8       // GPS serial pin TX
#define RTTY_PIN_1 		5	// RTTY space pin
#define RTTY_PIN_2 		6	// RTTY mark pin
#define RTTY_LED                30      // RTTY busy led
#define GPS_LED 		22	// Which LED to light when we have a GPS lock?
#define SDPWR                   48      // SD power pin
#define CHECKPIN                13      // Pin that lights up when things are checked
 
// Analog pins
#define HUMINTPIN               13      //Humidity
#define TEMPINTPIN              14      //Temp inside
#define TEMPEXTPIN              15      //Temp outside
 
// T68i
#define num_to_char(number)   ((number) < 10 ?                           
                                               ('0' + (number)) :        
                                               (('A' - 10) + (number)) )
#define first_four(byte)       (0x0F & (byte))
#define last_four(byte)      ((0xF0 & (byte)) >> 4)
#define hexdump_a(byte)  num_to_char( last_four(byte))
#define hexdump_b(byte)  num_to_char(first_four(byte))
 
 
// Misc
#define RTTY_ASCII      7       // ASCII set for RTTY (7/8bit)
#define GPSRATE         9600    // GPS baud rate
#define BUFFERSIZE      90      // How many bytes of input to buffer from the GPS?
#define ENDLN           "rn"  // SD
 
#include <util/crc16.h>
#include <fat16.h>
#include <flash.h>
#include <streaming.h>
#include <softwareSerial.h>
 
 
// Initialize flight variables
int numSats = 0;
int fixType = 0;
int time[] = {
  0, 0, 0};
double latitude = 0.0;
double longitude = 0.0;
long altitude = 0;
long maxAlt = 0;
int speed = 0;
int txCount = 0;
int intTemp = 0;
int extTemp = 0;
int intHum = 0;
int bitRate = 50;
int descent = 0;
 
int FalcomCheck = 0;
int LogCheck =0;
int GpsModule = 1;          // 1= Falcom,    2=Tyco
char* GpsModLock = "X";      // F=Falcom, T=Tyco
 
unsigned long GpsOffTime = 0;   // Tijd dat GPS uit gaat
unsigned long SmsStart = 0;     // SMS-time
unsigned long GpsAltTime = 1800000;    // 1.800.000ms=1.800s=30min
int ExecOnce=0;             // execut0r
int ExecOnceGPS=0;          // voor 24km limit ding
int ExecOncePin=0;          // zodat ie de pin niet steeds aanzet
char buffer[BUFFERSIZE];
byte nxtln[] = ENDLN;                       //SD ENDLNn
unsigned long NXTlength = sizeof(ENDLN)-1;  //SD ENDLNn length
 
SdCard card;
Fat16 Logger;
SoftwareSerial mySerial = SoftwareSerial(GPS_PIN_1, GPS_PIN_2);  //(rx,tx)
 
 
void setup() {
 
  // Start up serial ports
  Serial1.begin(38400);
  Serial3.begin(9600);
  Serial.begin(9600);
  Serial.println("Serial started");
  mySerial.begin(4800);
 
  // SD kaartje poweren met pin..
  pinMode(SDPWR, OUTPUT);
  digitalWrite(SDPWR, HIGH);
 
  // LED's
  pinMode(CHECKPIN, OUTPUT);
  pinMode(RTTY_LED, OUTPUT);
  pinMode(GPS_LED, OUTPUT);
  delay(1000);
  mySerial.println("$PSTMNMEACONFIG,0,4800,1,1");
  mySerial.println("$PSTMINITGPS,5200.000,N,00421.000,E,0197,22,10,2007,11,40,00");
  mySerial.println("$PSRF103,02,00,00,01*26");
 
  // Setup for RTTY
  pinMode(RTTY_PIN_1, OUTPUT);
  pinMode(RTTY_PIN_2, OUTPUT);
 
  //Extra GPS
  pinMode(GPS_PIN_1, INPUT);
  pinMode(GPS_PIN_2, OUTPUT);
  pinMode(7, OUTPUT);
  digitalWrite(7, HIGH);
 
  // Ensure we can access the SD card
  if (!card.init()) Serial << F("* SD card init. failed!") << ENDLN;
  if (!Fat16::init(card)) Serial << F("* FAT16 init. failed!") << ENDLN;
 
  // Check for an available filename
  char fileName[12];
  Serial << F("* Trying to open logging file...") << ENDLN;
  for (int i = 0; i < 1000; i++) {
    sprintf(fileName, "LOG%03d.TXT", i);
    if (Logger.open(fileName, O_CREAT | O_EXCL | O_WRITE)) break;
  }
 
  // Ensure we opened the file without error
  if (!Logger.isOpen()) {
    Serial << F("* Failed to open logging file!") << ENDLN;
    LogCheck = 0;
  }
  else {
    LogCheck = 1;
    Logger.writeError = false;
    Serial << F("* Logging to ") << fileName << ENDLN;
    Logger << F("Project HoHoHo booted!") << ENDLN;
    if (Logger.writeError || !Logger.sync()){
      Serial << F("* Error writing to SD card!") << ENDLN;
      LogCheck =0;
    }
  }
  Serial << ENDLN;
 
 
 
 
 
  delay(2000); // Give the GPS time to come boot
 
  // Set the baud rate to 9600, 38.4k is too fast
  Serial.print("Setting uBlox port mode: ");
  uint8_t setPort[] = {
    0xB5, 0x62, 0x06, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0xD0, 0x08, 0x00, 0x00, 0x80, 0x25, 0x00, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9E, 0x95            };
  sendUBX(setPort, sizeof(setPort)/sizeof(uint8_t));
 
  // Switch baud rates
  Serial.println("Switching to 9600b GPS serial");
  Serial1.begin(9600);
  delay(1000);
 
  // Set the navigation mode (Airborne, 1G)
  Serial.print("Setting uBlox nav mode: ");
  uint8_t setNav[] = {
    0xB5, 0x62, 0x06, 0x24, 0x24, 0x00, 0xFF, 0xFF, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00, 0x05, 0x00, 0xFA, 0x00, 0xFA, 0x00, 0x64, 0x00, 0x2C, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0xDC            };
  sendUBX(setNav, sizeof(setNav)/sizeof(uint8_t));
  getUBX_ACK(setNav);
 
  // Switch off GLL
  Serial.print("Switching off NMEA GLL: ");
  uint8_t setGLL[] = {
    0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x2B             };
  sendUBX(setGLL, sizeof(setGLL)/sizeof(uint8_t));
  getUBX_ACK(setGLL);
 
  // Switch off GSA
  Serial.print("Switching off NMEA GSA: ");
  uint8_t setGSA[] = {
    0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x32             };
  sendUBX(setGSA, sizeof(setGSA)/sizeof(uint8_t));
  getUBX_ACK(setGSA);
 
  // Switch off GSV
  Serial.print("Switching off NMEA GSV: ");
  uint8_t setGSV[] = {
    0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x39             };
  sendUBX(setGSV, sizeof(setGSV)/sizeof(uint8_t));
  getUBX_ACK(setGSV);
 
  // Switch off RMC
  Serial.print("Switching off NMEA RMC: ");
  uint8_t setRMC[] = {
    0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x40             };
  sendUBX(setRMC, sizeof(setRMC)/sizeof(uint8_t));
  getUBX_ACK(setRMC);
 
if (FalcomCheck == 5){
  Logger << F("OK uBlox Falcom checks") << ENDLN;
  if(LogCheck==1){
    digitalWrite(CHECKPIN, HIGH);
  }
  send_sms("Sms werkt, Falcom checks OK. Over en uit.");
}
else{
  Logger << F("FAILED uBlox Falcom checks") << ENDLN;
}
}
 
 
void loop() {
 
  // Flush() de zooi ivm delay's
  Serial1.flush();
 
  // Get a GGA string from the GPS,
  // check if it's a valid fix, and extract the data
  getNMEA("$GPGGA");
  Logger << buffer << ENDLN;
  numSats = getSats();
  fixType = getFixType();
 
  // Make sure we have a valid fix
  if (fixType != 0) {
    if (GpsModule == 1){    // 1 = Falcom
      GpsModLock = "F";     //Falcom heeft lock
    }
    else{                   // 2 = Tyco (Libelium)
      GpsModLock = "T" ;    //Tyco heeft lock
    }
    getTime(time);
    latitude = getLat();
    longitude = getLong();
    altitude = getAlt();
    digitalWrite(GPS_LED, HIGH);
 
    // Keep track of the maximum altitude
    if (altitude > maxAlt) {
      maxAlt = altitude;
    }
 
    // Check to see if we've fallen 1000m, if so switch to descent mode
    if (altitude < (maxAlt - 1000)) {
      descent = 1;
    }
   }
  else {
    digitalWrite(GPS_LED, LOW);
  }
 
  // Get the speed from a VTG message
  //getNMEA("$GPVTG");
  //Logger << buffer << ENDLN;                     ?
  //speed = getSpeed();
 
  // Convert lat & long into strings
  char latString[12];
  char longString[12];
  doubleToString(latitude, 4, latString);
  doubleToString(longitude, 4, longString);
 
  intTemp= getTemp(TEMPINTPIN);  //inside temp
  extTemp= getTemp(TEMPEXTPIN);  //outside temp
  intHum= getHum(HUMINTPIN);     //humidity
 
  Logger << ("Sensors;") << intTemp << F(";") << extTemp << F(";") << intHum << ENDLN;
 
  if (GpsModule ==1){ // = Falcom
  sprintf(buffer, "$$TBL,%d,%02d:%02d:%02d,%s,%s,%ld,%s", txCount, time[0], time[1], time[2], latString, longString, altitude, GpsModLock);
  GpsModule = 2;
  }
  else{               // = Tyco
  sprintf(buffer, "$$TBB,%d,%02d:%02d:%02d,%s,%s,%ld,%s", txCount, time[0], time[1], time[2], latString, longString, altitude, GpsModLock);
  GpsModule = 1;
  txCount++;
  }
 
  if(txCount == 10){
    send_sms(buffer);
  }
 
 
  // Transmit and log to SD card
  TxString(buffer);
 
 
  // Force an update on the SD card
  Logger.sync();
 
  // Delay a moment before restarting loop
  delay(100);
 
  // GPS 1 uitval boven 23.5km
  if (altitude > 23500){
    if (ExecOnceGPS == 0){
      ExecOnceGPS = 1;
      digitalWrite(7, LOW); // GPS voeding uit
      GpsOffTime = millis();
      TxString("lim reached lim reached lim reached lim reached lim reached");
    }
  }
 
 
  // Als hij boven de hoogte uit is gekomen..
  if (ExecOnceGPS == 1){         // Zorg dat hij gedefineerd is
    if ((millis() - GpsOffTime) > GpsAltTime){  //na 23.5km 30min wachten
      if (ExecOncePin == 0){  //zodat die de pin niet steeds aanzet
        digitalWrite(7, HIGH); // GPS weer aan
        ExecOncePin = 1;
        TxString("gps on gps on gps on gps on gps on gps on");
      }
    }
    if(altitude < 23000){ // als hij dan toch is gedropt door een lagere meting
      if (ExecOncePin == 0){  //zodat die de pin niet steeds aanzet
        digitalWrite(7, HIGH); // GPS weer aan
        ExecOncePin = 1;
        TxString("gps on gps on gps on gps on gps on gps on");
      }
    }
  }
 
 
 
  // SMS <1000m mode
  if (descent == 1){
    if (altitude < 900){
 
      if (ExecOnce == 0){
        SmsStart = millis();
        ExecOnce = 1;
        send_sms(buffer);
        Logger << F("SMS verstuurd...") << ENDLN;
        delay(50);
      }
      if (millis() - SmsStart > 26000){
        ExecOnce =0;
      }
 
    }
  }
 
 
} //end
 
 
 
 
 
 
 
// ----------  GPS MEUK -------------
 
// Send a byte array of UBX protocol to the GPS
void sendUBX(uint8_t *MSG, uint8_t len) {
  for(int i=0; i<len; i++) {
    Serial1.print(MSG[i], BYTE);
    Serial.print(MSG[i], HEX);
  }
  Serial.println();
}
 
// Calculate expected UBX ACK packet and parse UBX response from GPS
boolean getUBX_ACK(uint8_t *MSG) {
  uint8_t b;
  uint8_t ackByteID = 0;
  uint8_t ackPacket[10];
  int startTime = millis();
  Serial.print(" * Reading ACK response: ");
 
  // Construct the expected ACK packet
  ackPacket[0] = 0xB5;	// header
  ackPacket[1] = 0x62;	// header
  ackPacket[2] = 0x05;	// class
  ackPacket[3] = 0x01;	// id
  ackPacket[4] = 0x02;	// length
  ackPacket[5] = 0x00;
  ackPacket[6] = MSG[2];	// ACK class
  ackPacket[7] = MSG[3];	// ACK id
  ackPacket[8] = 0;		// CK_A
  ackPacket[9] = 0;		// CK_B
 
  // Calculate the checksums
  for (uint8_t i=2; i<8; i++) {
    ackPacket[8] = ackPacket[8] + ackPacket[i];
    ackPacket[9] = ackPacket[9] + ackPacket[8];
  }
 
  while (1) {
 
    // Test for success
    if (ackByteID > 9) {
      // All packets in order!
      Serial.println(" (SUCCESS!)");
      FalcomCheck=FalcomCheck+1;
      return true;
    }
 
    // Timeout if no valid response in 3 seconds
    if (millis() - startTime > 3000) {
      Serial.println(" (FAILED!)");
      return false;
    }
 
    // Make sure data is available to read
    if (Serial1.available()) {
      b = Serial1.read();
 
      // Check that bytes arrive in sequence as per expected ACK packet
      if (b == ackPacket[ackByteID]) {
        ackByteID++;
        Serial.print(b, HEX);
      }
      else {
        ackByteID = 0;	// Reset and look again, invalid order
      }
 
    }
  }
}
 
 
 
// ------- GPS Parsing ----------
 
// Reads a line from the GPS NMEA serial output
// Give up after trying to read 1000 bytes (~2 seconds)
int readLine(void) {
  char c;
  byte bufferIndex = 0;
  boolean startLine = 0;
  byte retries = 0;
  while (retries < 20) {
    //if (mySerial.available() > 0) {         //Serial1=falcom, mySerial= andere
      if (GpsModule==1){  //Falcom
      c = Serial1.read();
      }
      else{               //Tyco
      c= mySerial.read();
      }
 
      if (c == -1) {
        delay(2);
        continue;
      }
      if (c == 'n') continue;
      if (c == '$') startLine = 1;
      if ((bufferIndex == BUFFERSIZE-1) || (c == 'r')) {
        if (startLine) {
          buffer[bufferIndex] = 0;
          return 1;
        }
      }
      if (startLine) buffer[bufferIndex++] = c;
    //}
    else {
      retries++;
      delay(50);
    }
  }
  return 0;
}
 
// Returns a specific field from the buffer
void getField(int getId, char *field, int maxLen) {
  byte bufferIndex = 0;
  byte fieldId = 0;
  byte i = 0;
  while (bufferIndex < sizeof(buffer)) {
    if (fieldId == getId) {
      // End of string, or string overflow
      if (buffer[bufferIndex] == ',' || i > (maxLen - 2)) {
        field[i] = 0;	// Null terminate
        return;
      }
      // Buffer chars to field
      field[i++] = buffer[bufferIndex++];
    }
    else {
      // Advance field on comma
      if (buffer[bufferIndex] == ',') {
        bufferIndex++;						// Advance in buffer
        fieldId++;							// Increase field position counter
      }
      else {
        bufferIndex++;						// Advance in buffer
      }
    }
  }
  // Null terminate incase we didn't already..
  field[i] = 0;
}
 
// Polls for an NMEA sentence of type requested
// Validates checksum, silently retries on failed checksums
int getNMEA(char *getType) {
  char type[7];
  byte retries = 0;
  while (retries < 2) {
    if (readLine() && validateChecksum()) {
      ;
      getField(0, type, sizeof(type));
      if (strcmp(type, getType) == 0) {
        Serial.println(buffer);
        return 1;
      }
    }
    else {
      retries++;
    }
  }
  Serial.println("Failed to read GPS");
  Logger << F("Failed to read GPS") << ENDLN;
  return 0;
}
 
// Validates the checksum on an NMEA string
// Returns 1 on valid checksum, 0 otherwise
int validateChecksum(void) {
  char gotSum[2];
  gotSum[0] = buffer[strlen(buffer) - 2];
  gotSum[1] = buffer[strlen(buffer) - 1];
  // Check that the checksums match up
  if ((16 * atoh(gotSum[0])) + atoh(gotSum[1]) == getCheckSum(buffer)) return 1;
  else return 0;
}
 
// Calculates the checksum for a given string
// returns as integer
int getCheckSum(char *string) {
  int i;
  int XOR;
  int c;
  // Calculate checksum ignoring any $'s in the string
  for (XOR = 0, i = 0; i < strlen(string); i++) {
    c = (unsigned char)string[i];
    if (c == '*') break;
    if (c != '$') XOR ^= c;
  }
  return XOR;
}
 
// Returns the groundspeed in km/h
int getSpeed(void) {
  char field[10];
  getField(7, field, sizeof(field));
  int speed = atoi(field);
  return speed;
}
 
// Return the fix type from a GGA string
int getFixType(void) {
  char field[5];
  getField(6, field, sizeof(field));
  int fixType = atoi(field);
  return fixType;
}
 
// Return the altitude in meters from a GGA string
long getAlt(void) {
  char field[10];
  getField(9, field, sizeof(field));
  long altitude = atol(field);
  return altitude;
}
 
// Returns the number of satellites being tracked from a GGA string
int getSats(void) {
  char field[3];
  getField(7, field, sizeof(field));
  int numSats = atoi(field);
  return numSats;
}
 
// Read the latitude in decimal format from a GGA string
double getLat(void) {
  char field[12];
  getField(2, field, sizeof(field));			// read the latitude
  double latitude = atof(field);					// convert to a double (precise)
  int deg = (int) latitude / 100;				// extract the number of degrees
  double min = latitude - (100 * deg);			// work out the number of minutes
  latitude = deg + (double) min/60.0;			// convert to decimal format
  getField(3, field, sizeof(field));			// get the hemisphere (N/S)
  if (strcmp(field, "S") == 0) latitude *= -1;	// sign the decimal latitude correctly
  return latitude;
}
 
// Read the longitude in decimal format from a GGA string
double getLong(void) {
  char field[12];
  getField(4, field, sizeof(field));			// read the longitude
  double longitude = atof(field);					// convert to a double
  int deg = (int) longitude / 100;				// extract the number of degrees
  double min = longitude - (100 * deg);			// work out the number of minutes
  longitude = deg + (double) min/60.00;			// convert to decimal format
  getField(5, field, sizeof(field));			// get the E/W status
  if (strcmp(field, "W") == 0) longitude *= -1; // sign decimal latitude correctly
  return longitude;
}
 
// Converts UTC time to the correct timezone
void convertTime(int *time) {
  // How many hours off GMT are we?
  float offset = 2;
  long sectime = ((long)(time[0]) * 3600) + (time[1] * 60) + time[2];
  sectime += (offset * 3600.0);
  // Did we wrap around?
  if (sectime < 0) sectime += 86400;
  if (sectime > 86400) sectime -= 86400;
  // Convert back to time
  time[0] = (int)(sectime / 3600);
  time[1] = (int)((sectime % 3600) / 60);
  time[2] = (int)((sectime % 3600) % 60);
}
 
// Parses a time field from a GGA string
void parseTime(char *field, int *time) {
  char tmp[3];
  tmp[2] = 0; // Init tmp and null terminate
  tmp[0] = field[0];
  tmp[1] = field[1];
  time[0] = atoi(tmp); // Hours
  tmp[0] = field[2];
  tmp[1] = field[3];
  time[1] = atoi(tmp); // Minutes
  tmp[0] = field[4];
  tmp[1] = field[5];
  time[2] = atoi(tmp); // Seconds
}
 
// Gets the hours, minutes and seconds from a GGA string
void getTime(int *time) {
  char field[12];
  getField(1, field, sizeof(field));
  parseTime(field, time);
  convertTime(time);
}
 
// ------ RTTY ----------
 
// Transmit a string, log it to SD & produce debug output
void TxString(char *string) {
  digitalWrite(RTTY_LED, HIGH);
 
  // Checksum
  char txSum[4];
  int checkSum = getCheckSum(string);
  sprintf(txSum, "%02X", checkSum);
 
  Serial << F("RTTY: ") << string << "*" << txSum << ENDLN;
  Logger << string << F("*") << txSum << ENDLN;
  rtty_txstring(string);
  rtty_txstring("*");
  rtty_txstring(txSum);
  rtty_txstring("rn");
  digitalWrite(RTTY_LED, LOW);
}
 
// Transmit a string, one char at a time
void rtty_txstring (char *string) {
  for (int i = 0; i < strlen(string); i++) {
    rtty_txbyte(string[i]);
  }
}
 
// Transmit a byte, bit by bit, LSB first
// ASCII_BIT can be either 7bit or 8bit
void rtty_txbyte (char c) {
  int i;
  // Start bit
  rtty_txbit (0);
  // Send bits for for char LSB first
  for (i=0;i<rtty_ASCII;i++) {
    if (c & 1) rtty_txbit(1);
    else rtty_txbit(0);
    c = c >> 1;
  }
  // Stop bit
  rtty_txbit (1);
}
 
// Transmit a bit as a mark or space
void rtty_txbit (int bit) {
  if (bit) {
    // High - mark
    digitalWrite(RTTY_PIN_1, HIGH);
    digitalWrite(RTTY_PIN_2, LOW);
  }
  else {
    // Low - space
    digitalWrite(RTTY_PIN_2, HIGH);
    digitalWrite(RTTY_PIN_1, LOW);
  }
  // Delay appropriately - tuned to 50 baud.
  delay(19);
  delayMicroseconds(250);
}
 
// ------ MISC ----------
 
// Returns a string with a textual representation of a float
void doubleToString(double val, int precision, char *string){
 
  // Print the int part
  sprintf(string, "%d", (int)(val));
  if(precision > 0) {
    // Print the decimal point
    strcat(string, ".");
    unsigned long frac;
    unsigned long mult = 1;
    int padding = precision -1;
    while (precision--) {
      mult *=10;
    }
    if (val >= 0)
      frac = (val - (int)(val)) * mult;
    else
      frac = ((int)(val)- val ) * mult;
    unsigned long frac1 = frac;
    while (frac1 /= 10) {
      padding--;
    }
    while (padding--) {
      strcat(string, "0");
    }
 
    // Convert and print the fraction part
    sprintf(string+strlen(string), "%d", (int)(frac));
  }
}
 
 
// Converts a HEX string to an int
int atoh(char c) {
  if (c >= 'A' && c <= 'F')
    return c - 55;
  else if (c >= 'a' && c <= 'f')
    return c - 87;
  else
    return c - 48;
}
 
 
 
// ------ T68i GPRS ----------
 
void hexdump_byte(unsigned char byte)
{
  Serial3.print(hexdump_a(byte), BYTE);
  Serial3.print(hexdump_b(byte), BYTE);
}
 
void send_sms(char *data)
{
  size_t data_length, x;
  char c, l;
  long i;
  long n;
 
  data_length = strlen(data);
  i = data_length * 7;
 
  /* Round i up to a multiple of 8 */
  if (i & 0x07) i = (i & ~0x07) + 0x08;
 
  /* Calculate the number of message octets */
  i = i / 8;
 
  Serial3.println("AT+CMGF=0");
  delay(1500);
  Serial3.print("AT+CMGS=");
  delay(1500);
  Serial3.println(i + 14);
  delay(1500);
  Serial3.print("0011000B911356537837F80000AA");
  hexdump_byte(data_length & 0xFF);
 
  /* from sms_example_v2.c ALIEN Project Daniel Richman */
  l = 0;
  n = 0;
 
  for (x = 0; x < data_length; x++)
  {
    if (data[x] == '$')  data[x] = 0x02;
 
    n |= (data[x] & 0x7F) << l;
    l += 7;
 
    if (l >= 8)
    {
      hexdump_byte(n & 0xFF);
      l -= 8;
      n >>= 8;
    }
  }
 
  if (l != 0)
  {
    hexdump_byte(n & 0xFF);
  }
 
  Serial3.println(0x1A, BYTE);
}
 
 
// Sensoren
 
int getTemp(int pin){ //getTemp(<pinnr>)
return (((analogRead(pin) * .004882814)-2.89)*100);  //gecalibreerd op 3.11V=22C
}
 
int getHum(int pin){ //getHum(<pinnr>)
return (((analogRead(pin) * .004882814)-0.9)*33);  //gecalibreerd op start .9V, 33% per volt.
}