-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasset_tracker.ino
169 lines (143 loc) · 4.48 KB
/
asset_tracker.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
/*
* Asset Tracker
* Uses Arduino, Adafruit Ultimate GPS Breakout, and Adafruit FONA phone module
* Allows a phone to send an SMS, which is received by the FONA.
* The FONA then gets the GPS coordinates via Arduino, and sends them back to the phone.
* This code is based on the SMS functionality from Adafruit's "Open Sesame" code
* https://learn.adafruit.com/open-sesame-a-sms-controlled-door-lock/software
* By Alex Voto, 12/10/16
*/
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
#define FONA_TX 3
#define FONA_RX 2
#define FONA_RST 4
#define FONA_RI 5
#define LED 13
#define actuator 12
#define BUSYWAIT 5000 // milliseconds
// this is a large buffer for replies
char replybuffer[255];
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
//This is the FONA initialization function
boolean fonainit(void) {
Serial.println(F("Initializing....(May take 3 seconds)"));
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(100);
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(100);
// make it slow so its easy to read!
fonaSS.begin(4800); // if you're using software serial
// See if the FONA is responding
if (! fona.begin(fonaSS)) { // can also try fona.begin(Serial1)
Serial.println(F("Couldn't find FONA"));
return false;
}
Serial.println(F("FONA is OK"));
return true;
}
//end of FONA initialization Function
void setup() {
// set LED output for debugging
pinMode(LED, OUTPUT);
pinMode(actuator,OUTPUT);
Serial.begin(115200);
Serial.println(F("FONA basic test"));
while (! fonainit()) {
delay(5000);
}
// Print SIM card IMEI number.
char imei[15] = {0}; // MUST use a 16 character buffer for IMEI!
uint8_t imeiLen = fona.getIMEI(imei);
if (imeiLen > 0) {
Serial.print("SIM card IMEI: "); Serial.println(imei);
}
pinMode(FONA_RI, INPUT);
digitalWrite(FONA_RI, HIGH); // turn on pullup on RI
// turn on RI pin change on incoming SMS!
fona.sendCheckReply(F("AT+CFGRI=1"), F("OK"));
}
int8_t lastsmsnum = 0;
void loop() {
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
while (fona.getNetworkStatus() != 1) {
Serial.println("Waiting for cell connection");
delay(2000);
}
// this is a 'busy wait' loop, we check if the interrupt
// pin went low, and after BUSYWAIT milliseconds break out to check
// manually for SMS's and connection status
// This would be a good place to 'sleep'
for (uint16_t i=0; i<BUSYWAIT; i++) {
if (! digitalRead(FONA_RI)) {
// RI pin went low, SMS received?
Serial.println(F("RI went low"));
break;
}
delay(1);
}
int8_t smsnum = fona.getNumSMS();
if (smsnum < 0) {
Serial.println(F("Could not read # SMS"));
return;
} else {
Serial.print(smsnum);
Serial.println(F(" SMS's on SIM card!"));
}
if (smsnum == 0) return;
// if you've gotten this far, there's an SMS!
uint8_t n = 1;
while (true) {
uint16_t smslen;
char sender[25];
Serial.print(F("\n\rReading SMS #")); Serial.println(n);
uint8_t len = fona.readSMS(n, replybuffer, 250, &smslen); // pass in buffer and max len!
// if the length is zero, its a special case where the index number is higher
// so increase the max we'll look at!
if (len == 0) {
Serial.println(F("[empty slot]"));
n++;
continue;
}
if (! fona.getSMSSender(n, sender, sizeof(sender))) {
// failed to get the sender?
sender[0] = 0;
}
Serial.print(F("***** SMS #")); Serial.print(n);
Serial.print(" ("); Serial.print(len); Serial.println(F(") bytes *****"));
Serial.println(replybuffer);
Serial.print(F("From: ")); Serial.println(sender);
Serial.println(F("*****"));
//Trigger!!! Change text for different text commands. Change enclosed function to do different things.
if (strcasecmp(replybuffer, "do action 1") == 0) {
// turn on the led for a second
action1();
}
if (strcasecmp(replybuffer, "do action 2") == 0) {
// turn on the led for a second
action2();
}
delay(3000);
break;
}
fona.deleteSMS(n);
delay(1000);
}
void action1() {
digitalWrite(actuator, HIGH);
delay(1000);
digitalWrite(actuator,LOW);
}
void action2() {
digitalWrite(actuator, HIGH);
delay(10000);
digitalWrite(actuator,LOW);
}