Refactor: update task timer structure, enhance WiFi connection messages, and improve JSON debug output
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "globals.hpp"
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
#define DEVICE_ROLE SENSOR // se cambia entre SENSOR y ACTUATOR
|
#define DEVICE_ROLE SENSOR // se cambia entre SENSOR y ACTUATOR
|
||||||
@@ -17,6 +19,18 @@
|
|||||||
#define ACTUATOR 1
|
#define ACTUATOR 1
|
||||||
|
|
||||||
#define DEBUG
|
#define DEBUG
|
||||||
|
//#define JSON_PRINTS
|
||||||
|
|
||||||
|
struct TaskTimer_t
|
||||||
|
{
|
||||||
|
uint32_t lastRun = 0;
|
||||||
|
uint32_t interval = 1000;
|
||||||
|
|
||||||
|
TaskTimer_t() = default;
|
||||||
|
|
||||||
|
TaskTimer_t(uint32_t last, uint32_t interval)
|
||||||
|
: lastRun(last), interval(interval) {}
|
||||||
|
};
|
||||||
|
|
||||||
extern const uint32_t DEVICE_ID;
|
extern const uint32_t DEVICE_ID;
|
||||||
extern int GROUP_ID;
|
extern int GROUP_ID;
|
||||||
|
|||||||
@@ -23,17 +23,6 @@
|
|||||||
#include "MAX7219.hpp"
|
#include "MAX7219.hpp"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
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
|
struct SensorInfo
|
||||||
{
|
{
|
||||||
int id;
|
int id;
|
||||||
|
|||||||
@@ -32,7 +32,11 @@ String serializeSensorValue(
|
|||||||
|
|
||||||
String output;
|
String output;
|
||||||
serializeJson(doc, output);
|
serializeJson(doc, output);
|
||||||
|
#ifdef JSON_PRINTS
|
||||||
|
Serial.println("📜 JSON generado:");
|
||||||
|
Serial.print("\t");
|
||||||
Serial.println(output);
|
Serial.println(output);
|
||||||
|
#endif
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,7 +45,7 @@ MAX7219Status_t deserializeActuatorStatus(HTTPClient &http, int httpResponseCode
|
|||||||
{
|
{
|
||||||
if (httpResponseCode > 0)
|
if (httpResponseCode > 0)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef JSON_PRINTS
|
||||||
Serial.print("HTTP Response code: ");
|
Serial.print("HTTP Response code: ");
|
||||||
Serial.println(httpResponseCode);
|
Serial.println(httpResponseCode);
|
||||||
#endif
|
#endif
|
||||||
@@ -52,39 +56,36 @@ MAX7219Status_t deserializeActuatorStatus(HTTPClient &http, int httpResponseCode
|
|||||||
|
|
||||||
if (error)
|
if (error)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef JSON_PRINTS
|
||||||
Serial.print(F("deserializeJson() failed: "));
|
Serial.print(F("deserializeJson() failed: "));
|
||||||
Serial.println(error.f_str());
|
Serial.println(error.f_str());
|
||||||
#endif
|
#endif
|
||||||
return {
|
return {
|
||||||
.status = "error",
|
.status = "error",
|
||||||
.actuatorStatus = "Error"
|
.actuatorStatus = "Error"};
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String status = doc["status"] | "error";
|
String status = doc["status"] | "error";
|
||||||
String actuatorStatus = doc["actuatorStatus"] | "Unknown";
|
String actuatorStatus = doc["actuatorStatus"] | "Unknown";
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef JSON_PRINTS
|
||||||
Serial.println("Actuator status deserialized:");
|
Serial.println("Actuator status deserialized:");
|
||||||
Serial.printf(" Status: %s\n Actuator Status: %s\n\n", status.c_str(), actuatorStatus.c_str());
|
Serial.printf(" Status: %s\n Actuator Status: %s\n\n", status.c_str(), actuatorStatus.c_str());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return {
|
return {
|
||||||
.status = status,
|
.status = status,
|
||||||
.actuatorStatus = actuatorStatus
|
.actuatorStatus = actuatorStatus};
|
||||||
};
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef JSON_PRINTS
|
||||||
Serial.print("Error code: ");
|
Serial.print("Error code: ");
|
||||||
Serial.println(httpResponseCode);
|
Serial.println(httpResponseCode);
|
||||||
#endif
|
#endif
|
||||||
return {
|
return {
|
||||||
.status = "error",
|
.status = "error",
|
||||||
.actuatorStatus = "HTTP error"
|
.actuatorStatus = "HTTP error"};
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -93,24 +94,30 @@ int deserializeGroupId(HTTPClient &http, int httpResponseCode)
|
|||||||
{
|
{
|
||||||
if (httpResponseCode > 0)
|
if (httpResponseCode > 0)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef JSON_PRINTS
|
||||||
Serial.print("HTTP Response code: ");
|
Serial.print("HTTP Response code: ");
|
||||||
Serial.println(httpResponseCode);
|
Serial.println(httpResponseCode);
|
||||||
#endif
|
#endif
|
||||||
String responseJson = http.getString();
|
String responseJson = http.getString();
|
||||||
|
#ifdef JSON_PRINTS
|
||||||
|
Serial.println("Response JSON: " + responseJson);
|
||||||
|
#endif
|
||||||
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
|
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
|
||||||
DeserializationError error = deserializeJson(doc, responseJson);
|
DeserializationError error = deserializeJson(doc, responseJson);
|
||||||
|
|
||||||
if (error)
|
if (error)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef JSON_PRINTS
|
||||||
Serial.print(F("deserializeJson() failed: "));
|
Serial.print(F("deserializeJson() failed: "));
|
||||||
Serial.println(error.f_str());
|
Serial.println(error.f_str());
|
||||||
#endif
|
#endif
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (int)doc["message"] | -1;
|
if(doc["message"].isNull())
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return doc["message"].as<int>();
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -8,7 +8,7 @@ PubSubClient client(wifiClient);
|
|||||||
void MQTT_OnReceived(char *topic, byte *payload, unsigned int length)
|
void MQTT_OnReceived(char *topic, byte *payload, unsigned int length)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.print("Received on ");
|
Serial.print("📥 Received on ");
|
||||||
Serial.print(topic);
|
Serial.print(topic);
|
||||||
Serial.print(": ");
|
Serial.print(": ");
|
||||||
#endif
|
#endif
|
||||||
@@ -47,7 +47,7 @@ void MQTT_Init(const char *MQTTServerAddress, uint16_t MQTTServerPort)
|
|||||||
void MQTT_Connect(const char *MQTTClientName)
|
void MQTT_Connect(const char *MQTTClientName)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.print("Starting MQTT connection...");
|
Serial.println("🔌 Starting MQTT connection...");
|
||||||
#endif
|
#endif
|
||||||
if (client.connect(MQTTClientName, USER, MQTT_PASSWORD))
|
if (client.connect(MQTTClientName, USER, MQTT_PASSWORD))
|
||||||
{
|
{
|
||||||
@@ -58,9 +58,7 @@ void MQTT_Connect(const char *MQTTClientName)
|
|||||||
client.publish(statusTopic.c_str(), "connected");
|
client.publish(statusTopic.c_str(), "connected");
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.print("Failed MQTT connection, rc=");
|
Serial.println("❌ Failed MQTT connection, trying again in 5 seconds");
|
||||||
Serial.print(client.state());
|
|
||||||
Serial.println(" try again in 5 seconds");
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
#include <WifiConnection.hpp>
|
#include <WifiConnection.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
WiFiClient wifiClient;
|
WiFiClient wifiClient;
|
||||||
static bool wifiConnected = false;
|
bool wifiConnected = false;
|
||||||
static TaskTimer wifiTimer{0, 500};
|
TaskTimer_t wifiTimer{0, 1000};
|
||||||
|
|
||||||
void setColor(uint8_t r, uint8_t g, uint8_t b)
|
void setColor(uint8_t r, uint8_t g, uint8_t b)
|
||||||
{
|
{
|
||||||
@@ -39,7 +37,7 @@ void WiFi_Init()
|
|||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.begin(SSID, WIFI_PASSWORD);
|
WiFi.begin(SSID, WIFI_PASSWORD);
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.print("🟡 Intentando conectar a WiFi: ");
|
Serial.print("🟡 Trying to connect to WiFi: ");
|
||||||
Serial.println(SSID);
|
Serial.println(SSID);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -64,9 +62,10 @@ void WiFi_Handle()
|
|||||||
if (!wifiConnected)
|
if (!wifiConnected)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.println("🟢 Conectado a la red WiFi");
|
Serial.print("🟢 Connected to WiFi. IP: ");
|
||||||
Serial.print("IP: ");
|
Serial.print(WiFi.localIP());
|
||||||
Serial.println(WiFi.localIP());
|
Serial.print(" | SSID: ");
|
||||||
|
Serial.println(SSID);
|
||||||
#endif
|
#endif
|
||||||
setColor(0, 255, 0);
|
setColor(0, 255, 0);
|
||||||
wifiConnected = true;
|
wifiConnected = true;
|
||||||
@@ -74,13 +73,13 @@ void WiFi_Handle()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (now - wifiRetryTimer.lastRun >= wifiRetryTimer.interval)
|
if (now - wifiTimer.lastRun >= wifiTimer.interval)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.println("🔁 Reintentando conexión WiFi...");
|
Serial.println("🔁 Retrying WiFi connection...");
|
||||||
#endif
|
#endif
|
||||||
WiFi.disconnect(true);
|
WiFi.disconnect(true);
|
||||||
WiFi.begin(SSID, WIFI_PASSWORD);
|
WiFi.begin(SSID, WIFI_PASSWORD);
|
||||||
wifiRetryTimer.lastRun = now;
|
wifiTimer.lastRun = now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,15 +2,14 @@
|
|||||||
|
|
||||||
const uint32_t DEVICE_ID = getChipID();
|
const uint32_t DEVICE_ID = getChipID();
|
||||||
const String mqttId = "CUS-" + String(DEVICE_ID, HEX);
|
const String mqttId = "CUS-" + String(DEVICE_ID, HEX);
|
||||||
int GROUP_ID;
|
int GROUP_ID = -1;
|
||||||
|
|
||||||
TaskTimer globalTimer{0, 10000};
|
TaskTimer_t globalTimer{0, 30000};
|
||||||
TaskTimer mqttTimer{0, 5000};
|
TaskTimer_t mqttTimer{0, 2000};
|
||||||
TaskTimer wifiTimer{0, 5000};
|
|
||||||
|
|
||||||
#if DEVICE_ROLE == ACTUATOR
|
#if DEVICE_ROLE == ACTUATOR
|
||||||
TaskTimer matrixTimer{0, 25};
|
TaskTimer_t matrixTimer{0, 25};
|
||||||
TaskTimer displayTimer{0, 1000};
|
TaskTimer_t displayTimer{0, 1000};
|
||||||
String currentMessage = "";
|
String currentMessage = "";
|
||||||
String lastMessage = "";
|
String lastMessage = "";
|
||||||
extern MD_Parola display;
|
extern MD_Parola display;
|
||||||
@@ -41,7 +40,6 @@ void setup()
|
|||||||
{
|
{
|
||||||
|
|
||||||
#if DEVICE_ROLE == SENSOR
|
#if DEVICE_ROLE == SENSOR
|
||||||
GROUP_ID = getGroupId(DEVICE_ID);
|
|
||||||
BME280_Init();
|
BME280_Init();
|
||||||
Serial.println("Sensor BME280 inicializado");
|
Serial.println("Sensor BME280 inicializado");
|
||||||
GPS_Init();
|
GPS_Init();
|
||||||
@@ -76,13 +74,15 @@ void loop()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mqttStarted)
|
if(GROUP_ID == -1)
|
||||||
{
|
{
|
||||||
MQTT_Init(MQTT_SERVER, MQTT_PORT);
|
GROUP_ID = getGroupId(DEVICE_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!mqttStarted && GROUP_ID != -1)
|
||||||
|
{
|
||||||
|
MQTT_Init(MQTT_URI, MQTT_PORT);
|
||||||
mqttStarted = true;
|
mqttStarted = true;
|
||||||
#ifdef DEBUG
|
|
||||||
Serial.println("Iniciando conexión MQTT...");
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t now = millis();
|
uint32_t now = millis();
|
||||||
@@ -113,6 +113,7 @@ void loop()
|
|||||||
|
|
||||||
if (now - globalTimer.lastRun >= globalTimer.interval)
|
if (now - globalTimer.lastRun >= globalTimer.interval)
|
||||||
{
|
{
|
||||||
|
|
||||||
#if DEVICE_ROLE == SENSOR
|
#if DEVICE_ROLE == SENSOR
|
||||||
readBME280();
|
readBME280();
|
||||||
readGPS();
|
readGPS();
|
||||||
@@ -124,6 +125,7 @@ void loop()
|
|||||||
|
|
||||||
sendSensorData();
|
sendSensorData();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
globalTimer.lastRun = now;
|
globalTimer.lastRun = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,12 +161,14 @@ void readBME280()
|
|||||||
|
|
||||||
void readGPS()
|
void readGPS()
|
||||||
{
|
{
|
||||||
gpsData = GPS_Read();
|
gpsData = GPS_Read_Fake();
|
||||||
}
|
}
|
||||||
|
|
||||||
void printAllData()
|
void printAllData()
|
||||||
{
|
{
|
||||||
Serial.println("---------------------");
|
Serial.println("---------------------");
|
||||||
|
Serial.println("📦 Batch medida:");
|
||||||
|
Serial.println("---------------------");
|
||||||
|
|
||||||
Serial.print("ID: ");
|
Serial.print("ID: ");
|
||||||
Serial.println(DEVICE_ID, HEX);
|
Serial.println(DEVICE_ID, HEX);
|
||||||
@@ -188,6 +192,8 @@ void printAllData()
|
|||||||
Serial.println(mq7Data.co);
|
Serial.println(mq7Data.co);
|
||||||
Serial.print("D0: ");
|
Serial.print("D0: ");
|
||||||
Serial.println(mq7Data.threshold);
|
Serial.println(mq7Data.threshold);
|
||||||
|
|
||||||
|
Serial.println("---------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendSensorData()
|
void sendSensorData()
|
||||||
|
|||||||
Reference in New Issue
Block a user