This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWifiConnector.cpp
93 lines (72 loc) · 1.71 KB
/
WifiConnector.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
#include "WifiConnector.h"
#include "Settings.h"
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPClient.h>
#include <WiFiUdp.h>
#include "ESP8266mDNS.h"
// Create Wifi Connector
WifiConnector::WifiConnector(bool inDebugMode)
{
debugMode = inDebugMode;
tryingReconnect = false;
}
// Set params and try to connect
void WifiConnector::start()
{
if (debugMode){
Serial.println("Wifi) Start ");
}
// Setup Wifi
WiFi.mode(WIFI_STA);
WiFi.hostname(WIFI_HOSTNAME);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
// This funtion is called from main setup function,
// Wait until unit has wifi before continuing
while (WiFi.status() != WL_CONNECTED) {
delay(500);
if (debugMode){
Serial.print(".");
}
}
// Set wifi bool
hasWIFI = true;
if (debugMode)
{
Serial.println("Wifi) Connected!");
Serial.println("Wifi) IP: ");
Serial.println(WiFi.localIP());
}
}
// If wifi connection is lost, try to reconnect
void WifiConnector::reconnect()
{
if (debugMode){
Serial.println("Wifi) Try to reconnect!");
}
// First hit, try to reconnect.
if(!tryingReconnect)
{
WiFi.reconnect();
tryingReconnect = true;
}
}
// handle function called from main loop
void WifiConnector::handle()
{
// Check if WIfi is Connected
if(!WiFi.isConnected() && !tryingReconnect){
if (debugMode){
Serial.println("Wifi) Disconnected!");
}
// Set bool false and try to reconnect
hasWIFI = false;
reconnect();
}else if (WiFi.isConnected() && !hasWIFI){ // Wifi is Reconnected
if (debugMode){
Serial.println("Wifi) Reconnected!");
}
hasWIFI = true;
tryingReconnect = false;
}
}