Ajout support OTA
This commit is contained in:
1 parent
be18f5b7d0
commit
845205c60d
7 files changed
+118
No files matched your search
@@ -0,0 +1 @@
|
||||
void ReadConfig(void);
|
||||
@@ -0,0 +1,2 @@
|
||||
void OtaSetup(void);
|
||||
void OtaLoop(void);
|
||||
@@ -2,8 +2,11 @@
|
||||
void WifiSetup(void);
|
||||
|
||||
struct WifiConfig {
|
||||
long magic;
|
||||
char mqtt_server[40];
|
||||
char mqtt_user[32];
|
||||
char mqtt_passw[32];
|
||||
char api_token[32];
|
||||
char ssid[32];
|
||||
char password[32];
|
||||
};
|
||||
@@ -9,6 +9,7 @@ board = nodemcuv2
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
monitor_filters = direct
|
||||
upload_protocol = espota
|
||||
build_flags =
|
||||
-D CORE_DEBUG_LEVEL=0
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <arduino.h>
|
||||
#include <EEPROM.h>
|
||||
|
||||
#include "eeprom.h"
|
||||
#include "wifi.h"
|
||||
|
||||
/**
|
||||
* readConfig() - Read configuration from EEPROM
|
||||
*
|
||||
* read configuration structure from EEPROM and populate the global configuration variables.
|
||||
* if magic number is not correct, it means EEPROM is not initialized, so we will initialize it with default values.
|
||||
*
|
||||
* Value is stored inside a struct WifiConfig, which is defined in wifi.h. The struct contains the following fields:
|
||||
* - long magic: a magic number to check if EEPROM is initialized
|
||||
*
|
||||
*/
|
||||
void ReadConfig() {
|
||||
// Read configuration from EEPROM
|
||||
EEPROM.begin(sizeof(WifiConfig));
|
||||
WifiConfig config;
|
||||
EEPROM.get(0, config);
|
||||
|
||||
// Check magic number
|
||||
if (config.magic != 0xDEADBEEF) {
|
||||
Serial.println("EEPROM not initialized, initializing with default values...");
|
||||
// Initialize with default values
|
||||
config.magic = 0xDEADBEEF;
|
||||
strcpy(config.mqtt_server, "undefined");
|
||||
strcpy(config.mqtt_user, "undefined");
|
||||
strcpy(config.mqtt_passw, "undefined");
|
||||
strcpy(config.api_token, "undefined");
|
||||
strcpy(config.ssid, "undefined");
|
||||
strcpy(config.password, "undefined");
|
||||
EEPROM.put(0, config);
|
||||
EEPROM.commit();
|
||||
}
|
||||
|
||||
// Populate global configuration variables
|
||||
// Assuming you have global variables for these configurations
|
||||
// e.g., globalMqttServer, globalMqttUser, etc.
|
||||
// globalMqttServer = String(config.mqtt_server);
|
||||
// globalMqttUser = String(config.mqtt_user);
|
||||
// globalMqttPassw = String(config.mqtt_passw);
|
||||
// globalApiToken = String(config.api_token);
|
||||
// globalSsid = String(config.ssid);
|
||||
// globalPassword = String(config.password);
|
||||
|
||||
Serial.println("Configuration loaded from EEPROM.");
|
||||
}
|
||||
@@ -1,9 +1,14 @@
|
||||
#include <Arduino.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266mDNS.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <ArduinoOTA.h>
|
||||
|
||||
#include "pins.h"
|
||||
#include "wifi.h"
|
||||
#include "mqtt.h"
|
||||
#include "ota.h"
|
||||
#include "eeprom.h"
|
||||
|
||||
#if defined(DEVICE_ROLE_BOAT)
|
||||
constexpr const char *deviceRole = "boat";
|
||||
@@ -23,8 +28,15 @@ constexpr const char *networkMode = "ap";
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Démarrage...");
|
||||
delay(100);
|
||||
|
||||
// TODO: implement
|
||||
// Read config from EEPROM
|
||||
ReadConfig();
|
||||
//
|
||||
OtaSetup();
|
||||
|
||||
pinMode(Pins::batteryVoltage, INPUT);
|
||||
|
||||
#if defined(DEVICE_ROLE_BOAT)
|
||||
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
|
||||
void OtaSetup()
|
||||
{
|
||||
char hostname[32];
|
||||
// Lire 2 derniers digits de la MAC pour créer un nom unique
|
||||
uint8_t mac[6];
|
||||
|
||||
WiFi.macAddress(mac);
|
||||
|
||||
// Nom du module sur le réseau (Optionnel)
|
||||
strcpy(hostname, "Finist-RC-");
|
||||
sprintf(hostname + strlen(hostname), "%02X%02X", mac[4], mac[5]);
|
||||
ArduinoOTA.setHostname(hostname);
|
||||
|
||||
// Mot de passe pour sécuriser la mise à jour (Optionnel mais recommandé)
|
||||
ArduinoOTA.setPassword("FinistRC_2026");
|
||||
|
||||
// Callbacks de suivi (Optionnel, utile pour le debug)
|
||||
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) {
|
||||
Serial.printf("Progression : %u%%\r", (progress / (total / 100)));
|
||||
});
|
||||
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");
|
||||
});
|
||||
|
||||
// 3. Initialisation d'OTA
|
||||
ArduinoOTA.begin();
|
||||
Serial.println("Système OTA prêt !");
|
||||
}
|
||||
|
||||
void OtaLoop()
|
||||
{
|
||||
// 4. Écoute cruciale des requêtes OTA (Ne pas bloquer la loop !)
|
||||
ArduinoOTA.handle();
|
||||
}
|
||||
Reference in new issue
Block a user