diff --git a/include/main.h b/include/main.h new file mode 100644 index 0000000..7850cbb --- /dev/null +++ b/include/main.h @@ -0,0 +1,9 @@ +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]; +}; \ No newline at end of file diff --git a/include/mqtt.h b/include/mqtt.h index baf8649..1827939 100644 --- a/include/mqtt.h +++ b/include/mqtt.h @@ -1 +1,2 @@ -void setupMQTT(void); \ No newline at end of file +void setupMQTT(void); +void loopMQTT(void); \ No newline at end of file diff --git a/include/wifi.h b/include/wifi.h index abf8bd7..95679fc 100644 --- a/include/wifi.h +++ b/include/wifi.h @@ -1,12 +1,6 @@ +#include "main.h" + + +extern WifiConfig wifiConfig; 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]; -}; \ No newline at end of file diff --git a/src/eeprom.cpp b/src/eeprom.cpp index 61240dd..f619a55 100644 --- a/src/eeprom.cpp +++ b/src/eeprom.cpp @@ -1,6 +1,7 @@ #include #include +#include "main.h" #include "eeprom.h" #include "wifi.h" @@ -17,33 +18,44 @@ void ReadConfig() { // Read configuration from EEPROM EEPROM.begin(sizeof(WifiConfig)); - WifiConfig config; - EEPROM.get(0, config); + // WifiConfig config; + EEPROM.get(0, wifiConfig); // Check magic number - if (config.magic != 0xDEADBEEF) { + if (wifiConfig.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); + wifiConfig.magic = 0xDEADBEEF; + strcpy(wifiConfig.mqtt_server, "undefined"); + strcpy(wifiConfig.mqtt_user, "undefined"); + strcpy(wifiConfig.mqtt_passw, "undefined"); + strcpy(wifiConfig.api_token, "undefined"); + strcpy(wifiConfig.ssid, "undefined"); + strcpy(wifiConfig.password, "undefined"); + EEPROM.put(0, wifiConfig); 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); + // globalMqttServer = String(wifiConfig.mqtt_server); + // globalMqttUser = String(wifiConfig.mqtt_user); + // globalMqttPassw = String(wifiConfig.mqtt_passw); + // globalApiToken = String(wifiConfig.api_token); + // globalSsid = String(wifiConfig.ssid); + // globalPassword = String(wifiConfig.password); Serial.println("Configuration loaded from EEPROM."); +} + +/** + * saveConfig() - Save configuration to EEPROM + * + * save the current configuration structure to EEPROM. + */ +void SaveConfig() { + EEPROM.put(0, wifiConfig); + EEPROM.commit(); + Serial.println("Configuration saved to EEPROM."); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 28db11f..6dc7d1e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include #include +#include "main.h" #include "pins.h" #include "wifi.h" #include "mqtt.h" @@ -31,12 +32,7 @@ void setup() { Serial.println("Démarrage..."); delay(100); - // TODO: implement - // Read config from EEPROM - ReadConfig(); - // - OtaSetup(); - + // Configure hardware pins pinMode(Pins::batteryVoltage, INPUT); #if defined(DEVICE_ROLE_BOAT) @@ -49,8 +45,15 @@ void setup() { pinMode(Pins::hallRudderSignal, INPUT); #endif + // Read config from EEPROM + ReadConfig(); + // Connect to Wi-Fi and setup MQTT WifiSetup(); setupMQTT(); + // Setup OTA + OtaSetup(); + + Serial.printf("Stid maritime: role=%s, mode=%s, chip=%06X\n", deviceRole, networkMode, ESP.getChipId()); Serial.println("Firmware skeleton ready; motors remain stopped."); @@ -61,6 +64,8 @@ void setup() { } void loop() { + OtaLoop(); + loopMQTT(); // Keep the initial firmware passive until the command and failsafe layers exist. - delay(1000); + delay(100); } diff --git a/src/mqtt.cpp b/src/mqtt.cpp index 2ff65f4..8b0b2c4 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -7,4 +7,12 @@ void setupMQTT(void) { // Initialisation du client MQTT mqttClient.setServer(mqtt_server, 1883); mqttClient.setCallback(mqttCallback); +} + +void loopMQTT(void) { + // Vérifier la connexion MQTT + if (!mqttClient.connected()) { + reconnectMQTT(); + } + mqttClient.loop(); } \ No newline at end of file diff --git a/src/wifi.cpp b/src/wifi.cpp index 8c988b0..cf36b47 100644 --- a/src/wifi.cpp +++ b/src/wifi.cpp @@ -10,10 +10,27 @@ * @date 2024-06-15 * @version 1.0 */ +#include +#include +#include + +#include "main.h" + +WifiConfig wifiConfig; void WifiSetup() { - static WifiConfig wifiConfig; + WiFiManager wifiManager; + // Lire 2 derniers digits de la MAC pour créer un nom unique + uint8_t mac[6]; + char ssid[32]; + + WiFi.macAddress(mac); + + // Nom du module sur le réseau (Optionnel) + strcpy(ssid, "Finist-RC-"); + sprintf(ssid + strlen(ssid), "%02X%02X", mac[4], mac[5]); + // Définition du code HTML personnalisé pour la liste déroulante ("; + wifiManager.setPageTitle("Bateau FinistRC - Configuration Wi-Fi et MQTT"); // Création du paramètre personnalisé // Syntaxe : WiFiManagerParameter(id, placeholder/label, default_value, length, custom_html) WiFiManagerParameter custom_device_mode("device_mode", "Mode", "bauto", 40, custom_dropdown_html); @@ -35,8 +53,6 @@ void WifiSetup() WiFiManagerParameter custom_mqtt_passw("mqtt", "Mot de passe MQTT", wifiConfig.mqtt_passw, 32); WiFiManagerParameter custom_api_token("api", "Clé API", wifiConfig.api_token, 32); - WiFiManager wifiManager; - // Associer la fonction de rappel wifiManager.setSaveConfigCallback(saveConfigCallback); @@ -48,7 +64,7 @@ void WifiSetup() wifiManager.addParameter(&custom_api_token); // Lance le portail - if (!wifiManager.autoConnect("FinistRC_Config","Finist29PWD")) { + if (!wifiManager.autoConnect(ssid,"FinistRC_2026")) { Serial.println("Échec de connexion ou timeout..."); delay(3000); ESP.restart();