Implement MQTT connection and message publishing based on CO levels; refactor air quality status handling
This commit is contained in:
@@ -27,12 +27,6 @@ struct SensorInfo
|
||||
String type;
|
||||
};
|
||||
|
||||
enum AirQualityStatus
|
||||
{
|
||||
GOOD,
|
||||
BAD
|
||||
};
|
||||
|
||||
void readMQ7();
|
||||
void readBME280();
|
||||
void readGPS();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "MqttClient.hpp"
|
||||
|
||||
extern WiFiClient wifiClient;
|
||||
extern const char *currentMessage;
|
||||
|
||||
PubSubClient client(wifiClient);
|
||||
|
||||
@@ -19,9 +20,14 @@ void MQTT_OnReceived(char *topic, byte *payload, unsigned int length)
|
||||
content.concat((char)payload[i]);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.println(content);
|
||||
#endif
|
||||
if(content == "ECO")
|
||||
{
|
||||
currentMessage = "Solo vehiculos electricos/hibridos";
|
||||
}
|
||||
else
|
||||
{
|
||||
currentMessage = "Todo tipo de vehiculos";
|
||||
}
|
||||
}
|
||||
|
||||
void MQTT_Init(const char *MQTTServerAddress, uint16_t MQTTServerPort)
|
||||
@@ -38,7 +44,9 @@ void MQTT_Connect(const char *MQTTClientName)
|
||||
if (client.connect(MQTTClientName, USER, MQTT_PASSWORD))
|
||||
{
|
||||
String statusTopic = buildTopic(GROUP_ID, String(DEVICE_ID, HEX), "status");
|
||||
String matrixTopic = buildTopic(GROUP_ID, String(DEVICE_ID, HEX), "matrix");
|
||||
client.subscribe(statusTopic.c_str());
|
||||
client.subscribe(matrixTopic.c_str());
|
||||
client.publish(statusTopic.c_str(), "connected");
|
||||
}
|
||||
#ifdef DEBUG
|
||||
|
||||
@@ -28,6 +28,6 @@ GPSData_t GPS_Read()
|
||||
|
||||
GPSData_t GPS_Read_Fake()
|
||||
{
|
||||
float rnd = random(-0.005, 0.005);
|
||||
float rnd = random(-0.0005, 0.0005);
|
||||
return {37.358201f + rnd, -5.986640f + rnd};
|
||||
}
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
const uint32_t DEVICE_ID = getChipID();
|
||||
const int GROUP_ID = 1;
|
||||
const char ALL_VEHICLES[] = "Todo tipo de vehiculos";
|
||||
const char ELECTRIC_VEHICLES[] = "Solo vehiculos electricos/hibridos";
|
||||
const char *currentMessage = nullptr;
|
||||
const String id = "CUS-" + String(DEVICE_ID, HEX);
|
||||
|
||||
@@ -18,7 +16,6 @@ extern MD_Parola display;
|
||||
MQ7Data_t mq7Data;
|
||||
BME280Data_t bme280Data;
|
||||
GPSData_t gpsData;
|
||||
AirQualityStatus currentAirStatus = GOOD;
|
||||
|
||||
void setup()
|
||||
{
|
||||
@@ -38,7 +35,7 @@ void setup()
|
||||
MAX7219_Init();
|
||||
Serial.println("Display inicializado");
|
||||
|
||||
writeMatrix(ALL_VEHICLES);
|
||||
writeMatrix(currentMessage);
|
||||
}
|
||||
|
||||
void loop()
|
||||
@@ -79,23 +76,7 @@ void loop()
|
||||
void readMQ7()
|
||||
{
|
||||
const float CO_THRESHOLD = 100.0f;
|
||||
|
||||
mq7Data = MQ7_Read();
|
||||
|
||||
AirQualityStatus newStatus = (mq7Data.co >= CO_THRESHOLD) ? BAD : GOOD;
|
||||
|
||||
if (newStatus != currentAirStatus)
|
||||
{
|
||||
currentAirStatus = newStatus;
|
||||
if (currentAirStatus == BAD)
|
||||
{
|
||||
writeMatrix(ELECTRIC_VEHICLES);
|
||||
}
|
||||
else
|
||||
{
|
||||
writeMatrix(ALL_VEHICLES);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void readBME280()
|
||||
@@ -110,10 +91,6 @@ void readGPS()
|
||||
|
||||
void writeMatrix(const char *message)
|
||||
{
|
||||
if (currentMessage == message)
|
||||
return;
|
||||
currentMessage = message;
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.println("Escribiendo en el display...");
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user