-
Notifications
You must be signed in to change notification settings - Fork 12
/
Wifi.ino
171 lines (157 loc) · 4.5 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//********************************************************************************
// Set Wifi AP Mode config
//********************************************************************************
void WifiAPconfig()
{
// create and store unique AP SSID/PW to prevent ESP from starting AP mode with default SSID and No password!
char ap_ssid[20];
ap_ssid[0] = 0;
strcpy(ap_ssid, "ESP_");
sprintf_P(ap_ssid, PSTR("%s%u"), ap_ssid, Settings.Unit);
// setup ssid for AP Mode when needed
WiFi.softAP(ap_ssid, SecuritySettings.WifiAPKey);
// We start in STA mode
WiFi.mode(WIFI_STA);
}
//********************************************************************************
// Set Wifi AP Mode
//********************************************************************************
void WifiAPMode(boolean state)
{
if (state)
{
AP_Mode = true;
char ap_ssid[20];
ap_ssid[0] = 0;
strcpy(ap_ssid, "ESP_");
sprintf_P(ap_ssid, PSTR("%s%u"), ap_ssid, Settings.Unit);
WiFi.softAP(ap_ssid, SecuritySettings.WifiAPKey);
WiFi.mode(WIFI_AP_STA);
}
else
{
AP_Mode = false;
WiFi.mode(WIFI_STA);
}
}
//********************************************************************************
// Connect to Wifi AP
//********************************************************************************
boolean WifiConnect()
{
String log = F("WIFI : Connecting...");
addLog(LOG_LEVEL_INFO,log);
if (WiFi.status() != WL_CONNECTED)
{
if ((SecuritySettings.WifiSSID[0] != 0) && (strcasecmp(SecuritySettings.WifiSSID, "ssid") != 0))
{
WiFi.begin(SecuritySettings.WifiSSID, SecuritySettings.WifiKey);
for (byte x = 0; x < 10; x++)
{
if (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
else
break;
}
// fix ip if last octet is set
if (Settings.IP_Octet != 0 && Settings.IP_Octet != 255)
{
IPAddress ip = WiFi.localIP();
IPAddress gw = WiFi.gatewayIP();
IPAddress subnet = WiFi.subnetMask();
ip[3] = Settings.IP_Octet;
log = F("IP : Fixed IP :");
log += ip;
addLog(LOG_LEVEL_INFO,log);
WiFi.config(ip, gw, subnet);
}
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]);
log = F("IP : Static IP :");
log += str;
addLog(LOG_LEVEL_INFO,log);
IPAddress ip = Settings.IP;
IPAddress gw = Settings.Gateway;
IPAddress subnet = Settings.Subnet;
WiFi.config(ip, gw, subnet);
}
}
else
{
log = F("WIFI : No SSID!");
addLog(LOG_LEVEL_INFO,log);
NC_Count = 1;
WifiAPMode(true);
}
}
}
//********************************************************************************
// Disconnect from Wifi AP
//********************************************************************************
boolean WifiDisconnect()
{
WiFi.disconnect();
}
//********************************************************************************
// Scan all Wifi Access Points
//********************************************************************************
void WifiScan()
{
// Direct Serial is allowed here, since this function will only be called from serial input.
Serial.println(F("WIFI : SSID Scan start"));
int n = WiFi.scanNetworks();
if (n == 0)
Serial.println(F("WIFI : No networks found"));
else
{
Serial.print(F("WIFI : "));
Serial.print(n);
Serial.println(F(" networks found"));
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
Serial.print(F("WIFI : "));
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println("");
delay(10);
}
}
Serial.println("");
}
//********************************************************************************
// Check if we are still connected to a Wifi AP
//********************************************************************************
void WifiCheck()
{
if (WiFi.status() != WL_CONNECTED)
{
NC_Count++;
if (NC_Count > 10 && !AP_Mode)
{
C_Count = 0;
WifiAPMode(true);
}
}
else
{
C_Count++;
NC_Count = 0;
if (C_Count > 60)
{
byte wifimode = wifi_get_opmode();
if (wifimode == 2 || wifimode == 3) //apmode is active
{
WifiAPMode(false);
}
}
}
}