1
0

In the current state all the timings work flawlessly but MQ is still 0.0f and GPS is not read

This commit is contained in:
Jose
2025-05-06 22:56:41 +02:00
parent fa1b457004
commit b2c7b7677b
6 changed files with 116 additions and 60 deletions

View File

@@ -1,8 +0,0 @@
#include <Wire.h>
#include <BMP280_DEV.h>
#define I2C_BMP280_ADDRESS 0x76
void BMP280_Init();
uint8_t BMP280_DataReady();
void BMP280_Read();

View File

@@ -1,7 +1,7 @@
#include "TinyGPSPlus.h"
#define RX 4
#define TX 5
#define RX 26
#define TX 14
struct GPSData_t
{

View File

@@ -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();

View File

@@ -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();

View File

@@ -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};
}

View File

@@ -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;