-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwaterer.js
80 lines (71 loc) · 2.12 KB
/
waterer.js
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
var WATER = D1;
var FEED = D2;
var TIME_WATERING = 3*60; // in seconds
var TIME_FEEDING = 60; // in seconds
var AVERAGE_READINGS = 2; // readings per entry in our history arrays
var hadWater = false;
var tempHistory = new Int8Array(100);
var lightHistory = new Uint8Array(100);
var tempAverage = 0;
var lightAverage = 0;
var readingCount = 0;
function waterPlants(feedAsWell) {
console.log("Watering plants");
WATER.set();
setTimeout(function() {
WATER.reset();
}, TIME_WATERING*1000);
if (feedAsWell) {
FEED.set();
setTimeout(function() {
FEED.reset();
}, TIME_FEEDING*1000);
}
}
function onTick() {
// take readings and store them in a history array
tempAverage += E.getTemperature();
lightAverage += Puck.light()*100;
readingCount++;
if (readingCount>=AVERAGE_READINGS) {
for (var i=0;i<tempHistory.length-1;i++)
tempHistory[i]=tempHistory[i+1];
tempHistory[tempHistory.length-1] = tempAverage / readingCount;
tempAverage = 0;
for (i=0;i<lightHistory.length-1;i++)
lightHistory[i]=lightHistory[i+1];
lightHistory[tempHistory.length-1] = lightAverage / readingCount;
lightAverage = 0;
readingCount = 0;
}
// check about our plant watering
var now = new Date();
var h = now.getHours();
var day = now.getDay(); // day of week
var isMorning = h==8;
var isEvening = h==19;
if (isMorning || isEvening) {
// feed in the morning on mon, weds, fri
var doFeed = isMorning && (day==1 || day==3 || day==5);
if (!hadWater) waterPlants(doFeed);
hadWater = true;
} else {
hadWater = false;
}
}
/* In 1v93 and later you can use this to set your timezone
so the time matches where you are. For 1v92 you'll have to
change the hours in 'onTick' manually */
// E.setTimeZone(-8 /* PST */);
// Change the name that appears when you connect
NRF.setAdvertising({
}, {name:"Puck.js Waterer"});
// Check watering every 30 minutes
setInterval(onTick, 30*60000);
// When a button is pressed, do 30 seconds of watering
setWatch(function() {
WATER.set();
setTimeout(function() {
WATER.reset();
}, 30000);
}, BTN, {edge:"rising",debounce:50,repeat:true});