diff --git a/hardware/include/json.hpp b/hardware/include/json.hpp index b79a9a3..b21cff0 100644 --- a/hardware/include/json.hpp +++ b/hardware/include/json.hpp @@ -1,28 +1,45 @@ -#include - -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 +#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 +); \ No newline at end of file diff --git a/hardware/include/main.hpp b/hardware/include/main.hpp index c079e00..af92752 100644 --- a/hardware/include/main.hpp +++ b/hardware/include/main.hpp @@ -1,9 +1,9 @@ -#include - -#include "json.hpp" -#include "rest.hpp" -#include "wifi.hpp" -#include "test.hpp" -#include "mqtt.hpp" - +#include + +#include "json.hpp" +#include "rest.hpp" +#include "wifi.hpp" +#include "test.hpp" +#include "mqtt.hpp" + #define LED 2 \ No newline at end of file diff --git a/hardware/include/rest.hpp b/hardware/include/rest.hpp index 41f6c46..f422903 100644 --- a/hardware/include/rest.hpp +++ b/hardware/include/rest.hpp @@ -1,4 +1,4 @@ -#include - -#define SERVER_IP "192.168.48.151" +#include + +#define SERVER_IP "192.168.48.151" #define SERVER_PORT 80 \ No newline at end of file diff --git a/hardware/include/wifi.hpp b/hardware/include/wifi.hpp index 1d7a7f1..000b0db 100644 --- a/hardware/include/wifi.hpp +++ b/hardware/include/wifi.hpp @@ -1,7 +1,7 @@ -#include -#include - -#define SSID "Redmi Note 14 Pro 5G" -#define PASSWORD "aitv5949" - +#include +#include + +#define SSID "Redmi Note 14 Pro 5G" +#define PASSWORD "aitv5949" + int setup_wifi(); \ No newline at end of file diff --git a/hardware/src/json.cpp b/hardware/src/json.cpp index 862f7d2..b8af490 100644 --- a/hardware/src/json.cpp +++ b/hardware/src/json.cpp @@ -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; -} \ No newline at end of file +#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(); + 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(); + 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(); + 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); + } +} \ No newline at end of file diff --git a/hardware/src/main.cpp b/hardware/src/main.cpp index 1554bd2..98d2386 100644 --- a/hardware/src/main.cpp +++ b/hardware/src/main.cpp @@ -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() { + } \ No newline at end of file diff --git a/hardware/src/rest.cpp b/hardware/src/rest.cpp index 50777ab..c20cfa8 100644 --- a/hardware/src/rest.cpp +++ b/hardware/src/rest.cpp @@ -1,4 +1,4 @@ -#include "rest.hpp" - -RestClient client = RestClient(SERVER_IP, SERVER_PORT); +#include "rest.hpp" + +RestClient client = RestClient(SERVER_IP, SERVER_PORT); \ No newline at end of file diff --git a/hardware/src/wifi.cpp b/hardware/src/wifi.cpp index 7f54b49..7734e63 100644 --- a/hardware/src/wifi.cpp +++ b/hardware/src/wifi.cpp @@ -1,35 +1,35 @@ -#include - -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 + +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; + } +}