-
Notifications
You must be signed in to change notification settings - Fork 0
/
caffeinator.ino
51 lines (43 loc) · 1.2 KB
/
caffeinator.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
// This #include statement was automatically added by the Particle IDE.
#include "RunningAverage/RunningAverage.h"
// This #include statement was automatically added by the Particle IDE.
#include "EmonLib/EmonLib.h"
EnergyMonitor emon1;
RunningAverage samples(10);
int i = 0;
bool brewin = false;
unsigned long startTime;
unsigned long brewTime = 0;
double power;
static const int LOWER_THRESHOLD = 200;
static const int UPPER_THRESHOLD = 1000;
void setup()
{
Spark.variable("power", &power, DOUBLE);
Spark.variable("brewin", &brewin, BOOLEAN);
Spark.variable("brew_time", &brewTime, INT);
emon1.current(1, 111.1); // Current: input pin, calibration.
}
void loop()
{
double Irms = emon1.calcIrms(1480); // Calculate Irms only
i++;
samples.addValue(Irms * 230);
if (i == 20) {
power = samples.getAverage();
samples.clear();
i = 0;
if (brewin) {
brewTime = millis() - startTime;
}
if (power < LOWER_THRESHOLD && brewin) {
brewin = false;
Spark.publish("finished_brewin", (String)brewTime);
}
if (power > UPPER_THRESHOLD && !brewin) {
startTime = millis();
brewin = true;
Spark.publish("started_brewin");
}
}
}