feat: module réseau WiFi STA/AP, mDNS, OTA
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
// Initialise WiFi (STA d'abord, AP en fallback), mDNS et OTA
|
||||
void network_init();
|
||||
|
||||
// À appeler à chaque loop() : gère OTA, reconnexion WiFi non-bloquante
|
||||
void network_update();
|
||||
+3
-1
@@ -1,7 +1,7 @@
|
||||
#include <Arduino.h>
|
||||
#include "config.h"
|
||||
#include "network.h"
|
||||
|
||||
// ── Définitions des variables globales ─────────────────────────────
|
||||
SondeConfig sondesConfig[NB_SONDES] = {
|
||||
{ "T°C Ext", "maison/jardin/ext/temperature", 60000, 0.2f },
|
||||
{ "T°C Serre", "maison/jardin/serre/temperature", 60000, 0.1f },
|
||||
@@ -15,7 +15,9 @@ NetworkStatus netStatus = {};
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("[BOOT] esp_jardin démarrage...");
|
||||
network_init();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
network_update();
|
||||
}
|
||||
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
#include "network.h"
|
||||
#include "config.h"
|
||||
#include <WiFi.h>
|
||||
#include <ESPmDNS.h>
|
||||
#include <ArduinoOTA.h>
|
||||
|
||||
static uint32_t _dernierRetryMs = 0;
|
||||
static uint32_t _debutConnexionMs = 0;
|
||||
static bool _connexionEnCours = false;
|
||||
|
||||
// Démarre la tentative de connexion STA (non-bloquant)
|
||||
static void _demarrerSTA() {
|
||||
Serial.printf("[WIFI] Connexion STA → SSID: %s\n", WIFI_SSID);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASS);
|
||||
_debutConnexionMs = millis();
|
||||
_connexionEnCours = true;
|
||||
}
|
||||
|
||||
static void _demarrerAP() {
|
||||
Serial.println("[WIFI] Bascule AP → ESP_CHEF_JARDIN");
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP(AP_SSID, AP_PASS);
|
||||
netStatus.modeAP = true;
|
||||
netStatus.wifiConnecte = false;
|
||||
Serial.printf("[WIFI] AP IP: %s\n", WiFi.softAPIP().toString().c_str());
|
||||
}
|
||||
|
||||
static void _configurerMDNS() {
|
||||
if (MDNS.begin(MDNS_NOM)) {
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
Serial.printf("[mDNS] Accessible via http://%s.local\n", MDNS_NOM);
|
||||
}
|
||||
}
|
||||
|
||||
static void _configurerOTA() {
|
||||
ArduinoOTA.setPassword(OTA_PASS);
|
||||
ArduinoOTA.onStart([]() {
|
||||
Serial.println("[OTA] Mise à jour démarrée");
|
||||
});
|
||||
ArduinoOTA.onEnd([]() {
|
||||
Serial.println("[OTA] Terminée — redémarrage");
|
||||
});
|
||||
ArduinoOTA.onError([](ota_error_t err) {
|
||||
Serial.printf("[OTA] Erreur [%u]\n", err);
|
||||
});
|
||||
ArduinoOTA.begin();
|
||||
Serial.println("[OTA] Prêt");
|
||||
}
|
||||
|
||||
void network_init() {
|
||||
_demarrerSTA();
|
||||
}
|
||||
|
||||
void network_update() {
|
||||
// ── Gestion OTA ─────────────────────────────────────────────────
|
||||
if (netStatus.wifiConnecte && !netStatus.modeAP) {
|
||||
ArduinoOTA.handle();
|
||||
}
|
||||
|
||||
// ── Connexion STA en cours ───────────────────────────────────────
|
||||
if (_connexionEnCours) {
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
_connexionEnCours = false;
|
||||
netStatus.wifiConnecte = true;
|
||||
netStatus.modeAP = false;
|
||||
netStatus.uptimeDemarrage = millis();
|
||||
Serial.printf("[WIFI] Connecté — IP: %s\n", WiFi.localIP().toString().c_str());
|
||||
_configurerMDNS();
|
||||
_configurerOTA();
|
||||
} else if (millis() - _debutConnexionMs > WIFI_TIMEOUT_MS) {
|
||||
_connexionEnCours = false;
|
||||
Serial.println("[WIFI] Timeout STA");
|
||||
_demarrerAP();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// ── STA connecté : surveiller les déconnexions ──────────────────
|
||||
if (netStatus.wifiConnecte && !netStatus.modeAP) {
|
||||
if (WiFi.status() != WL_CONNECTED) {
|
||||
netStatus.wifiConnecte = false;
|
||||
Serial.println("[WIFI] Déconnexion détectée — retry dans 30s");
|
||||
_dernierRetryMs = millis();
|
||||
} else {
|
||||
netStatus.rssi = WiFi.RSSI();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// ── Mode AP : retry STA toutes les 60s ──────────────────────────
|
||||
if (netStatus.modeAP) {
|
||||
if (millis() - _dernierRetryMs > 60000) {
|
||||
Serial.println("[WIFI] Mode AP — retry STA...");
|
||||
_dernierRetryMs = millis();
|
||||
_demarrerSTA();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// ── STA déconnecté (hors AP) : retry toutes les 30s ─────────────
|
||||
if (!netStatus.wifiConnecte && millis() - _dernierRetryMs > WIFI_RETRY_MS) {
|
||||
_dernierRetryMs = millis();
|
||||
_demarrerSTA();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user