-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection_wifi.ino
129 lines (102 loc) · 4.48 KB
/
connection_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
#include "connection_wifi.h"
/**
* @brief Set the WiFi connection with the credentials given in the .env file.
*/
void connectionToWifi() {
WiFi.mode(WIFI_STA); // Optionnal, mode station
WiFi.begin(SSID_NAME, SSID_PASS); // Set credentials for WiFi connection
Serial.println((String)"\nConnecting to " + SSID_NAME + " with " + SSID_PASS);
int timeout_counter = 0;
while (WiFi.status() != WL_CONNECTED) // While not connected to WiFi, try again
{
Serial.print(".");
delay(200);
timeout_counter++;
if (timeout_counter >= 500) { // If timeout, reload the connection
Serial.println("Can't establish WiFi connexion. Reload...");
connectionToWifi();
}
}
Serial.println("\nConnected to the WiFi network");
Serial.print("Local ESP32 IP: ");
Serial.println(WiFi.localIP()); // Print local ESP32 IP
}
/**
* @brief Get the WiFi status in a string, according to the int given in parameter.
*
* @param status Status of the WiFi connection.
* @return String Returns the status in string format.
*/
String getWifiStatus(int status) {
switch(status) {
case WL_IDLE_STATUS:
return "WL_IDLE_STATUS";
case WL_SCAN_COMPLETED:
return "WL_SCAN_COMPLETED";
case WL_NO_SSID_AVAIL:
return "WL_NO_SSID_AVAIL";
case WL_CONNECT_FAILED:
return "WL_CONNECT_FAILED";
case WL_CONNECTION_LOST:
return "WL_CONNECTION_LOST";
case WL_CONNECTED:
return "WL_CONNECTED";
case WL_DISCONNECTED:
return "WL_DISCONNECTED";
}
}
/**
* @brief Check the WiFi connection. If status != connected then count time since the deconnection. If connection lost for more than one minute, connection is reload. If status is back to "connected", then send embed with the time lost.
*
* @return bool If WiFi status is "connected" then returns true ; otherwise false.
*/
bool checkConnection() {
static int other_status = 0;
wifiStatus = WiFi.status();
if (wifiStatus != WL_CONNECTED) { // If WiFi connection lost
Serial.println("\nThere is some issues with the connection.");
Serial.println((String)"Status is: " + getWifiStatus(wifiStatus));
if (begin == NULL) { begin = time(NULL); } // Set begin time for connection lost
delay(1000);
other_status++;
if (other_status >= 100) { connectionToWifi(); other_status = 0; } // If status is not connected for more than 1 minute and a half, reload connection.
previousWifiStatus = wifiStatus;
return false;
} else if (wifiStatus == WL_CONNECTED && previousWifiStatus != WL_CONNECTED) { // If connection is restored
time_t end = time(NULL);
unsigned long secondes = (unsigned long) difftime(end, begin); // Get diff time of the disconnection in secondes.
String str = (String)"Connection lost for " + secondsToTime(secondes) + ".";
sendDiscordWebhook("Connection lost!", str, true, DARK_ORANGE); // Send discord webhook with the time it disconnected.
begin = NULL;
}
other_status = 0;
previousWifiStatus = wifiStatus;
return true;
}
/**
* @brief Transform seconds to a string with hours/minutes/secondes.
*
* @param long Secondes to transform.
* @return String The string with hours/minutes/secondes.
*/
String secondsToTime(unsigned long secondes)
{
String str = "";
unsigned long minutes = 0, hours = 0;
minutes = long(secondes / 60); secondes = secondes % 60; // Get minutes in unsigned long
hours = long(minutes / 60); minutes = minutes % 60; // Get hours in unsigned long
char sec[50]; ltoa(secondes, sec, 10); // Get secondes to char[]
char min[50]; ltoa(minutes, min, 10); // Get minutes to char[]
char h[50]; ltoa(hours, h, 10); // Get hours to char[]
// Get the whole string
if (hours > 0) { str += (String)h + " hour.s"; }
if (minutes > 0) {
if (str != "") { str += " "; }
str += (String)min + " minute.s";
}
if (secondes > 0) {
if (str != "") { str += " and "; }
str += (String)sec + " seconde.s";
}
return str;
}