added some improved classes from my custom backend and introduced DAOs
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <HTTPClient.h>
|
||||
#include "BME280.hpp"
|
||||
#include "MQ7v2.hpp"
|
||||
#include "GPS.hpp"
|
||||
|
||||
String serializeSensorValue(
|
||||
int sensorId,
|
||||
@@ -12,15 +15,13 @@ String serializeSensorValue(
|
||||
const BME280Data_t &bme,
|
||||
const MQ7Data_t &mq7,
|
||||
const GPSData_t &gps,
|
||||
long timestamp
|
||||
);
|
||||
long timestamp);
|
||||
|
||||
String serializeActuatorStatus(
|
||||
int actuatorId,
|
||||
const String &deviceId,
|
||||
int status,
|
||||
long timestamp
|
||||
);
|
||||
long timestamp);
|
||||
|
||||
void deserializeSensorValue(HTTPClient &http, int httpResponseCode);
|
||||
void deserializeActuatorStatus(HTTPClient &http, int httpResponseCode);
|
||||
|
||||
@@ -1,51 +1,51 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
#define SERVER_IP "https://contaminus.miarma.net/api/v1/"
|
||||
#define REST_PORT 443
|
||||
#define MQTT_PORT 1883
|
||||
|
||||
#define MQ7_ID 1
|
||||
#define BME280_ID 2
|
||||
#define GPS_ID 3
|
||||
#define MAX7219_ID 1
|
||||
|
||||
#define DEBUG
|
||||
|
||||
#include "JsonTools.hpp"
|
||||
#include "RestClient.hpp"
|
||||
#include "WifiConnection.hpp"
|
||||
#include "MqttClient.hpp"
|
||||
#include "BME280.hpp"
|
||||
#include "GPS.hpp"
|
||||
#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) {}
|
||||
};
|
||||
|
||||
struct SensorInfo
|
||||
{
|
||||
int id;
|
||||
String type;
|
||||
};
|
||||
|
||||
enum AirQualityStatus
|
||||
{
|
||||
GOOD,
|
||||
BAD
|
||||
};
|
||||
|
||||
void readMQ7();
|
||||
void readBME280();
|
||||
void readGPS();
|
||||
void writeMatrix(const char* message);
|
||||
void printAllData();
|
||||
#include <Arduino.h>
|
||||
|
||||
#define SERVER_IP "https://contaminus.miarma.net/api/v1/"
|
||||
#define REST_PORT 443
|
||||
#define MQTT_PORT 1883
|
||||
|
||||
#define MQ7_ID 1
|
||||
#define BME280_ID 2
|
||||
#define GPS_ID 3
|
||||
#define MAX7219_ID 1
|
||||
|
||||
#define DEBUG
|
||||
|
||||
#include "JsonTools.hpp"
|
||||
#include "RestClient.hpp"
|
||||
#include "WifiConnection.hpp"
|
||||
#include "MqttClient.hpp"
|
||||
#include "BME280.hpp"
|
||||
#include "GPS.hpp"
|
||||
#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) {}
|
||||
};
|
||||
|
||||
struct SensorInfo
|
||||
{
|
||||
int id;
|
||||
String type;
|
||||
};
|
||||
|
||||
enum AirQualityStatus
|
||||
{
|
||||
GOOD,
|
||||
BAD
|
||||
};
|
||||
|
||||
void readMQ7();
|
||||
void readBME280();
|
||||
void readGPS();
|
||||
void writeMatrix(const char *message);
|
||||
void printAllData();
|
||||
uint32_t getChipID();
|
||||
@@ -1,159 +1,179 @@
|
||||
#include "JsonTools.hpp"
|
||||
|
||||
String serializeSensorValue(
|
||||
int sensorId,
|
||||
const String &deviceId,
|
||||
const String &sensorType,
|
||||
const String &unit,
|
||||
int sensorStatus,
|
||||
const BME280Data_t &bme,
|
||||
const MQ7Data_t &mq7,
|
||||
const GPSData_t &gps,
|
||||
long timestamp
|
||||
) {
|
||||
DynamicJsonDocument doc(1024);
|
||||
|
||||
doc["sensorId"] = sensorId;
|
||||
doc["deviceId"] = deviceId;
|
||||
doc["sensorType"] = sensorType;
|
||||
doc["unit"] = unit;
|
||||
doc["sensorStatus"] = sensorStatus;
|
||||
doc["temperature"] = bme.temperature;
|
||||
doc["humidity"] = bme.humidity;
|
||||
doc["pressure"] = bme.pressure;
|
||||
doc["carbonMonoxide"] = mq7.co;
|
||||
doc["lat"] = gps.lat;
|
||||
doc["lon"] = gps.lon;
|
||||
doc["timestamp"] = timestamp;
|
||||
|
||||
String output;
|
||||
serializeJson(doc, output);
|
||||
Serial.println(output);
|
||||
return output;
|
||||
}
|
||||
|
||||
String serializeActuatorStatus(const int actuatorId, const String &deviceId, const int status, const long timestamp) {
|
||||
DynamicJsonDocument doc(512);
|
||||
|
||||
doc["actuatorId"] = actuatorId;
|
||||
doc["deviceId"] = deviceId;
|
||||
doc["status"] = status;
|
||||
doc["timestamp"] = timestamp;
|
||||
|
||||
String output;
|
||||
serializeJson(doc, output);
|
||||
Serial.println(output);
|
||||
return output;
|
||||
}
|
||||
|
||||
String serializeDevice(const String &deviceId, int groupId, const String &deviceName) {
|
||||
DynamicJsonDocument doc(512);
|
||||
|
||||
doc["deviceId"] = deviceId;
|
||||
doc["groupId"] = groupId;
|
||||
doc["deviceName"] = deviceName;
|
||||
|
||||
String output;
|
||||
serializeJson(doc, output);
|
||||
Serial.println(output);
|
||||
return output;
|
||||
}
|
||||
|
||||
void deserializeSensorValue(HTTPClient &http, int httpResponseCode) {
|
||||
if (httpResponseCode > 0) {
|
||||
Serial.print("HTTP Response code: ");
|
||||
Serial.println(httpResponseCode);
|
||||
String responseJson = http.getString();
|
||||
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
|
||||
DeserializationError error = deserializeJson(doc, responseJson);
|
||||
|
||||
if (error) {
|
||||
Serial.print(F("deserializeJson() failed: "));
|
||||
Serial.println(error.f_str());
|
||||
return;
|
||||
}
|
||||
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
for (JsonObject sensor : array) {
|
||||
int sensorId = sensor["sensorId"];
|
||||
String deviceId = sensor["deviceId"];
|
||||
String sensorType = sensor["sensorType"];
|
||||
String unit = sensor["unit"];
|
||||
int sensorStatus = sensor["sensorStatus"];
|
||||
float temperature = sensor["temperature"];
|
||||
float humidity = sensor["humidity"];
|
||||
float carbonMonoxide = sensor["carbonMonoxide"];
|
||||
float lat = sensor["lat"];
|
||||
float lon = sensor["lon"];
|
||||
long timestamp = sensor["timestamp"];
|
||||
|
||||
Serial.println("Sensor deserialized:");
|
||||
Serial.printf(" ID: %d\n Device: %s\n Type: %s\n Unit: %s\n Status: %d\n Temp: %.2f\n Hum: %.2f\n CO: %.2f\n Lat: %.6f\n Lon: %.6f\n Time: %ld\n\n",
|
||||
sensorId, deviceId.c_str(), sensorType.c_str(), unit.c_str(), sensorStatus,
|
||||
temperature, humidity, carbonMonoxide, lat, lon, timestamp);
|
||||
}
|
||||
} else {
|
||||
Serial.print("Error code: ");
|
||||
Serial.println(httpResponseCode);
|
||||
}
|
||||
}
|
||||
|
||||
void deserializeActuatorStatus(HTTPClient &http, int httpResponseCode) {
|
||||
if (httpResponseCode > 0) {
|
||||
Serial.print("HTTP Response code: ");
|
||||
Serial.println(httpResponseCode);
|
||||
String responseJson = http.getString();
|
||||
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
|
||||
DeserializationError error = deserializeJson(doc, responseJson);
|
||||
|
||||
if (error) {
|
||||
Serial.print(F("deserializeJson() failed: "));
|
||||
Serial.println(error.f_str());
|
||||
return;
|
||||
}
|
||||
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
for (JsonObject actuator : array) {
|
||||
int actuatorId = actuator["actuatorId"];
|
||||
String deviceId = actuator["deviceId"];
|
||||
int status = actuator["status"];
|
||||
long timestamp = actuator["timestamp"];
|
||||
|
||||
Serial.println("Actuator deserialized:");
|
||||
Serial.printf(" ID: %d\n Device: %s\n Status: %d\n Time: %ld\n\n",
|
||||
actuatorId, deviceId.c_str(), status, timestamp);
|
||||
}
|
||||
} else {
|
||||
Serial.print("Error code: ");
|
||||
Serial.println(httpResponseCode);
|
||||
}
|
||||
}
|
||||
|
||||
void deserializeDevice(HTTPClient &http, int httpResponseCode) {
|
||||
if (httpResponseCode > 0) {
|
||||
Serial.print("HTTP Response code: ");
|
||||
Serial.println(httpResponseCode);
|
||||
String responseJson = http.getString();
|
||||
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
|
||||
DeserializationError error = deserializeJson(doc, responseJson);
|
||||
|
||||
if (error) {
|
||||
Serial.print(F("deserializeJson() failed: "));
|
||||
Serial.println(error.f_str());
|
||||
return;
|
||||
}
|
||||
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
for (JsonObject device : array) {
|
||||
String deviceId = device["deviceId"];
|
||||
int groupId = device["groupId"];
|
||||
String deviceName = device["deviceName"];
|
||||
|
||||
Serial.println("Device deserialized:");
|
||||
Serial.printf(" ID: %s\n Group: %d\n Name: %s\n\n", deviceId.c_str(), groupId, deviceName.c_str());
|
||||
}
|
||||
} else {
|
||||
Serial.print("Error code: ");
|
||||
Serial.println(httpResponseCode);
|
||||
}
|
||||
#include "JsonTools.hpp"
|
||||
|
||||
String serializeSensorValue(
|
||||
int sensorId,
|
||||
const String &deviceId,
|
||||
const String &sensorType,
|
||||
const String &unit,
|
||||
int sensorStatus,
|
||||
const BME280Data_t &bme,
|
||||
const MQ7Data_t &mq7,
|
||||
const GPSData_t &gps,
|
||||
long timestamp)
|
||||
{
|
||||
DynamicJsonDocument doc(1024);
|
||||
|
||||
doc["sensorId"] = sensorId;
|
||||
doc["deviceId"] = deviceId;
|
||||
doc["sensorType"] = sensorType;
|
||||
doc["unit"] = unit;
|
||||
doc["sensorStatus"] = sensorStatus;
|
||||
doc["temperature"] = bme.temperature;
|
||||
doc["humidity"] = bme.humidity;
|
||||
doc["pressure"] = bme.pressure;
|
||||
doc["carbonMonoxide"] = mq7.co;
|
||||
doc["lat"] = gps.lat;
|
||||
doc["lon"] = gps.lon;
|
||||
doc["timestamp"] = timestamp;
|
||||
|
||||
String output;
|
||||
serializeJson(doc, output);
|
||||
Serial.println(output);
|
||||
return output;
|
||||
}
|
||||
|
||||
String serializeActuatorStatus(const int actuatorId, const String &deviceId, const int status, const long timestamp)
|
||||
{
|
||||
DynamicJsonDocument doc(512);
|
||||
|
||||
doc["actuatorId"] = actuatorId;
|
||||
doc["deviceId"] = deviceId;
|
||||
doc["status"] = status;
|
||||
doc["timestamp"] = timestamp;
|
||||
|
||||
String output;
|
||||
serializeJson(doc, output);
|
||||
Serial.println(output);
|
||||
return output;
|
||||
}
|
||||
|
||||
String serializeDevice(const String &deviceId, int groupId, const String &deviceName)
|
||||
{
|
||||
DynamicJsonDocument doc(512);
|
||||
|
||||
doc["deviceId"] = deviceId;
|
||||
doc["groupId"] = groupId;
|
||||
doc["deviceName"] = deviceName;
|
||||
|
||||
String output;
|
||||
serializeJson(doc, output);
|
||||
Serial.println(output);
|
||||
return output;
|
||||
}
|
||||
|
||||
void deserializeSensorValue(HTTPClient &http, int httpResponseCode)
|
||||
{
|
||||
if (httpResponseCode > 0)
|
||||
{
|
||||
Serial.print("HTTP Response code: ");
|
||||
Serial.println(httpResponseCode);
|
||||
String responseJson = http.getString();
|
||||
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
|
||||
DeserializationError error = deserializeJson(doc, responseJson);
|
||||
|
||||
if (error)
|
||||
{
|
||||
Serial.print(F("deserializeJson() failed: "));
|
||||
Serial.println(error.f_str());
|
||||
return;
|
||||
}
|
||||
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
for (JsonObject sensor : array)
|
||||
{
|
||||
int sensorId = sensor["sensorId"];
|
||||
String deviceId = sensor["deviceId"];
|
||||
String sensorType = sensor["sensorType"];
|
||||
String unit = sensor["unit"];
|
||||
int sensorStatus = sensor["sensorStatus"];
|
||||
float temperature = sensor["temperature"];
|
||||
float humidity = sensor["humidity"];
|
||||
float carbonMonoxide = sensor["carbonMonoxide"];
|
||||
float lat = sensor["lat"];
|
||||
float lon = sensor["lon"];
|
||||
long timestamp = sensor["timestamp"];
|
||||
|
||||
Serial.println("Sensor deserialized:");
|
||||
Serial.printf(" ID: %d\n Device: %s\n Type: %s\n Unit: %s\n Status: %d\n Temp: %.2f\n Hum: %.2f\n CO: %.2f\n Lat: %.6f\n Lon: %.6f\n Time: %ld\n\n",
|
||||
sensorId, deviceId.c_str(), sensorType.c_str(), unit.c_str(), sensorStatus,
|
||||
temperature, humidity, carbonMonoxide, lat, lon, timestamp);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("Error code: ");
|
||||
Serial.println(httpResponseCode);
|
||||
}
|
||||
}
|
||||
|
||||
void deserializeActuatorStatus(HTTPClient &http, int httpResponseCode)
|
||||
{
|
||||
if (httpResponseCode > 0)
|
||||
{
|
||||
Serial.print("HTTP Response code: ");
|
||||
Serial.println(httpResponseCode);
|
||||
String responseJson = http.getString();
|
||||
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
|
||||
DeserializationError error = deserializeJson(doc, responseJson);
|
||||
|
||||
if (error)
|
||||
{
|
||||
Serial.print(F("deserializeJson() failed: "));
|
||||
Serial.println(error.f_str());
|
||||
return;
|
||||
}
|
||||
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
for (JsonObject actuator : array)
|
||||
{
|
||||
int actuatorId = actuator["actuatorId"];
|
||||
String deviceId = actuator["deviceId"];
|
||||
int status = actuator["status"];
|
||||
long timestamp = actuator["timestamp"];
|
||||
|
||||
Serial.println("Actuator deserialized:");
|
||||
Serial.printf(" ID: %d\n Device: %s\n Status: %d\n Time: %ld\n\n",
|
||||
actuatorId, deviceId.c_str(), status, timestamp);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("Error code: ");
|
||||
Serial.println(httpResponseCode);
|
||||
}
|
||||
}
|
||||
|
||||
void deserializeDevice(HTTPClient &http, int httpResponseCode)
|
||||
{
|
||||
if (httpResponseCode > 0)
|
||||
{
|
||||
Serial.print("HTTP Response code: ");
|
||||
Serial.println(httpResponseCode);
|
||||
String responseJson = http.getString();
|
||||
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
|
||||
DeserializationError error = deserializeJson(doc, responseJson);
|
||||
|
||||
if (error)
|
||||
{
|
||||
Serial.print(F("deserializeJson() failed: "));
|
||||
Serial.println(error.f_str());
|
||||
return;
|
||||
}
|
||||
|
||||
JsonArray array = doc.as<JsonArray>();
|
||||
for (JsonObject device : array)
|
||||
{
|
||||
String deviceId = device["deviceId"];
|
||||
int groupId = device["groupId"];
|
||||
String deviceName = device["deviceName"];
|
||||
|
||||
Serial.println("Device deserialized:");
|
||||
Serial.printf(" ID: %s\n Group: %d\n Name: %s\n\n", deviceId.c_str(), groupId, deviceName.c_str());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("Error code: ");
|
||||
Serial.println(httpResponseCode);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,34 @@
|
||||
#include "RestClient.hpp"
|
||||
|
||||
HTTPClient httpClient; // HTTP client object
|
||||
|
||||
void getRequest(const String url, String &response)
|
||||
{
|
||||
httpClient.begin(url);
|
||||
int httpCode = httpClient.GET();
|
||||
if (httpCode > 0) {
|
||||
response = httpClient.getString();
|
||||
} else {
|
||||
response = "Error: " + String(httpCode);
|
||||
}
|
||||
httpClient.end();
|
||||
}
|
||||
|
||||
void postRequest(const String url, String &payload, String &response)
|
||||
{
|
||||
httpClient.begin(url);
|
||||
httpClient.addHeader("Content-Type", "application/json");
|
||||
int httpCode = httpClient.POST(payload);
|
||||
if (httpCode > 0) {
|
||||
response = httpClient.getString();
|
||||
} else {
|
||||
response = "Error: " + String(httpCode);
|
||||
}
|
||||
httpClient.end();
|
||||
#include "RestClient.hpp"
|
||||
|
||||
HTTPClient httpClient; // HTTP client object
|
||||
|
||||
void getRequest(const String url, String &response)
|
||||
{
|
||||
httpClient.begin(url);
|
||||
int httpCode = httpClient.GET();
|
||||
if (httpCode > 0)
|
||||
{
|
||||
response = httpClient.getString();
|
||||
}
|
||||
else
|
||||
{
|
||||
response = "Error: " + String(httpCode);
|
||||
}
|
||||
httpClient.end();
|
||||
}
|
||||
|
||||
void postRequest(const String url, String &payload, String &response)
|
||||
{
|
||||
httpClient.begin(url);
|
||||
httpClient.addHeader("Content-Type", "application/json");
|
||||
int httpCode = httpClient.POST(payload);
|
||||
if (httpCode > 0)
|
||||
{
|
||||
response = httpClient.getString();
|
||||
}
|
||||
else
|
||||
{
|
||||
response = "Error: " + String(httpCode);
|
||||
}
|
||||
httpClient.end();
|
||||
}
|
||||
@@ -1,61 +1,62 @@
|
||||
#include "MqttClient.hpp"
|
||||
|
||||
// MQTT configuration
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
void OnMqttReceived(char *topic, byte *payload, unsigned int length)
|
||||
{
|
||||
Serial.print("Received on ");
|
||||
Serial.print(topic);
|
||||
Serial.print(": ");
|
||||
|
||||
String content = "";
|
||||
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
content.concat((char)payload[i]);
|
||||
}
|
||||
|
||||
Serial.print(content);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void InitMqtt(const char * MQTTServerAddress, uint16_t MQTTServerPort)
|
||||
{
|
||||
client.setServer(MQTTServerAddress, MQTTServerPort);
|
||||
client.setCallback(OnMqttReceived);
|
||||
}
|
||||
|
||||
// conecta o reconecta al MQTT
|
||||
// consigue conectar -> suscribe a topic y publica un mensaje
|
||||
// no -> espera 5 segundos
|
||||
void ConnectMqtt(const char * MQTTClientName)
|
||||
{
|
||||
Serial.print("Starting MQTT connection...");
|
||||
if (client.connect(MQTTClientName))
|
||||
{
|
||||
client.subscribe("hello/world");
|
||||
client.publish("hello/world", "connected");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("Failed MQTT connection, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
|
||||
// gestiona la comunicación MQTT
|
||||
// comprueba que el cliente está conectado
|
||||
// no -> intenta reconectar
|
||||
// si -> llama al MQTT loop
|
||||
void HandleMqtt(const char * MQTTClientName)
|
||||
{
|
||||
if (!client.connected())
|
||||
{
|
||||
ConnectMqtt(MQTTClientName);
|
||||
}
|
||||
client.loop();
|
||||
}
|
||||
#include "MqttClient.hpp"
|
||||
|
||||
// MQTT configuration
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
void OnMqttReceived(char *topic, byte *payload, unsigned int length)
|
||||
{
|
||||
Serial.print("Received on ");
|
||||
Serial.print(topic);
|
||||
Serial.print(": ");
|
||||
|
||||
String content = "";
|
||||
|
||||
for (size_t i = 0; i < length; i++)
|
||||
{
|
||||
content.concat((char)payload[i]);
|
||||
}
|
||||
|
||||
Serial.print(content);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void InitMqtt(const char *MQTTServerAddress, uint16_t MQTTServerPort)
|
||||
{
|
||||
client.setServer(MQTTServerAddress, MQTTServerPort);
|
||||
client.setCallback(OnMqttReceived);
|
||||
}
|
||||
|
||||
// conecta o reconecta al MQTT
|
||||
// consigue conectar -> suscribe a topic y publica un mensaje
|
||||
// no -> espera 5 segundos
|
||||
void ConnectMqtt(const char *MQTTClientName)
|
||||
{
|
||||
Serial.print("Starting MQTT connection...");
|
||||
if (client.connect(MQTTClientName))
|
||||
{
|
||||
client.subscribe("hello/world");
|
||||
client.publish("hello/world", "connected");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print("Failed MQTT connection, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" try again in 5 seconds");
|
||||
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
|
||||
// gestiona la comunicación MQTT
|
||||
// comprueba que el cliente está conectado
|
||||
// no -> intenta reconectar
|
||||
// si -> llama al MQTT loop
|
||||
void HandleMqtt(const char *MQTTClientName)
|
||||
{
|
||||
if (!client.connected())
|
||||
{
|
||||
ConnectMqtt(MQTTClientName);
|
||||
}
|
||||
client.loop();
|
||||
}
|
||||
|
||||
@@ -1,83 +1,86 @@
|
||||
#include <WifiConnection.hpp>
|
||||
|
||||
#define PIN_R 12
|
||||
#define PIN_G 13
|
||||
#define PIN_B 14
|
||||
|
||||
WiFiClient wifiClient;
|
||||
|
||||
void setColor(uint8_t r, uint8_t g, uint8_t b) {
|
||||
ledcWrite(0, r);
|
||||
ledcWrite(1, g);
|
||||
ledcWrite(2, b);
|
||||
}
|
||||
|
||||
void setupLED() {
|
||||
ledcAttachPin(PIN_R, 0);
|
||||
ledcAttachPin(PIN_G, 1);
|
||||
ledcAttachPin(PIN_B, 2);
|
||||
|
||||
ledcSetup(0, 5000, 8);
|
||||
ledcSetup(1, 5000, 8);
|
||||
ledcSetup(2, 5000, 8);
|
||||
}
|
||||
|
||||
// hue cycle
|
||||
void hueCycle(uint8_t pos) {
|
||||
uint8_t r = (uint8_t)(sin((pos + 0) * 0.024) * 127 + 128);
|
||||
uint8_t g = (uint8_t)(sin((pos + 85) * 0.024) * 127 + 128);
|
||||
uint8_t b = (uint8_t)(sin((pos + 170) * 0.024) * 127 + 128);
|
||||
setColor(r, g, b);
|
||||
}
|
||||
|
||||
int setupWifi()
|
||||
{
|
||||
setupLED();
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(SSID, PASSWORD);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.print("Conectando a la red WiFi: ");
|
||||
Serial.print(SSID);
|
||||
#endif
|
||||
|
||||
int hue = 0;
|
||||
uint32_t start = millis();
|
||||
const uint32_t timeout = 15000;
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED && millis() - start < timeout)
|
||||
{
|
||||
hueCycle(hue++);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.print(".");
|
||||
#endif
|
||||
|
||||
delay(30);
|
||||
}
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
{
|
||||
setColor(0, 255, 0);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.println("Conectado a la red WiFi");
|
||||
Serial.print("Dirección IP: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
setColor(255, 0, 0);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.println("No se pudo conectar a la red WiFi");
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
#include <WifiConnection.hpp>
|
||||
|
||||
#define PIN_R 12
|
||||
#define PIN_G 13
|
||||
#define PIN_B 14
|
||||
|
||||
WiFiClient wifiClient;
|
||||
|
||||
void setColor(uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
ledcWrite(0, r);
|
||||
ledcWrite(1, g);
|
||||
ledcWrite(2, b);
|
||||
}
|
||||
|
||||
void setupLED()
|
||||
{
|
||||
ledcSetup(0, 5000, 8);
|
||||
ledcAttachPin(PIN_R, 0);
|
||||
|
||||
ledcSetup(1, 5000, 8);
|
||||
ledcAttachPin(PIN_G, 1);
|
||||
|
||||
ledcSetup(2, 5000, 8);
|
||||
ledcAttachPin(PIN_B, 2);
|
||||
}
|
||||
|
||||
// hue cycle
|
||||
void hueCycle(uint8_t pos)
|
||||
{
|
||||
uint8_t r = (uint8_t)(sin((pos + 0) * 0.024) * 127 + 128);
|
||||
uint8_t g = (uint8_t)(sin((pos + 85) * 0.024) * 127 + 128);
|
||||
uint8_t b = (uint8_t)(sin((pos + 170) * 0.024) * 127 + 128);
|
||||
setColor(r, g, b);
|
||||
}
|
||||
|
||||
int setupWifi()
|
||||
{
|
||||
setupLED();
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(SSID, PASSWORD);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.print("Conectando a la red WiFi: ");
|
||||
Serial.print(SSID);
|
||||
#endif
|
||||
|
||||
int hue = 0;
|
||||
uint32_t start = millis();
|
||||
const uint32_t timeout = 15000;
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED && millis() - start < timeout)
|
||||
{
|
||||
hueCycle(hue++);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.print(".");
|
||||
#endif
|
||||
|
||||
delay(30);
|
||||
}
|
||||
|
||||
if (WiFi.status() == WL_CONNECTED)
|
||||
{
|
||||
setColor(0, 255, 0);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.println("Conectado a la red WiFi");
|
||||
Serial.print("Dirección IP: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
setColor(255, 0, 0);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.println("No se pudo conectar a la red WiFi");
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -1,34 +1,34 @@
|
||||
#include "BME280.hpp"
|
||||
|
||||
BME280I2C bme;
|
||||
|
||||
void BME280_Init()
|
||||
{
|
||||
Wire.setPins(21, 22);
|
||||
Wire.begin();
|
||||
|
||||
BME280I2C::Settings settings(
|
||||
BME280I2C::OSR::OSR_X1,
|
||||
BME280I2C::OSR::OSR_X1,
|
||||
BME280I2C::OSR::OSR_X1,
|
||||
BME280I2C::Mode::Mode_Forced, // modo forzado
|
||||
BME280I2C::StandbyTime::StandbyTime_1000ms,
|
||||
BME280I2C::Filter::Filter_16,
|
||||
BME280I2C::SpiEnable::SpiEnable_False,
|
||||
BME280I2C::I2CAddr::I2CAddr_0x76 // dirección I2C del BME280
|
||||
);
|
||||
|
||||
bme.setSettings(settings);
|
||||
|
||||
while (!bme.begin());
|
||||
}
|
||||
|
||||
BME280Data_t BME280_Read()
|
||||
{
|
||||
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};
|
||||
}
|
||||
|
||||
#include "BME280.hpp"
|
||||
|
||||
BME280I2C bme;
|
||||
|
||||
void BME280_Init()
|
||||
{
|
||||
Wire.setPins(21, 22);
|
||||
Wire.begin();
|
||||
|
||||
BME280I2C::Settings settings(
|
||||
BME280I2C::OSR::OSR_X1,
|
||||
BME280I2C::OSR::OSR_X1,
|
||||
BME280I2C::OSR::OSR_X1,
|
||||
BME280I2C::Mode::Mode_Forced, // modo forzado
|
||||
BME280I2C::StandbyTime::StandbyTime_1000ms,
|
||||
BME280I2C::Filter::Filter_16,
|
||||
BME280I2C::SpiEnable::SpiEnable_False,
|
||||
BME280I2C::I2CAddr::I2CAddr_0x76 // dirección I2C del BME280
|
||||
);
|
||||
|
||||
bme.setSettings(settings);
|
||||
|
||||
while (!bme.begin())
|
||||
;
|
||||
}
|
||||
|
||||
BME280Data_t BME280_Read()
|
||||
{
|
||||
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};
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
const uint32_t DEVICE_ID = getChipID();
|
||||
const char ALL_VEHICLES[] = "Todo tipo de vehiculos";
|
||||
const char ELECTRIC_VEHICLES[] = "Solo vehiculos electricos/hibridos";
|
||||
const char* currentMessage = nullptr;
|
||||
const char *currentMessage = nullptr;
|
||||
|
||||
TaskTimer matrixTimer{0, 25};
|
||||
TaskTimer globalTimer{0, 60000};
|
||||
@@ -29,7 +29,7 @@ void setup()
|
||||
GPS_Init();
|
||||
Serial.println("Sensor GPS inicializado");
|
||||
MQ7_Init();
|
||||
Serial.println("Sensor MQ7 inicializado");
|
||||
Serial.println("Sensor MQ7 inicializado");
|
||||
MAX7219_Init();
|
||||
Serial.println("Display inicializado");
|
||||
|
||||
@@ -40,8 +40,10 @@ void loop()
|
||||
{
|
||||
uint32_t now = millis();
|
||||
|
||||
if (now - matrixTimer.lastRun >= matrixTimer.interval) {
|
||||
if (MAX7219_Animate()) {
|
||||
if (now - matrixTimer.lastRun >= matrixTimer.interval)
|
||||
{
|
||||
if (MAX7219_Animate())
|
||||
{
|
||||
MAX7219_ResetAnimation();
|
||||
}
|
||||
matrixTimer.lastRun = now;
|
||||
@@ -52,10 +54,10 @@ void loop()
|
||||
readBME280();
|
||||
readGPS();
|
||||
readMQ7();
|
||||
|
||||
#ifdef DEBUG
|
||||
|
||||
#ifdef DEBUG
|
||||
printAllData();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
globalTimer.lastRun = now;
|
||||
}
|
||||
@@ -69,11 +71,15 @@ void readMQ7()
|
||||
|
||||
AirQualityStatus newStatus = (mq7Data.co >= CO_THRESHOLD) ? BAD : GOOD;
|
||||
|
||||
if (newStatus != currentAirStatus) {
|
||||
if (newStatus != currentAirStatus)
|
||||
{
|
||||
currentAirStatus = newStatus;
|
||||
if (currentAirStatus == BAD) {
|
||||
if (currentAirStatus == BAD)
|
||||
{
|
||||
writeMatrix(ELECTRIC_VEHICLES);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
writeMatrix(ALL_VEHICLES);
|
||||
}
|
||||
}
|
||||
@@ -89,15 +95,16 @@ void readGPS()
|
||||
gpsData = GPS_Read();
|
||||
}
|
||||
|
||||
void writeMatrix(const char* message)
|
||||
void writeMatrix(const char *message)
|
||||
{
|
||||
if (currentMessage == message) return;
|
||||
if (currentMessage == message)
|
||||
return;
|
||||
currentMessage = message;
|
||||
|
||||
#ifdef DEBUG
|
||||
#ifdef DEBUG
|
||||
Serial.println("Escribiendo en el display...");
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
MAX7219_DisplayText(message, PA_LEFT, 50, 0);
|
||||
}
|
||||
|
||||
@@ -105,17 +112,28 @@ void printAllData()
|
||||
{
|
||||
Serial.println("---------------------");
|
||||
|
||||
Serial.print("ID: "); Serial.println(DEVICE_ID, HEX);
|
||||
Serial.print("ID: ");
|
||||
Serial.println(DEVICE_ID, HEX);
|
||||
|
||||
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(" %");
|
||||
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(" %");
|
||||
|
||||
Serial.print("Latitud: "); Serial.println(gpsData.lat);
|
||||
Serial.print("Longitud: "); Serial.println(gpsData.lon);
|
||||
Serial.print("Latitud: ");
|
||||
Serial.println(gpsData.lat);
|
||||
Serial.print("Longitud: ");
|
||||
Serial.println(gpsData.lon);
|
||||
|
||||
Serial.print("CO: "); Serial.println(mq7Data.co);
|
||||
Serial.print("D0: "); Serial.println(mq7Data.threshold);
|
||||
Serial.print("CO: ");
|
||||
Serial.println(mq7Data.co);
|
||||
Serial.print("D0: ");
|
||||
Serial.println(mq7Data.threshold);
|
||||
}
|
||||
|
||||
uint32_t getChipID()
|
||||
@@ -125,8 +143,9 @@ uint32_t getChipID()
|
||||
{
|
||||
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
|
||||
}
|
||||
#ifdef DEBUG
|
||||
Serial.print("Chip ID: "); Serial.println(chipId, HEX);
|
||||
#endif
|
||||
#ifdef DEBUG
|
||||
Serial.print("Chip ID: ");
|
||||
Serial.println(chipId, HEX);
|
||||
#endif
|
||||
return chipId;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user