Refactor GPS and MQ7 sensor implementations: update function signatures, improve initialization, and streamline data reading logic
This commit is contained in:
@@ -1,19 +1,25 @@
|
||||
#include "GPS.hpp"
|
||||
|
||||
TinyGPSPlus gps;
|
||||
HardwareSerial gpsSerial(1);
|
||||
|
||||
void GPS_Init()
|
||||
{
|
||||
gpsSerial.begin(9600, SERIAL_8N1, RX, TX);
|
||||
}
|
||||
|
||||
GPSData_t GPS_Read()
|
||||
{
|
||||
float lat, lon;
|
||||
if (gps.location.isValid())
|
||||
{
|
||||
while (gpsSerial.available() > 0) {
|
||||
gps.encode(gpsSerial.read());
|
||||
}
|
||||
|
||||
float lat = 0.0f, lon = 0.0f;
|
||||
|
||||
if (gps.location.isValid()) {
|
||||
lat = gps.location.lat();
|
||||
lon = gps.location.lng();
|
||||
}
|
||||
else
|
||||
{
|
||||
lat = 0.0f;
|
||||
lon = 0.0f;
|
||||
}
|
||||
|
||||
return {lat, lon};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,81 +1,20 @@
|
||||
#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;
|
||||
pinMode(MQ7_A0, INPUT);
|
||||
pinMode(MQ7_D0, INPUT);
|
||||
}
|
||||
|
||||
MQ7Data_t MQ7_Read()
|
||||
{
|
||||
return {lastPPM};
|
||||
int16_t raw = analogRead(MQ7_A0);
|
||||
float voltage = raw * (5.0 / 4095.0);
|
||||
float Rs = (5.0 - voltage) * RL / voltage;
|
||||
float ratio = Rs / R0;
|
||||
|
||||
float ppm = pow(10, (-1.5 * log10(ratio) + 0.8));
|
||||
bool d0 = digitalRead(MQ7_D0);
|
||||
|
||||
return {ppm, d0};
|
||||
}
|
||||
@@ -8,6 +8,7 @@ const char ELECTRIC_VEHICLES[] = "Solo vehiculos electricos/hibridos";
|
||||
const char* currentMessage = nullptr;
|
||||
|
||||
TaskTimer matrixTimer{0, 25};
|
||||
TaskTimer globalTimer{0, 30000};
|
||||
|
||||
extern HTTPClient httpClient;
|
||||
extern MD_Parola display;
|
||||
@@ -20,15 +21,17 @@ AirQualityStatus currentAirStatus = GOOD;
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial.println("Iniciando...");
|
||||
BME280_Init();
|
||||
Serial.println("Sensor BME280 inicializado");
|
||||
MAX7219_Init();
|
||||
Serial.println("Display inicializado");
|
||||
GPS_Init();
|
||||
Serial.println("Sensor GPS inicializado");
|
||||
MQ7_Init();
|
||||
Serial.println("Sensor MQ7 inicializado");
|
||||
MAX7219_Init();
|
||||
Serial.println("Display inicializado");
|
||||
|
||||
// inicializar el estado de la matriz
|
||||
writeMatrix(ALL_VEHICLES);
|
||||
}
|
||||
|
||||
@@ -36,7 +39,6 @@ void loop()
|
||||
{
|
||||
uint32_t now = millis();
|
||||
|
||||
// animación matriz de leds
|
||||
if (now - matrixTimer.lastRun >= matrixTimer.interval) {
|
||||
if (MAX7219_Animate()) {
|
||||
MAX7219_ResetAnimation();
|
||||
@@ -44,26 +46,23 @@ void loop()
|
||||
matrixTimer.lastRun = now;
|
||||
}
|
||||
|
||||
// MQ7 tarda mas y marca el ritmo
|
||||
if (MQ7_Update()) {
|
||||
mq7Data.co = MQ7_Read().co;
|
||||
if (now - globalTimer.lastRun >= globalTimer.interval)
|
||||
{
|
||||
readBME280();
|
||||
readGPS();
|
||||
processMQ7();
|
||||
readMQ7();
|
||||
#ifdef DEBUG
|
||||
printAllData();
|
||||
#endif
|
||||
// sendToServer();
|
||||
globalTimer.lastRun = now;
|
||||
}
|
||||
}
|
||||
|
||||
void processMQ7()
|
||||
void readMQ7()
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.print("CO: "); Serial.println(mq7Data.co);
|
||||
#endif
|
||||
const float CO_THRESHOLD = 100.0f;
|
||||
|
||||
const float CO_THRESHOLD = 10.0f;
|
||||
mq7Data = MQ7_Read();
|
||||
|
||||
AirQualityStatus newStatus = (mq7Data.co >= CO_THRESHOLD) ? BAD : GOOD;
|
||||
|
||||
@@ -113,6 +112,7 @@ void printAllData()
|
||||
Serial.print("Longitud: "); Serial.println(gpsData.lon);
|
||||
|
||||
Serial.print("CO: "); Serial.println(mq7Data.co);
|
||||
Serial.print("D0: "); Serial.println(mq7Data.threshold);
|
||||
}
|
||||
|
||||
uint32_t getChipID()
|
||||
|
||||
Reference in New Issue
Block a user