-
Notifications
You must be signed in to change notification settings - Fork 1
/
ESP_fauxmo_relay.ino
228 lines (190 loc) · 5.24 KB
/
ESP_fauxmo_relay.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
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif
#include "fauxmoESP.h"
#include <Bounce2.h>
#include "config.h"
// buttons
Bounce * buttons = new Bounce[RelayCount];
// this library does all the wemo things
fauxmoESP fauxmo;
// variables used for auto power off; they store the turn-on-time for each relay.
unsigned long powerOnMillis[RelayCount]; // 0 is off; other values are the ticks at turn on
// setup WIFI connection
void wifiSetup()
{
// Set WIFI module to STA mode
WiFi.mode(WIFI_STA);
// set IP - there is contriting info on when to call config. before or after begin
WiFi.config(ip, gateway, subnet);
Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
// connect to wifi
WiFi.begin(WIFI_SSID, WIFI_PASS);
// Wait until connected
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
// Connected!
Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}
void setup()
{
// setup serial port and clean garbage
Serial.begin(SERIAL_BAUDRATE);
Serial.println();
Serial.println();
// setup Wifi
wifiSetup();
// setup LED
pinMode(LED, OUTPUT);
pinMode(LED, HIGH);
// setup relays
for (int i = 0; i < RelayCount; i = i + 1)
{
Serial.print("setup pin: ");
Serial.print(RelayPins[i]);
Serial.print(" for device: ");
Serial.print(RelayNames[i]);
Serial.println();
pinMode(RelayPins[i], OUTPUT);
digitalWrite(RelayPins[i], HIGH);
powerOnMillis[i] = 0;
}
// setup Buttons
if (UseButtons)
{
Serial.println("setup buttons:");
for (int i = 0; i < RelayCount; i++)
{
if (ButtonPins[i] != 0)
{
Serial.print("setup pin as button: ");
Serial.print(ButtonPins[i]);
Serial.print(" for device: ");
Serial.print(RelayNames[i]);
Serial.println();
buttons[i].attach( ButtonPins[i] , INPUT_PULLUP ); //setup the bounce instance for the current button
buttons[i].interval(ButtonDelay); // interval in ms
}
}
}
// start FAUXMO library
fauxmo.enable(true);
// Add virtual devices
for (int i = 0; i < RelayCount; i = i + 1)
{
fauxmo.addDevice(RelayNames[i]);
}
// fauxmoESP 2.0.0 has changed the callback signature to add the device_id,
// this way it's easier to match devices to action without having to compare strings.
fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state)
{
Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
digitalWrite(LED, !state);
setRelay(device_id, state);
});
// Callback to retrieve current state (for GetBinaryState queries)
fauxmo.onGetState([](unsigned char device_id, const char * device_name)
{
if (device_id < RelayCount)
{
return !digitalRead(RelayPins[device_id]);
}
else
{
Serial.print("Error: got invalid device id: ");
Serial.println(device_id);
}
});
}
void loop()
{
// Let the Library do its thing
fauxmo.handle();
// check buttons
if (UseButtons)
{
for (int i = 0; i < RelayCount; i = i + 1)
{
// do not check uninitialized button pins
if (ButtonPins[i] != 0)
{
// Update the Bounce instance
buttons[i].update();
// If it fell (it was pressed for x ms and was now released), flag the need to toggle
if ( buttons[i].fell() )
{
Serial.print("Button ");
Serial.print(i);
Serial.println(" was pressed!");
if (powerOnMillis[i] == 0)
{
// turn on
setRelay(i, true);
}
else
{
// turn off
setRelay(i, false);
}
}
}
}
}
// check timers for auto power off
unsigned long timeNow = millis();
for (int i = 0; i < RelayCount; i = i + 1)
{
// if relay is active and there is a shutdown time for this relay
if (powerOnMillis[i] > 0 && ShutDownSeconds[i] > 0)
{
unsigned long duration = timeNow - powerOnMillis[i];
// above shutdown duration?
if (duration > ShutDownSeconds[i] * 1000)
{
Serial.print("Auto Shutdown Time exceeded for Relay: ");
Serial.println(RelayNames[i]);
Serial.print("Shutdown after ");
Serial.print(duration / 1000);
Serial.println(" seconds.");
// turn off
setRelay(i, false);
}
}
}
// make sure the ESP has some time to do whatever it wants to do (wifi..)
yield();
}
// turns a relay on or off
void setRelay(unsigned char device_id, bool state)
{
if (device_id < RelayCount)
{
Serial.print("Set Relay: ");
Serial.print(RelayNames[device_id]);
if (state)
{
digitalWrite(RelayPins[device_id], LOW);
Serial.println(" -> on");
// store turn-on time
powerOnMillis[device_id] = millis();;
}
else
{
digitalWrite(RelayPins[device_id], HIGH);
Serial.println(" -> off");
// reset turn-on time (its off)
powerOnMillis[device_id] = 0;
}
}
else
{
Serial.print("Error: got invalid device id: ");
Serial.println(device_id);
}
}