-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fireplace-Screen.ino
208 lines (175 loc) · 4.87 KB
/
Fireplace-Screen.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
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <fauxmoESP.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <RCSwitch.h>
#define ledPin 2
const uint16_t kIrLed = 5; //Pin D1 for IR led
const uint16_t radio = 4; //Pin D2 for radio transmit
fauxmoESP fauxmo;
ESP8266WebServer server(81);
IRsend irsend(kIrLed);
RCSwitch rf = RCSwitch();
void setup() {
Serial.begin(115200);
irsend.begin();
rf.enableTransmit(radio);
rf.setProtocol(1);
if (setupNetwork()) {
// Setup fauxmo
Serial.println("Adding Devices");
fauxmo.setPort(80);
fauxmo.enable(true);
// Add devices here
fauxmo.addDevice("Hearth");
fauxmo.addDevice("Screen");
// Set a callback function for when a device is found
fauxmo.onSetState([](unsigned char device_id, const char * device_name, bool state, unsigned char value) {
Serial.printf("[MAIN] Device #%d (%s) state: %s value: %d\n", device_id, device_name, state ? "ON" : "OFF", value);
if (strcmp(device_name, "Hearth") == 0) {
if (state) {
Serial.println("Turning on Hearth");
fireplaceOnOff();
}
else {
Serial.println("Turning off Hearth");
fireplaceOnOff();
}
}
if (strcmp(device_name, "Screen") == 0) {
if (state) {
Serial.println("Lowering Screen");
screenDown();
}
else {
Serial.println("Raising Screen");
screenUp();
}
}
});
}
}
void loop() {
fauxmo.handle();
server.handleClient();
}
boolean setupNetwork() {
// Let us connect to WiFi
WiFi.mode(WIFI_STA);
String ssid = "<Wifi-Network-Name>";
String password = "<Wifi-Network-Password>";
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(".......");
Serial.println("WiFi Connected....IP Address:");
Serial.println(WiFi.localIP());
Serial.println(WiFi.macAddress());
routeServer();
server.enableCORS(true);
server.begin();
Serial.println("HTTP server live");
return true;
}
//Server Routing
void routeServer () {
server.on("/", HTTP_GET, []() {
server.send(200, F("text/html"),
F("ESP8266 Controller Basement"));
});
server.on("/screenUp", HTTP_GET, screenUp);
server.on("/screenDown", HTTP_GET, screenDown);
server.on("/screenStop", HTTP_GET, screenStop);
server.on("/fireplaceOn", HTTP_GET, fireplaceOnOff);
server.on("/fireplaceHeat", HTTP_GET, fireplaceHeat);
server.on("/fireplaceTimer", HTTP_GET, fireplaceTimer);
server.on("/fireplaceDimmer", HTTP_GET, fireplaceDimmer);
server.on("/fireplaceSpeed", HTTP_GET, fireplaceSpeed);
server.on("/fireplaceA", HTTP_GET, fireplaceA);
server.on("/fireplaceB", HTTP_GET, fireplaceB);
server.onNotFound(handleNotFound);
}
void handleNotFound()
{
if (server.method() == HTTP_OPTIONS)
{
server.sendHeader("Access-Control-Allow-Origin", "*");
server.sendHeader("Access-Control-Max-Age", "10000");
server.sendHeader("Access-Control-Allow-Methods", "PUT,POST,GET,OPTIONS");
server.sendHeader("Access-Control-Allow-Headers", "*");
server.send(204);
}
else
{
server.send(404, "text/plain", "");
}
}
//Fireplace Commands
void fireplaceOnOff() {
Serial.println("Fireplace On/off");
server.send(200, "text/plain", "On/off");
irsend.sendNEC(0x10EF8877, 32, 3);
delay(500);
}
void fireplaceHeat() {
Serial.println("Heat");
server.send(200, "text/plain", "Heat");
irsend.sendNEC(0x10EF28D7, 32, 3);
delay(500);
}
void fireplaceTimer() {
Serial.println("Timer");
server.send(200, "text/plain", "Timer");
irsend.sendNEC(0x10EFC837, 32, 3);
delay(500);
}
void fireplaceDimmer() {
Serial.println("Dimmer");
server.send(200, "text/plain", "Dimmer");
irsend.sendNEC(0x10EF48B7, 32, 3);
delay(500);
}
void fireplaceSpeed() {
Serial.println("Speed");
server.send(200, "text/plain", "Speed");
irsend.sendNEC(0x10EFA857, 32, 3);
delay(500);
}
void fireplaceA() {
Serial.println("Color A");
server.send(200, "text/plain", "A");
irsend.sendNEC(0x10EFB04F, 32, 3);
delay(500);
}
void fireplaceB() {
Serial.println("Color B");
server.send(200, "text/plain", "B");
irsend.sendNEC(0x10EF708F, 32, 3);
delay(500);
}
//Screen Commands
void screenUp() {
server.send(200, "text/plain", "Screen Up");
Serial.println("Up a doo");
rf.send(6582737, 24);
delay(300);
rf.send(6582737, 24);
}
void screenDown() {
server.send(200, "text/plain", "Screen Down");
Serial.println("Down a doo");
rf.send(6582740, 24);
delay(300);
rf.send(6582740, 24);
}
void screenStop() {
server.send(200, "text/plain", "Screen Stop");
Serial.println("Stop a doo");
rf.send(6582738, 24);
}