Refactor GPS and MQ7 sensor implementations: update function signatures, improve initialization, and streamline data reading logic
This commit is contained in:
3
hardware/.vscode/settings.json
vendored
3
hardware/.vscode/settings.json
vendored
@@ -15,7 +15,8 @@
|
|||||||
"vector": "cpp",
|
"vector": "cpp",
|
||||||
"string_view": "cpp",
|
"string_view": "cpp",
|
||||||
"initializer_list": "cpp",
|
"initializer_list": "cpp",
|
||||||
"system_error": "cpp"
|
"system_error": "cpp",
|
||||||
|
"cmath": "cpp"
|
||||||
},
|
},
|
||||||
"github.copilot.enable": {
|
"github.copilot.enable": {
|
||||||
"*": true,
|
"*": true,
|
||||||
|
|||||||
@@ -9,4 +9,5 @@ struct GPSData_t
|
|||||||
float lon;
|
float lon;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void GPS_Init();
|
||||||
GPSData_t GPS_Read();
|
GPSData_t GPS_Read();
|
||||||
@@ -1,23 +1,15 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
#define MQ7_PIN 34
|
#define MQ7_A0 34
|
||||||
#define VCC 5.0
|
#define MQ7_D0 35
|
||||||
#define ADC_RES 1023.0
|
#define R0 20000
|
||||||
#define RL 10000.0 // 10kΩ
|
#define RL 10000
|
||||||
#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
|
struct MQ7Data_t
|
||||||
{
|
{
|
||||||
float co;
|
float co;
|
||||||
|
bool threshold;
|
||||||
};
|
};
|
||||||
|
|
||||||
void MQ7_Init();
|
void MQ7_Init();
|
||||||
bool MQ7_Update();
|
|
||||||
MQ7Data_t MQ7_Read();
|
MQ7Data_t MQ7_Read();
|
||||||
@@ -4,6 +4,11 @@
|
|||||||
#define REST_PORT 443
|
#define REST_PORT 443
|
||||||
#define MQTT_PORT 1883
|
#define MQTT_PORT 1883
|
||||||
|
|
||||||
|
#define MQ7_ID 1
|
||||||
|
#define BME280_ID 2
|
||||||
|
#define GPS_ID 3
|
||||||
|
#define MAX7219_ID 1
|
||||||
|
|
||||||
#include "JsonTools.hpp"
|
#include "JsonTools.hpp"
|
||||||
#include "RestClient.hpp"
|
#include "RestClient.hpp"
|
||||||
#include "WifiConnection.hpp"
|
#include "WifiConnection.hpp"
|
||||||
@@ -28,7 +33,7 @@ enum AirQualityStatus {
|
|||||||
BAD
|
BAD
|
||||||
};
|
};
|
||||||
|
|
||||||
void processMQ7();
|
void readMQ7();
|
||||||
void readBME280();
|
void readBME280();
|
||||||
void readGPS();
|
void readGPS();
|
||||||
void writeMatrix(const char* message);
|
void writeMatrix(const char* message);
|
||||||
|
|||||||
@@ -1,19 +1,25 @@
|
|||||||
#include "GPS.hpp"
|
#include "GPS.hpp"
|
||||||
|
|
||||||
TinyGPSPlus gps;
|
TinyGPSPlus gps;
|
||||||
|
HardwareSerial gpsSerial(1);
|
||||||
|
|
||||||
|
void GPS_Init()
|
||||||
|
{
|
||||||
|
gpsSerial.begin(9600, SERIAL_8N1, RX, TX);
|
||||||
|
}
|
||||||
|
|
||||||
GPSData_t GPS_Read()
|
GPSData_t GPS_Read()
|
||||||
{
|
{
|
||||||
float lat, lon;
|
while (gpsSerial.available() > 0) {
|
||||||
if (gps.location.isValid())
|
gps.encode(gpsSerial.read());
|
||||||
{
|
}
|
||||||
|
|
||||||
|
float lat = 0.0f, lon = 0.0f;
|
||||||
|
|
||||||
|
if (gps.location.isValid()) {
|
||||||
lat = gps.location.lat();
|
lat = gps.location.lat();
|
||||||
lon = gps.location.lng();
|
lon = gps.location.lng();
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
lat = 0.0f;
|
|
||||||
lon = 0.0f;
|
|
||||||
}
|
|
||||||
return {lat, lon};
|
return {lat, lon};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,81 +1,20 @@
|
|||||||
#include "MQ7v2.hpp"
|
#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()
|
void MQ7_Init()
|
||||||
{
|
{
|
||||||
pinMode(MQ7_PIN, INPUT);
|
pinMode(MQ7_A0, INPUT);
|
||||||
pinMode(RELAY_CONTROL_PIN, OUTPUT);
|
pinMode(MQ7_D0, INPUT);
|
||||||
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()
|
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;
|
const char* currentMessage = nullptr;
|
||||||
|
|
||||||
TaskTimer matrixTimer{0, 25};
|
TaskTimer matrixTimer{0, 25};
|
||||||
|
TaskTimer globalTimer{0, 30000};
|
||||||
|
|
||||||
extern HTTPClient httpClient;
|
extern HTTPClient httpClient;
|
||||||
extern MD_Parola display;
|
extern MD_Parola display;
|
||||||
@@ -20,15 +21,17 @@ AirQualityStatus currentAirStatus = GOOD;
|
|||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
Serial.println("Iniciando...");
|
Serial.println("Iniciando...");
|
||||||
BME280_Init();
|
BME280_Init();
|
||||||
Serial.println("Sensor BME280 inicializado");
|
Serial.println("Sensor BME280 inicializado");
|
||||||
MAX7219_Init();
|
GPS_Init();
|
||||||
Serial.println("Display inicializado");
|
Serial.println("Sensor GPS inicializado");
|
||||||
MQ7_Init();
|
MQ7_Init();
|
||||||
Serial.println("Sensor MQ7 inicializado");
|
Serial.println("Sensor MQ7 inicializado");
|
||||||
|
MAX7219_Init();
|
||||||
|
Serial.println("Display inicializado");
|
||||||
|
|
||||||
// inicializar el estado de la matriz
|
|
||||||
writeMatrix(ALL_VEHICLES);
|
writeMatrix(ALL_VEHICLES);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +39,6 @@ void loop()
|
|||||||
{
|
{
|
||||||
uint32_t now = millis();
|
uint32_t now = millis();
|
||||||
|
|
||||||
// animación matriz de leds
|
|
||||||
if (now - matrixTimer.lastRun >= matrixTimer.interval) {
|
if (now - matrixTimer.lastRun >= matrixTimer.interval) {
|
||||||
if (MAX7219_Animate()) {
|
if (MAX7219_Animate()) {
|
||||||
MAX7219_ResetAnimation();
|
MAX7219_ResetAnimation();
|
||||||
@@ -44,26 +46,23 @@ void loop()
|
|||||||
matrixTimer.lastRun = now;
|
matrixTimer.lastRun = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
// MQ7 tarda mas y marca el ritmo
|
if (now - globalTimer.lastRun >= globalTimer.interval)
|
||||||
if (MQ7_Update()) {
|
{
|
||||||
mq7Data.co = MQ7_Read().co;
|
|
||||||
readBME280();
|
readBME280();
|
||||||
readGPS();
|
readGPS();
|
||||||
processMQ7();
|
readMQ7();
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printAllData();
|
printAllData();
|
||||||
#endif
|
#endif
|
||||||
// sendToServer();
|
globalTimer.lastRun = now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void processMQ7()
|
void readMQ7()
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
const float CO_THRESHOLD = 100.0f;
|
||||||
Serial.print("CO: "); Serial.println(mq7Data.co);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
const float CO_THRESHOLD = 10.0f;
|
mq7Data = MQ7_Read();
|
||||||
|
|
||||||
AirQualityStatus newStatus = (mq7Data.co >= CO_THRESHOLD) ? BAD : GOOD;
|
AirQualityStatus newStatus = (mq7Data.co >= CO_THRESHOLD) ? BAD : GOOD;
|
||||||
|
|
||||||
@@ -113,6 +112,7 @@ void printAllData()
|
|||||||
Serial.print("Longitud: "); Serial.println(gpsData.lon);
|
Serial.print("Longitud: "); Serial.println(gpsData.lon);
|
||||||
|
|
||||||
Serial.print("CO: "); Serial.println(mq7Data.co);
|
Serial.print("CO: "); Serial.println(mq7Data.co);
|
||||||
|
Serial.print("D0: "); Serial.println(mq7Data.threshold);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t getChipID()
|
uint32_t getChipID()
|
||||||
|
|||||||
Reference in New Issue
Block a user