From 5d1b2feeab37b6a9c9aa3256457dd707c8d1bccb Mon Sep 17 00:00:00 2001 From: Jose Date: Sun, 4 May 2025 23:23:37 +0200 Subject: [PATCH] Refactor sensor data handling: update sensor read functions to return structured data and improve timer management in main loop --- hardware/.vscode/settings.json | 5 +- hardware/include/BME280.hpp | 10 +- hardware/include/GPS.hpp | 9 +- hardware/include/MAX7219.hpp | 2 +- hardware/include/MQ7v2.hpp | 7 +- hardware/include/main.hpp | 25 ++++- hardware/platformio.ini | 1 + hardware/src/lib/actuator/MAX7219.cpp | 3 +- hardware/src/lib/sensor/BME280.cpp | 12 +-- hardware/src/lib/sensor/GPS.cpp | 9 +- hardware/src/lib/sensor/MQ7v2.cpp | 7 +- hardware/src/main.cpp | 144 +++++++++++++++++--------- 12 files changed, 156 insertions(+), 78 deletions(-) diff --git a/hardware/.vscode/settings.json b/hardware/.vscode/settings.json index d626a36..336e462 100644 --- a/hardware/.vscode/settings.json +++ b/hardware/.vscode/settings.json @@ -14,10 +14,11 @@ "unordered_set": "cpp", "vector": "cpp", "string_view": "cpp", - "initializer_list": "cpp" + "initializer_list": "cpp", + "system_error": "cpp" }, "github.copilot.enable": { - "*": false, + "*": true, "plaintext": false, "markdown": false, "scminput": false, diff --git a/hardware/include/BME280.hpp b/hardware/include/BME280.hpp index 38efd2d..4aae487 100644 --- a/hardware/include/BME280.hpp +++ b/hardware/include/BME280.hpp @@ -3,6 +3,12 @@ #define I2C_BME280_ADDRESS 0x76 +struct BME280Data_t +{ + float pressure; + float temperature; + float humidity; +}; + void BME280_Init(); -bool BME280_DataReady(); -bool BME280_Read(float &pressure, float &temperature, float &humidity); \ No newline at end of file +BME280Data_t BME280_Read(); \ No newline at end of file diff --git a/hardware/include/GPS.hpp b/hardware/include/GPS.hpp index 91ca80e..c361b2c 100644 --- a/hardware/include/GPS.hpp +++ b/hardware/include/GPS.hpp @@ -3,5 +3,10 @@ #define RX 4 #define TX 5 -void GPS_Init(); -void GPS_Read(float &lat, float &lon); \ No newline at end of file +struct GPSData_t +{ + float lat; + float lon; +}; + +GPSData_t GPS_Read(); \ No newline at end of file diff --git a/hardware/include/MAX7219.hpp b/hardware/include/MAX7219.hpp index 0e7f26b..a5b493f 100644 --- a/hardware/include/MAX7219.hpp +++ b/hardware/include/MAX7219.hpp @@ -10,7 +10,7 @@ void MAX7219_Init(); void MAX7219_DisplayText(const char *text, textPosition_t align, uint16_t speed, uint16_t pause); -bool MAX7219_StartAnimation(); +bool MAX7219_Animate(); void MAX7219_ResetAnimation(); void MAX7219_ClearDisplay(); void MAX7219_SetBrightness(uint8_t brightness); \ No newline at end of file diff --git a/hardware/include/MQ7v2.hpp b/hardware/include/MQ7v2.hpp index a1099d0..2a97340 100644 --- a/hardware/include/MQ7v2.hpp +++ b/hardware/include/MQ7v2.hpp @@ -6,5 +6,10 @@ #define RL 10000.0 // 10kΩ #define RO 10000.0 // Resistencia del aire limpio +struct MQ7Data_t +{ + float co; +}; + void MQ7_Init(); -void MQ7_Read(float &sensorValue); \ No newline at end of file +MQ7Data_t MQ7_Read(); \ No newline at end of file diff --git a/hardware/include/main.hpp b/hardware/include/main.hpp index eb472fe..0f24511 100644 --- a/hardware/include/main.hpp +++ b/hardware/include/main.hpp @@ -13,8 +13,23 @@ #include "MAX7219.hpp" #include "MQ7v2.hpp" -uint32_t getChipID(); -void prettyReadMQ7(); -void prettyReadBME280(); -void prettyReadGPS(); -void testMatrix(); \ No newline at end of file +struct TaskTimer { + uint32_t lastRun = 0; + uint32_t interval = 1000; + + TaskTimer() = default; + + TaskTimer(uint32_t last, uint32_t interval) + : lastRun(last), interval(interval) {} +}; + +enum AirQualityStatus { + GOOD, + BAD +}; + +void readMQ7(); +void readBME280(); +void readGPS(); +void writeMatrix(const char* message); +uint32_t getChipID(); \ No newline at end of file diff --git a/hardware/platformio.ini b/hardware/platformio.ini index b9b1585..009ee27 100644 --- a/hardware/platformio.ini +++ b/hardware/platformio.ini @@ -12,6 +12,7 @@ platform = espressif32 board = esp32dev framework = arduino +monitor_speed = 115200 lib_deps = knolleary/PubSubClient@^2.8 mikalhart/TinyGPSPlus@^1.0.2 diff --git a/hardware/src/lib/actuator/MAX7219.cpp b/hardware/src/lib/actuator/MAX7219.cpp index b2843ea..a5a807f 100644 --- a/hardware/src/lib/actuator/MAX7219.cpp +++ b/hardware/src/lib/actuator/MAX7219.cpp @@ -7,6 +7,7 @@ void MAX7219_Init() display.begin(); display.setIntensity(1); // 0-15 display.displayClear(); + display.displaySuspend(false); } void MAX7219_DisplayText(const char *text, textPosition_t align, uint16_t speed, uint16_t pause) @@ -14,7 +15,7 @@ void MAX7219_DisplayText(const char *text, textPosition_t align, uint16_t speed, display.displayText(text, align, speed, pause, PA_SCROLL_LEFT, PA_SCROLL_LEFT); } -bool MAX7219_StartAnimation() +bool MAX7219_Animate() { return display.displayAnimate(); } diff --git a/hardware/src/lib/sensor/BME280.cpp b/hardware/src/lib/sensor/BME280.cpp index 05402d3..b129470 100644 --- a/hardware/src/lib/sensor/BME280.cpp +++ b/hardware/src/lib/sensor/BME280.cpp @@ -23,12 +23,12 @@ void BME280_Init() while (!bme.begin()); } -bool BME280_Read(float &pressure, float &temperature, float &humidity) +BME280Data_t BME280_Read() { - BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); - BME280::PresUnit presUnit(BME280::PresUnit_Pa); - bme.read(pressure, temperature, humidity, tempUnit, presUnit); - - return (temperature != 0.0f && pressure != 0.0f); + float p, t, h; + BME280::TempUnit tUnit(BME280::TempUnit_Celsius); + BME280::PresUnit pUnit(BME280::PresUnit_Pa); + bme.read(p, t, h, tUnit, pUnit); + return {p, t, h}; } diff --git a/hardware/src/lib/sensor/GPS.cpp b/hardware/src/lib/sensor/GPS.cpp index 51bf9c1..3bd1d67 100644 --- a/hardware/src/lib/sensor/GPS.cpp +++ b/hardware/src/lib/sensor/GPS.cpp @@ -2,13 +2,9 @@ TinyGPSPlus gps; -void GPS_Init() -{ - Serial.begin(9600); -} - -void GPS_Read(float &lat, float &lon) +GPSData_t GPS_Read() { + float lat, lon; if (gps.location.isValid()) { lat = gps.location.lat(); @@ -19,4 +15,5 @@ void GPS_Read(float &lat, float &lon) lat = 0.0f; lon = 0.0f; } + return {lat, lon}; } \ No newline at end of file diff --git a/hardware/src/lib/sensor/MQ7v2.cpp b/hardware/src/lib/sensor/MQ7v2.cpp index 3055265..569d687 100644 --- a/hardware/src/lib/sensor/MQ7v2.cpp +++ b/hardware/src/lib/sensor/MQ7v2.cpp @@ -5,19 +5,18 @@ void MQ7_Init() pinMode(MQ7_PIN, INPUT); } -void MQ7_Read(float &sensorValue) +MQ7Data_t MQ7_Read() { int adcValue = analogRead(MQ7_PIN); float voltage = (adcValue / ADC_RES) * VCC; if (voltage <= 0.1) { - sensorValue = 0; - return; + return {0.0}; } float rs = (VCC - voltage) * RL / voltage; float ratio = rs / RO; float ppm = pow(10, (-1.5 * log10(ratio) + 0.8)); - sensorValue = ppm; + return {ppm}; } \ No newline at end of file diff --git a/hardware/src/main.cpp b/hardware/src/main.cpp index ba8a518..4aa99d5 100644 --- a/hardware/src/main.cpp +++ b/hardware/src/main.cpp @@ -1,18 +1,28 @@ #include "main.hpp" -const uint32_t deviceId = getChipID(); +#define DEBUG -extern HTTPClient httpClient; // HTTP client object -String response; // HTTP Response -float sensorVolt, sensorValue; // MQ7 vars -float temperature, pressure, humidity; // BME280 vars -float lon, lat; // GPS vars -extern MD_Parola display; // Display object +const uint32_t DEVICE_ID = getChipID(); +const char ALL_VEHICLES[] = "Todo tipo de vehiculos"; +const char ELECTRIC_VEHICLES[] = "Solo vehiculos electricos/hibridos"; + +TaskTimer bmeTimer{0, 1000}; +TaskTimer mq7Timer{0, 1000}; +TaskTimer gpsTimer{0, 1000}; +TaskTimer printTimer{0, 1000}; +TaskTimer matrixTimer{0, 25}; + +extern HTTPClient httpClient; +extern MD_Parola display; + +MQ7Data_t mq7Data; +BME280Data_t bme280Data; +GPSData_t gpsData; +AirQualityStatus currentAirStatus = GOOD; void setup() { - Serial.begin(9600); - + Serial.begin(115200); Serial.println("Iniciando..."); BME280_Init(); Serial.println("Sensor BME280 inicializado"); @@ -21,65 +31,103 @@ void setup() MQ7_Init(); Serial.println("Sensor MQ7 inicializado"); - /* - GPS_Init(); - Serial.println("GPS inicializado"); - */ - - prettyReadBME280(); - testMatrix(); - prettyReadMQ7(); - - /* - - */ + writeMatrix(ALL_VEHICLES); } void loop() { - if(MAX7219_StartAnimation()) - { - MAX7219_ResetAnimation(); + uint32_t now = millis(); + + if (now - matrixTimer.lastRun >= matrixTimer.interval) { + if (MAX7219_Animate()) { + MAX7219_ResetAnimation(); + } + matrixTimer.lastRun = now; + } + + if (now - bmeTimer.lastRun >= bmeTimer.interval) { + readBME280(); + bmeTimer.lastRun = now; + } + + if (now - mq7Timer.lastRun >= mq7Timer.interval) { + readMQ7(); + mq7Timer.lastRun = now; + } + + if (now - gpsTimer.lastRun >= gpsTimer.interval) { + readGPS(); + gpsTimer.lastRun = now; + } + + #ifdef DEBUG + if (now - printTimer.lastRun >= printTimer.interval) { + Serial.print("---------------------\n"); + Serial.println("Leyendo sensores..."); + printTimer.lastRun = now; + } + #endif +} + +void readMQ7() +{ + mq7Data = MQ7_Read(); + + #ifdef DEBUG + Serial.print("CO: "); Serial.println(mq7Data.co); + #endif + + const float CO_THRESHOLD = 10.0f; + + AirQualityStatus newStatus = (mq7Data.co >= CO_THRESHOLD) ? BAD : GOOD; + + if (newStatus != currentAirStatus) { + currentAirStatus = newStatus; + if (currentAirStatus == BAD) { + writeMatrix(ELECTRIC_VEHICLES); + } else { + writeMatrix(ALL_VEHICLES); + } } } -void prettyReadMQ7() + +void readBME280() { - Serial.println("Leyendo sensor MQ7..."); - MQ7_Read(sensorValue); - Serial.print("\t - Valor sensor: "); Serial.print(sensorValue); Serial.print("\r\n"); + bme280Data = BME280_Read(); + #ifdef DEBUG + Serial.print("Presión: "); Serial.print(bme280Data.pressure / 100); Serial.println(" hPa"); + Serial.print("Temperatura: "); Serial.print(bme280Data.temperature); Serial.println(" °C"); + Serial.print("Humedad: "); Serial.print(bme280Data.humidity); Serial.println(" %"); + #endif } -void prettyReadBME280() +void readGPS() { - Serial.println("Leyendo sensor BME280..."); - BME280_Read(pressure, temperature, humidity); - Serial.print("\t - Presión: "); Serial.print(pressure/100); Serial.print("hPa\r\n"); - Serial.print("\t - Temperatura: "); Serial.print(temperature); Serial.print("°C\r\n"); - Serial.print("\t - Humedad: "); Serial.print(humidity); Serial.print("%\r\n"); + gpsData = GPS_Read(); + #ifdef DEBUG + Serial.print("Latitud: "); Serial.println(gpsData.lat); + Serial.print("Longitud: "); Serial.println(gpsData.lon); + #endif } -void prettyReadGPS() -{ - Serial.println("Leyendo GPS..."); - GPS_Read(lat, lon); - Serial.print("\t - Latitud: "); Serial.print(lat); Serial.print("\r\n"); - Serial.print("\t - Longitud: "); Serial.print(lon); Serial.print("\r\n"); -} - -void testMatrix() +void writeMatrix(const char* message) { + #ifdef DEBUG Serial.println("Escribiendo en el display..."); - MAX7219_DisplayText("Prueba de texto", PA_LEFT, 50, 0); - + #endif + MAX7219_DisplayText(message, PA_LEFT, 50, 0); } uint32_t getChipID() { - uint32_t chipId; - for (int i = 0; i < 17; i = i + 8) + uint32_t chipId = 0; + for (int i = 0; i < 17; i += 8) { chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i; } + #ifdef DEBUG + Serial.print("Chip ID: "); Serial.println(chipId, HEX); + #endif return chipId; -} \ No newline at end of file +}