|
|
|
@ -3,6 +3,13 @@ |
|
|
|
#include <LittleFS.h> |
|
|
|
#include <LittleFS.h> |
|
|
|
#include <ArduinoJson.h> |
|
|
|
#include <ArduinoJson.h> |
|
|
|
#include <ArduinoOTA.h> |
|
|
|
#include <ArduinoOTA.h> |
|
|
|
|
|
|
|
#if __has_include(<esp_wpa2.h>) |
|
|
|
|
|
|
|
#include <esp_wpa2.h> |
|
|
|
|
|
|
|
#include <esp_wifi.h> |
|
|
|
|
|
|
|
#define HAS_LEGACY_WPA2_ENTERPRISE 1 |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
#define HAS_LEGACY_WPA2_ENTERPRISE 0 |
|
|
|
|
|
|
|
#endif |
|
|
|
#include "auth.h" |
|
|
|
#include "auth.h" |
|
|
|
#include "server.h" |
|
|
|
#include "server.h" |
|
|
|
#include "utils.h" |
|
|
|
#include "utils.h" |
|
|
|
@ -11,6 +18,9 @@ |
|
|
|
String hostname = "ESP32-Webapp"; |
|
|
|
String hostname = "ESP32-Webapp"; |
|
|
|
String wifiSSID = ""; |
|
|
|
String wifiSSID = ""; |
|
|
|
String wifiPassword = ""; |
|
|
|
String wifiPassword = ""; |
|
|
|
|
|
|
|
String wifiSecurity = "wpa2"; |
|
|
|
|
|
|
|
String wifiEapUsername = ""; |
|
|
|
|
|
|
|
String wifiEapPassword = ""; |
|
|
|
String apPassword = "webapp123"; |
|
|
|
String apPassword = "webapp123"; |
|
|
|
|
|
|
|
|
|
|
|
void createDefaultConfig() { |
|
|
|
void createDefaultConfig() { |
|
|
|
@ -25,6 +35,9 @@ void createDefaultConfig() { |
|
|
|
// Configuration WiFi
|
|
|
|
// Configuration WiFi
|
|
|
|
doc["wifi"]["ssid"] = ""; |
|
|
|
doc["wifi"]["ssid"] = ""; |
|
|
|
doc["wifi"]["password"] = ""; |
|
|
|
doc["wifi"]["password"] = ""; |
|
|
|
|
|
|
|
doc["wifi"]["security"] = "wpa2"; |
|
|
|
|
|
|
|
doc["wifi"]["eap_username"] = ""; |
|
|
|
|
|
|
|
doc["wifi"]["eap_password"] = ""; |
|
|
|
doc["wifi"]["ap_password"] = apPassword; |
|
|
|
doc["wifi"]["ap_password"] = apPassword; |
|
|
|
|
|
|
|
|
|
|
|
// Configuration système
|
|
|
|
// Configuration système
|
|
|
|
@ -90,6 +103,18 @@ void loadConfig() { |
|
|
|
wifiPassword = doc["wifi"]["password"].as<String>(); |
|
|
|
wifiPassword = doc["wifi"]["password"].as<String>(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (doc["wifi"]["security"]) { |
|
|
|
|
|
|
|
wifiSecurity = doc["wifi"]["security"].as<String>(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (doc["wifi"]["eap_username"]) { |
|
|
|
|
|
|
|
wifiEapUsername = doc["wifi"]["eap_username"].as<String>(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (doc["wifi"]["eap_password"]) { |
|
|
|
|
|
|
|
wifiEapPassword = doc["wifi"]["eap_password"].as<String>(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (doc["wifi"]["ap_password"]) { |
|
|
|
if (doc["wifi"]["ap_password"]) { |
|
|
|
apPassword = doc["wifi"]["ap_password"].as<String>(); |
|
|
|
apPassword = doc["wifi"]["ap_password"].as<String>(); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -97,13 +122,7 @@ void loadConfig() { |
|
|
|
logMessage(LOG_INFO, "Configuration chargée"); |
|
|
|
logMessage(LOG_INFO, "Configuration chargée"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void setupWiFi() { |
|
|
|
bool waitForWiFiConnection() { |
|
|
|
if (wifiSSID.length() > 0) { |
|
|
|
|
|
|
|
logMessage(LOG_INFO, "Connexion au WiFi: " + wifiSSID); |
|
|
|
|
|
|
|
WiFi.mode(WIFI_STA); |
|
|
|
|
|
|
|
WiFi.setHostname(hostname.c_str()); |
|
|
|
|
|
|
|
WiFi.begin(wifiSSID.c_str(), wifiPassword.c_str()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int attempts = 0; |
|
|
|
int attempts = 0; |
|
|
|
while (WiFi.status() != WL_CONNECTED && attempts < 20) { |
|
|
|
while (WiFi.status() != WL_CONNECTED && attempts < 20) { |
|
|
|
delay(500); |
|
|
|
delay(500); |
|
|
|
@ -114,6 +133,68 @@ void setupWiFi() { |
|
|
|
if (WiFi.status() == WL_CONNECTED) { |
|
|
|
if (WiFi.status() == WL_CONNECTED) { |
|
|
|
logMessage(LOG_INFO, "WiFi connecté"); |
|
|
|
logMessage(LOG_INFO, "WiFi connecté"); |
|
|
|
logMessage(LOG_INFO, "Adresse IP: " + WiFi.localIP().toString()); |
|
|
|
logMessage(LOG_INFO, "Adresse IP: " + WiFi.localIP().toString()); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool connectWPA2PEAP() { |
|
|
|
|
|
|
|
if (wifiEapUsername.isEmpty() || wifiEapPassword.isEmpty()) { |
|
|
|
|
|
|
|
logMessage(LOG_WARN, "WPA2-PEAP: identifiant ou mot de passe manquant"); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logMessage(LOG_INFO, "Connexion WPA2-PEAP au WiFi: " + wifiSSID); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(WPA2_AUTH_PEAP) |
|
|
|
|
|
|
|
WiFi.begin( |
|
|
|
|
|
|
|
wifiSSID.c_str(), |
|
|
|
|
|
|
|
WPA2_AUTH_PEAP, |
|
|
|
|
|
|
|
wifiEapUsername.c_str(), |
|
|
|
|
|
|
|
wifiEapUsername.c_str(), |
|
|
|
|
|
|
|
wifiEapPassword.c_str(), |
|
|
|
|
|
|
|
nullptr |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
return waitForWiFiConnection(); |
|
|
|
|
|
|
|
#elif HAS_LEGACY_WPA2_ENTERPRISE |
|
|
|
|
|
|
|
WiFi.begin(wifiSSID.c_str()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
esp_wifi_sta_wpa2_ent_set_identity((uint8_t*)wifiEapUsername.c_str(), wifiEapUsername.length()); |
|
|
|
|
|
|
|
esp_wifi_sta_wpa2_ent_set_username((uint8_t*)wifiEapUsername.c_str(), wifiEapUsername.length()); |
|
|
|
|
|
|
|
esp_wifi_sta_wpa2_ent_set_password((uint8_t*)wifiEapPassword.c_str(), wifiEapPassword.length()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef ESP_EAP_TTLS_PHASE2_MSCHAPV2 |
|
|
|
|
|
|
|
esp_wifi_sta_wpa2_ent_set_ttls_phase2_method(ESP_EAP_TTLS_PHASE2_MSCHAPV2); |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
esp_wifi_sta_wpa2_ent_enable(); |
|
|
|
|
|
|
|
return waitForWiFiConnection(); |
|
|
|
|
|
|
|
#else |
|
|
|
|
|
|
|
logMessage(LOG_ERROR, "WPA2-PEAP non supporté par cette version du framework"); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool connectWPA2PSK() { |
|
|
|
|
|
|
|
logMessage(LOG_INFO, "Connexion WPA2-PSK au WiFi: " + wifiSSID); |
|
|
|
|
|
|
|
WiFi.begin(wifiSSID.c_str(), wifiPassword.c_str()); |
|
|
|
|
|
|
|
return waitForWiFiConnection(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void setupWiFi() { |
|
|
|
|
|
|
|
if (wifiSSID.length() > 0) { |
|
|
|
|
|
|
|
WiFi.mode(WIFI_STA); |
|
|
|
|
|
|
|
WiFi.setHostname(hostname.c_str()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool connected = false; |
|
|
|
|
|
|
|
if (wifiSecurity == "wpa2_peap") { |
|
|
|
|
|
|
|
connected = connectWPA2PEAP(); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
connected = connectWPA2PSK(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (connected) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|