From b2c7b7677bec2b568fef076a551c46ef09648d73 Mon Sep 17 00:00:00 2001 From: Jose Date: Tue, 6 May 2025 22:56:41 +0200 Subject: [PATCH] In the current state all the timings work flawlessly but MQ is still 0.0f and GPS is not read --- hardware/include/BMP280.hpp | 8 --- hardware/include/GPS.hpp | 4 +- hardware/include/MQ7v2.hpp | 8 +++ hardware/include/main.hpp | 3 +- hardware/src/lib/sensor/MQ7v2.cpp | 83 ++++++++++++++++++++++++++----- hardware/src/main.cpp | 70 ++++++++++++-------------- 6 files changed, 116 insertions(+), 60 deletions(-) delete mode 100644 hardware/include/BMP280.hpp diff --git a/hardware/include/BMP280.hpp b/hardware/include/BMP280.hpp deleted file mode 100644 index 858bbf3..0000000 --- a/hardware/include/BMP280.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#include -#include - -#define I2C_BMP280_ADDRESS 0x76 - -void BMP280_Init(); -uint8_t BMP280_DataReady(); -void BMP280_Read(); \ No newline at end of file diff --git a/hardware/include/GPS.hpp b/hardware/include/GPS.hpp index c361b2c..385ec08 100644 --- a/hardware/include/GPS.hpp +++ b/hardware/include/GPS.hpp @@ -1,7 +1,7 @@ #include "TinyGPSPlus.h" -#define RX 4 -#define TX 5 +#define RX 26 +#define TX 14 struct GPSData_t { diff --git a/hardware/include/MQ7v2.hpp b/hardware/include/MQ7v2.hpp index 2a97340..704a52c 100644 --- a/hardware/include/MQ7v2.hpp +++ b/hardware/include/MQ7v2.hpp @@ -5,6 +5,13 @@ #define ADC_RES 1023.0 #define RL 10000.0 // 10kΩ #define RO 10000.0 // Resistencia del aire limpio +#define RELAY_CONTROL_PIN 2 +#define MAX_SAMPLES 90 + +enum MQ7State_t +{ + HEATING, SAMPLING +}; struct MQ7Data_t { @@ -12,4 +19,5 @@ struct MQ7Data_t }; void MQ7_Init(); +bool MQ7_Update(); MQ7Data_t MQ7_Read(); \ No newline at end of file diff --git a/hardware/include/main.hpp b/hardware/include/main.hpp index 0f24511..ddd1859 100644 --- a/hardware/include/main.hpp +++ b/hardware/include/main.hpp @@ -28,8 +28,9 @@ enum AirQualityStatus { BAD }; -void readMQ7(); +void processMQ7(); void readBME280(); void readGPS(); void writeMatrix(const char* message); +void printAllData(); uint32_t getChipID(); \ No newline at end of file diff --git a/hardware/src/lib/sensor/MQ7v2.cpp b/hardware/src/lib/sensor/MQ7v2.cpp index 569d687..237db30 100644 --- a/hardware/src/lib/sensor/MQ7v2.cpp +++ b/hardware/src/lib/sensor/MQ7v2.cpp @@ -1,22 +1,81 @@ #include "MQ7v2.hpp" +MQ7State_t mq7State = HEATING; +uint32_t mq7StateStart = 0; // t_0 +float lastPPM = 0.0f; +float mq7Samples[MAX_SAMPLES]; +uint8_t sampleIndex = 0; + void MQ7_Init() { pinMode(MQ7_PIN, INPUT); + pinMode(RELAY_CONTROL_PIN, OUTPUT); + mq7State = HEATING; + mq7StateStart = millis(); // t_0 + digitalWrite(RELAY_CONTROL_PIN, LOW); // NC +} + +bool MQ7_Update() +{ + unsigned long now = millis(); + + switch (mq7State) { + case HEATING: + if (now - mq7StateStart >= 60000) { + digitalWrite(RELAY_CONTROL_PIN, HIGH); // NO - lectura + mq7State = SAMPLING; + mq7StateStart = now; + } + break; + + case SAMPLING: + static uint32_t lastSampleTime = 0; + const uint32_t sampleInterval = 1000; // 1 muestra/segundo + + if(now - lastSampleTime >= sampleInterval && + sampleIndex < MAX_SAMPLES) + { + lastSampleTime = now; + + int adcValue = analogRead(MQ7_PIN); + float voltage = (adcValue / ADC_RES) * VCC; + + if (voltage > 0.1) + { + float rs = (VCC - voltage) * RL / voltage; + float ratio = rs / RO; + float ppm = pow(10, (-1.5 * log10(ratio) + 0.8)); + mq7Samples[sampleIndex++] = ppm; + } + else + { + mq7Samples[sampleIndex++] = 0.0f; + } + } + + if(now - mq7StateStart >= 90000) + { + float sum = 0; + for(uint8_t i = 0; i < sampleIndex; i++) + { + sum += mq7Samples[i]; + } + + lastPPM = (sampleIndex > 0) ? (sum / sampleIndex) : 0.0f; + + sampleIndex = 0; + digitalWrite(RELAY_CONTROL_PIN, LOW); // NC + mq7State = HEATING; + mq7StateStart = now; // t_0 + return true; + } + break; + } + + return false; } MQ7Data_t MQ7_Read() { - int adcValue = analogRead(MQ7_PIN); - float voltage = (adcValue / ADC_RES) * VCC; - - if (voltage <= 0.1) { - return {0.0}; - } - - float rs = (VCC - voltage) * RL / voltage; - float ratio = rs / RO; - float ppm = pow(10, (-1.5 * log10(ratio) + 0.8)); - - return {ppm}; + return {lastPPM}; } \ No newline at end of file diff --git a/hardware/src/main.cpp b/hardware/src/main.cpp index 4aa99d5..7cebf64 100644 --- a/hardware/src/main.cpp +++ b/hardware/src/main.cpp @@ -5,11 +5,8 @@ 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 bmeTimer{0, 1000}; -TaskTimer mq7Timer{0, 1000}; -TaskTimer gpsTimer{0, 1000}; -TaskTimer printTimer{0, 1000}; TaskTimer matrixTimer{0, 25}; extern HTTPClient httpClient; @@ -31,6 +28,7 @@ void setup() MQ7_Init(); Serial.println("Sensor MQ7 inicializado"); + // inicializar el estado de la matriz writeMatrix(ALL_VEHICLES); } @@ -38,41 +36,29 @@ void loop() { uint32_t now = millis(); + // animación matriz de leds if (now - matrixTimer.lastRun >= matrixTimer.interval) { if (MAX7219_Animate()) { MAX7219_ResetAnimation(); } matrixTimer.lastRun = now; } - - if (now - bmeTimer.lastRun >= bmeTimer.interval) { + + // MQ7 tarda mas y marca el ritmo + if (MQ7_Update()) { + mq7Data.co = MQ7_Read().co; readBME280(); - bmeTimer.lastRun = now; - } - - if (now - mq7Timer.lastRun >= mq7Timer.interval) { - readMQ7(); - mq7Timer.lastRun = now; - } - - if (now - gpsTimer.lastRun >= gpsTimer.interval) { readGPS(); - gpsTimer.lastRun = now; + processMQ7(); + #ifdef DEBUG + printAllData(); + #endif + // sendToServer(); } - - #ifdef DEBUG - if (now - printTimer.lastRun >= printTimer.interval) { - Serial.print("---------------------\n"); - Serial.println("Leyendo sensores..."); - printTimer.lastRun = now; - } - #endif } -void readMQ7() +void processMQ7() { - mq7Data = MQ7_Read(); - #ifdef DEBUG Serial.print("CO: "); Serial.println(mq7Data.co); #endif @@ -91,34 +77,44 @@ void readMQ7() } } - void readBME280() { 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 readGPS() { gpsData = GPS_Read(); - #ifdef DEBUG - Serial.print("Latitud: "); Serial.println(gpsData.lat); - Serial.print("Longitud: "); Serial.println(gpsData.lon); - #endif } void writeMatrix(const char* message) { + if (currentMessage == message) return; + currentMessage = message; + #ifdef DEBUG Serial.println("Escribiendo en el display..."); #endif + MAX7219_DisplayText(message, PA_LEFT, 50, 0); } +void printAllData() +{ + Serial.println("---------------------"); + + Serial.print("ID: "); Serial.println(DEVICE_ID, HEX); + + 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(" %"); + + Serial.print("Latitud: "); Serial.println(gpsData.lat); + Serial.print("Longitud: "); Serial.println(gpsData.lon); + + Serial.print("CO: "); Serial.println(mq7Data.co); +} + uint32_t getChipID() { uint32_t chipId = 0;