forked from HermanKopinga/Jetpack
/
Wireless.ino
713 lines (633 loc) · 18.9 KB
/
Wireless.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
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
void doFona() {
char command = Serial.read();
Serial.println(command);
switch (command) {
case '?': {
printMenu();
break;
}
case 'a': {
// read the ADC
uint16_t adc;
if (! fona.getADCVoltage(&adc)) {
Serial.println(F("Failed to read ADC"));
} else {
Serial.print(F("ADC = ")); Serial.print(adc); Serial.println(F(" mV"));
}
break;
}
case 'b': {
// read the battery voltage and percentage
uint16_t vbat;
if (! fona.getBattVoltage(&vbat)) {
Serial.println(F("Failed to read Batt"));
} else {
Serial.print(F("VBat = ")); Serial.print(vbat); Serial.println(F(" mV"));
}
if (! fona.getBattPercent(&vbat)) {
Serial.println(F("Failed to read Batt"));
} else {
Serial.print(F("VPct = ")); Serial.print(vbat); Serial.println(F("%"));
}
break;
}
case 'U': {
// Unlock the SIM with a PIN code
char PIN[5];
flushSerial();
Serial.println(F("Enter 4-digit PIN"));
readline(PIN, 3);
Serial.println(PIN);
Serial.print(F("Unlocking SIM card: "));
if (! fona.unlockSIM(PIN)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
case 'C': {
// read the CCID
fona.getSIMCCID(replybuffer); // make sure replybuffer is at least 21 bytes!
Serial.print(F("SIM CCID = ")); Serial.println(replybuffer);
break;
}
case 'i': {
// read the RSSI
uint8_t n = fona.getRSSI();
int8_t r;
Serial.print(F("RSSI = ")); Serial.print(n); Serial.print(": ");
if (n == 0) r = -115;
if (n == 1) r = -111;
if (n == 31) r = -52;
if ((n >= 2) && (n <= 30)) {
r = map(n, 2, 30, -110, -54);
}
Serial.print(r); Serial.println(F(" dBm"));
break;
}
case 'n': {
// read the network/cellular status
uint8_t n = fona.getNetworkStatus();
Serial.print(F("Network status "));
Serial.print(n);
Serial.print(F(": "));
if (n == 0) Serial.println(F("Not registered"));
if (n == 1) Serial.println(F("Registered (home)"));
if (n == 2) Serial.println(F("Not registered (searching)"));
if (n == 3) Serial.println(F("Denied"));
if (n == 4) Serial.println(F("Unknown"));
if (n == 5) {
Serial.println(F("Registered roaming"));
display.drawChar(0,8,'-',GREY,BLACK,1);
}
break;
}
/*** Audio ***/
case 'v': {
// set volume
flushSerial();
Serial.print(F("Set Vol %"));
uint8_t vol = readnumber();
Serial.println();
if (! fona.setVolume(vol)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
case 'V': {
uint8_t v = fona.getVolume();
Serial.print(v); Serial.println("%");
break;
}
case 'H': {
// Set Headphone output
if (! fona.setAudio(FONA_HEADSETAUDIO)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
fona.setMicVolume(FONA_HEADSETAUDIO, 15);
break;
}
case 'e': {
// Set External output
if (! fona.setAudio(FONA_EXTAUDIO)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
fona.setMicVolume(FONA_EXTAUDIO, 10);
break;
}
case 'T': {
// play tone
flushSerial();
Serial.print(F("Play tone #"));
uint8_t kittone = readnumber();
Serial.println();
// play for 1 second (1000 ms)
if (! fona.playToolkitTone(kittone, 1000)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
/*** FM Radio ***/
case 'f': {
// get freq
flushSerial();
Serial.print(F("FM Freq (eg 1011 == 101.1 MHz): "));
uint16_t station = readnumber();
Serial.println();
// FM radio ON using headset
if (fona.FMradio(true, FONA_HEADSETAUDIO)) {
Serial.println(F("Opened"));
}
if (! fona.tuneFMradio(station)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Tuned"));
}
break;
}
case 'F': {
// FM radio off
if (! fona.FMradio(false)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
case 'm': {
// Set FM volume.
flushSerial();
Serial.print(F("Set FM Vol [0-6]:"));
uint8_t vol = readnumber();
Serial.println();
if (!fona.setFMVolume(vol)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
case 'M': {
// Get FM volume.
uint8_t fmvol = fona.getFMVolume();
if (fmvol < 0) {
Serial.println(F("Failed"));
} else {
Serial.print(F("FM volume: "));
Serial.println(fmvol, DEC);
}
break;
}
case 'q': {
// Get FM station signal level (in decibels).
flushSerial();
Serial.print(F("FM Freq (eg 1011 == 101.1 MHz): "));
uint16_t station = readnumber();
Serial.println();
int8_t level = fona.getFMSignalLevel(station);
if (level < 0) {
Serial.println(F("Failed! Make sure FM radio is on (tuned to station)."));
} else {
Serial.print(F("Signal level (dB): "));
Serial.println(level, DEC);
}
break;
}
/*** PWM ***/
case 'P': {
// PWM Buzzer output @ 2KHz max
flushSerial();
Serial.print(F("PWM Freq, 0 = Off, (1-2000): "));
uint16_t freq= readnumber();
Serial.println();
if (! fona.PWM(freq)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
/*** Call ***/
case 'c': {
// call a phone!
char number[30];
flushSerial();
Serial.print(F("Call #"));
readline(number, 30);
Serial.println();
Serial.print(F("Calling ")); Serial.println(number);
if (!fona.callPhone(number)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Sent!"));
}
break;
}
case 'h': {
// hang up!
if (! fona.hangUp()) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
case 'p': {
// pick up!
if (! fona.pickUp()) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
break;
}
/*** SMS ***/
case 'N': {
// read the number of SMS's!
int8_t smsnum = fona.getNumSMS();
if (smsnum < 0) {
Serial.println(F("Could not read # SMS"));
} else {
Serial.print(smsnum);
Serial.println(F(" SMS's on SIM card!"));
}
break;
}
case 'r': {
// read an SMS
flushSerial();
Serial.print(F("Read #"));
uint8_t smsn = readnumber();
Serial.print(F("\n\rReading SMS #")); Serial.println(smsn);
// Retrieve SMS sender address/phone number.
if (! fona.getSMSSender(smsn, replybuffer, 250)) {
Serial.println("Failed!");
break;
}
Serial.print(F("FROM: ")); Serial.println(replybuffer);
// Retrieve SMS value.
uint16_t smslen;
if (! fona.readSMS(smsn, replybuffer, 250, &smslen)) { // pass in buffer and max len!
Serial.println("Failed!");
break;
}
Serial.print(F("***** SMS #")); Serial.print(smsn);
Serial.print(" ("); Serial.print(smslen); Serial.println(F(") bytes *****"));
Serial.println(replybuffer);
Serial.println(F("*****"));
break;
}
case 'R': {
// read all SMS
int8_t smsnum = fona.getNumSMS();
uint16_t smslen;
for (int8_t smsn=1; smsn<=smsnum; smsn++) {
Serial.print(F("\n\rReading SMS #")); Serial.println(smsn);
if (!fona.readSMS(smsn, replybuffer, 250, &smslen)) { // pass in buffer and max len!
Serial.println(F("Failed!"));
break;
}
// if the length is zero, its a special case where the index number is higher
// so increase the max we'll look at!
if (smslen == 0) {
Serial.println(F("[empty slot]"));
smsnum++;
continue;
}
Serial.print(F("***** SMS #")); Serial.print(smsn);
Serial.print(" ("); Serial.print(smslen); Serial.println(F(") bytes *****"));
Serial.println(replybuffer);
Serial.println(F("*****"));
}
break;
}
case 'd': {
// delete an SMS
flushSerial();
Serial.print(F("Delete #"));
uint8_t smsn = readnumber();
Serial.print(F("\n\rDeleting SMS #")); Serial.println(smsn);
if (fona.deleteSMS(smsn)) {
Serial.println(F("OK!"));
} else {
Serial.println(F("Couldn't delete"));
}
break;
}
case 's': {
// send an SMS!
char sendto[21], message[141];
flushSerial();
Serial.print(F("Send to #"));
readline(sendto, 20);
Serial.println(sendto);
Serial.print(F("Type out one-line message (140 char): "));
readline(message, 140);
Serial.println(message);
if (!fona.sendSMS(sendto, message)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Sent!"));
}
break;
}
/*** Time ***/
case 'y': {
// enable network time sync
if (!fona.enableNetworkTimeSync(true))
Serial.println(F("Failed to enable"));
break;
}
case 'Y': {
// enable NTP time sync
if (!fona.enableNTPTimeSync(true, F("pool.ntp.org")))
Serial.println(F("Failed to enable"));
break;
}
case 't': {
// read the time
char buffer[23];
fona.getTime(buffer, 23); // make sure replybuffer is at least 23 bytes!
Serial.print(F("Time = ")); Serial.println(buffer);
break;
}
/*********************************** GPRS */
case 'g': {
// turn GPRS off
if (!fona.enableGPRS(false))
Serial.println(F("Failed to turn off"));
break;
}
case 'G': {
// turn GPRS on
if (!fona.enableGPRS(true))
Serial.println(F("Failed to turn on"));
break;
}
case 'l': {
// check for GSMLOC (requires GPRS)
uint16_t returncode;
if (!fona.getGSMLoc(&returncode, replybuffer, 250))
Serial.println(F("Failed!"));
if (returncode == 0) {
Serial.println(replybuffer);
} else {
Serial.print(F("Fail code #")); Serial.println(returncode);
}
break;
}
case 'w': {
// read website URL
uint16_t statuscode;
int16_t length;
char url[80];
flushSerial();
Serial.println(F("NOTE: in beta! Use small webpages to read!"));
Serial.println(F("URL to read (e.g. www.adafruit.com/testwifi/index.html):"));
Serial.print(F("http://")); readline(url, 79);
Serial.println(url);
Serial.println(F("****"));
if (!fona.HTTP_GET_start(url, &statuscode, (uint16_t *)&length)) {
Serial.println("Failed!");
break;
}
while (length > 0) {
while (fona.available()) {
char c = fona.read();
// Serial.write is too slow, we'll write directly to Serial register!
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
UDR0 = c;
#else
Serial.write(c);
#endif
length--;
if (! length) break;
}
}
Serial.println(F("\n****"));
fona.HTTP_GET_end();
break;
}
case 'W': {
// Post data to website
uint16_t statuscode;
int16_t length;
char url[80];
char data[80];
flushSerial();
Serial.println(F("NOTE: in beta! Use simple websites to post!"));
Serial.println(F("URL to post (e.g. httpbin.org/post):"));
Serial.print(F("http://")); readline(url, 79);
Serial.println(url);
Serial.println(F("Data to post (e.g. \"foo\" or \"{\"simple\":\"json\"}\"):"));
readline(data, 79);
Serial.println(data);
Serial.println(F("****"));
if (!fona.HTTP_POST_start(url, F("text/plain"), (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) {
Serial.println("Failed!");
break;
}
while (length > 0) {
while (fona.available()) {
char c = fona.read();
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
UDR0 = c;
#else
Serial.write(c);
#endif
length--;
if (! length) break;
}
}
Serial.println(F("\n****"));
fona.HTTP_POST_end();
break;
}
/*****************************************/
case 'S': {
Serial.println(F("Creating SERIAL TUBE"));
while (1) {
while (Serial.available()) {
fona.write(Serial.read());
}
if (fona.available()) {
Serial.write(fona.read());
}
}
break;
}
default: {
Serial.println(F("Unknown command"));
printMenu();
break;
}
}
// flush input
flushSerial();
while (fona.available()) {
Serial.write(fona.read());
}
}
void sendLocation (uint16_t vbat) {
char url[160];
uint16_t statuscode;
int16_t length;
uint32_t latint;
uint32_t longint;
char vbatc[4];
char headingc[4];
char Lat_c1[3];
char Lat_c2[8];
char Long_c1[3];
char Long_c2[8];
fona.getBattPercent(&vbat);
itoa(vbat, vbatc, 10);
itoa(orientation.heading + 180, headingc, 10);
itoa(current_location.latitude,Lat_c1,10);
itoa(current_location.longitude,Long_c1,10);
latint = (current_location.latitude-int(current_location.latitude))*10000000;
itoa(latint,Lat_c2,10);
longint = (current_location.longitude-int(current_location.longitude))*10000000;
itoa(longint,Long_c2,10);
sprintf (url, "http://data.sparkfun.com/input/%s?private_key=%s&latitude=%s.%s&longitude=%s.%s&battery=%s&heading=%s&herman=%s",
SPARKFUN_PUBLIC_KEY, SPARKFUN_PRIVATE_KEY, Lat_c1, Lat_c2, Long_c1, Long_c2, vbatc, headingc, "1");
Serial.print(F("Sending: ")); Serial.println(url);
uint8_t rssi = fona.getRSSI();
if (rssi > 5) {
// Make an attempt to turn GPRS mode on. Sometimes the FONA gets freaked out and GPRS doesn't turn off.
// When this happens you can't turn it on aagin, but you don't need to because it's on. So don't sweat
// the error case here -- GPRS could already be on -- just keep on keeping on and let HTTP_GET_start()
// error if there's a problem with GPRS.
display.setCursor(0,8);
display.print("GPRS ");
if (!fona.enableGPRS(true)) {
Serial.println(F("Failed to turn GPRS on!"));
}
if (fona.HTTP_GET_start(url, &statuscode, (uint16_t *)&length)) {
display.setCursor(0,8);
while (length > 0) {
while (fona.available()) {
char c = fona.read();
display.print(c);
Serial.print(c);
length--;
if (! length) break;
}
}
fona.HTTP_GET_end();
} else {
Serial.println(F("Failed to send GPRS data!"));
}
if (!fona.enableGPRS(false)) {
Serial.println(F("Failed to turn GPRS off!"));
}
} else {
Serial.println(F("Can't transmit, network signal strength is crap!"));
}
}
void printMenu(void) {
Serial.println(F("-------------------------------------"));
Serial.println(F("[?] Print this menu"));
Serial.println(F("[a] read the ADC (2.8V max)"));
Serial.println(F("[b] read the Battery V and % charged"));
Serial.println(F("[C] read the SIM CCID"));
Serial.println(F("[U] Unlock SIM with PIN code"));
Serial.println(F("[i] read RSSI"));
Serial.println(F("[n] get Network status"));
Serial.println(F("[v] set audio Volume"));
Serial.println(F("[V] get Volume"));
Serial.println(F("[H] set Headphone audio"));
Serial.println(F("[e] set External audio"));
Serial.println(F("[T] play audio Tone"));
Serial.println(F("[f] tune FM radio"));
Serial.println(F("[F] turn off FM"));
Serial.println(F("[m] set FM volume"));
Serial.println(F("[M] get FM volume"));
Serial.println(F("[q] get FM station signal level"));
Serial.println(F("[P] PWM/Buzzer out"));
Serial.println(F("[c] make phone Call"));
Serial.println(F("[h] Hang up phone"));
Serial.println(F("[p] Pick up phone"));
Serial.println(F("[N] Number of SMSs"));
Serial.println(F("[r] Read SMS #"));
Serial.println(F("[R] Read All SMS"));
Serial.println(F("[d] Delete SMS #"));
Serial.println(F("[s] Send SMS"));
Serial.println(F("[y] Enable network time sync"));
Serial.println(F("[Y] Enable NTP time sync (GPRS)"));
Serial.println(F("[t] Get network time"));
Serial.println(F("[G] Enable GPRS"));
Serial.println(F("[g] Disable GPRS"));
Serial.println(F("[l] Query GSMLOC (GPRS)"));
Serial.println(F("[w] Read webpage (GPRS)"));
Serial.println(F("[W] Post to website (GPRS)"));
Serial.println(F("[S] create Serial passthru tunnel"));
Serial.println(F("-------------------------------------"));
Serial.println(F(""));
}
void flushSerial() {
while (Serial.available())
Serial.read();
}
char readBlocking() {
while (!Serial.available());
return Serial.read();
}
uint16_t readnumber() {
uint16_t x = 0;
char c;
while (! isdigit(c = readBlocking())) {
//Serial.print(c);
}
Serial.print(c);
x = c - '0';
while (isdigit(c = readBlocking())) {
Serial.print(c);
x *= 10;
x += c - '0';
}
return x;
}
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) {
uint16_t buffidx = 0;
boolean timeoutvalid = true;
if (timeout == 0) timeoutvalid = false;
while (true) {
if (buffidx > maxbuff) {
//Serial.println(F("SPACE"));
break;
}
while(Serial.available()) {
char c = Serial.read();
//Serial.print(c, HEX); Serial.print("#"); Serial.println(c);
if (c == '\r') continue;
if (c == 0xA) {
if (buffidx == 0) // the first 0x0A is ignored
continue;
timeout = 0; // the second 0x0A is the end of the line
timeoutvalid = true;
break;
}
buff[buffidx] = c;
buffidx++;
}
if (timeoutvalid && timeout == 0) {
//Serial.println(F("TIMEOUT"));
break;
}
delay(1);
}
buff[buffidx] = 0; // null term
return buffidx;
}