serializers and deserializers
This commit is contained in:
@@ -1,28 +1,45 @@
|
|||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
#include "HTTPClient.h"
|
||||||
String serializeSensorValue (
|
|
||||||
int sensorId,
|
HTTPClient http;
|
||||||
int deviceId,
|
|
||||||
String sensorType,
|
String serializeSensorValue (
|
||||||
String unit,
|
int sensorId,
|
||||||
int sensorStatus,
|
int deviceId,
|
||||||
float temperature,
|
String sensorType,
|
||||||
float humidity,
|
String unit,
|
||||||
float carbonMonoxide,
|
int sensorStatus,
|
||||||
float lat,
|
float temperature,
|
||||||
float lon,
|
float humidity,
|
||||||
long timestamp
|
float carbonMonoxide,
|
||||||
);
|
float lat,
|
||||||
|
float lon,
|
||||||
String serializeActuatorStatus (
|
long timestamp
|
||||||
|
);
|
||||||
);
|
|
||||||
|
String serializeActuatorStatus (
|
||||||
String serializeDevice (
|
int actuatorId,
|
||||||
|
int deviceId,
|
||||||
);
|
int status,
|
||||||
|
long timestamp
|
||||||
void deserializeSensorValue (
|
);
|
||||||
|
|
||||||
);
|
String serializeDevice (
|
||||||
|
int sensorId,
|
||||||
|
int deviceId,
|
||||||
|
String sensorType,
|
||||||
|
int status,
|
||||||
|
long timestamp
|
||||||
|
);
|
||||||
|
|
||||||
|
void deserializeSensorValue (
|
||||||
|
int httpResponseCode
|
||||||
|
);
|
||||||
|
|
||||||
|
void deserializeActuatorStatus (
|
||||||
|
int httpResponseCode
|
||||||
|
);
|
||||||
|
|
||||||
|
void deserializeDevice (
|
||||||
|
int httpResponseCode
|
||||||
|
);
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
#include "json.hpp"
|
#include "json.hpp"
|
||||||
#include "rest.hpp"
|
#include "rest.hpp"
|
||||||
#include "wifi.hpp"
|
#include "wifi.hpp"
|
||||||
#include "test.hpp"
|
#include "test.hpp"
|
||||||
#include "mqtt.hpp"
|
#include "mqtt.hpp"
|
||||||
|
|
||||||
#define LED 2
|
#define LED 2
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
|
|
||||||
#define SERVER_IP "192.168.48.151"
|
#define SERVER_IP "192.168.48.151"
|
||||||
#define SERVER_PORT 80
|
#define SERVER_PORT 80
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
#define SSID "Redmi Note 14 Pro 5G"
|
#define SSID "Redmi Note 14 Pro 5G"
|
||||||
#define PASSWORD "aitv5949"
|
#define PASSWORD "aitv5949"
|
||||||
|
|
||||||
int setup_wifi();
|
int setup_wifi();
|
||||||
@@ -1,38 +1,179 @@
|
|||||||
#include "json.hpp"
|
#include "json.hpp"
|
||||||
|
|
||||||
String response;
|
String response;
|
||||||
|
|
||||||
String serializeSensorValue (
|
|
||||||
int sensorId,
|
String serializeSensorValue(int sensorId, int deviceId, String sensorType, String unit, int sensorStatus, float temperature, float humidity, float carbonMonoxide, float lat, float lon, long timestamp)
|
||||||
int deviceId,
|
{
|
||||||
String sensorType,
|
DynamicJsonDocument doc(2048);
|
||||||
String unit,
|
|
||||||
int sensorStatus,
|
doc["sensorId"] = sensorId;
|
||||||
float temperature,
|
doc["deviceId"] = deviceId;
|
||||||
float humidity,
|
doc["sensorType"] = sensorType;
|
||||||
float carbonMonoxide,
|
doc["unit"] = unit;
|
||||||
float lat,
|
doc["sesnsorStatuts"] = sensorStatus;
|
||||||
float lon,
|
doc["temperature"] = temperature;
|
||||||
long timestamp
|
doc["humidity"] = humidity;
|
||||||
)
|
doc["carbonMonoxide"] = carbonMonoxide;
|
||||||
{
|
doc["lat"] = lat;
|
||||||
DynamicJsonDocument doc(2048);
|
doc["lon"] = lon;
|
||||||
|
doc["timestamp"] = timestamp;
|
||||||
String output;
|
|
||||||
|
String output;
|
||||||
doc["sensorId"] = sensorId;
|
serializeJson(doc, output);
|
||||||
doc["deviceId"] = deviceId;
|
Serial.println(output);
|
||||||
doc["sensorType"] = sensorType;
|
|
||||||
doc["unit"] = unit;
|
return output;
|
||||||
doc["sesnsorStatuts"] = sensorStatus;
|
}
|
||||||
doc["temperature"] = temperature;
|
|
||||||
doc["humidity"] = humidity;
|
String serializeActuatorStatus (int actuatorId, int deviceId, int status, long timestamp)
|
||||||
doc["carbonMonoxide"] = carbonMonoxide;
|
{
|
||||||
doc["lat"] = lat;
|
DynamicJsonDocument doc(2048);
|
||||||
doc["lon"] = lon;
|
|
||||||
doc["timestamp"] = timestamp;
|
doc["actuatorId"] = actuatorId;
|
||||||
|
doc["deviceId"] = deviceId;
|
||||||
serializeJson(doc, output);
|
doc["status"] = status;
|
||||||
Serial.println(output);
|
doc["timestamp"] = timestamp;
|
||||||
return output;
|
|
||||||
}
|
String output;
|
||||||
|
serializeJson(doc, output);
|
||||||
|
Serial.println(output);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
String serializeDevice(int sensorId, int deviceId, String sensorType, int status, long timestamp)
|
||||||
|
{
|
||||||
|
DynamicJsonDocument doc(2048);
|
||||||
|
|
||||||
|
doc["sensorId"] = sensorId;
|
||||||
|
doc["deviceId"] = deviceId;
|
||||||
|
doc["sensorType"] = sensorType;
|
||||||
|
doc["status"] = status;
|
||||||
|
doc["timestamp"] = timestamp;
|
||||||
|
|
||||||
|
String output;
|
||||||
|
serializeJson(doc, output);
|
||||||
|
Serial.println(output);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
void deserializeSensorValue (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"];
|
||||||
|
int deviceId = sensor["deviceId"];
|
||||||
|
String sensorType = sensor["sensorType"];
|
||||||
|
String unit = sensor["unit"];
|
||||||
|
int sesnsorStatuts = sensor["sesnsorStatuts"];
|
||||||
|
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: [sensorId: " + String(sensorId) + ", deviceId: " + String(deviceId) + ", sensorType: " + sensorType + ", unit: " + unit +", sesnsorStatuts: " + String(sesnsorStatuts) +", temperature: " + String(temperature) +", humidity: " + String(humidity) +", carbonMonoxide: " + String(carbonMonoxide) +", lat: " + String(lat) +", lon: " + String(lon) +", timestamp: " + String(timestamp) + "]").c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.print("Error code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void deserializeActuatorStatus (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 actuadorId = actuator["actuadorId"];
|
||||||
|
int deviceId = actuator["deviceId"];
|
||||||
|
int statuts = actuator["statuts"];
|
||||||
|
long timestamp = actuator["timestamp"];
|
||||||
|
|
||||||
|
Serial.println(("Actuador deserialized: [actuadorId: " + String(actuadorId) +
|
||||||
|
", deviceId: " + String(deviceId) +
|
||||||
|
", statuts: " + String(statuts) +
|
||||||
|
", timestamp: " + String(timestamp) + "]").c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.print("Error code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void deserializeDevice (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)
|
||||||
|
{
|
||||||
|
int sensorId = device["sensorId"];
|
||||||
|
int deviceId = device["deviceId"];
|
||||||
|
String sensorType = device["sensorType"];
|
||||||
|
long timestamp = device["timestamp"];
|
||||||
|
|
||||||
|
Serial.println(("Sensor deserialized: [sensorId: " + String(sensorId) +
|
||||||
|
", deviceId: " + String(deviceId) +
|
||||||
|
", sensorType: " + sensorType +
|
||||||
|
", timestamp: " + String(timestamp) + "]").c_str());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Serial.print("Error code: ");
|
||||||
|
Serial.println(httpResponseCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
#include "main.hpp"
|
#include "main.hpp"
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
if(setup_wifi() != 0)
|
if(setup_wifi() != 0)
|
||||||
{
|
{
|
||||||
Serial.print("Error connecting to WiFI");
|
Serial.print("Error connecting to WiFI");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include "rest.hpp"
|
#include "rest.hpp"
|
||||||
|
|
||||||
RestClient client = RestClient(SERVER_IP, SERVER_PORT);
|
RestClient client = RestClient(SERVER_IP, SERVER_PORT);
|
||||||
|
|
||||||
@@ -1,35 +1,35 @@
|
|||||||
#include <wifi.hpp>
|
#include <wifi.hpp>
|
||||||
|
|
||||||
WiFiClient espClient;
|
WiFiClient espClient;
|
||||||
PubSubClient client(espClient);
|
PubSubClient client(espClient);
|
||||||
|
|
||||||
int setup_wifi()
|
int setup_wifi()
|
||||||
{
|
{
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.print("Connecting to ");
|
Serial.print("Connecting to ");
|
||||||
Serial.println(SSID);
|
Serial.println(SSID);
|
||||||
|
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
WiFi.begin(SSID, PASSWORD);
|
WiFi.begin(SSID, PASSWORD);
|
||||||
|
|
||||||
while (WiFi.status() != WL_CONNECTED)
|
while (WiFi.status() != WL_CONNECTED)
|
||||||
{
|
{
|
||||||
delay(500);
|
delay(500);
|
||||||
Serial.print(".");
|
Serial.print(".");
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial.println("");
|
Serial.println("");
|
||||||
Serial.println("WiFi connected");
|
Serial.println("WiFi connected");
|
||||||
Serial.println("IP address: ");
|
Serial.println("IP address: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
Serial.println("Setup!");
|
Serial.println("Setup!");
|
||||||
|
|
||||||
if(WiFi.status() == WL_CONNECTED)
|
if(WiFi.status() == WL_CONNECTED)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user