60 lines
1.4 KiB
C++
60 lines
1.4 KiB
C++
#include <ESP8266WiFi.h>
|
|
#include <ESP8266WiFiMulti.h>
|
|
#include <ESP8266mDNS.h>
|
|
|
|
class Wifi {
|
|
|
|
private:
|
|
String currentSSID;
|
|
String currentPsk;
|
|
|
|
public:
|
|
void setup() {
|
|
ESP8266WiFiMulti wifiMulti;
|
|
wifiMulti.addAP("OpenWrt", "***REMOVED***");
|
|
// wifiMulti.addAP("IoT", "***REMOVED***");
|
|
wifiMulti.addAP("Miracle", "***REMOVED***");
|
|
|
|
Serial.println("Connecting to WiFi netowrk.");
|
|
while (wifiMulti.run() != WL_CONNECTED) {
|
|
delay(500);
|
|
}
|
|
Serial.println("Connected to network.");
|
|
currentSSID = WiFi.SSID();
|
|
currentPsk = WiFi.psk();
|
|
}
|
|
|
|
void reconnect() {
|
|
if (WiFi.status() != WL_CONNECTED) {
|
|
WiFi.forceSleepWake();
|
|
WiFi.begin(currentSSID.c_str(), currentPsk.c_str());
|
|
Serial.println("Reconnecting to WiFi netowrk...");
|
|
for (int i = 0; i < 4; i++) {
|
|
delay(1000);
|
|
}
|
|
}
|
|
}
|
|
|
|
void disconnect() {
|
|
Serial.println("Disconnecting WiFi");
|
|
WiFi.disconnect();
|
|
WiFi.forceSleepBegin();
|
|
}
|
|
|
|
void printStatus() {
|
|
// print the SSID of the network you're attached to:
|
|
Serial.print("SSID: ");
|
|
Serial.println(WiFi.SSID());
|
|
|
|
// print your WiFi shield's IP address:
|
|
IPAddress ip = WiFi.localIP();
|
|
Serial.print("IP Address: ");
|
|
Serial.println(ip);
|
|
|
|
// print the received signal strength:
|
|
long rssi = WiFi.RSSI();
|
|
Serial.print("signal strength (RSSI):");
|
|
Serial.print(rssi);
|
|
Serial.println(" dBm");
|
|
}
|
|
}; |