-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.h
95 lines (76 loc) · 2.25 KB
/
misc.h
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
void drawErrorMessage(String message)
{
tft.fillScreen(TFT_BLACK);
tft.setCursor(20, 20);
tft.setTextFont(2);
tft.setTextSize(1);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.println(message);
}
String httpGETRequest(const char* url) {
HTTPClient http;
Serial.print("GET: ");
Serial.println(url);
// Your IP address with path or Domain name with URL path
http.begin(url);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
bool loadWifiConfig() {
if (!FILESYSTEM.exists("/config/wificonfig.json")) {
Serial.println("[WARNING]: Config file not found!");
return false;
}
fs::File configfile = FILESYSTEM.open("/config/wificonfig.json");
DynamicJsonDocument doc(256);
DeserializationError error = deserializeJson(doc, configfile);
strlcpy(wificonfig.ssid, doc["ssid"] | "FAILED", sizeof(wificonfig.ssid));
strlcpy(wificonfig.password, doc["password"] | "FAILED", sizeof(wificonfig.password));
strlcpy(wificonfig.hostname, doc["wifihostname"] | "freetouchdeck", sizeof(wificonfig.hostname));
uint8_t attempts = doc["attempts"] | 10 ;
wificonfig.attempts = attempts;
uint16_t attemptdelay = doc["attemptdelay"] | 500 ;
wificonfig.attemptdelay = attemptdelay;
configfile.close();
if (error) {
Serial.println("[ERROR]: deserializeJson() error");
Serial.println(error.c_str());
return false;
}
return true;
}
bool startWifiOnly() {
Serial.printf("[INFO]: Connecting to %s\n", wificonfig.ssid);
if (String(WiFi.SSID()) != String(wificonfig.ssid))
{
WiFi.mode(WIFI_STA);
WiFi.begin(wificonfig.ssid, wificonfig.password);
uint8_t attempts = wificonfig.attempts;
while (WiFi.status() != WL_CONNECTED)
{
if (attempts == 0) {
WiFi.disconnect();
Serial.println("");
return false;
}
delay(wificonfig.attemptdelay);
Serial.print(".");
attempts--;
}
}
Serial.println("");
return WiFi.status() == WL_CONNECTED;
}