-
Notifications
You must be signed in to change notification settings - Fork 1
/
network.h
88 lines (74 loc) · 2.01 KB
/
network.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
boolean connectToWifi() {
Serial.print("\nconnecting to ");
Serial.println(WIFI_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
unsigned int retries = 50;
while (WiFi.status() != WL_CONNECTED && (retries-- > 0)) {
Serial.print(".");
delay(1000);
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("\nWifi connection failed");
return false;
}
Serial.println("");
Serial.println("wifi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("DNS: ");
Serial.println(WiFi.dnsIP(0));
Serial.println("");
return true;
}
boolean disconnectFromWifi() {
WiFi.disconnect();
}
boolean getWeatherJSON(const char* url) {
boolean success = false;
if ((WiFi.status() == WL_CONNECTED)) {
Serial.print("Connecting to ");
Serial.println(url);
HTTPClient http;
http.begin(url);
int httpCode = http.GET();
Serial.print("HTTP code : ");
Serial.println(httpCode);
if (httpCode > 0) {
weatherJson = JSON.parse(http.getString());
if (JSON.typeof(weatherJson) == "undefined") {
Serial.println("Parsing weatherJson input failed!");
} else {
success = true;
}
}
http.end();
}
return success;
}
boolean getLocalJSON(const char* url, const char* authorization) {
boolean success = false;
WiFiClientSecure secureClient;
secureClient.setTimeout(20000);
secureClient.setInsecure();
if ((WiFi.status() == WL_CONNECTED)) {
Serial.print("Connecting to ");
Serial.println(url);
HTTPClient http;
http.begin(url);
http.addHeader("Authorization", authorization);
int httpCode = http.GET();
Serial.print("HTTP code : ");
Serial.println(httpCode);
if (httpCode > 0) {
localJson = JSON.parse(http.getString());
if (JSON.typeof(localJson) == "undefined") {
Serial.println("Parsing localJson input failed!");
} else {
success = true;
}
}
http.end();
}
return success;
}