-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConnectedBottle.ino
296 lines (234 loc) · 6.18 KB
/
ConnectedBottle.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
#include <SigFox.h>
#include <ArduinoLowPower.h>
#include <Adafruit_ADXL345_U.h>
#include <Adafruit_GPS.h>
// Test values
#define SENSORS_READING_TIME 5000 // prod value : 36000000ms
#define KEEPALIVE_TIME 30000 // prod value : 180000ms
// # of "wrong state" needed to confirm
#define STATE_TRIGGER 3
// Adjust the gap defining the "wrong state"
#define ANGLE_MARGIN 45
#define GPSSerial Serial1
#define GPSECHO false
#define DEBUG false
#define SEND true
// Flag allows to send different types of alerts
// Keepalive : "K" | Fallen : "F"
struct Payload {
char flag;
float l_lat;
float l_long;
};
struct Coordinates {
float l_lat;
float l_long;
};
struct Accel {
float x;
float y;
float z;
float roll;
float pitch;
};
Adafruit_GPS GPS(&GPSSerial);
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(1);
uint32_t timer = millis();
int timer_increment = 0;
int state = 0;
int current_state;
float latitude = 0.0f;
float longitude = 0.0f;
Coordinates tmp_coordinates;
void setup() {
if(DEBUG) {
Serial.begin(9600);
while (!Serial) {
;
}
}
pinMode(LED_BUILTIN, OUTPUT);
if (!SigFox.begin()) {
reboot();
}
SigFox.debug();
SigFox.end();
GPS.begin(9600);
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
GPS.sendCommand(PGCMD_ANTENNA);
delay(1000);
if (DEBUG == true) {
GPSSerial.println(PMTK_Q_RELEASE);
}
if (!accel.begin())
{
if (DEBUG == true) {
Serial.println("! Accelerometer not detected");
}
digitalWrite(LED_BUILTIN, HIGH);
while (1);
}
accel.setRange(ADXL345_RANGE_16_G);
}
void loop() {
GPS.read();
if (GPS.newNMEAreceived()) {
GPS.lastNMEA();
GPS.parse(GPS.lastNMEA());
}
if (timer > millis()) timer = millis();
if (millis() - timer > SENSORS_READING_TIME) {
timer = millis();
timer_increment++;
current_state = updateState(getAccel());
if(current_state == 1) {
state++;
if (DEBUG == true) {
Serial.print("- State increment : ");
Serial.print(state);
Serial.print("/");
Serial.println(STATE_TRIGGER);
}
} else {
state = 0;
digitalWrite(LED_BUILTIN, LOW);
if (DEBUG == true) {
Serial.println("- Increment (re)initialized");
}
}
if(state == STATE_TRIGGER) {
if (DEBUG == true) {
Serial.println("- State definitely abnormal, sending alarm");
}
// Sending alarm
// Alarm code : F
if (SEND == true) {
tmp_coordinates = getLocation();
delay(100);
sendMessage(tmp_coordinates, 'F');
} else {
getLocation();
}
// Onboard light is flashing once alarm is sent
for(int i = 0; i < 3; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
delay(400);
}
}
// Sending keepalive
// Alarm code : K
if (timer_increment == (KEEPALIVE_TIME / SENSORS_READING_TIME)) {
if (DEBUG == true) {
Serial.println("- Sending keepalive");
}
if (SEND == true) {
tmp_coordinates = getLocation();
delay(100);
sendMessage(tmp_coordinates, 'K');
} else {
getLocation();
}
timer_increment = 0;
}
}
}
// Getting accelerometer current values
Accel getAccel() {
Accel d_accel;
float x_angle, y_angle, roll, pitch;
sensors_event_t event_accel;
accel.getEvent(&event_accel);
if (DEBUG == true) {
Serial.print("- Accelerometer (m/s^2) : X");
Serial.print(event_accel.acceleration.x);
Serial.print(" Y");
Serial.print(event_accel.acceleration.y);
Serial.print(" Z");
Serial.println(event_accel.acceleration.z);
}
d_accel.x = event_accel.acceleration.x;
d_accel.y = event_accel.acceleration.y;
d_accel.z = event_accel.acceleration.z;
x_angle = atan(event_accel.acceleration.y / sqrt((event_accel.acceleration.x * event_accel.acceleration.x) + (event_accel.acceleration.z * event_accel.acceleration.z)));
y_angle = atan(event_accel.acceleration.x / sqrt((event_accel.acceleration.y * event_accel.acceleration.y) + (event_accel.acceleration.z * event_accel.acceleration.z)));
d_accel.roll = x_angle * 180 / M_PI;
d_accel.pitch = y_angle * 180 / M_PI;
if (DEBUG == true) {
Serial.print("- Angles (°) : roll ");
Serial.print(d_accel.roll);
Serial.print(" pitch ");
Serial.println(d_accel.pitch);
}
return d_accel;
}
// Detecting if the state is wrong or not
// Wrong : 1 | OK : 0
int updateState(Accel d_accel) {
if (((d_accel.roll < (90 - ANGLE_MARGIN)) || (d_accel.roll > (90 + ANGLE_MARGIN))) || ((d_accel.pitch < (0 - ANGLE_MARGIN)) || (d_accel.pitch > (0 + ANGLE_MARGIN)))) {
if (DEBUG == true) {
Serial.println("- Device state abnormal");
}
return 1;
} else {
if (DEBUG == true) {
Serial.println("- Device state OK");
}
return 0;
}
}
// Getting current location
// Can't get location values : -1, -1
Coordinates getLocation() {
Coordinates location;
if (GPS.fix) {
if (DEBUG == true) {
Serial.print("- Location : ");
Serial.print(GPS.latitudeDegrees, 4);
Serial.print(", ");
Serial.println(GPS.longitudeDegrees, 4);
}
location.l_lat = GPS.latitudeDegrees;
location.l_long = GPS.longitudeDegrees;
} else {
if (DEBUG == true) {
Serial.println("! Can't fix GPS signal");
}
location.l_lat = -1;
location.l_long = -1;
}
return location;
}
// Sending a message to SigFox
void sendMessage(Coordinates location, char flag) {
Payload payload;
payload.flag = flag;
payload.l_lat = location.l_lat;
payload.l_long = location.l_long;
int ret;
Serial.println("- Sending message to SigFox");
SigFox.begin();
delay(100);
SigFox.status();
delay(1);
SigFox.beginPacket();
SigFox.write(payload);
ret = SigFox.endPacket();
if (DEBUG == true) {
Serial.println(SigFox.status(SIGFOX));
Serial.println(SigFox.status(ATMEL));
Serial.println(ret);
if (ret > 0) {
Serial.println("- Error while sending");
} else {
Serial.println("! Sent to SigFox network");
}
}
SigFox.end();
}
void reboot() {
NVIC_SystemReset();
while (1);
}