Step
This commit is contained in:
1 parent
644e2363f7
commit
75bbcce5b4
10 files changed
+226
-98
No files matched your search
@@ -1,35 +1,16 @@
|
|||||||
# ESPTest
|
# ESPTest
|
||||||
|
|
||||||
ESPTest est un exemple de firmware Arduino pour une carte **ESP32-S3 DevKitC-1**. Il configure le Wi-Fi, démarre un serveur web et met à disposition un tableau de bord avec une température simulée, une mise à jour du firmware par OTA et un terminal série accessible depuis le navigateur.
|
Firmware Arduino pour une carte **ESP32-S3 DevKitC-1**. Au démarrage, l'appareil lit sa configuration MQTT en EEPROM, se connecte au Wi-Fi ou ouvre un portail de configuration, puis active les mises à jour OTA.
|
||||||
|
|
||||||
## Fonctionnalités
|
## Fonctionnalités
|
||||||
|
|
||||||
- **Configuration Wi-Fi** avec NetWizard : l'appareil se connecte au réseau mémorisé ou démarre le portail de configuration « My Device ».
|
- **Configuration Wi-Fi** avec WiFiManager. Le point d'accès de configuration porte un nom `Legende-XXXX`, où `XXXX` correspond aux deux derniers octets de l'adresse MAC.
|
||||||
- **Tableau de bord** ESP-DASH affichant une valeur de température simulée, actualisée toutes les trois secondes.
|
- **Paramètres MQTT** (serveur, utilisateur, mot de passe et topic) saisis dans le portail et enregistrés en EEPROM. Le client MQTT n'est pas encore implémenté.
|
||||||
- **Mise à jour OTA** avec ElegantOTA.
|
- **Mise à jour du firmware** par ArduinoOTA après connexion au Wi-Fi.
|
||||||
- **Terminal série web** avec WebSerial. Les messages reçus dans le terminal sont également écrits sur le port série USB.
|
|
||||||
- **Serveur web asynchrone** sur le port 80.
|
|
||||||
|
|
||||||
## Matériel et prérequis
|
## Prérequis et compilation
|
||||||
|
|
||||||
- Une carte ESP32-S3 DevKitC-1.
|
Installer Visual Studio Code avec l'extension PlatformIO IDE, ou PlatformIO Core. La cible et la dépendance WiFiManager sont définies dans [`platformio.ini`](./platformio.ini).
|
||||||
- [Visual Studio Code](https://code.visualstudio.com/) avec l'extension [PlatformIO IDE](https://platformio.org/install/ide?install=vscode), ou PlatformIO Core en ligne de commande.
|
|
||||||
- Un câble USB adapté à la carte.
|
|
||||||
- Un navigateur sur un appareil connecté au même réseau local que l'ESP32 pour accéder au tableau de bord.
|
|
||||||
|
|
||||||
Les bibliothèques nécessaires sont déclarées dans [`platformio.ini`](./platformio.ini) et PlatformIO les installe lors de la compilation.
|
|
||||||
|
|
||||||
## Compiler et téléverser
|
|
||||||
|
|
||||||
### Avec Visual Studio Code
|
|
||||||
|
|
||||||
1. Ouvrir le dossier du projet dans VS Code.
|
|
||||||
2. Laisser PlatformIO installer les dépendances.
|
|
||||||
3. Connecter la carte en USB.
|
|
||||||
4. Utiliser les actions PlatformIO **Build**, puis **Upload**.
|
|
||||||
5. Ouvrir le moniteur série à **115200 bauds** pour suivre le démarrage.
|
|
||||||
|
|
||||||
### En ligne de commande
|
|
||||||
|
|
||||||
Depuis la racine du projet :
|
Depuis la racine du projet :
|
||||||
|
|
||||||
@@ -39,36 +20,30 @@ pio run -e esp32-s3-devkitc-1 -t upload
|
|||||||
pio device monitor -b 115200
|
pio device monitor -b 115200
|
||||||
```
|
```
|
||||||
|
|
||||||
Si plusieurs ports série sont disponibles, sélectionner celui de la carte dans les paramètres de PlatformIO ou préciser le port avec `--port`.
|
Le premier téléversement se fait par USB. Les mises à jour OTA sont disponibles après le démarrage du firmware et la connexion de la carte au réseau.
|
||||||
|
|
||||||
## Première connexion et utilisation
|
## Première configuration
|
||||||
|
|
||||||
1. Après démarrage, l'ESP32 se connecte au Wi-Fi mémorisé. Si aucun réseau enregistré n'est disponible, il lance le portail de configuration NetWizard nommé **My Device**.
|
1. Téléverser le firmware par USB et ouvrir le moniteur série à **115200 bauds**.
|
||||||
2. Depuis un téléphone ou un ordinateur, rejoindre le point d'accès de configuration et suivre le portail pour fournir les identifiants du réseau Wi-Fi.
|
2. Si aucun Wi-Fi enregistré n'est disponible, connecter un téléphone ou un ordinateur au point d'accès `Legende-XXXX`. Son mot de passe initial est `Legende_2026`.
|
||||||
3. Une fois l'ESP32 connecté au réseau local, repérer son adresse IP dans le moniteur série ou dans la liste des appareils du routeur.
|
3. Ouvrir le portail captif et saisir les identifiants du réseau ainsi que les paramètres MQTT.
|
||||||
4. Ouvrir `http://<adresse-ip-de-l-esp32>/` dans un navigateur. La racine redirige vers le tableau de bord. Son URL directe est `http://<adresse-ip-de-l-esp32>/dashboard`.
|
4. Après connexion, le firmware démarre ArduinoOTA sous le nom réseau `Legende-XXXX`.
|
||||||
|
|
||||||
Autres interfaces :
|
Le mot de passe OTA initial est également `Legende_2026`. Changez les mots de passe configurés dans les sources avant tout déploiement réel. Les paramètres MQTT sont conservés en EEPROM ; les identifiants Wi-Fi sont gérés par WiFiManager.
|
||||||
|
|
||||||
| Chemin | Fonction |
|
|
||||||
| --- | --- |
|
|
||||||
| `/dashboard` | Tableau de bord et valeur de température simulée |
|
|
||||||
| `/update` | Interface de mise à jour du firmware par OTA |
|
|
||||||
| `/webserial` | Terminal série dans le navigateur |
|
|
||||||
|
|
||||||
L'interface OTA et le terminal WebSerial sont accessibles depuis le réseau local de l'appareil. Pour les mises à jour OTA, utiliser un firmware compilé pour cette carte et suivre les indications de l'interface.
|
|
||||||
|
|
||||||
## Structure du projet
|
## Structure du projet
|
||||||
|
|
||||||
```text
|
```text
|
||||||
.
|
.
|
||||||
├── platformio.ini # Carte, framework, dépendances et options de compilation
|
├── include/
|
||||||
|
│ ├── eeprom_storage.h
|
||||||
|
│ ├── main.h
|
||||||
|
│ ├── ota.h
|
||||||
|
│ └── wifi_config.h
|
||||||
|
├── platformio.ini
|
||||||
└── src/
|
└── src/
|
||||||
└── main.cpp # Initialisation Wi-Fi et serveur, boucle et mise à jour du tableau de bord
|
├── eeprom.cpp
|
||||||
|
├── main.cpp
|
||||||
|
├── ota.cpp
|
||||||
|
└── wifi.cpp
|
||||||
```
|
```
|
||||||
|
|
||||||
## Configuration et personnalisation
|
|
||||||
|
|
||||||
- La carte cible, le débit série, les bibliothèques et les options de compilation se configurent dans [`platformio.ini`](./platformio.ini).
|
|
||||||
- Le nom du point d'accès, les routes web et la fréquence d'actualisation du tableau de bord se trouvent dans [`src/main.cpp`](./src/main.cpp).
|
|
||||||
- La température affichée est générée aléatoirement entre 0 et 49 ; elle ne provient pas d'un capteur physique.
|
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
void ReadConfig(void);
|
||||||
|
void SaveConfig(void);
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
struct MyConfig {
|
||||||
|
uint32_t magic;
|
||||||
|
char mqtt_server[40];
|
||||||
|
char mqtt_user[32];
|
||||||
|
char mqtt_passw[32];
|
||||||
|
char topic[32];
|
||||||
|
char ssid[32];
|
||||||
|
char password[32];
|
||||||
|
};
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
void OtaSetup(void);
|
||||||
|
void OtaLoop(void);
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
extern MyConfig myConfig;
|
||||||
|
|
||||||
|
void WifiSetup(void);
|
||||||
+1
-6
@@ -7,11 +7,6 @@ monitor_speed = 115200
|
|||||||
|
|
||||||
lib_compat_mode = strict
|
lib_compat_mode = strict
|
||||||
lib_deps =
|
lib_deps =
|
||||||
ayushsharma82/NetWizard@2.0.0
|
tzapu/WiFiManager@^2.0.17
|
||||||
ayushsharma82/ESP-DASH@5.0.2
|
|
||||||
ayushsharma82/ElegantOTA@4.0.0
|
|
||||||
ayushsharma82/WebSerial@2.1.2
|
|
||||||
|
|
||||||
build_flags =
|
build_flags =
|
||||||
-D NETWIZARD_USE_ASYNC_WEBSERVER=1
|
|
||||||
-D ELEGANTOTA_USE_ASYNC_WEBSERVER=1
|
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include <EEPROM.h>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#include "eeprom_storage.h"
|
||||||
|
#include "wifi_config.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
constexpr uint32_t ConfigMagic = 0xDEADBEEF;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReadConfig(void) {
|
||||||
|
if (!EEPROM.begin(sizeof(myConfig))) {
|
||||||
|
Serial.println("EEPROM initialization failed.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
EEPROM.get(0, myConfig);
|
||||||
|
if (myConfig.magic != ConfigMagic) {
|
||||||
|
Serial.println("EEPROM is not initialized; loading default configuration.");
|
||||||
|
myConfig = {};
|
||||||
|
myConfig.magic = ConfigMagic;
|
||||||
|
strcpy(myConfig.mqtt_server, "undefined");
|
||||||
|
strcpy(myConfig.mqtt_user, "undefined");
|
||||||
|
strcpy(myConfig.mqtt_passw, "undefined");
|
||||||
|
strcpy(myConfig.topic, "undefined");
|
||||||
|
strcpy(myConfig.ssid, "undefined");
|
||||||
|
strcpy(myConfig.password, "undefined");
|
||||||
|
SaveConfig();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("Configuration loaded from EEPROM.");
|
||||||
|
}
|
||||||
|
|
||||||
|
void SaveConfig(void) {
|
||||||
|
EEPROM.put(0, myConfig);
|
||||||
|
if (!EEPROM.commit()) {
|
||||||
|
Serial.println("Failed to save configuration to EEPROM.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Serial.println("Configuration saved to EEPROM.");
|
||||||
|
}
|
||||||
+23
-44
@@ -1,54 +1,33 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <WiFi.h>
|
|
||||||
#include <AsyncTCP.h>
|
|
||||||
#include <ESPAsyncWebServer.h>
|
|
||||||
#include <NetWizard.h> // NetWizard Pro: <NetWizardPro.h>
|
|
||||||
#include <ESPDash.h>
|
|
||||||
#include <ElegantOTA.h> // ElegantOTA Pro: <ElegantOTAPro.h>
|
|
||||||
#include <WebSerial.h>
|
|
||||||
|
|
||||||
AsyncWebServer server(80);
|
#include "main.h"
|
||||||
|
#include "eeprom_storage.h"
|
||||||
|
#include "ota.h"
|
||||||
|
#include "wifi_config.h"
|
||||||
|
|
||||||
// WiFi provisioning: captive portal at / for access point clients
|
|
||||||
NetWizard NW(&server);
|
|
||||||
|
|
||||||
// Dashboard at /dashboard, so it does not conflict with the portal
|
/**
|
||||||
ESPDash dashboard(server, "/dashboard", true);
|
* initialisation de l'ESP32
|
||||||
dash::TemperatureCard temperature(dashboard, "Temperature");
|
*/
|
||||||
|
void setup()
|
||||||
unsigned long lastUpdate = 0;
|
{
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
Serial.println("Démarrage...");
|
||||||
|
delay(100);
|
||||||
|
|
||||||
// Connect to the saved network, or start the captive portal
|
// Lire la configuration depuis l' EEPROM (simulée dans le cas d'un ESP32)
|
||||||
NW.setStrategy(NetWizardStrategy::BLOCKING);
|
Serial.println("Lecture de la configuration...");
|
||||||
NW.autoConnect("My Device", "");
|
ReadConfig();
|
||||||
|
// Connect to Wi-Fi and setup MQTT
|
||||||
// Over-the-air updates at /update
|
Serial.println("Configuration du Wi-Fi...");
|
||||||
ElegantOTA.begin(&server);
|
WifiSetup();
|
||||||
|
Serial.print("SSID: ");
|
||||||
// Remote serial terminal at /webserial
|
Serial.println(myConfig.ssid);
|
||||||
WebSerial.begin(&server);
|
//setupMQTT();
|
||||||
WebSerial.onMessage([](const String& msg) {
|
OtaSetup();
|
||||||
Serial.println(msg);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Show the dashboard at / for clients on the local network
|
|
||||||
server.rewrite("/", "/dashboard").setFilter(ON_STA_FILTER);
|
|
||||||
|
|
||||||
server.begin();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
NW.loop();
|
OtaLoop();
|
||||||
ElegantOTA.loop();
|
delay(100);
|
||||||
WebSerial.loop();
|
|
||||||
|
|
||||||
if (millis() - lastUpdate > 3000) {
|
|
||||||
lastUpdate = millis();
|
|
||||||
temperature.setValue((int)random(0, 50));
|
|
||||||
dashboard.sendUpdates();
|
|
||||||
WebSerial.println("Dashboard updated");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+45
@@ -0,0 +1,45 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include <ArduinoOTA.h>
|
||||||
|
#include <WiFi.h>
|
||||||
|
|
||||||
|
#include "ota.h"
|
||||||
|
|
||||||
|
void OtaSetup()
|
||||||
|
{
|
||||||
|
char hostname[32];
|
||||||
|
uint8_t mac[6];
|
||||||
|
|
||||||
|
WiFi.macAddress(mac);
|
||||||
|
snprintf(hostname, sizeof(hostname), "Legende-%02X%02X", mac[4], mac[5]);
|
||||||
|
ArduinoOTA.setHostname(hostname);
|
||||||
|
|
||||||
|
ArduinoOTA.setPassword("Legende_2026");
|
||||||
|
|
||||||
|
ArduinoOTA.onStart([]() {
|
||||||
|
String type = (ArduinoOTA.getCommand() == U_FLASH) ? "sketch" : "filesystem";
|
||||||
|
Serial.println("Début de la mise à jour " + type);
|
||||||
|
});
|
||||||
|
ArduinoOTA.onEnd([]() {
|
||||||
|
Serial.println("\nMise à jour terminée !");
|
||||||
|
});
|
||||||
|
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||||
|
const unsigned int percentage = total == 0 ? 0 : (progress * 100ULL) / total;
|
||||||
|
Serial.printf("Progression : %u%%\r", percentage);
|
||||||
|
});
|
||||||
|
ArduinoOTA.onError([](ota_error_t error) {
|
||||||
|
Serial.printf("Erreur [%u]: ", error);
|
||||||
|
if (error == OTA_AUTH_ERROR) Serial.println("Échec d'authentification");
|
||||||
|
else if (error == OTA_BEGIN_ERROR) Serial.println("Échec du lancement");
|
||||||
|
else if (error == OTA_CONNECT_ERROR) Serial.println("Échec de connexion");
|
||||||
|
else if (error == OTA_RECEIVE_ERROR) Serial.println("Échec de réception");
|
||||||
|
else if (error == OTA_END_ERROR) Serial.println("Échec de fin");
|
||||||
|
});
|
||||||
|
|
||||||
|
ArduinoOTA.begin();
|
||||||
|
Serial.println("Système OTA prêt !");
|
||||||
|
}
|
||||||
|
|
||||||
|
void OtaLoop()
|
||||||
|
{
|
||||||
|
ArduinoOTA.handle();
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <WiFiManager.h>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
#include "eeprom_storage.h"
|
||||||
|
#include "main.h"
|
||||||
|
#include "wifi_config.h"
|
||||||
|
|
||||||
|
MyConfig myConfig;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
void copyConfigString(char* destination, size_t destinationSize, const char* value) {
|
||||||
|
if (destinationSize == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
strncpy(destination, value, destinationSize - 1);
|
||||||
|
destination[destinationSize - 1] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WifiSetup(void) {
|
||||||
|
WiFiManager wifiManager;
|
||||||
|
uint8_t mac[6];
|
||||||
|
char accessPointName[32];
|
||||||
|
|
||||||
|
WiFi.macAddress(mac);
|
||||||
|
snprintf(accessPointName, sizeof(accessPointName), "Legende-%02X%02X", mac[4], mac[5]);
|
||||||
|
|
||||||
|
wifiManager.setTitle("Légende de trains - Configuration Wi-Fi et MQTT");
|
||||||
|
|
||||||
|
WiFiManagerParameter customMqttServer(
|
||||||
|
"mqtt_server", "Serveur MQTT", myConfig.mqtt_server, sizeof(myConfig.mqtt_server) - 1);
|
||||||
|
WiFiManagerParameter customMqttUser(
|
||||||
|
"mqtt_user", "Utilisateur MQTT", myConfig.mqtt_user, sizeof(myConfig.mqtt_user) - 1);
|
||||||
|
WiFiManagerParameter customMqttPassword(
|
||||||
|
"mqtt_password", "Mot de passe MQTT", myConfig.mqtt_passw, sizeof(myConfig.mqtt_passw) - 1);
|
||||||
|
WiFiManagerParameter customTopic(
|
||||||
|
"topic", "Topic", myConfig.topic, sizeof(myConfig.topic) - 1);
|
||||||
|
|
||||||
|
wifiManager.addParameter(&customMqttServer);
|
||||||
|
wifiManager.addParameter(&customMqttUser);
|
||||||
|
wifiManager.addParameter(&customMqttPassword);
|
||||||
|
wifiManager.addParameter(&customTopic);
|
||||||
|
|
||||||
|
if (!wifiManager.autoConnect(accessPointName, "Legende_2026")) {
|
||||||
|
Serial.println("Échec de connexion Wi-Fi ou délai dépassé.");
|
||||||
|
delay(3000);
|
||||||
|
ESP.restart();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
copyConfigString(myConfig.mqtt_server, sizeof(myConfig.mqtt_server), customMqttServer.getValue());
|
||||||
|
copyConfigString(myConfig.mqtt_user, sizeof(myConfig.mqtt_user), customMqttUser.getValue());
|
||||||
|
copyConfigString(myConfig.mqtt_passw, sizeof(myConfig.mqtt_passw), customMqttPassword.getValue());
|
||||||
|
copyConfigString(myConfig.topic, sizeof(myConfig.topic), customTopic.getValue());
|
||||||
|
SaveConfig();
|
||||||
|
|
||||||
|
Serial.println("Connecté au Wi-Fi !");
|
||||||
|
Serial.print("Serveur MQTT enregistré : ");
|
||||||
|
Serial.println(myConfig.mqtt_server);
|
||||||
|
Serial.print("Topic enregistré : ");
|
||||||
|
Serial.println(myConfig.topic);
|
||||||
|
}
|
||||||
Reference in new issue
Block a user