-
Notifications
You must be signed in to change notification settings - Fork 6
/
Wasp.ino
83 lines (58 loc) · 1.72 KB
/
Wasp.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
#include "Arduino.h"
#include "webserver.h"
#include "relay.h"
#include "rules.h"
//#include "rtc.h"
#include "ntp.h"
long previousTimer = 0;
uint8_t today = 0;
void setup(void) {
//ntp client init
ntp_init();
//init relay shield
rel_init();
//init web server
srv_init();
}
String lastRuleTriggered = "";
void loop(void) {
srv_handle_client();
ntp_refresh();
//check the rules and eventually perfoms action
if (!rules_load()){
DBG_OUTPUT_PORT.println(F("wasp|Can't load rules file!"));
while(1);
}
long l_currentTimer = millis();
//check rules each CHECK_RULES_DELAY ms if server available
if (l_currentTimer - previousTimer > CHECK_RULES_INTERVAL){
//ntp_print_date_time();
String l_currentRule = "";
for(size_t l_nIndex = 1; l_nIndex < MAX_RULES_NUM; l_nIndex++){
l_currentRule = "rule"+String(l_nIndex);
if(jsonRules->containsKey(l_currentRule)){
JsonVariant l_rule = jsonRules->get<JsonVariant>(l_currentRule);
int l_ruleHour = atoi(l_rule["hour"]);
int l_ruleMinute = atoi(l_rule["minute"]);
bool l_ruleAction = l_rule["action"];
//it's time to perform an action?
if (l_ruleHour == ntp_hour_now()
&& l_ruleMinute == ntp_minute_now()
&& l_currentRule != lastRuleTriggered){
DBG_OUTPUT_PORT.println(F("wasp|time to rule!"));
//toggle output if actual state is the high and rule action is false (or viceversa)
if ((rel_get_status() == HIGH && !l_ruleAction)
|| (rel_get_status() == LOW && l_ruleAction)){
rel_toggle_output();
}
lastRuleTriggered = l_currentRule;
}
}
}
if (today != ntp_day_now()){
lastRuleTriggered = "";
today = ntp_day_now();
}
previousTimer = l_currentTimer;
}
}