last fixes
This commit is contained in:
@@ -4,8 +4,8 @@
|
|||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
#define SSID "DIGIFIBRA-D2ys"
|
#define SSID "iPhone de Jose"
|
||||||
#define WIFI_PASSWORD "4EEATsyTcZ"
|
#define WIFI_PASSWORD "bombardeenlaus"
|
||||||
|
|
||||||
#define PIN_R 12
|
#define PIN_R 12
|
||||||
#define PIN_G 13
|
#define PIN_G 13
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
#define DEVICE_ROLE ACTUATOR // se cambia entre SENSOR y ACTUATOR
|
#define DEVICE_ROLE SENSOR // se cambia entre SENSOR y ACTUATOR
|
||||||
|
|
||||||
#define MQTT_URI "miarma.net"
|
#define MQTT_URI "miarma.net"
|
||||||
#define API_URI "https://contaminus.miarma.net/api/v1/"
|
#define API_URI "https://contaminus.miarma.net/api/v1/"
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ String serializeSensorValue(
|
|||||||
String output;
|
String output;
|
||||||
serializeJson(doc, output);
|
serializeJson(doc, output);
|
||||||
#ifdef JSON_PRINTS
|
#ifdef JSON_PRINTS
|
||||||
Serial.println("📜 JSON generado:");
|
Serial.println("📜 Generated JSON:");
|
||||||
Serial.print("\t");
|
Serial.print("\t");
|
||||||
Serial.println(output);
|
Serial.println(output);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -29,5 +29,5 @@ GPSData_t GPS_Read()
|
|||||||
GPSData_t GPS_Read_Fake()
|
GPSData_t GPS_Read_Fake()
|
||||||
{
|
{
|
||||||
float rnd = random(-0.0005, 0.0005);
|
float rnd = random(-0.0005, 0.0005);
|
||||||
return {37.326371f + rnd, -5.966001f + rnd};
|
return {37.358087f + rnd, -5.987494f + rnd};
|
||||||
}
|
}
|
||||||
@@ -27,11 +27,11 @@ MQ7Data_t MQ7_Read_Fake()
|
|||||||
bool d0;
|
bool d0;
|
||||||
|
|
||||||
if (flag == 0) {
|
if (flag == 0) {
|
||||||
ppm = 100.0f; // valores entre 101 y 500 ppm
|
ppm = 100.0f;
|
||||||
d0 = true;
|
d0 = true;
|
||||||
flag = 1;
|
flag = 1;
|
||||||
} else {
|
} else {
|
||||||
ppm = 10.0f; // valores entre 10 y 99 ppm
|
ppm = 10.0f;
|
||||||
d0 = false;
|
d0 = false;
|
||||||
flag = 0;
|
flag = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,32 +31,27 @@ void setup()
|
|||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.println("Iniciando...");
|
Serial.println("Starting...");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
WiFi_Init();
|
WiFi_Init();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
||||||
#if DEVICE_ROLE == SENSOR
|
#if DEVICE_ROLE == SENSOR
|
||||||
BME280_Init();
|
BME280_Init();
|
||||||
Serial.println("Sensor BME280 inicializado");
|
Serial.println("BME280 initialized");
|
||||||
GPS_Init();
|
GPS_Init();
|
||||||
Serial.println("Sensor GPS inicializado");
|
Serial.println("GPS initialized");
|
||||||
MQ7_Init();
|
MQ7_Init();
|
||||||
Serial.println("Sensor MQ7 inicializado");
|
Serial.println("MQ7 initialized");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if DEVICE_ROLE == ACTUATOR
|
#if DEVICE_ROLE == ACTUATOR
|
||||||
MAX7219_Init();
|
MAX7219_Init();
|
||||||
Serial.println("Display inicializado");
|
Serial.println("Display initialized");
|
||||||
writeMatrix(currentMessage.c_str());
|
writeMatrix(currentMessage.c_str());
|
||||||
|
|
||||||
String url = String(API_URI) + "groups/" + GROUP_ID + "/devices/" + String(DEVICE_ID, HEX) + "/actuators/" + MAX7219_ID + "/status";
|
|
||||||
getRequest(url, response);
|
|
||||||
MAX7219Status_t statusData = deserializeActuatorStatus(httpClient, httpClient.GET());
|
|
||||||
currentMessage = statusData.actuatorStatus;
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
catch (const char *e)
|
catch (const char *e)
|
||||||
@@ -69,15 +64,15 @@ void loop()
|
|||||||
{
|
{
|
||||||
WiFi_Handle();
|
WiFi_Handle();
|
||||||
|
|
||||||
if(!WiFi_IsConnected())
|
if (!WiFi_IsConnected())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(GROUP_ID == -1)
|
if (GROUP_ID == -1)
|
||||||
{
|
{
|
||||||
GROUP_ID = getGroupId(DEVICE_ID);
|
GROUP_ID = getGroupId(DEVICE_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!mqttStarted && GROUP_ID != -1)
|
if (!mqttStarted && GROUP_ID != -1)
|
||||||
{
|
{
|
||||||
@@ -85,6 +80,16 @@ void loop()
|
|||||||
mqttStarted = true;
|
mqttStarted = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if DEVICE_ROLE == ACTUATOR
|
||||||
|
if (currentMessage.isEmpty() && GROUP_ID != -1)
|
||||||
|
{
|
||||||
|
String url = String(API_URI) + "groups/" + GROUP_ID + "/devices/" + String(DEVICE_ID, HEX) + "/actuators/" + MAX7219_ID + "/status";
|
||||||
|
getRequest(url, response);
|
||||||
|
MAX7219Status_t statusData = deserializeActuatorStatus(httpClient, httpClient.GET());
|
||||||
|
currentMessage = statusData.actuatorStatus;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
uint32_t now = millis();
|
uint32_t now = millis();
|
||||||
|
|
||||||
#if DEVICE_ROLE == ACTUATOR
|
#if DEVICE_ROLE == ACTUATOR
|
||||||
@@ -104,7 +109,7 @@ void loop()
|
|||||||
writeMatrix(currentMessage.c_str());
|
writeMatrix(currentMessage.c_str());
|
||||||
lastMessage = currentMessage;
|
lastMessage = currentMessage;
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.println("Display actualizado");
|
Serial.println("Display has been updated");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
displayTimer.lastRun = now;
|
displayTimer.lastRun = now;
|
||||||
@@ -140,8 +145,8 @@ void loop()
|
|||||||
void writeMatrix(const char *message)
|
void writeMatrix(const char *message)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.println("Escribiendo mensaje: ");
|
Serial.print("Writing message: ");
|
||||||
Serial.print(message);
|
Serial.println(message);
|
||||||
#endif
|
#endif
|
||||||
MAX7219_DisplayText(message, PA_LEFT, 50, 0);
|
MAX7219_DisplayText(message, PA_LEFT, 50, 0);
|
||||||
}
|
}
|
||||||
@@ -167,25 +172,25 @@ void readGPS()
|
|||||||
void printAllData()
|
void printAllData()
|
||||||
{
|
{
|
||||||
Serial.println("---------------------");
|
Serial.println("---------------------");
|
||||||
Serial.println("📦 Batch medida:");
|
Serial.println("📦 Measured batch:");
|
||||||
Serial.println("---------------------");
|
Serial.println("---------------------");
|
||||||
|
|
||||||
Serial.print("ID: ");
|
Serial.print("ID: ");
|
||||||
Serial.println(DEVICE_ID, HEX);
|
Serial.println(DEVICE_ID, HEX);
|
||||||
|
|
||||||
Serial.print("Presión: ");
|
Serial.print("Pressure: ");
|
||||||
Serial.print(bme280Data.pressure);
|
Serial.print(bme280Data.pressure);
|
||||||
Serial.println(" hPa");
|
Serial.println(" hPa");
|
||||||
Serial.print("Temperatura: ");
|
Serial.print("Temperature: ");
|
||||||
Serial.print(bme280Data.temperature);
|
Serial.print(bme280Data.temperature);
|
||||||
Serial.println(" °C");
|
Serial.println(" °C");
|
||||||
Serial.print("Humedad: ");
|
Serial.print("Humidity: ");
|
||||||
Serial.print(bme280Data.humidity);
|
Serial.print(bme280Data.humidity);
|
||||||
Serial.println(" %");
|
Serial.println(" %");
|
||||||
|
|
||||||
Serial.print("Latitud: ");
|
Serial.print("Latitude: ");
|
||||||
Serial.println(gpsData.lat);
|
Serial.println(gpsData.lat);
|
||||||
Serial.print("Longitud: ");
|
Serial.print("Longitude: ");
|
||||||
Serial.println(gpsData.lon);
|
Serial.println(gpsData.lon);
|
||||||
|
|
||||||
Serial.print("CO: ");
|
Serial.print("CO: ");
|
||||||
@@ -209,7 +214,7 @@ void sendSensorData()
|
|||||||
if (!gpsValid || !weatherValid || !coValid)
|
if (!gpsValid || !weatherValid || !coValid)
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.println("❌ Datos inválidos. No se envía el batch.");
|
Serial.println("❌ Invalid batch. Won't be stored.");
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -219,13 +224,13 @@ void sendSensorData()
|
|||||||
bme280Data, mq7Data, gpsData);
|
bme280Data, mq7Data, gpsData);
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.println("📤 Enviando datos al servidor...");
|
Serial.println("📤 Sending batch to server...");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
postRequest(String(API_URI) + "/batch", json, response);
|
postRequest(String(API_URI) + "/batch", json, response);
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
Serial.println("📥 Respuesta del servidor:");
|
Serial.println("📥 Server response:");
|
||||||
Serial.println(response);
|
Serial.println(response);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user