Refactor sensor data handling: update sensor read functions to return structured data and improve timer management in main loop
This commit is contained in:
5
hardware/.vscode/settings.json
vendored
5
hardware/.vscode/settings.json
vendored
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
BME280Data_t BME280_Read();
|
||||
@@ -3,5 +3,10 @@
|
||||
#define RX 4
|
||||
#define TX 5
|
||||
|
||||
void GPS_Init();
|
||||
void GPS_Read(float &lat, float &lon);
|
||||
struct GPSData_t
|
||||
{
|
||||
float lat;
|
||||
float lon;
|
||||
};
|
||||
|
||||
GPSData_t GPS_Read();
|
||||
@@ -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);
|
||||
@@ -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);
|
||||
MQ7Data_t MQ7_Read();
|
||||
@@ -13,8 +13,23 @@
|
||||
#include "MAX7219.hpp"
|
||||
#include "MQ7v2.hpp"
|
||||
|
||||
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();
|
||||
void prettyReadMQ7();
|
||||
void prettyReadBME280();
|
||||
void prettyReadGPS();
|
||||
void testMatrix();
|
||||
@@ -12,6 +12,7 @@
|
||||
platform = espressif32
|
||||
board = esp32dev
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
lib_deps =
|
||||
knolleary/PubSubClient@^2.8
|
||||
mikalhart/TinyGPSPlus@^1.0.2
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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};
|
||||
}
|
||||
|
||||
|
||||
@@ -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};
|
||||
}
|
||||
@@ -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};
|
||||
}
|
||||
@@ -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())
|
||||
{
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user