-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainSensorBis.pde
361 lines (252 loc) · 7.86 KB
/
mainSensorBis.pde
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
#include <WaspSensorEvent_v20.h>
#include <WaspXBee802.h>
/* ----- Configuration ----- */
#define USB_LOGS 1 // 0 : No log, 1 : Descriptives logs, 2 : Verbose
// Sensors Board
bool SB_HAS_TEMPERATURE = true;
bool SB_HAS_LUMINOSITY = true;
bool SB_HAS_HUMIDITY = false;
// XBee Module
char* XBEE_COORDINATOR_ADDRESS = "0013A20041023C33";
int XBEE_MAX_CONNECTION_RETRY = 2;
int XBEE_DELAY_BETWEEN_CONNECTION_RETRY = 5000;
int XBEE_MAX_SUBROUTINE_RETRY = 10;
int XBEE_DELAY_BETWEEN_SENDING_RETRY = 5000;
int XBEE_PASSES_BEFORE_REBOOT = 36;
// Others
char* SLEEP_TIME = "00:00:10:00";
int LISTENING_TIME = 10000;
// Variables
uint8_t battery, receive;
int i, k, answer;
int passesBeforeReboot = 0;
float temperature[5], luminosity[5], humidity[5];
char temperatureBuf[10], luminosityBuf[10], humidityBuf[10], frame[50];
bool keep;
// Float array ascending sort
void ascSort(float f[]) {
int a, b; float tmp;
for (a = 0; a < sizeof(f); a++)
for (b = 0; b < sizeof(f); b++)
if (f[b] > f[a]) {
tmp = f[a];
f[a] = f[b];
f[b] = tmp;
}
}
// At Waspmote boot
void setup() {
#if USB_LOGS > 0
USB.println(F("Setup :"));
USB.println(F(" > Booting Waspmote ..."));
#endif
RTC.ON(); // Needed for sleeping
#if USB_LOGS > 0
USB.println(F(" > Waspmote booted."));
#endif
}
/* ----- Main Program ----- */
void loop() {
/* ----- Sensors and Data ----- */
#if USB_LOGS > 0
USB.println(F("----- New loop -----"));
USB.println(F("Data :"));
#endif
battery = PWR.getBatteryLevel();
SensorEventv20.ON();
// 5 consecutives sensor data are captured, then the average of the 3 median ones is kept
if (SB_HAS_TEMPERATURE) {
#if USB_LOGS > 1
USB.print(F(" > Temperature : "));
#endif
for (i = 0; i < 5; i++) {
delay(500);
temperature[i] = SensorEventv20.readValue(SENS_SOCKET5, SENS_TEMPERATURE); // Socket 5 or 6
#if USB_LOGS > 1
USB.print(temperature[i]);
USB.print(" ");
#endif
}
#if USB_LOGS > 1
USB.println();
#endif
ascSort(temperature);
dtostrf((temperature[1] + temperature[2] + temperature[3]) / 3, 1, 1, temperatureBuf);
} else { strcpy(temperatureBuf, "N\\A"); }
if (SB_HAS_LUMINOSITY) {
#if USB_LOGS > 1
USB.print(F(" > Luminosity : "));
#endif
for (i = 0; i < 5; i++) {
delay(500);
luminosity[i] = SensorEventv20.readValue(SENS_SOCKET2, SENS_RESISTIVE); // Socket 1 (LL) or 2 or 3 (HL)
#if USB_LOGS > 1
USB.print(luminosity[i]);
USB.print(" ");
#endif
}
#if USB_LOGS > 1
USB.println();
#endif
ascSort(luminosity);
dtostrf((luminosity[1] + luminosity[2] + luminosity[3]) / 3, 1, 1, luminosityBuf);
} else { strcpy(luminosityBuf, "N\\A"); }
if (SB_HAS_HUMIDITY) {
#if USB_LOGS > 1
USB.print(F(" > Humidity : "));
#endif
for (i = 0; i < 5; i++) {
delay(500);
humidity[i] = SensorEventv20.readValue(SENS_SOCKET6, SENS_HUMIDITY); // Socket 5 or 6
#if USB_LOGS > 1
USB.print(humidity[i]);
USB.print(" ");
#endif
}
#if USB_LOGS > 1
USB.println();
#endif
ascSort(humidity);
dtostrf((humidity[1] + humidity[2] + humidity[3]) / 3, 1, 1, humidityBuf);
} else { strcpy(humidityBuf, "N\\A"); }
#if USB_LOGS > 0
USB.print(F(" > Battery : "));
USB.println(battery, DEC);
USB.print(F(" > Temperature : "));
USB.println(temperatureBuf);
USB.print(F(" > Luminosity : "));
USB.println(luminosityBuf);
USB.print(F(" > Humidity : "));
USB.println(humidityBuf);
#endif
SensorEventv20.OFF();
// Storing sensor data in frame array
sprintf(frame, "%lu/%d/%s/%s/%s",
_serial_id, battery, temperatureBuf, luminosityBuf, humidityBuf);
#if USB_LOGS > 0
USB.print(F(" > Frame : "));
USB.println(frame);
#endif
/* ----- Sending Data ----- */
#if USB_LOGS > 0
USB.println(F("Sending data : "));
USB.println(F(" > Booting XBee module ..."));
#endif
// Activation with retries of XBee module
i = XBEE_MAX_CONNECTION_RETRY;
while (xbee802.ON() != 0 && i > 0) {
#if USB_LOGS > 0
USB.print(F(" > > Error. Retry in "));
USB.print(XBEE_DELAY_BETWEEN_CONNECTION_RETRY / 1000);
USB.println(F("s ..."));
#endif
delay(XBEE_DELAY_BETWEEN_CONNECTION_RETRY);
i--;
}
// Activation of XBee module failed
if (i == 0) {
#if USB_LOGS > 0
USB.println(F(" > XBee module failed to boot."));
USB.println(F(" > Rebooting Waspmote ..."));
#endif
PWR.reboot();
}
// XBee module activated
else if (i > 0) {
#if USB_LOGS > 0
USB.println(F(" > XBee module booted."));
USB.println(F(" > Sending frame ..."));
#endif
// Sending with retries of the frame
k = XBEE_MAX_SUBROUTINE_RETRY;
keep = false;
do {
answer = xbee802.send(XBEE_COORDINATOR_ADDRESS, frame);
if (answer != 0) {
#if USB_LOGS > 0
USB.print(F(" > > Error. Retry in "));
USB.print(XBEE_DELAY_BETWEEN_SENDING_RETRY / 1000);
USB.println(F("s ..."));
#endif
delay(XBEE_DELAY_BETWEEN_SENDING_RETRY);
k--;
}
else if (answer == 0) {
delay(50);
#if USB_LOGS > 0
USB.println(F(" > Listening for confirmation ..."));
#endif
receive = xbee802.receivePacketTimeout(LISTENING_TIME);
if (receive == 0) {
#if USB_LOGS > 1
USB.print(F(" > Confirmation packet : "));
USB.println(xbee802._payload, xbee802._length);
USB.print(F(" > Confirmation packet length : "));
USB.println(xbee802._length, DEC);
USB.print(F(" > Confirmation packet MAC : "));
for (i = 0; i < 8; i++) {
USB.printHex(xbee802._srcMAC[i]);
}
USB.println();
#endif
if (((int) xbee802._length) == 2) {
#if USB_LOGS > 0
USB.println(F(" > Confirmation received."));
#endif
keep = true;
}
}
}
} while (!keep && k > 0);
if (keep && k > 0) {
#if USB_LOGS > 0
USB.println(F(" > Frame sent."));
#endif
}
else if (k == 0) {
#if USB_LOGS > 0
USB.println(F(" > Frame not sent."));
USB.println(F(" > Rebooting Waspmote ..."));
#endif
PWR.reboot();
}
#if USB_LOGS > 0
USB.println(F(" > XBee module turned off."));
#endif
xbee802.OFF();
}
/* ----- Reboot Cycle ----- */
// Rebooting after several loops is recommanded by the documentation to avoid various problems
passesBeforeReboot++;
#if USB_LOGS > 1
USB.print(F(" > Reboot cycle ("));
USB.print(passesBeforeReboot);
USB.print(F("/"));
USB.print(XBEE_PASSES_BEFORE_REBOOT);
USB.println(F(")"));
#endif
if (passesBeforeReboot == XBEE_PASSES_BEFORE_REBOOT) {
#if USB_LOGS > 0
USB.println(F("Reboot :"));
USB.println(F(" > An entire cycle has already occured."));
USB.println(F(" > Rebooting Waspmote..."));
#endif
PWR.reboot();
}
/* ----- Sleeping ----- */
#if USB_LOGS > 0
USB.println(F("Sleeping :"));
USB.print(F(" > Wake up set in "));
USB.println(SLEEP_TIME);
USB.println(F(" > Sleeping ..."));
#endif
// Alarm1 Mode2 is for Date, Hour, Minutes, Seconds comparison with seconds precision
PWR.deepSleep(SLEEP_TIME, RTC_OFFSET, RTC_ALM1_MODE2, ALL_OFF);
#if USB_LOGS > 0
USB.println(F(" > Waking up."));
#endif
// Clear interrupt flag, otherwise any future interruption wouldn't have been detected
if (intFlag & RTC_INT) {
RTC.clearAlarmFlag();
}
}