-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspecifics.h
227 lines (187 loc) · 7.63 KB
/
specifics.h
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
/* notes:
* blinds and catfeeder are using feather huzzah.
* bedroom lamp and teddy night light
* todo: keep next event last event somewhere visible.
* todo: individual refresh.
* todo: serial print can go quiet, should I remove?
* think about adding days of week to schedule.
* think about allowing someone to choose the schedule via web ui.
* html has an error. it doesn't poll the local devices for updates.
*/
/* nomenclature */
#define AP_NAME "Bedroom" //gets used for access point name on wifi configuration and mDNS
#define ALEXA_DEVICE_1 "bedroom" //gets used for Alexa discovery and commands (must be lowercase)
#define AP_DESC "Modded Lamp from The Range" //used for dash.
#define AP_VERSION "1.1t"
/* parameters for this implementation */
#define BLUE_LED BUILTIN_LED // pin for wemos D1 MINI PRO's onboard blue led
#define LED_OFF HIGH // let's me flip the values if required, as the huzzah onboard LEDs are reversed.
#define LED_ON LOW // as line above.
#define SWITCH_PIN D2 // pin connected to PUSH TO CLOSE switch.
#define RELAY_PIN D1
#define SWITCH_WIRED_IN true
#define EVENT_COUNT 1 + 4 // zero based array. Option 0 is reserved.
#define BUTTON_PUSHED 1
#define BUTTON_RELEASED 0
int lastDayFromWeb = 0; //I use this to detect a new date has occurred.
int currentMinuteOfDay = 0;
typedef struct {
bool online;
bool usingTimer;
bool dst;
bool powered;
String operating_mode;
bool skippingNext;
String lastAction;
/* unused members */
byte percentage;
String perc_label;
} progLogic;
progLogic thisDevice = {false, true, false, true,"toggle",false,"Powered on",
0,""};
typedef struct {
byte h;
byte m;
bool enacted;
char label[14];
byte powered;
} event_time; // event_time is my custom data type.
event_time dailyEvents[EVENT_COUNT] = {
{ 0, 0, false, "reserved", 0},
{ 8, 0, false, "wakeup", 1},
{ 8, 20, false, "power-down", 0},
{22, 00, false, "bed-time", 1},
{23, 00, false, "good-night", 0}
}; // this is my array of dailyEvents.
void doEvent(byte eventIndex = 0) {
Serial.print(F("\n called doEvent for scheduled item "));
Serial.println(dailyEvents[eventIndex].label);
dailyEvents[eventIndex].enacted = true;
thisDevice.powered = dailyEvents[eventIndex].powered;
Serial.println(thisDevice.powered ? "-> Powered" : "-> Off");
}
void handleLocalSwitch(){
static int prevSwitchPosition = 0;
static unsigned long nextTriggerAfter = 0;
const long DEBOUNCE = 500;
bool debounced = false;
int thisReading = 0;
//now read the switch and see if its changed
if(SWITCH_WIRED_IN){
thisReading = digitalRead(SWITCH_PIN);
}else{
if (Serial.available()) {
int incomingByte = Serial.read();
thisReading = prevSwitchPosition +1;
}
}
if (thisReading != prevSwitchPosition){
//test for debounce
debounced = ( (long) ( millis() - nextTriggerAfter ) >= 0);
if(debounced){
Serial.print(F("toggling power level: "));
thisDevice.powered = !thisDevice.powered;
Serial.println(thisDevice.powered ? "Powered" : "Off");
prevSwitchPosition = thisReading;
nextTriggerAfter = millis() + DEBOUNCE;
}else{
Serial.println(F("bouncing"));
}
}
}
void RunImplementationSetup(){
pinMode(BLUE_LED, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(SWITCH_PIN, INPUT);
digitalWrite(BLUE_LED, LED_OFF);
}
void RunImplementationLoop(){
static int minuteOfLastCheck = -1;
static int dayOfLastCheck = -1;
byte activeEvent = 0;
time_t t = now();
thisDevice.online = (timeStatus()==timeSet);
if(day(t) != dayOfLastCheck){
setupForNewDay();
dayOfLastCheck = day(t);
}
if (minute(t) != minuteOfLastCheck){
currentMinuteOfDay = (hour(t) * 60) + (minute(t)); //already includes DST adjustment.
//now use currentMinuteOfDay to work out whether we should perform an event.
if(thisDevice.usingTimer){
activeEvent = (isEventTime(currentMinuteOfDay)); //activeEvent will be 0 if none, or the index+1 of the event struct.
if (activeEvent > 0) {
if(thisDevice.skippingNext){
Serial.println(F("skipping this event"));
thisDevice.lastAction = "Skipped the scheduled event at " + padDigit(hour()) + ":" + padDigit(minute()) + ":" + padDigit(second());
thisDevice.skippingNext = false;
}else{
thisDevice.lastAction = "Set master from schedule at " + padDigit(hour()) + ":" + padDigit(minute()) + ":" + padDigit(second());
doEvent(activeEvent); //pass in the index of the active event, so that we can set it to enacted, and access the label.
}
dailyEvents[activeEvent].enacted = true; //either way, this has now happened.
}
}
minuteOfLastCheck = minute(t);
}
handleLocalSwitch();
digitalWrite(RELAY_PIN, thisDevice.powered);
digitalWrite(BLUE_LED, (thisDevice.powered ? LED_ON : LED_OFF));
}
bool FirstDeviceOn() {
Serial.print("Switch 1 turn on ...");
thisDevice.lastAction = "Powered on by Alexa at " + padDigit(hour()) + ":" + padDigit(minute()) + ":" + padDigit(second());
thisDevice.powered = true;
}
bool FirstDeviceOff() {
Serial.print(F("Switch 1 turn off ..."));
thisDevice.lastAction = "Powered off by Alexa at " + padDigit(hour()) + ":" + padDigit(minute()) + ":" + padDigit(second());
thisDevice.powered = false;
}
void handleAction(){
Serial.println("action request");
Serial.println(httpServer.args());
bool settingMaster = (httpServer.argName(0) == "master");
bool settingSkip = (httpServer.argName(0) == "skip");
bool settingTimer = (httpServer.argName(0) == "timer");
bool usingCallback = (httpServer.hasArg("callback"));
String actionResult = "";
if(settingMaster){
thisDevice.lastAction = "Set master from web UI at " + padDigit(hour()) + ":" + padDigit(minute()) + ":" + padDigit(second());
thisDevice.powered = (httpServer.arg(0)=="true");
if(thisDevice.powered){
actionResult = "{\"message\":\"Master turned on manually\"}";
}else{
actionResult = "{\"message\":\"Master turned off manually\"}";
}
}else if(settingSkip){
thisDevice.skippingNext = (httpServer.arg(0)=="true");
if(thisDevice.skippingNext){
actionResult = "{\"message\":\"Next event will be skipped\"}";
}else{
actionResult = "{\"message\":\"Skip event request cancelled\"}";
}
}else if(settingTimer){
thisDevice.usingTimer = (httpServer.arg(0)=="true");
if(thisDevice.usingTimer){
actionResult = "{\"message\":\"Timer has been enabled\"}";
}else{
actionResult = "{\"message\":\"Timer has been disabled\"}";
}
}else{
actionResult = "{\"message\":\"Did not recognise action\"}";
}
httpServer.sendHeader("Access-Control-Allow-Origin","*");
if(usingCallback){
httpServer.send(200, "text/javascript", httpServer.arg("callback") + "(" + actionResult + ");");
Serial.println(F("Sending response with callback"));
}else{
httpServer.sendHeader("Access-Control-Allow-Origin","*");
httpServer.sendHeader("Server","ESP8266-AA");
httpServer.send(200, "application/json", actionResult);
Serial.println(F("Sending response without callback"));
}
Serial.print(F("Master:")); Serial.println(thisDevice.powered ? "Powered" : "Off");
Serial.print(F("Timer:")); Serial.println(thisDevice.usingTimer ? "Enabled" : "Disabled");
Serial.print(F("Skipping:"));Serial.println(thisDevice.skippingNext ? "Yes" : "No");
}