-
Notifications
You must be signed in to change notification settings - Fork 0
/
iotlabACremote.ino
215 lines (190 loc) · 4.53 KB
/
iotlabACremote.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
#include <ESP8266WiFi.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <FS.h>
#include <ArduinoJson.h>
#include "ir_LG.h"
const char *ssid = "!!"; //
const char *password = "findyourself";
int irpin = 4; // D2 in our configuration
IRsend irsend(irpin);
WiFiServer server(80);
typedef struct ACSettings
{
uint8_t power;
uint8_t temperature;
uint8_t fanSpeed;
uint8_t mode;
} ACSettings;
ACSettings acSettings;
void setup()
{
Serial.begin(115200);
irsend.begin();
if (!SPIFFS.begin())
{
Serial.println("SPIFFS Mount Failed");
return;
}
loadSettings();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
server.begin();
}
void loop()
{
WiFiClient client = server.available();
if (client)
{
String request = client.readStringUntil('\r');
client.flush();
if (request.indexOf("/power") != -1)
{
acSettings.power = !acSettings.power;
irsend.sendLG(acSettings.power ? kLgAcPowerOn : kLgAcPowerOff); // Power code
}
else if (request.indexOf("/tempup") != -1)
{
if (acSettings.temperature < kLgAcMaxTemp)
{
acSettings.temperature++;
irsend.sendLG(0x20DF40BF); // Temp Up code
}
}
else if (request.indexOf("/tempdown") != -1)
{
if (acSettings.temperature > kLgAcMinTemp)
{
acSettings.temperature--;
irsend.sendLG(0x20DF20DF); // Temp Down code
}
}
else if (request.indexOf("/fan") != -1)
{
acSettings.fanSpeed = (acSettings.fanSpeed + 1) % kLgAcFanEntries; // fan speeds
irsend.sendLG(convertFan(acSettings.fanSpeed)); // Fan Speed code
}
else if (request.indexOf("/mode") != -1)
{
acSettings.mode = (acSettings.mode + 1) % 5; // modes (0-4)
irsend.sendLG(convertMode(acSettings.mode)); // Mode code
}
else if (request.indexOf("/style.css") != -1)
{
serveFile("/style.css", "text/css");
return;
}
else if (request.indexOf("/script.js") != -1)
{
serveFile("/script.js", "application/javascript");
return;
}
else if (request.indexOf("/") != -1)
{
serveFile("/index.html", "text/html");
return;
}
saveSettings();
}
}
void serveFile(const char *filename, const char *contentType)
{
File file = SPIFFS.open(filename, "r");
if (!file)
{
Serial.println("Failed to open file for reading");
return;
}
WiFiClient client = server.available();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: " + String(contentType));
client.println("Connection: close");
client.println();
while (file.available())
{
client.write(file.read());
}
file.close();
}
void saveSettings()
{
File file = SPIFFS.open("/settings.json", "w");
if (!file)
{
Serial.println("Failed to open file for writing");
return;
}
StaticJsonDocument<200> doc;
doc["power"] = acSettings.power;
doc["temperature"] = acSettings.temperature;
doc["fanSpeed"] = acSettings.fanSpeed;
doc["mode"] = acSettings.mode;
if (serializeJson(doc, file) == 0)
{
Serial.println("Failed to write to file");
}
file.close();
}
void loadSettings()
{
File file = SPIFFS.open("/settings.json", "r");
if (!file)
{
Serial.println("Failed to open file for reading");
return;
}
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, file);
if (error)
{
Serial.print("Failed to read file, using default configuration: ");
Serial.println(error.f_str());
return;
}
file.close();
acSettings.power = doc["power"];
acSettings.temperature = doc["temperature"];
acSettings.fanSpeed = doc["fanSpeed"];
acSettings.mode = doc["mode"];
}
uint32_t convertFan(uint8_t speed)
{
switch (speed)
{
case kLgAcFanLowest:
return 0x20DF00FF; // Replace
case kLgAcFanLow:
return 0x20DF01FE; // Replace
case kLgAcFanMedium:
return 0x20DF02FD; // Replace
case kLgAcFanMax:
return 0x20DF03FC; // Replace
case kLgAcFanAuto:
return 0x20DF04FB; // Replace
default:
return 0x20DF00FF; // lowest
}
}
uint32_t convertMode(uint8_t mode)
{
switch (mode)
{
case kLgAcCool:
return 0x20DF05FA; // Replace
case kLgAcDry:
return 0x20DF06F9; // Replace
case kLgAcFan:
return 0x20DF07F8; // Replace
case kLgAcAuto:
return 0x20DF08F7; // Replace
case kLgAcHeat:
return 0x20DF09F6; // Replace
default:
return 0x20DF05FA; // code for cool mode
}
}