-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathesp8266-AC-control.ino
259 lines (207 loc) · 7.71 KB
/
esp8266-AC-control.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <FS.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ESP8266HTTPUpdateServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <ArduinoJson.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
//// ###### User configuration space for AC library classes ##########
#include <ir_Coolix.h> // replace library based on your AC unit model, check https://github.com/markszabo/IRremoteESP8266
#define auto_mode kCoolixAuto
#define cool_mode kCoolixCool
#define dry_mode kCoolixDry
#define heat_mode kCoolixHeat
#define fan_mode kCoolixFan
#define fan_auto kCoolixFanAuto
#define fan_min kCoolixFanMin
#define fan_med kCoolixFanMed
#define fan_hi kCoolixFanMax
const uint16_t kIrLed = D2; // ESP8266 GPIO pin to use. Recommended: 4 (D2).
IRCoolixAC ac(kIrLed); // Library initialization, change it according to the imported library file.
/// ##### End user configuration ######
struct state {
uint8_t temperature = 22, fan = 0, operation = 0;
bool powerStatus;
};
File fsUploadFile;
//core
state acState;
//settings
char deviceName[] = "AC Remote Control";
ESP8266WebServer server(80);
ESP8266HTTPUpdateServer httpUpdateServer;
bool handleFileRead(String path) { // send the right file to the client (if it exists)
//Serial.println("handleFileRead: " + path);
if (path.endsWith("/")) path += "index.html"; // If a folder is requested, send the index file
String contentType = getContentType(path); // Get the MIME type
String pathWithGz = path + ".gz";
if (SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) { // If the file exists, either as a compressed archive, or normal
if (SPIFFS.exists(pathWithGz)) // If there's a compressed version available
path += ".gz"; // Use the compressed verion
File file = SPIFFS.open(path, "r"); // Open the file
size_t sent = server.streamFile(file, contentType); // Send it to the client
file.close(); // Close the file again
//Serial.println(String("\tSent file: ") + path);
return true;
}
//Serial.println(String("\tFile Not Found: ") + path); // If the file doesn't exist, return false
return false;
}
String getContentType(String filename) { // convert the file extension to the MIME type
if (filename.endsWith(".html")) return "text/html";
else if (filename.endsWith(".css")) return "text/css";
else if (filename.endsWith(".js")) return "application/javascript";
else if (filename.endsWith(".ico")) return "image/x-icon";
else if (filename.endsWith(".gz")) return "application/x-gzip";
return "text/plain";
}
void handleFileUpload() { // upload a new file to the SPIFFS
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
String filename = upload.filename;
if (!filename.startsWith("/")) filename = "/" + filename;
//Serial.print("handleFileUpload Name: "); //Serial.println(filename);
fsUploadFile = SPIFFS.open(filename, "w"); // Open the file for writing in SPIFFS (create if it doesn't exist)
filename = String();
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (fsUploadFile)
fsUploadFile.write(upload.buf, upload.currentSize); // Write the received bytes to the file
} else if (upload.status == UPLOAD_FILE_END) {
if (fsUploadFile) { // If the file was successfully created
fsUploadFile.close(); // Close the file again
//Serial.print("handleFileUpload Size: "); //Serial.println(upload.totalSize);
server.sendHeader("Location", "/success.html"); // Redirect the client to the success page
server.send(303);
} else {
server.send(500, "text/plain", "500: couldn't create file");
}
}
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup() {
//Serial.begin(115200);
//Serial.println();
ac.begin();
delay(1000);
//Serial.println("mounting FS...");
if (!SPIFFS.begin()) {
//Serial.println("Failed to mount file system");
return;
}
WiFiManager wifiManager;
if (!wifiManager.autoConnect(deviceName)) {
delay(3000);
ESP.reset();
delay(5000);
}
httpUpdateServer.setup(&server);
server.on("/state", HTTP_PUT, []() {
DynamicJsonDocument root(1024);
DeserializationError error = deserializeJson(root, server.arg("plain"));
if (error) {
server.send(404, "text/plain", "FAIL. " + server.arg("plain"));
} else {
if (root.containsKey("temp")) {
acState.temperature = (uint8_t) root["temp"];
}
if (root.containsKey("fan")) {
acState.fan = (uint8_t) root["fan"];
}
if (root.containsKey("power")) {
acState.powerStatus = root["power"];
}
if (root.containsKey("mode")) {
acState.operation = root["mode"];
}
String output;
serializeJson(root, output);
server.send(200, "text/plain", output);
delay(200);
if (acState.powerStatus) {
ac.on();
ac.setTemp(acState.temperature);
if (acState.operation == 0) {
ac.setMode(auto_mode);
ac.setFan(fan_auto);
acState.fan = 0;
} else if (acState.operation == 1) {
ac.setMode(cool_mode);
} else if (acState.operation == 2) {
ac.setMode(dry_mode);
} else if (acState.operation == 3) {
ac.setMode(heat_mode);
} else if (acState.operation == 4) {
ac.setMode(fan_mode);
}
if (acState.operation != 0) {
if (acState.fan == 0) {
ac.setFan(fan_auto);
} else if (acState.fan == 1) {
ac.setFan(fan_min);
} else if (acState.fan == 2) {
ac.setFan(fan_med);
} else if (acState.fan == 3) {
ac.setFan(fan_hi);
}
}
} else {
ac.off();
}
ac.send();
}
});
server.on("/file-upload", HTTP_POST, // if the client posts to the upload page
[]() {
server.send(200);
}, // Send status 200 (OK) to tell the client we are ready to receive
handleFileUpload // Receive and save the file
);
server.on("/file-upload", HTTP_GET, []() { // if the client requests the upload page
String html = "<form method=\"post\" enctype=\"multipart/form-data\">";
html += "<input type=\"file\" name=\"name\">";
html += "<input class=\"button\" type=\"submit\" value=\"Upload\">";
html += "</form>";
server.send(200, "text/html", html);
});
server.on("/", []() {
server.sendHeader("Location", String("ui.html"), true);
server.send ( 302, "text/plain", "");
});
server.on("/state", HTTP_GET, []() {
DynamicJsonDocument root(1024);
root["mode"] = acState.operation;
root["fan"] = acState.fan;
root["temp"] = acState.temperature;
root["power"] = acState.powerStatus;
String output;
serializeJson(root, output);
server.send(200, "text/plain", output);
});
server.on("/reset", []() {
server.send(200, "text/html", "reset");
delay(100);
ESP.reset();
});
server.serveStatic("/", SPIFFS, "/", "max-age=86400");
server.onNotFound(handleNotFound);
server.begin();
}
void loop() {
server.handleClient();
}