From c74ddc626c3ff85c8debe0fea19826a3c02ff636 Mon Sep 17 00:00:00 2001 From: Jose Date: Tue, 3 Jun 2025 23:21:59 +0200 Subject: [PATCH] Refactor WiFi connection handling: move PIN definitions, enhance connection logic, and integrate MQTT initialization in setup --- hardware/include/WifiConnection.hpp | 8 ++- hardware/src/lib/inet/WifiConnection.cpp | 64 ++++++++++++------------ hardware/src/main.cpp | 29 ++++++++--- 3 files changed, 60 insertions(+), 41 deletions(-) diff --git a/hardware/include/WifiConnection.hpp b/hardware/include/WifiConnection.hpp index 35b1f7e..c22f82b 100644 --- a/hardware/include/WifiConnection.hpp +++ b/hardware/include/WifiConnection.hpp @@ -6,4 +6,10 @@ #define SSID "DIGIFIBRA-D2ys" #define WIFI_PASSWORD "4EEATsyTcZ" -int WiFi_Init(); \ No newline at end of file +#define PIN_R 12 +#define PIN_G 13 +#define PIN_B 14 + +void WiFi_Init(); +void WiFi_Handle(); +bool WiFi_IsConnected(); \ No newline at end of file diff --git a/hardware/src/lib/inet/WifiConnection.cpp b/hardware/src/lib/inet/WifiConnection.cpp index 010f9ed..350cc78 100644 --- a/hardware/src/lib/inet/WifiConnection.cpp +++ b/hardware/src/lib/inet/WifiConnection.cpp @@ -1,10 +1,10 @@ #include -#define PIN_R 12 -#define PIN_G 13 -#define PIN_B 14 + WiFiClient wifiClient; +static bool wifiConnected = false; +static TaskTimer wifiTimer{0, 500}; void setColor(uint8_t r, uint8_t g, uint8_t b) { @@ -25,7 +25,6 @@ void setupLED() ledcAttachPin(PIN_B, 2); } -// hue cycle void hueCycle(uint8_t pos) { uint8_t r = (uint8_t)(sin((pos + 0) * 0.024) * 127 + 128); @@ -34,53 +33,54 @@ void hueCycle(uint8_t pos) setColor(r, g, b); } -int WiFi_Init() +void WiFi_Init() { setupLED(); - WiFi.mode(WIFI_STA); WiFi.begin(SSID, WIFI_PASSWORD); - #ifdef DEBUG - Serial.print("Conectando a la red WiFi: "); - Serial.print(SSID); + Serial.print("🟡 Intentando conectar a WiFi: "); + Serial.println(SSID); #endif +} - int hue = 0; - uint32_t start = millis(); - const uint32_t timeout = 15000; +bool WiFi_IsConnected() +{ + return wifiConnected; +} - while (WiFi.status() != WL_CONNECTED && millis() - start < timeout) +void WiFi_Handle() +{ + static uint8_t hue = 0; + uint32_t now = millis(); + + if (!wifiConnected) { hueCycle(hue++); - -#ifdef DEBUG - Serial.print("."); -#endif - - delay(30); } if (WiFi.status() == WL_CONNECTED) { - setColor(0, 255, 0); - + if (!wifiConnected) + { #ifdef DEBUG - Serial.println("Conectado a la red WiFi"); - Serial.print("Dirección IP: "); - Serial.println(WiFi.localIP()); + Serial.println("🟢 Conectado a la red WiFi"); + Serial.print("IP: "); + Serial.println(WiFi.localIP()); #endif - - return 0; + setColor(0, 255, 0); + wifiConnected = true; + } + return; } - else + + if (now - wifiRetryTimer.lastRun >= wifiRetryTimer.interval) { - setColor(255, 0, 0); - #ifdef DEBUG - Serial.println("No se pudo conectar a la red WiFi"); + Serial.println("🔁 Reintentando conexión WiFi..."); #endif - - return 1; + WiFi.disconnect(true); + WiFi.begin(SSID, WIFI_PASSWORD); + wifiRetryTimer.lastRun = now; } } \ No newline at end of file diff --git a/hardware/src/main.cpp b/hardware/src/main.cpp index 028be4b..e8cc304 100644 --- a/hardware/src/main.cpp +++ b/hardware/src/main.cpp @@ -25,6 +25,8 @@ BME280Data_t bme280Data; GPSData_t gpsData; #endif +static bool mqttStarted = false; + void setup() { Serial.begin(115200); @@ -33,16 +35,11 @@ void setup() Serial.println("Iniciando..."); #endif - while(WiFi_Init() != WL_CONNECTED) - { - Serial.println("Esperando conexión WiFi..."); - delay(1000); - } - MQTT_Init(MQTT_URI, MQTT_PORT); - + WiFi_Init(); + try { - + #if DEVICE_ROLE == SENSOR GROUP_ID = getGroupId(DEVICE_ID); BME280_Init(); @@ -72,6 +69,22 @@ void setup() void loop() { + WiFi_Handle(); + + if(!WiFi_IsConnected()) + { + return; + } + + if (!mqttStarted) + { + MQTT_Init(MQTT_SERVER, MQTT_PORT); + mqttStarted = true; +#ifdef DEBUG + Serial.println("Iniciando conexión MQTT..."); +#endif + } + uint32_t now = millis(); #if DEVICE_ROLE == ACTUATOR