-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheneargy.ino
479 lines (396 loc) · 11.3 KB
/
eneargy.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
#include <EddystoneBeacon.h>
#include <RN487x_CONST.h>
#include <RN487x_BLE.h>
#include <iBeacon.h>
#include <Led.h>
#include <Sodaq_wdt.h>
#include <Switchable_Device.h>
#include <LpwaOrange.h>
#include <SodaqExt_RN2483.h>
#include <Utils.h>
#include <StringLiterals.h>
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#include "Arduino.h"
#define SIZE_INPUT 2
#define SIZE_OUTPUT 3
#define PIN_PIXELS 6
#define NUMPIXELS 59
#define LORA_PORT 5
#define neighbourSerial Serial
#define debugSerial SerialUSB
#define loraSerial Serial2
#define FRM_PAYLOAD_MAX_LENGTH 255
#define FIXED_VOLTAGE 230
//inputs
#define NATIONAL 7u
#define NEIGHBOUR 8u
#define ALT 9u
#define NET_INDEX 0
#define ALT_INDEX 1
#define PLUS_BUTTON 10u
#define MINUS_BUTTON 11u
#define SERIAL_NEW_CONSUMPTION_COMMAND 'c' // consumption
#define SERIAL_NEED_POWER_COMMAND 'b' // buy
#define SERIAL_POWER_AVAILABLE_COMMAND 's' // sell
// OTAA PRODUCTION Keys - Use your own KEYS!
const uint8_t devEUI[8] = {0x21, 0x5F, 0xD5, 0x69, 0x16, 0x7F, 0x5D, 0x9A} ;
const uint8_t appEUI[8] = {0xA0, 0xAC, 0xB2, 0x67, 0x0D, 0xA9, 0xB1, 0xAE} ;
const uint8_t appKey[16] = {0x0A, 0x6E, 0x2C, 0x57, 0x0D, 0x3B, 0xEF, 0x60, 0x13, 0xD1, 0x14, 0xCB, 0x13, 0xD8, 0x7B, 0xDA};
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN_PIXELS, NEO_GRB + NEO_KHZ800);
//initialisation des variables
long timestamp = 0;
long energyTs = 0;
int minusButton = 0;
int plusButton = 0;
int maxButton = 0;
uint8_t frameReceived[FRM_PAYLOAD_MAX_LENGTH];
int powerSrc = NET_INDEX;
int currentNeighbourCons = 0;
int powerThreshold[SIZE_OUTPUT] = {0, 0, 0};
double output[SIZE_OUTPUT] = {0, 0, 0};
double input[SIZE_INPUT] = {0, 0};
bool is_input[SIZE_INPUT] = {false, false};
int nb_led = 0;
int max_led = 6;
void initPin() {
//Initialize the LEDs and turn them all off
pinMode(LED_RED, OUTPUT) ;
pinMode(LED_GREEN, OUTPUT) ;
pinMode(LED_BLUE, OUTPUT) ;
pinMode(BUTTON, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
//set power source as input
pinMode(NATIONAL, INPUT);
pinMode(NEIGHBOUR, INPUT);
pinMode(ALT, INPUT);
pinMode(PLUS_BUTTON, INPUT_PULLUP);
pinMode(MINUS_BUTTON, INPUT_PULLUP);
//Set the energy sensor pin as input
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
pinMode(A5, INPUT);
//Set ADC resolution to 12 bits
analogReadResolution(12) ;
// Init Serial connection with neighbour
neighbourSerial.begin(9600);
// Init power src pin
initPowerSrc(NATIONAL, NET_INDEX);
initPowerSrc(ALT, ALT_INDEX);
initPoducer();
}
void initPowerSrc(int pin, int srcIndex) {
pinMode(pin, INPUT);
is_input[srcIndex] = (digitalRead(pin) == HIGH);
}
void initConsumer() {
nb_led = 5;
is_input[0] = true;
is_input[1] = false;
input[ALT_INDEX] = 0;
powerThreshold[0] = 1;
powerThreshold[1] = 3;
powerThreshold[2] = 5;
}
void initPoducer() {
nb_led = 8;
is_input[0] = true;
is_input[1] = true;
powerThreshold[0] = 3;
powerThreshold[1] = 4;
powerThreshold[2] = 5;
}
void red() {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_BLUE, HIGH);
}
void green() {
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, HIGH);
}
void blue() {
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_BLUE, LOW);
}
void white() {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, LOW);
}
void orange() {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_BLUE, HIGH);
}
void setup() {
debugSerial.begin(57600);
loraSerial.begin(LpwaOrange.getDefaultBaudRate());
initPin();
pixels.begin(); // This initializes the NeoPixel library.
orange();
delay(10000) ;
LpwaOrange.flush();
LpwaOrange.setDiag(debugSerial) ; // optional
bool res = 0;
int tentative = 0;
do {
blue();
debugSerial.println("OTAA Join Request") ;
res = LpwaOrange.joinLoRaNetwork(loraSerial, devEUI, appEUI, appKey, true, 4);
debugSerial.println(res ? "OTAA Join Accepted." : "OTAA Join Failed! Trying again. Waiting 10 seconds.") ;
if (!res) {
red();
tentative++;
delay(10000);
}
if (tentative == 3) {
while (1) {
red();
delay(1000);
orange();
delay(1000);
white();
delay(1000);
}
}
} while (res == 0);
debugSerial.println("Sleeping for 5 seconds before starting sending out test packets.");
sleep(5);
}
void sleep(unsigned short count) {
for (uint8_t i = count; i > 0; i--) {
debugSerial.println(i) ;
delay(1000) ;
}
}
void debugFrame(const char* frame, int len) {
if (frame != NULL) {
debugSerial.print("Payload: ");
int i = 0;
for (i = 0; i < len; i++) {
unsigned char value = (unsigned char)frame[i];
debugSerial.print(value, HEX); debugSerial.print(" ");
}
debugSerial.print("\r\n");
}
}
float getPower(int load) {
double Irms = load * 46;
double power = Irms * FIXED_VOLTAGE;
return power;
}
double getEnergy(double power, float period) { //power in watt, period in second.
return power * period / 3600; //energy in Wh
}
void sendNewConsumption(int consumption) {
char value = SERIAL_NEW_CONSUMPTION_COMMAND;
neighbourSerial.write(value);
value = consumption;
neighbourSerial.write(value);
}
void processNeighbourRequest() {
char command;
char data;
command = neighbourSerial.read();
data = neighbourSerial.read();
switch(command) {
case SERIAL_NEW_CONSUMPTION_COMMAND:
currentNeighbourCons = data;
break;
case SERIAL_NEED_POWER_COMMAND:
break;
case SERIAL_POWER_AVAILABLE_COMMAND:
break;
}
}
void loop() {
//mise à jour compte LED
int old_led_nr = nb_led;
if (digitalRead(PLUS_BUTTON) == LOW)
plusButton = 1;
if (digitalRead(PLUS_BUTTON) == HIGH && plusButton == 1) {
nb_led++;
debugSerial.println("add led : ");
debugSerial.println(nb_led);
plusButton = 0;
}
if (digitalRead(MINUS_BUTTON) == LOW)
minusButton = 1;
if (digitalRead(MINUS_BUTTON) == HIGH && minusButton == 1) {
nb_led--;
debugSerial.println("rm led : ");
debugSerial.println(nb_led);
minusButton = 0;
}
if (digitalRead(BUTTON) == LOW)
maxButton = 1;
if (digitalRead(BUTTON) == HIGH && maxButton == 1){
if(max_led == 9){
max_led = 6;
}
else{
max_led = 9;
}
maxButton = 0;
}
//mise à jour LED
for(int i=0; i< 9; i++){
if(i < 3){
if(i < min(nb_led, max_led)){
pixels.setPixelColor(i, pixels.Color(0,255,0)); // green
}else{
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); //off
}
}else if(i < 6){
if (i < min(nb_led, max_led)){
pixels.setPixelColor(i, pixels.Color(255, 150, 0)); // orange
}else{
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); //off
}
}else{
if(i < min(nb_led, max_led)){
pixels.setPixelColor(i, pixels.Color(255, 0, 0)); // red
}else{
pixels.setPixelColor(i, pixels.Color(0, 0, 0)); //off
}
}
pixels.show(); // This sends the updated pixel color to the hardware.
}
if(nb_led != old_led_nr) {
sendNewConsumption(nb_led);
}
// compteur de mise à jour du calcul de l'énergie (en ms) 15000 = 15 secondes
if (millis() - energyTs > 15000) {
//energie consomé en Wh durant 15 secondes
int load = (nb_led > max_led) ? max_led : nb_led;
debugSerial.println("DATAS0 :");
debugSerial.println(nb_led);
debugSerial.println(max_led);
debugSerial.println(load);
if(max_led >= 3) {
if(load > 3)
output[0] += getEnergy(getPower(3), 15);
else if(load > 0)
output[0] += getEnergy(getPower(load), 15);
else
output[0] += getEnergy(0, 15);
load -= 3;
}
if(max_led >= 6) {
if(load > 3)
output[1] += getEnergy(getPower(3), 15);
else if(load > 0)
output[1] += getEnergy(getPower(load), 15);
else
output[1] += getEnergy(0, 15);
load -= 3;
}
if(max_led >= 9) {
if(load > 3)
output[2] += getEnergy(getPower(3), 15);
else if(load > 0)
output[2] += getEnergy(getPower(load), 15);
else
output[2] += getEnergy(0, 15);
load -= 3;
}
if(is_input[ALT_INDEX])
input[ALT_INDEX] += getEnergy(getPower(2), 15);
debugSerial.println("DATAS :");
debugSerial.println(output[0]);
debugSerial.println(output[1]);
debugSerial.println(output[2]);
debugSerial.println(input[ALT_INDEX]);
energyTs = millis();
}
// compteur pour le timing d'envoi des messages (en ms) 300000 = 5 minutes
if (millis() - timestamp > 60000) {
short state = 0;
if(nb_led < max_led) state = 7;
else if(max_led > 3) state = 3;
else if(max_led == 0) state = 0;
else state = 1;
LpwaOrange.flush();
LpwaOrange.addByte(state);
LpwaOrange.addShort(output[0]);
LpwaOrange.addShort(output[1]);
LpwaOrange.addShort(output[2]);
short total_value = output[0] + output[1] + output[2];
LpwaOrange.addShort(total_value);
LpwaOrange.addShort(input[ALT_INDEX]);
int len;
const char* frame = LpwaOrange.getFramePayload(&len);
debugFrame(frame, len);
switch (LpwaOrange.send(LORA_PORT, (const uint8_t*)frame, len)) {
case NoError:
debugSerial.println("Successful transmission.");
digitalWrite(LED_BUILTIN, LOW);
green();
delay(2000);
white();
break;
case NoResponse:
debugSerial.println("There was no response from the device.");
red();
break ;
case Timeout:
debugSerial.println("Connection timed-out. Check your serial connection to the device! Sleeping for 20sec.");
red();
delay(20000) ;
break ;
case PayloadSizeError:
debugSerial.println("The size of the payload is greater than allowed. Transmission failed!");
red();
break ;
case InternalError:
debugSerial.println("Oh No! This shouldn't happen. Something is really wrong! Try restarting the device!\r\nThe program will now halt.");
red();
while (1) {} ;
break ;
case Busy:
debugSerial.println("The device is busy. Sleeping for 10 extra seconds.");
red();
delay(10000) ;
break ;
case NetworkFatalError:
debugSerial.println("There is a non-recoverable error with the network connection. You should re-connect.\r\nThe program will now halt.");
red();
while (1) {} ;
break ;
case NotConnected:
debugSerial.println("The device is not connected to the network. Please connect to the network before attempting to send data.\r\nThe program will now halt.");
red();
while (1) {} ;
break ;
case NoAcknowledgment:
debugSerial.println("There was no acknowledgment sent back!");
red();
break ;
default:
break ;
}
int frameReceivedSize = LpwaOrange.receive(frameReceived, FRM_PAYLOAD_MAX_LENGTH);
debugSerial.print("received : ");
for (int i = 0; i < frameReceivedSize; i++) {
debugSerial.print(frameReceived[i], HEX);
debugSerial.print(" ");
}
debugSerial.println("");
timestamp = millis();
for (int i = 0; i < SIZE_OUTPUT; i++)
output[i] = 0;
for (int i = 0; i < SIZE_INPUT; i++)
input[i] = 0;
}
if(neighbourSerial.available()) {
processNeighbourRequest();
}
}