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