-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWifiHandler.cpp
131 lines (106 loc) · 3.11 KB
/
WifiHandler.cpp
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
#include "WifiHandler.hpp"
#include <App.hpp>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <Util.hpp>
WifiHandler wifiHandler;
const char *phyModes[] = {"11B", "11G", "11N"};
void WifiHandler::wifiInitStationMode() {
int bestChannel = 0;
int bestRSSI = -100;
WiFi.macAddress(mac);
sprintf( macAddress, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0],
mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.printf("WiFi MAC Address : %s\n", macAddress);
Serial.println( "\nScanning WiFi networks...");
int n = WiFi.scanNetworks();
Serial.println( "done.");
if (n == 0)
{
Serial.println("no networks found");
}
else
{
char scannedSSID[256];
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i)
{
strncpy( scannedSSID, WiFi.SSID(i).c_str(), 255 );
scannedSSID[255] = 0;
int scannedRSSI = WiFi.RSSI(i);
Serial.printf( "%2d: %s (%d,%d)%s\n", i+1,
scannedSSID, WiFi.channel(i), scannedRSSI,
(WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
if ( strcmp(WIFI_SSID, scannedSSID) == 0 && scannedRSSI > bestRSSI )
{
bestChannel = WiFi.channel(i);
bestRSSI = scannedRSSI;
}
delay( 5 );
}
}
Serial.printf( "\nBest Channel = %d\n", bestChannel );
Serial.printf( "Best RSSI = %d\n\n", bestRSSI );
LOG0("Starting Wifi in Station Mode (SSID=" WIFI_SSID ")\n");
sprintf( hostname, "%s-%06x", OTA_HOSTNAME, ESP.getChipId() );
WiFi.persistent(false);
WiFi.disconnect(true);
delay(200);
WiFi.mode(WIFI_STA);
WiFi.hostname(hostname);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
if ( bestChannel != 0 )
{
WiFi.begin(WIFI_SSID, WIFI_PASS, bestChannel );
}
else
{
WiFi.begin(WIFI_SSID, WIFI_PASS);
}
while (WiFi.status() != WL_CONNECTED) {
delay(500);
#ifdef WIFI_LED
alterPin(WIFI_LED);
#endif
Serial.print(".");
}
Serial.println("\n");
connectCounter++;
Serial.println("WiFi connected to : " WIFI_SSID);
Serial.printf("WiFi connection # : %d\n", connectCounter );
Serial.printf("WiFi Channel : %d\n", WiFi.channel());
Serial.printf("WiFi phy mode : %s\n", getPhyMode() );
WiFi.macAddress(mac);
sprintf( macAddress, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0],
mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.printf("WiFi MAC Address : %s\n", macAddress);
Serial.printf("WiFi Hostname : %s\n", WiFi.hostname().c_str());
Serial.print("WiFi IP-Address : ");
Serial.println(WiFi.localIP());
Serial.print("WiFi Gateway-IP : ");
Serial.println(WiFi.gatewayIP());
Serial.print("WiFi Subnetmask : ");
Serial.println(WiFi.subnetMask());
Serial.print("WiFi DNS Server : ");
Serial.println(WiFi.dnsIP());
Serial.println();
#ifdef WIFI_LED
digitalWrite(WIFI_LED, LOW);
#endif
}
int WifiHandler::getConnectCounter() {
return connectCounter;
}
const char* WifiHandler::getMacAddress()
{
return macAddress;
}
const char* WifiHandler::getPhyMode()
{
return phyModes[WiFi.getPhyMode() - 1];
}
const char* WifiHandler::getHostname()
{
return hostname;
}