Implemented (partially) Voronoi algorithm for zone-dividing in Seville map. Also refactored some things in frontend. Modified hardware firmware for conditional compilation for both SENSOR and ACTUATOR type boards.
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#define SSID "iPhone de Álvaro"
|
||||
#define WIFI_PASSWORD "alvarito123"
|
||||
#define SSID "DIGIFIBRA-D2ys"
|
||||
#define WIFI_PASSWORD "4EEATsyTcZ"
|
||||
|
||||
int setupWifi();
|
||||
int WiFi_Init();
|
||||
@@ -10,7 +10,13 @@
|
||||
#define GPS_ID 1
|
||||
#define MAX7219_ID 1
|
||||
|
||||
#define ECO "Solo vehiculos electricos/hibridos"
|
||||
#define ALL "Todo tipo de vehiculos"
|
||||
|
||||
#define DEBUG
|
||||
|
||||
#define SENSOR 0
|
||||
#define ACTUATOR 1
|
||||
|
||||
extern const uint32_t DEVICE_ID;
|
||||
extern const int GROUP_ID;
|
||||
@@ -1,14 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include "globals.hpp"
|
||||
|
||||
#define DEVICE_ROLE SENSOR // se cambia entre SENSOR y ACTUATOR
|
||||
|
||||
#if DEVICE_ROLE == SENSOR
|
||||
#warning "Compilando firmware para SENSOR"
|
||||
#elif DEVICE_ROLE == ACTUATOR
|
||||
#warning "Compilando firmware para ACTUATOR"
|
||||
#else
|
||||
#warning "DEVICE_ROLE no definido correctamente"
|
||||
#endif
|
||||
|
||||
#include "JsonTools.hpp"
|
||||
#include "RestClient.hpp"
|
||||
#include "WifiConnection.hpp"
|
||||
#include "MqttClient.hpp"
|
||||
#if DEVICE_ROLE == SENSOR
|
||||
#include "BME280.hpp"
|
||||
#include "GPS.hpp"
|
||||
#include "MAX7219.hpp"
|
||||
#include "MQ7v2.hpp"
|
||||
#endif
|
||||
#if DEVICE_ROLE == ACTUATOR
|
||||
#include "MAX7219.hpp"
|
||||
#endif
|
||||
|
||||
struct TaskTimer
|
||||
{
|
||||
|
||||
@@ -20,14 +20,16 @@ void MQTT_OnReceived(char *topic, byte *payload, unsigned int length)
|
||||
content.concat((char)payload[i]);
|
||||
}
|
||||
|
||||
#if DEVICE_ROLE == ACTUATOR
|
||||
if(content == "ECO")
|
||||
{
|
||||
currentMessage = "Solo vehiculos electricos/hibridos";
|
||||
currentMessage = ECO;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentMessage = "Todo tipo de vehiculos";
|
||||
currentMessage = ALL;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void MQTT_Init(const char *MQTTServerAddress, uint16_t MQTTServerPort)
|
||||
|
||||
@@ -34,7 +34,7 @@ void hueCycle(uint8_t pos)
|
||||
setColor(r, g, b);
|
||||
}
|
||||
|
||||
int setupWifi()
|
||||
int WiFi_Init()
|
||||
{
|
||||
setupLED();
|
||||
|
||||
|
||||
@@ -1,47 +1,67 @@
|
||||
#include "main.hpp"
|
||||
|
||||
const uint32_t DEVICE_ID = getChipID();
|
||||
const String mqttId = "CUS-" + String(DEVICE_ID, HEX);
|
||||
const int GROUP_ID = 1;
|
||||
const char *currentMessage = nullptr;
|
||||
const String id = "CUS-" + String(DEVICE_ID, HEX);
|
||||
|
||||
TaskTimer matrixTimer{0, 25};
|
||||
TaskTimer globalTimer{0, 60000};
|
||||
TaskTimer mqttTimer{0, 5000};
|
||||
|
||||
#if DEVICE_ROLE == ACTUATOR
|
||||
TaskTimer matrixTimer{0, 25};
|
||||
const char *currentMessage = ALL;
|
||||
extern MD_Parola display;
|
||||
#endif
|
||||
|
||||
extern HTTPClient httpClient;
|
||||
String response;
|
||||
extern MD_Parola display;
|
||||
|
||||
#if DEVICE_ROLE == SENSOR
|
||||
MQ7Data_t mq7Data;
|
||||
BME280Data_t bme280Data;
|
||||
GPSData_t gpsData;
|
||||
#endif
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.println("Iniciando...");
|
||||
#endif
|
||||
|
||||
setupWifi();
|
||||
WiFi_Init();
|
||||
MQTT_Init(MQTT_URI, MQTT_PORT);
|
||||
|
||||
BME280_Init();
|
||||
Serial.println("Sensor BME280 inicializado");
|
||||
GPS_Init();
|
||||
Serial.println("Sensor GPS inicializado");
|
||||
MQ7_Init();
|
||||
Serial.println("Sensor MQ7 inicializado");
|
||||
MAX7219_Init();
|
||||
Serial.println("Display inicializado");
|
||||
try
|
||||
{
|
||||
|
||||
#if DEVICE_ROLE == SENSOR
|
||||
BME280_Init();
|
||||
Serial.println("Sensor BME280 inicializado");
|
||||
GPS_Init();
|
||||
Serial.println("Sensor GPS inicializado");
|
||||
MQ7_Init();
|
||||
Serial.println("Sensor MQ7 inicializado");
|
||||
#endif
|
||||
|
||||
writeMatrix(currentMessage);
|
||||
#if DEVICE_ROLE == ACTUATOR
|
||||
MAX7219_Init();
|
||||
Serial.println("Display inicializado");
|
||||
writeMatrix(currentMessage);
|
||||
#endif
|
||||
}
|
||||
catch (const char *e)
|
||||
{
|
||||
Serial.println(e);
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
uint32_t now = millis();
|
||||
|
||||
#if DEVICE_ROLE == ACTUATOR
|
||||
if (now - matrixTimer.lastRun >= matrixTimer.interval)
|
||||
{
|
||||
if (MAX7219_Animate())
|
||||
@@ -50,9 +70,11 @@ void loop()
|
||||
}
|
||||
matrixTimer.lastRun = now;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (now - globalTimer.lastRun >= globalTimer.interval)
|
||||
{
|
||||
#if DEVICE_ROLE == SENSOR
|
||||
readBME280();
|
||||
readGPS();
|
||||
readMQ7();
|
||||
@@ -62,17 +84,29 @@ void loop()
|
||||
#endif
|
||||
|
||||
sendSensorData();
|
||||
|
||||
#endif
|
||||
globalTimer.lastRun = now;
|
||||
}
|
||||
|
||||
if (now - mqttTimer.lastRun >= mqttTimer.interval)
|
||||
{
|
||||
MQTT_Handle(id.c_str());
|
||||
MQTT_Handle(mqttId.c_str());
|
||||
mqttTimer.lastRun = now;
|
||||
}
|
||||
}
|
||||
|
||||
#if DEVICE_ROLE == ACTUATOR
|
||||
void writeMatrix(const char *message)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.println("Escribiendo en el display...");
|
||||
#endif
|
||||
|
||||
MAX7219_DisplayText(message, PA_LEFT, 50, 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if DEVICE_ROLE == SENSOR
|
||||
void readMQ7()
|
||||
{
|
||||
const float CO_THRESHOLD = 100.0f;
|
||||
@@ -89,15 +123,6 @@ void readGPS()
|
||||
gpsData = GPS_Read_Fake();
|
||||
}
|
||||
|
||||
void writeMatrix(const char *message)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.println("Escribiendo en el display...");
|
||||
#endif
|
||||
|
||||
MAX7219_DisplayText(message, PA_LEFT, 50, 0);
|
||||
}
|
||||
|
||||
void printAllData()
|
||||
{
|
||||
Serial.println("---------------------");
|
||||
@@ -130,7 +155,6 @@ void sendSensorData()
|
||||
{
|
||||
const String deviceId = String(DEVICE_ID, HEX);
|
||||
|
||||
// Validaciones básicas (puedes añadir más si quieres)
|
||||
bool gpsValid = gpsData.lat != 0.0f && gpsData.lon != 0.0f;
|
||||
bool weatherValid = bme280Data.temperature != 0.0f &&
|
||||
bme280Data.humidity != 0.0f &&
|
||||
@@ -156,10 +180,11 @@ void sendSensorData()
|
||||
postRequest(String(API_URI) + "/batch", json, response);
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.println("📬 Respuesta del servidor:");
|
||||
Serial.println("📥 Respuesta del servidor:");
|
||||
Serial.println(response);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
uint32_t getChipID()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user