forked from N1s0/Mutualab-Make-Make
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino.ino
344 lines (283 loc) · 8.25 KB
/
arduino.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
///////////////////
// Includes //
///////////////////
#include <avr/sleep.h>
#include <avr/power.h>
#include "printf.h"
#include <EmonLib.h> // https://github.com/openenergymonitor/EmonLib
#include <LCD5110_Graph.h> //http://http://www.rinkydinkelectronics.com/download.php?f=LCD5110_Graph.zip
///////////////////////
// CONFIGURATION //
///////////////////////
#define DEBUG 0
#define SERIAL_BAUDRATE 57600
//Value text wich will change
String ConsoW = "Conso (W)";
String ConsoWh = "Conso (Wh)";
// Calibration factor for the intensity
double ICAL = 85.0;
uint8_t valeurs[40];
// Set this to 0 to enable voltage measurement.
// Else, set this to your mean voltage.
double VOLTAGE = 240.0;
// Number of samples over which the mean must be done for the current measurement
int NUMBER_SAMPLES_I = 1480;
// Pin for current measurement
const int CURRENT_PIN = A0;
// Pin for voltage measurement
const int VOLTAGE_PIN = 0;
// Pin for the green LED
const int GREEN_LED_PIN = 2;
// Pin for the red LED
const int RED_LED_PIN = 3;
// Pin for the LCD BackLight
const int BACKLIGHT_PIN = 8;
//external data (from other file)
extern uint8_t CitizenLogo[];
extern uint8_t SmallFont[];
extern uint8_t TinyFont[];
extern uint8_t MediumNumbers[];
// Energy Monitor object
EnergyMonitor emon1;
///////////////////////
// Declarations //
///////////////////////
#define TIMEOUT 250 // timeout in ms
// Struct to send RF data
typedef struct {
int power;
int voltage;
int battery;
unsigned long timer;
long padding4;
int padding2;
} PayloadTX;
PayloadTX nrf={0,0,0,0,0,0};
////////////////////////////////
// Calculations //
////////////////////////////////
int nb_ech =0; // Number of captations
int tot_ech =0; //
float Conso_cumu = 0.0;
////////////////////////////////
// Hardware configuration //
////////////////////////////////
// LCD Definition
LCD5110 myGLCD(13, 12, 11, 9, 10);
//////////////////////////////
// Sleep configuration //
//////////////////////////////
typedef enum { wdt_16ms = 0, wdt_32ms, wdt_64ms, wdt_128ms, wdt_250ms, wdt_500ms, wdt_1s, wdt_2s, wdt_4s, wdt_8s } wdt_prescalar_e;
void setup_watchdog(uint8_t prescalar);
void do_sleep(void);
unsigned long start, finished, elapsed;
//////////////////////////
// Setup operation //
//////////////////////////
void setup(void)
{
Serial.begin(57600);
Serial.begin(SERIAL_BAUDRATE);
Serial.println(F("/!\\ STARTING CitizenWatt Sensor"));
Serial.println(F("//////////////////////////////"));
Serial.println(F("// CitizenWatt sensor //"));
Serial.println(F("// citizenwatt.paris //"));
Serial.println(F("////////////////////////////// \n"));
//Define "put" mode of each thing
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(BACKLIGHT_PIN, OUTPUT);
//startup's light
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(BACKLIGHT_PIN, HIGH);
//Start screen
myGLCD.InitLCD(60);
myGLCD.clrScr();
myGLCD.drawBitmap(0, 0, CitizenLogo, 84, 48);
myGLCD.update();
myGLCD.setFont(TinyFont);
myGLCD.print("Initialisation...", CENTER, 40);
myGLCD.print("Le Mutualab presente", CENTER, 1);
myGLCD.update();
// Init EnergyMonitor, with calibration factor for R1 = 22 Ohms
emon1.current(0, ICAL);
//
// Prepare sleep parameters
//
setup_watchdog(wdt_1s); // /!\ 8s sleeping
//Calibration of the sensor
for (int i=0; i<9;i++){
digitalWrite(GREEN_LED_PIN, HIGH);
nrf.power = (int) (emon1.calcIrms(NUMBER_SAMPLES_I) * VOLTAGE);
digitalWrite(GREEN_LED_PIN, LOW);
do_sleep();
}
//Same but with a coolest screen :)
for (int fg=0; fg<15; fg++) {
digitalWrite(GREEN_LED_PIN, HIGH);
nrf.power = (int) (emon1.calcIrms(NUMBER_SAMPLES_I) * VOLTAGE);
digitalWrite(GREEN_LED_PIN, LOW);
myGLCD.clrScr();
myGLCD.setFont(SmallFont);
myGLCD.print("Chargement", 3, 20);
myGLCD.update();
digitalWrite(GREEN_LED_PIN, HIGH);
nrf.power = (int) (emon1.calcIrms(NUMBER_SAMPLES_I) * VOLTAGE);
digitalWrite(GREEN_LED_PIN, LOW);
myGLCD.clrScr();
myGLCD.print("Chargement.", 3, 20);
myGLCD.update();
digitalWrite(GREEN_LED_PIN, HIGH);
nrf.power = (int) (emon1.calcIrms(NUMBER_SAMPLES_I) * VOLTAGE);
digitalWrite(GREEN_LED_PIN, LOW);
myGLCD.clrScr();
myGLCD.print("Chargement..", 3, 20);
myGLCD.update();
digitalWrite(GREEN_LED_PIN, HIGH);
nrf.power = (int) (emon1.calcIrms(NUMBER_SAMPLES_I) * VOLTAGE);
digitalWrite(GREEN_LED_PIN, LOW);
myGLCD.clrScr();
myGLCD.print("Chargement...", 3, 20);
myGLCD.update();
}
delay(1500);
myGLCD.clrScr();
myGLCD.print("COMPLETE :)", CENTER, 20);
myGLCD.update();
delay(2000);
if ( DEBUG )
printf_begin();
// Init Done, turn off LEDs.
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
start=millis();
}
///////////////////////////////
// Loop part of the code //
///////////////////////////////
void loop(void)
{
//
// Read Current
digitalWrite(BACKLIGHT_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, HIGH);
nrf.power = (int) (emon1.calcIrms(NUMBER_SAMPLES_I) * VOLTAGE);
digitalWrite(GREEN_LED_PIN, LOW);
// Set voltage
nrf.voltage = VOLTAGE;
// Read Vcc
nrf.battery = (int) emon1.readVcc();
//
// Calulation
//
int tot = 0;
valeurs[35] = nrf.power;
for (int i = 0; i < 35; i++) {
valeurs[i] = valeurs[i + 1];
tot = tot + valeurs[i];
}
//define the clock at start of mesures
finished=millis();
float h,m,s,ms;
unsigned long over;
elapsed=finished-start;
h=int(elapsed/3600000);
over=elapsed%3600000;
m=int(over/60000);
over=over%60000;
s=int(over/1000);
if (m == 30) {
digitalWrite(RED_LED_PIN, HIGH); // Funny things happen at 30min and 1 hour :)
};
if (h >= 1) {
digitalWrite(BACKLIGHT_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
};
nb_ech++;
if (nb_ech>=10){
tot_ech+= nrf.power;
Conso_cumu+=(float)nrf.power/3600;
}
//
// Display
//
myGLCD.clrScr();
myGLCD.drawRect(0, 0, 35, 20);
for (int i = 0; i <= 35; i++)
{
myGLCD.drawLine(i , 20, i, 20 - map(valeurs[i], 0, 300, 0, 20));
}
ConsoW = "Conso (W)";
ConsoWh = "Conso (Wh)";
//Display of the screen
myGLCD.setFont(TinyFont);
myGLCD.print(ConsoW, 0, 22);
myGLCD.print(ConsoWh, 37, 0);
myGLCD.print("Duree", 52, 22);
myGLCD.printNumI((int)h, 52, 30);
myGLCD.printNumI((int)m, 62, 30);
myGLCD.printNumI((int)s, 72, 30);
if (nrf.power >= 1000) { //Transform Value of Instant conso into kiloWatt
nrf.power = nrf.power /1000; //
ConsoW = "Conso (kW)"; //Transform name of the value "W" into "kW"
};
if (Conso_cumu >= 1000) { //
Conso_cumu = Conso_cumu /1000; // Same for the total consommation
ConsoWh = "Conso (kWh)"; //
};
myGLCD.setFont(MediumNumbers);
myGLCD.printNumI(nrf.power /2, LEFT, 28);
myGLCD.printNumF(Conso_cumu,2, 36,5 );
myGLCD.setFont(TinyFont);
myGLCD.print("mutualab.org", 37, 39); //Some advert for our association :)
myGLCD.print(ConsoW, 0, 22);
myGLCD.print(ConsoWh, 37, 0);
myGLCD.update();
if ( DEBUG )
{
Serial.print("|");
Serial.print(nrf.power);
Serial.print("\t");
Serial.print("|");
Serial.print("\t");
Serial.print(nrf.voltage);
Serial.print("\t");
Serial.print("|");
Serial.print("\t");
Serial.print(nrf.battery);
Serial.print("\t");
Serial.println("|");
}
// Sleep the MCU. The watchdog timer will awaken in a short while, and
// continue execution here.
delay(1000);
// 100ms for the MCU to settle
delay(100);
}
//////////////////////////
// Sleep functions //
//////////////////////////
// 0=16ms, 1=32ms,2=64ms,3=125ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(uint8_t prescalar)
{
prescalar = min(9, prescalar);
uint8_t wdtcsr = prescalar & 7;
if ( prescalar & 8 )
wdtcsr |= _BV(WDP3);
MCUSR &= ~_BV(WDRF);
WDTCSR = _BV(WDCE) | _BV(WDE);
WDTCSR = _BV(WDCE) | wdtcsr | _BV(WDIE);
}
ISR(WDT_vect)
{
}
void do_sleep(void)
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
sleep_mode(); // System sleeps here
sleep_disable(); // System continues execution here when watchdog timed out
}
//by Nino