From 5665cb3e5e282eda757237e23bbdaf776b14d7f5 Mon Sep 17 00:00:00 2001 From: Jose Date: Thu, 8 May 2025 07:24:28 +0200 Subject: [PATCH] Refactor MqttVerticle and GPS implementations: add stop method, update GPS serial handling, and improve WiFi connection logic with LED feedback --- .../contaminus/server/MqttVerticle.java | 11 +++ frontend/src/css/index.css | 10 +-- hardware/include/GPS.hpp | 3 - hardware/include/WifiConnection.hpp | 4 +- hardware/include/main.hpp | 2 + hardware/src/lib/inet/WifiConnection.cpp | 76 +++++++++++++++---- hardware/src/lib/sensor/GPS.cpp | 12 +-- hardware/src/main.cpp | 11 ++- 8 files changed, 98 insertions(+), 31 deletions(-) diff --git a/backend/src/main/java/net/miarma/contaminus/server/MqttVerticle.java b/backend/src/main/java/net/miarma/contaminus/server/MqttVerticle.java index b30baf2..b79b3b6 100644 --- a/backend/src/main/java/net/miarma/contaminus/server/MqttVerticle.java +++ b/backend/src/main/java/net/miarma/contaminus/server/MqttVerticle.java @@ -1,7 +1,18 @@ package net.miarma.contaminus.server; import io.vertx.core.AbstractVerticle; +import io.vertx.core.Promise; public class MqttVerticle extends AbstractVerticle { + + @Override + public void start(Promise startPromise) { + + } + + @Override + public void stop(Promise startPromise) { + + } } diff --git a/frontend/src/css/index.css b/frontend/src/css/index.css index f40eb4e..772d116 100644 --- a/frontend/src/css/index.css +++ b/frontend/src/css/index.css @@ -1,13 +1,13 @@ :root { - --primary-color: #be0f2e; - --secondary-color: #a8223a; - --text-shadow: #be0f2e80; - --box-shadow: #be0f2e33; + --primary-color: #018E55; + --secondary-color: #007946; + --text-shadow: #00794680; + --box-shadow: #00794633; --gradient-primary: #1A1A1A; --gradient-secondary: #2A2A2A; - --card-background: #be0f2e1a; + --card-background: #0079461a; --card-gradient-primary: #252525; --card-gradient-secondary: #353535; } diff --git a/hardware/include/GPS.hpp b/hardware/include/GPS.hpp index b32c679..c686927 100644 --- a/hardware/include/GPS.hpp +++ b/hardware/include/GPS.hpp @@ -1,8 +1,5 @@ #include "TinyGPSPlus.h" -#define RX 26 -#define TX 14 - struct GPSData_t { float lat; diff --git a/hardware/include/WifiConnection.hpp b/hardware/include/WifiConnection.hpp index 93ff3b2..4b0e79a 100644 --- a/hardware/include/WifiConnection.hpp +++ b/hardware/include/WifiConnection.hpp @@ -1,7 +1,7 @@ #include #include -#define SSID "iPhone de Álvaro" -#define PASSWORD "alvarito123" +#define SSID "DIGIFIBRA-D2ys" +#define PASSWORD "4EEATsyTcZ" int setupWifi(); \ No newline at end of file diff --git a/hardware/include/main.hpp b/hardware/include/main.hpp index 8c3db30..a33e96c 100644 --- a/hardware/include/main.hpp +++ b/hardware/include/main.hpp @@ -9,6 +9,8 @@ #define GPS_ID 3 #define MAX7219_ID 1 +#define DEBUG + #include "JsonTools.hpp" #include "RestClient.hpp" #include "WifiConnection.hpp" diff --git a/hardware/src/lib/inet/WifiConnection.cpp b/hardware/src/lib/inet/WifiConnection.cpp index 4b0cfc1..ab65eb2 100644 --- a/hardware/src/lib/inet/WifiConnection.cpp +++ b/hardware/src/lib/inet/WifiConnection.cpp @@ -1,33 +1,83 @@ #include - + +#define PIN_R 12 +#define PIN_G 13 +#define PIN_B 14 + WiFiClient wifiClient; +void setColor(uint8_t r, uint8_t g, uint8_t b) { + ledcWrite(0, r); + ledcWrite(1, g); + ledcWrite(2, b); +} + +void setupLED() { + ledcAttachPin(PIN_R, 0); + ledcAttachPin(PIN_G, 1); + ledcAttachPin(PIN_B, 2); + + ledcSetup(0, 5000, 8); + ledcSetup(1, 5000, 8); + ledcSetup(2, 5000, 8); +} + +// hue cycle +void hueCycle(uint8_t pos) { + uint8_t r = (uint8_t)(sin((pos + 0) * 0.024) * 127 + 128); + uint8_t g = (uint8_t)(sin((pos + 85) * 0.024) * 127 + 128); + uint8_t b = (uint8_t)(sin((pos + 170) * 0.024) * 127 + 128); + setColor(r, g, b); +} + int setupWifi() { - Serial.println(); - Serial.print("Connecting to "); - Serial.println(SSID); + setupLED(); + WiFi.mode(WIFI_STA); WiFi.begin(SSID, PASSWORD); - while (WiFi.status() != WL_CONNECTED) + #ifdef DEBUG + Serial.print("Conectando a la red WiFi: "); + Serial.print(SSID); + #endif + + int hue = 0; + uint32_t start = millis(); + const uint32_t timeout = 15000; + + while (WiFi.status() != WL_CONNECTED && millis() - start < timeout) { - delay(500); + hueCycle(hue++); + + #ifdef DEBUG Serial.print("."); + #endif + + delay(30); } - Serial.println(""); - Serial.println("WiFi connected"); - Serial.println("IP address: "); - Serial.println(WiFi.localIP()); - Serial.println("Setup!"); - - if(WiFi.status() == WL_CONNECTED) + if (WiFi.status() == WL_CONNECTED) { + setColor(0, 255, 0); + + #ifdef DEBUG + Serial.println("Conectado a la red WiFi"); + Serial.print("Dirección IP: "); + Serial.println(WiFi.localIP()); + #endif + return 0; } else { + setColor(255, 0, 0); + + #ifdef DEBUG + Serial.println("No se pudo conectar a la red WiFi"); + #endif + return 1; } } + diff --git a/hardware/src/lib/sensor/GPS.cpp b/hardware/src/lib/sensor/GPS.cpp index 327eeb9..ac1eb6f 100644 --- a/hardware/src/lib/sensor/GPS.cpp +++ b/hardware/src/lib/sensor/GPS.cpp @@ -1,22 +1,24 @@ #include "GPS.hpp" TinyGPSPlus gps; -HardwareSerial gpsSerial(1); +HardwareSerial SerialGPS(1); void GPS_Init() { - gpsSerial.begin(9600, SERIAL_8N1, RX, TX); + SerialGPS.begin(9600, SERIAL_8N1, 25, 26); // RX, TX } GPSData_t GPS_Read() { - while (gpsSerial.available() > 0) { - gps.encode(gpsSerial.read()); + while (SerialGPS.available() > 0) + { + gps.encode(SerialGPS.read()); } float lat = 0.0f, lon = 0.0f; - if (gps.location.isValid()) { + if (gps.location.isUpdated()) + { lat = gps.location.lat(); lon = gps.location.lng(); } diff --git a/hardware/src/main.cpp b/hardware/src/main.cpp index 61965b6..165dc56 100644 --- a/hardware/src/main.cpp +++ b/hardware/src/main.cpp @@ -1,14 +1,12 @@ #include "main.hpp" -#define DEBUG - const uint32_t DEVICE_ID = getChipID(); const char ALL_VEHICLES[] = "Todo tipo de vehiculos"; const char ELECTRIC_VEHICLES[] = "Solo vehiculos electricos/hibridos"; const char* currentMessage = nullptr; TaskTimer matrixTimer{0, 25}; -TaskTimer globalTimer{0, 30000}; +TaskTimer globalTimer{0, 60000}; extern HTTPClient httpClient; extern MD_Parola display; @@ -23,6 +21,9 @@ void setup() Serial.begin(115200); Serial.println("Iniciando..."); + + setupWifi(); + BME280_Init(); Serial.println("Sensor BME280 inicializado"); GPS_Init(); @@ -51,9 +52,13 @@ void loop() readBME280(); readGPS(); readMQ7(); + #ifdef DEBUG printAllData(); #endif + + + globalTimer.lastRun = now; } }