-
Notifications
You must be signed in to change notification settings - Fork 1
/
Smart_RGBW_Controller.ino
337 lines (298 loc) · 8.7 KB
/
Smart_RGBW_Controller.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
ESP8266WebServer server(80);
char* ssid = "Smart_Connect";
char* passphrase = "";
String st;
String content;
int statusCode;
int restartmin = 60;
const int d1 = 5;
const int d2 = 4;
const int d3 = 0;
const int d4 = 2;
const int d5 = 14;
const int d6 = 12;
const int d7 = 13;
const int d8 = 15;
const int d9 = 3;
const int d10 = 1;
//Pins
const int ledPinR = d1;
const int ledPinG = d2;
const int ledPinB = d3;
const int ledPinW = d5;
const int powerRelais = d6; // GPIO13 D7
const int powerRelais2 = d7;
//RGBW
int r = 0;
int g = 0;
int b = 0;
int w = 0;
int cycle;
float cycleNum;
void setup() {
Serial.begin(115200); //baud
EEPROM.begin(512);
//Define Pins
pinMode(ledPinR,OUTPUT);
pinMode(ledPinG,OUTPUT);
pinMode(ledPinB,OUTPUT);
pinMode(ledPinW,OUTPUT);
pinMode(powerRelais,OUTPUT);
pinMode(powerRelais2,OUTPUT);
//Start PWm Pins max duty = 255 o 1023
cycle = 1023;
cycleNum = cycle / 10;
analogWrite(ledPinR, 0);
analogWrite(ledPinG, 0);
analogWrite(ledPinB, 0);
analogWrite(ledPinW, 0);
digitalWrite(powerRelais, HIGH);
digitalWrite(powerRelais2, HIGH);
Serial.println("System Online");
Serial.println("Version: 1.2");
restartmin = restartmin *60 * 1000;
//connection
// read eeprom for ssid and pass
Serial.println("------------");
Serial.println("Reading ssid");
int nullen = 0;
String esid;
for (int i = 0; i < 32; ++i)
{
esid += char(EEPROM.read(i));
if (EEPROM.read(i) == 0){
nullen++;
}
}
Serial.println("Reading password");
String epass = "";
for (int i = 32; i < 96; i++)
{
epass += char(EEPROM.read(i));
}
/*Serial.println("SSID: ");
Serial.print(esid);
Serial.println("PSW: ");
Serial.print(epass);*/
if ( esid.length() > 1 && nullen < 32) {
WiFi.begin(esid.c_str(), epass.c_str());
if (testWifi()) {
WiFi.softAPdisconnect(true);
launchWeb(0);
return;
}
} else {
Serial.println("Entering Config Mode");
setupAP();
}
}
bool testWifi(void) {
int c = 0;
Serial.println("Waiting for Wifi to connect to ESP8266");
while ( c < 30 ) {
if (WiFi.status() == WL_CONNECTED) { return true; }
delay(500);
Serial.print(WiFi.status());
c++;
}
Serial.println("");
Serial.println("Connect timed out, opening AP");
setupAP();
return false;
}
void launchWeb(int webtype) {
Serial.println("");
Serial.println("WiFi started");
Serial.print("Local IP: ");
Serial.println(WiFi.localIP());
Serial.print("SoftAP IP: ");
Serial.println(WiFi.softAPIP());
createWebServer(webtype);
// Start the server
server.begin();
Serial.println("Server started");
}
void setupAP(void) {
WiFi.mode(WIFI_STA);
WiFi.disconnect();
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
}
else
{
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*");
}
Serial.println("");
st = "<ol>";
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
st += "<li>";
st += WiFi.SSID(i);
st += " (";
st += WiFi.RSSI(i);
st += ")";
st += (WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*";
st += "</li>";
}
st += "</ol>";
}
WiFi.softAP(ssid, passphrase);
Serial.println("softAp");
launchWeb(1);
Serial.println("Config Server started");
}
void createWebServer(int webtype)
{
Serial.println("Webtype: ");
Serial.println(webtype);
if ( webtype == 1 ) {
server.on("/", []() {
IPAddress ip = WiFi.softAPIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
content = "<!DOCTYPE HTML>\r\n<html>WiFi configuration page for : ";
content += ipStr;
content += "<p>";
content += st;
content += "</p><form method='get' action='setting'><label>SSID: </label><input name='ssid' length=32><input name='pass' length=64><input type='submit'></form>";
content += "</html>";
server.send(200, "text/html", content);
});
server.on("/setting", []() {
String qsid = server.arg("ssid");
String qpass = server.arg("pass");
if (qsid.length() > 0 && qpass.length() > 0) {
Serial.println("clearing eeprom");
for (int i = 0; i < 96; ++i) { EEPROM.write(i, 0); }
Serial.println(qsid);
Serial.println("");
Serial.println(qpass);
Serial.println("");
Serial.println("writing eeprom ssid:");
for (int i = 0; i < qsid.length(); ++i)
{
EEPROM.write(i, qsid[i]);
//Serial.print("Wrote: ");
//Serial.println(qsid[i]);
}
Serial.println("writing eeprom pass:");
for (int i = 0; i < qpass.length(); ++i)
{
EEPROM.write(32+i, qpass[i]);
//Serial.print("Wrote: ");
//Serial.println(qpass[i]);
}
EEPROM.commit();
content = "{\"Success\":\"saved to eeprom....... restarting to boot into new wifi\"}";
statusCode = 200;
server.send(statusCode, "application/json", content);
delay(100);
ESP.restart();
} else {
content = "{\"Error\":\"404 not found\"}";
statusCode = 404;
Serial.println("Sending 404");
}
server.send(statusCode, "application/json", content);
});
} else if (webtype == 0) {
server.on("/", []() {
String message = "";
if (server.arg("r")== ""){ //Parameter not found
}else{ //Parameter found
float ppR = server.arg("r").toInt();
ppR * cycleNum;
int pR = (int)ppR;
if(pR > cycle) pR = cycle;
if(pR >= 0) r = pR;
}
if (server.arg("g")== ""){ //Parameter not found
}else{ //Parameter found
float ppG = server.arg("g").toInt();
ppG * cycleNum;
int pG = (int)ppG;
if(pG > cycle) pG = cycle;
if(pG >= 0) g = pG;
}
if (server.arg("b")== ""){ //Parameter not found
}else{ //Parameter found
float ppB = server.arg("b").toInt();
ppB * cycleNum;
int pB = (int)ppB;
if(pB > cycle) pB = cycle;
if(pB >= 0) b = pB;
}
if (server.arg("w")== ""){ //Parameter not found
}else{ //Parameter found
float ppW = server.arg("w").toInt();
ppW * cycleNum;
int pW = (int)ppW;
if(pW > cycle) pW = cycle;
if(pW >= 0) w = pW;
}
Serial.print("R: ");
Serial.println(r);
Serial.print("G: ");
Serial.println(g);
Serial.print("B: ");
Serial.println(b);
Serial.print("W: ");
Serial.println(w);
Serial.println("----------");
message += millis() / 1000;
server.send(200, "text/html", message);
});
server.on("/cleareeprom", []() {
content = "<!DOCTYPE HTML>\r\n<html>";
content += "<p>Clearing the EEPROM and Restarting</p></html>";
server.send(200, "text/html", content);
Serial.println("clearing eeprom and restarting");
for (int f= 0; f < 95; f++) {
EEPROM.write(f, 0);
}
EEPROM.commit();
delay(200);
ESP.restart();
});
server.on("/runtime", []() { //Timer
int time = millis() / 1000;
content = "<!DOCTYPE HTML>\r\n<html>";
content += "<p>Time</p>";
content += time;
content += "</html>";
server.send(200, "text/html", content);
});
}
}
void reboot(){
if(millis() > restartmin){
if(r == 0 && g == 0 && b == 0 && w == 0){
ESP.restart();
}
}
}
void loop() {
server.handleClient();
//Color Change
analogWrite(ledPinR, r);
analogWrite(ledPinG, g);
analogWrite(ledPinB, b);
analogWrite(ledPinW, w);
reboot();
}