-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetup.cpp
More file actions
105 lines (100 loc) · 3 KB
/
Setup.cpp
File metadata and controls
105 lines (100 loc) · 3 KB
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
#include "Setup.h"
Strip strip;
// publics
bool Setup::run() {
init();
digitalWrite(LED_BUILTIN, LOW);
WifiManager::connect("devlight", "HatJaNur5StundenGedauert");
bool success = Storage::getIsSetup() ? restart() : first();
if (!success) return false;
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
ESP.restart();
});
ArduinoOTA.begin();
return true;
}
// privates
void Setup::init() {
strip = Strip();
Serial.begin(115200);
EEPROM.begin(2048);
pinMode(LED_BUILTIN, OUTPUT);
}
bool Setup::first() {
WiFiClient c;
HttpClient client(c, "192.168.0.236", 80);
int err = 0;
String request = "/esp/setup?ip=";
request += Utils::ipToString(WifiManager::getIp());
err = client.get(request);
if (err == 0) {
err = client.responseStatusCode();
if (err == 200) {
err = client.skipResponseHeaders();
if (err >= 0) {
int bodyLength = client.contentLength();
unsigned long timeoutStart = millis();
String response = "";
char c;
while ((client.available() || client.connected()) &&
((millis() - timeoutStart) < 30000)) {
if (client.available()) {
c = client.read();
response += c;
bodyLength--;
timeoutStart = millis();
} else {
delay(500);
}
}
Storage::setId(response);
Storage::setIp(WifiManager::getIp());
Storage::setIsSetup(true);
Storage::setCount(150);
Storage::setBrightness(255);
Storage::setIsOn(true);
StripPattern startup;
startup.colors[0] = {29, 233, 182};
startup.pattern = 1;
Storage::setStripPattern(startup);
return true;
}
} else {
return false;
}
} else {
return false;
}
return false;
}
bool Setup::restart() {
// if ip changes notify server
Serial.println("restart");
if (Utils::ipToString(Storage::getIp()) !=
Utils::ipToString(WifiManager::getIp())) {
Serial.println("ipchange");
WiFiClient c;
HttpClient client(c, "192.168.0.236", 80);
int err = 0;
// TODO save and read id
String id = Storage::getId();
String request =
"/esp/update?ip=" + Utils::ipToString(WifiManager::getIp()) +
"&id=" + id;
err = client.get(request);
if (err == 0) {
err = client.responseStatusCode();
if (err == 200) {
Storage::setIp(WifiManager::getIp());
return true;
} else {
return false;
}
} else {
return false;
}
return false;
}
return true;
}