forked from shashwat001/hollow_clock_wifi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwifi_service.cpp
More file actions
65 lines (53 loc) · 1.64 KB
/
Copy pathwifi_service.cpp
File metadata and controls
65 lines (53 loc) · 1.64 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
#include "wifi_service.h"
#include <ESP8266WiFi.h>
#include "config.h"
namespace {
void enable_dhcp_mode() {
IPAddress empty_ip(0U, 0U, 0U, 0U);
WiFi.config(empty_ip, empty_ip, empty_ip);
}
}
void connect_wifi(ESP8266WiFiMulti *wifi_multi, uint32_t timeout_ms,
String *wifi_connected) {
WiFi.mode(WIFI_STA);
#if USE_STATIC_IP
IPAddress local_ip;
IPAddress gateway;
IPAddress subnet;
IPAddress dns1;
IPAddress dns2;
bool has_dns2 = STATIC_IP_DNS2[0] != '\0';
bool static_ip_valid =
local_ip.fromString(STATIC_IP_ADDRESS) &&
gateway.fromString(STATIC_IP_GATEWAY) &&
subnet.fromString(STATIC_IP_SUBNET) &&
dns1.fromString(STATIC_IP_DNS1) &&
(!has_dns2 || dns2.fromString(STATIC_IP_DNS2));
if (!static_ip_valid) {
Serial.println("Invalid static IP config; fallback to DHCP.");
enable_dhcp_mode();
} else if (!(has_dns2
? WiFi.config(local_ip, gateway, subnet, dns1, dns2)
: WiFi.config(local_ip, gateway, subnet, dns1))) {
Serial.println("Failed to apply static IP; fallback to DHCP.");
enable_dhcp_mode();
} else {
Serial.print("Using static IP: ");
Serial.println(local_ip);
}
#else
enable_dhcp_mode();
#endif
wifi_multi->addAP(WIFI1_SSID, WIFI1_PASSWD);
wifi_multi->addAP(WIFI2_SSID, WIFI2_PASSWD);
wifi_multi->addAP(WIFI3_SSID, WIFI3_PASSWD);
if (wifi_multi->run(timeout_ms) == WL_CONNECTED) {
Serial.print("WiFi connected: ");
Serial.print(WiFi.SSID());
Serial.print(" ");
Serial.println(WiFi.localIP());
*wifi_connected = WiFi.SSID();
} else {
Serial.println("WiFi not connected!");
}
}