Update WiFi connection handling, adjust MQTT timer, and implement group ID deserialization
This commit is contained in:
@@ -2,10 +2,14 @@
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <HTTPClient.h>
|
||||
#if DEVICE_ROLE == SENSOR
|
||||
#include "BME280.hpp"
|
||||
#include "MQ7v2.hpp"
|
||||
#include "GPS.hpp"
|
||||
#endif
|
||||
#if DEVICE_ROLE == ACTUATOR
|
||||
#include "MAX7219.hpp"
|
||||
#endif
|
||||
|
||||
String serializeSensorValue(
|
||||
int groupId,
|
||||
@@ -17,4 +21,7 @@ String serializeSensorValue(
|
||||
const MQ7Data_t &mq7,
|
||||
const GPSData_t &gps);
|
||||
|
||||
#if DEVICE_ROLE == ACTUATOR
|
||||
MAX7219Status_t deserializeActuatorStatus(HTTPClient &http, int httpResponseCode);
|
||||
#endif
|
||||
int deserializeGroupId(HTTPClient &http, int httpResponseCode);
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#define SSID "iPhone de Jose"
|
||||
#define WIFI_PASSWORD "bombardeenlaus"
|
||||
#define SSID "DIGIFIBRA-D2ys"
|
||||
#define WIFI_PASSWORD "4EEATsyTcZ"
|
||||
|
||||
int WiFi_Init();
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#define MQTT_URI "miarma.net"
|
||||
#define API_URI "https://contaminus.miarma.net/api/v1/"
|
||||
#define RAW_API_URI "https://contaminus.miarma.net/api/raw/v1/"
|
||||
#define REST_PORT 443
|
||||
#define MQTT_PORT 1883
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ String serializeSensorValue(
|
||||
return output;
|
||||
}
|
||||
|
||||
#if DEVICE_ROLE == ACTUATOR
|
||||
MAX7219Status_t deserializeActuatorStatus(HTTPClient &http, int httpResponseCode)
|
||||
{
|
||||
if (httpResponseCode > 0)
|
||||
@@ -86,3 +87,30 @@ MAX7219Status_t deserializeActuatorStatus(HTTPClient &http, int httpResponseCode
|
||||
};
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
int deserializeGroupId(HTTPClient &http, int httpResponseCode)
|
||||
{
|
||||
if (httpResponseCode > 0)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.print("HTTP Response code: ");
|
||||
Serial.println(httpResponseCode);
|
||||
#endif
|
||||
String responseJson = http.getString();
|
||||
DynamicJsonDocument doc(ESP.getMaxAllocHeap());
|
||||
DeserializationError error = deserializeJson(doc, responseJson);
|
||||
|
||||
if (error)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
Serial.print(F("deserializeJson() failed: "));
|
||||
Serial.println(error.f_str());
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int)doc["message"] | -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -5,7 +5,8 @@ const String mqttId = "CUS-" + String(DEVICE_ID, HEX);
|
||||
int GROUP_ID;
|
||||
|
||||
TaskTimer globalTimer{0, 10000};
|
||||
TaskTimer mqttTimer{0, 2500};
|
||||
TaskTimer mqttTimer{0, 5000};
|
||||
TaskTimer wifiTimer{0, 5000};
|
||||
|
||||
#if DEVICE_ROLE == ACTUATOR
|
||||
TaskTimer matrixTimer{0, 25};
|
||||
@@ -32,7 +33,11 @@ void setup()
|
||||
Serial.println("Iniciando...");
|
||||
#endif
|
||||
|
||||
WiFi_Init();
|
||||
while(WiFi_Init() != WL_CONNECTED)
|
||||
{
|
||||
Serial.println("Esperando conexión WiFi...");
|
||||
delay(1000);
|
||||
}
|
||||
MQTT_Init(MQTT_URI, MQTT_PORT);
|
||||
|
||||
try
|
||||
@@ -223,8 +228,7 @@ uint32_t getChipID()
|
||||
|
||||
int getGroupId(int deviceId)
|
||||
{
|
||||
String url = String(API_URI) + "groups/" + GROUP_ID + "/devices/" + String(DEVICE_ID, HEX) + "/actuators/" + MAX7219_ID + "/status";
|
||||
String url = String(RAW_API_URI) + "devices/" + String(DEVICE_ID, HEX) + "/my-group";
|
||||
getRequest(url, response);
|
||||
MAX7219Status_t statusData = deserializeActuatorStatus(httpClient, httpClient.GET());
|
||||
currentMessage = statusData.actuatorStatus;
|
||||
return deserializeGroupId(httpClient, httpClient.GET());
|
||||
}
|
||||
Reference in New Issue
Block a user