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",
|
"unordered_set": "cpp",
|
||||||
"vector": "cpp",
|
"vector": "cpp",
|
||||||
"string_view": "cpp",
|
"string_view": "cpp",
|
||||||
"initializer_list": "cpp"
|
"initializer_list": "cpp",
|
||||||
|
"system_error": "cpp"
|
||||||
},
|
},
|
||||||
"github.copilot.enable": {
|
"github.copilot.enable": {
|
||||||
"*": false,
|
"*": true,
|
||||||
"plaintext": false,
|
"plaintext": false,
|
||||||
"markdown": false,
|
"markdown": false,
|
||||||
"scminput": false,
|
"scminput": false,
|
||||||
|
|||||||
@@ -3,6 +3,12 @@
|
|||||||
|
|
||||||
#define I2C_BME280_ADDRESS 0x76
|
#define I2C_BME280_ADDRESS 0x76
|
||||||
|
|
||||||
|
struct BME280Data_t
|
||||||
|
{
|
||||||
|
float pressure;
|
||||||
|
float temperature;
|
||||||
|
float humidity;
|
||||||
|
};
|
||||||
|
|
||||||
void BME280_Init();
|
void BME280_Init();
|
||||||
bool BME280_DataReady();
|
BME280Data_t BME280_Read();
|
||||||
bool BME280_Read(float &pressure, float &temperature, float &humidity);
|
|
||||||
@@ -3,5 +3,10 @@
|
|||||||
#define RX 4
|
#define RX 4
|
||||||
#define TX 5
|
#define TX 5
|
||||||
|
|
||||||
void GPS_Init();
|
struct GPSData_t
|
||||||
void GPS_Read(float &lat, float &lon);
|
{
|
||||||
|
float lat;
|
||||||
|
float lon;
|
||||||
|
};
|
||||||
|
|
||||||
|
GPSData_t GPS_Read();
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
void MAX7219_Init();
|
void MAX7219_Init();
|
||||||
void MAX7219_DisplayText(const char *text, textPosition_t align, uint16_t speed, uint16_t pause);
|
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_ResetAnimation();
|
||||||
void MAX7219_ClearDisplay();
|
void MAX7219_ClearDisplay();
|
||||||
void MAX7219_SetBrightness(uint8_t brightness);
|
void MAX7219_SetBrightness(uint8_t brightness);
|
||||||
@@ -6,5 +6,10 @@
|
|||||||
#define RL 10000.0 // 10kΩ
|
#define RL 10000.0 // 10kΩ
|
||||||
#define RO 10000.0 // Resistencia del aire limpio
|
#define RO 10000.0 // Resistencia del aire limpio
|
||||||
|
|
||||||
|
struct MQ7Data_t
|
||||||
|
{
|
||||||
|
float co;
|
||||||
|
};
|
||||||
|
|
||||||
void MQ7_Init();
|
void MQ7_Init();
|
||||||
void MQ7_Read(float &sensorValue);
|
MQ7Data_t MQ7_Read();
|
||||||
@@ -13,8 +13,23 @@
|
|||||||
#include "MAX7219.hpp"
|
#include "MAX7219.hpp"
|
||||||
#include "MQ7v2.hpp"
|
#include "MQ7v2.hpp"
|
||||||
|
|
||||||
uint32_t getChipID();
|
struct TaskTimer {
|
||||||
void prettyReadMQ7();
|
uint32_t lastRun = 0;
|
||||||
void prettyReadBME280();
|
uint32_t interval = 1000;
|
||||||
void prettyReadGPS();
|
|
||||||
void testMatrix();
|
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();
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
platform = espressif32
|
platform = espressif32
|
||||||
board = esp32dev
|
board = esp32dev
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
monitor_speed = 115200
|
||||||
lib_deps =
|
lib_deps =
|
||||||
knolleary/PubSubClient@^2.8
|
knolleary/PubSubClient@^2.8
|
||||||
mikalhart/TinyGPSPlus@^1.0.2
|
mikalhart/TinyGPSPlus@^1.0.2
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ void MAX7219_Init()
|
|||||||
display.begin();
|
display.begin();
|
||||||
display.setIntensity(1); // 0-15
|
display.setIntensity(1); // 0-15
|
||||||
display.displayClear();
|
display.displayClear();
|
||||||
|
display.displaySuspend(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MAX7219_DisplayText(const char *text, textPosition_t align, uint16_t speed, uint16_t pause)
|
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);
|
display.displayText(text, align, speed, pause, PA_SCROLL_LEFT, PA_SCROLL_LEFT);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MAX7219_StartAnimation()
|
bool MAX7219_Animate()
|
||||||
{
|
{
|
||||||
return display.displayAnimate();
|
return display.displayAnimate();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,12 +23,12 @@ void BME280_Init()
|
|||||||
while (!bme.begin());
|
while (!bme.begin());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BME280_Read(float &pressure, float &temperature, float &humidity)
|
BME280Data_t BME280_Read()
|
||||||
{
|
{
|
||||||
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
|
float p, t, h;
|
||||||
BME280::PresUnit presUnit(BME280::PresUnit_Pa);
|
BME280::TempUnit tUnit(BME280::TempUnit_Celsius);
|
||||||
bme.read(pressure, temperature, humidity, tempUnit, presUnit);
|
BME280::PresUnit pUnit(BME280::PresUnit_Pa);
|
||||||
|
bme.read(p, t, h, tUnit, pUnit);
|
||||||
return (temperature != 0.0f && pressure != 0.0f);
|
return {p, t, h};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,13 +2,9 @@
|
|||||||
|
|
||||||
TinyGPSPlus gps;
|
TinyGPSPlus gps;
|
||||||
|
|
||||||
void GPS_Init()
|
GPSData_t GPS_Read()
|
||||||
{
|
|
||||||
Serial.begin(9600);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GPS_Read(float &lat, float &lon)
|
|
||||||
{
|
{
|
||||||
|
float lat, lon;
|
||||||
if (gps.location.isValid())
|
if (gps.location.isValid())
|
||||||
{
|
{
|
||||||
lat = gps.location.lat();
|
lat = gps.location.lat();
|
||||||
@@ -19,4 +15,5 @@ void GPS_Read(float &lat, float &lon)
|
|||||||
lat = 0.0f;
|
lat = 0.0f;
|
||||||
lon = 0.0f;
|
lon = 0.0f;
|
||||||
}
|
}
|
||||||
|
return {lat, lon};
|
||||||
}
|
}
|
||||||
@@ -5,19 +5,18 @@ void MQ7_Init()
|
|||||||
pinMode(MQ7_PIN, INPUT);
|
pinMode(MQ7_PIN, INPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MQ7_Read(float &sensorValue)
|
MQ7Data_t MQ7_Read()
|
||||||
{
|
{
|
||||||
int adcValue = analogRead(MQ7_PIN);
|
int adcValue = analogRead(MQ7_PIN);
|
||||||
float voltage = (adcValue / ADC_RES) * VCC;
|
float voltage = (adcValue / ADC_RES) * VCC;
|
||||||
|
|
||||||
if (voltage <= 0.1) {
|
if (voltage <= 0.1) {
|
||||||
sensorValue = 0;
|
return {0.0};
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float rs = (VCC - voltage) * RL / voltage;
|
float rs = (VCC - voltage) * RL / voltage;
|
||||||
float ratio = rs / RO;
|
float ratio = rs / RO;
|
||||||
float ppm = pow(10, (-1.5 * log10(ratio) + 0.8));
|
float ppm = pow(10, (-1.5 * log10(ratio) + 0.8));
|
||||||
|
|
||||||
sensorValue = ppm;
|
return {ppm};
|
||||||
}
|
}
|
||||||
@@ -1,18 +1,28 @@
|
|||||||
#include "main.hpp"
|
#include "main.hpp"
|
||||||
|
|
||||||
const uint32_t deviceId = getChipID();
|
#define DEBUG
|
||||||
|
|
||||||
extern HTTPClient httpClient; // HTTP client object
|
const uint32_t DEVICE_ID = getChipID();
|
||||||
String response; // HTTP Response
|
const char ALL_VEHICLES[] = "Todo tipo de vehiculos";
|
||||||
float sensorVolt, sensorValue; // MQ7 vars
|
const char ELECTRIC_VEHICLES[] = "Solo vehiculos electricos/hibridos";
|
||||||
float temperature, pressure, humidity; // BME280 vars
|
|
||||||
float lon, lat; // GPS vars
|
TaskTimer bmeTimer{0, 1000};
|
||||||
extern MD_Parola display; // Display object
|
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()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(9600);
|
Serial.begin(115200);
|
||||||
|
|
||||||
Serial.println("Iniciando...");
|
Serial.println("Iniciando...");
|
||||||
BME280_Init();
|
BME280_Init();
|
||||||
Serial.println("Sensor BME280 inicializado");
|
Serial.println("Sensor BME280 inicializado");
|
||||||
@@ -21,65 +31,103 @@ void setup()
|
|||||||
MQ7_Init();
|
MQ7_Init();
|
||||||
Serial.println("Sensor MQ7 inicializado");
|
Serial.println("Sensor MQ7 inicializado");
|
||||||
|
|
||||||
/*
|
writeMatrix(ALL_VEHICLES);
|
||||||
GPS_Init();
|
|
||||||
Serial.println("GPS inicializado");
|
|
||||||
*/
|
|
||||||
|
|
||||||
prettyReadBME280();
|
|
||||||
testMatrix();
|
|
||||||
prettyReadMQ7();
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
if(MAX7219_StartAnimation())
|
uint32_t now = millis();
|
||||||
{
|
|
||||||
MAX7219_ResetAnimation();
|
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...");
|
bme280Data = BME280_Read();
|
||||||
MQ7_Read(sensorValue);
|
#ifdef DEBUG
|
||||||
Serial.print("\t - Valor sensor: "); Serial.print(sensorValue); Serial.print("\r\n");
|
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...");
|
gpsData = GPS_Read();
|
||||||
BME280_Read(pressure, temperature, humidity);
|
#ifdef DEBUG
|
||||||
Serial.print("\t - Presión: "); Serial.print(pressure/100); Serial.print("hPa\r\n");
|
Serial.print("Latitud: "); Serial.println(gpsData.lat);
|
||||||
Serial.print("\t - Temperatura: "); Serial.print(temperature); Serial.print("°C\r\n");
|
Serial.print("Longitud: "); Serial.println(gpsData.lon);
|
||||||
Serial.print("\t - Humedad: "); Serial.print(humidity); Serial.print("%\r\n");
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void prettyReadGPS()
|
void writeMatrix(const char* message)
|
||||||
{
|
|
||||||
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()
|
|
||||||
{
|
{
|
||||||
|
#ifdef DEBUG
|
||||||
Serial.println("Escribiendo en el display...");
|
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 getChipID()
|
||||||
{
|
{
|
||||||
uint32_t chipId;
|
uint32_t chipId = 0;
|
||||||
for (int i = 0; i < 17; i = i + 8)
|
for (int i = 0; i < 17; i += 8)
|
||||||
{
|
{
|
||||||
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
|
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
|
||||||
}
|
}
|
||||||
|
#ifdef DEBUG
|
||||||
|
Serial.print("Chip ID: "); Serial.println(chipId, HEX);
|
||||||
|
#endif
|
||||||
return chipId;
|
return chipId;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user