-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi.ino
102 lines (90 loc) · 2.49 KB
/
wifi.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
void WifiAPMode(boolean state)
{
// ticker.detach();
if (state)
{
// Serial.printf("Enabling soft-AP %s\n", WiFi.mode(WIFI_AP_STA) ? "..." : "Failed!");
Serial.printf("Enabling soft-AP\n");
WiFi.enableAP(true);
ticker.attach(0.5, changeLED);
}
else
{
// Serial.printf("Disabling soft-AP %s\n", WiFi.mode(WIFI_STA) ? "..." : "Failed!");
Serial.printf("Disabling soft-AP\n");
WiFi.enableAP(false);
}
delay(500); // Without delay I've seen the IP address blank
MDNS.notifyAPChange();
}
boolean WifiConnect(byte connectAttempts)
{
String log = "";
// char hostName[sizeof(Settings.Name)];
// strcpy(hostName, Settings.Name);
// for (byte x = 0; x < sizeof(hostName); x++)
// if (hostName[x] == ' ')
// hostName[x] = '-';
// wifi_station_set_hostname(hostName);
if (Settings.IP[0] != 0 && Settings.IP[0] != 255)
{
char str[20];
sprintf_P(str, PSTR("%u.%u.%u.%u"), Settings.IP[0], Settings.IP[1], Settings.IP[2], Settings.IP[3]);
Serial.println("IP : Static IP :");
IPAddress ip = Settings.IP;
IPAddress gw = Settings.Gateway;
IPAddress subnet = Settings.Subnet;
IPAddress dns = Settings.DNS;
WiFi.config(ip, gw, subnet, dns);
}
if (WiFi.status() != WL_CONNECTED)
{
if ((Settings.WifiSSID[0] != 0) && (strcasecmp(Settings.WifiSSID, "ssid") != 0))
{
for (byte tryConnect = 1; tryConnect <= connectAttempts; tryConnect++)
{
Serial.print("WIFI : Connecting... ");
Serial.println(tryConnect);
if (tryConnect == 1)
{
WiFi.begin(Settings.WifiSSID, Settings.WifiKey);
}
else
WiFi.begin();
for (byte x = 0; x < 20; x++)
{
if (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
else
break;
}
if (WiFi.status() == WL_CONNECTED)
{
Serial.print("WIFI : Connected! IP: ");
IPAddress ip = WiFi.localIP();
char str[20];
sprintf_P(str, PSTR("%u.%u.%u.%u"), ip[0], ip[1], ip[2], ip[3]);
Serial.println(str);
break;
}
else
{
Serial.println("WIFI : Disconnecting!");
ETS_UART_INTR_DISABLE();
wifi_station_disconnect();
ETS_UART_INTR_ENABLE();
delay(1000);
}
}
}
else
{
Serial.println("WIFI : No SSID!");
}
}
if (WiFi.status() == WL_CONNECTED)
return true;
return false;
}