From d77fc3edf3066a2faf39ee5524d31787d7506f20 Mon Sep 17 00:00:00 2001 From: Jose Date: Wed, 9 Apr 2025 10:14:46 +0200 Subject: [PATCH 1/5] Init commit --- hardware/include/README | 37 ------------------------------- hardware/include/json.hpp | 28 ++++++++++++++++++++++++ hardware/include/rest.hpp | 5 ++++- hardware/include/wifi.hpp | 4 ++-- hardware/lib/README | 46 --------------------------------------- hardware/src/json.cpp | 39 ++++++++++++++++++++++++++++++++- hardware/src/rest.cpp | 5 ++++- hardware/test/README | 11 ---------- 8 files changed, 76 insertions(+), 99 deletions(-) delete mode 100644 hardware/include/README delete mode 100644 hardware/lib/README delete mode 100644 hardware/test/README diff --git a/hardware/include/README b/hardware/include/README deleted file mode 100644 index 49819c0..0000000 --- a/hardware/include/README +++ /dev/null @@ -1,37 +0,0 @@ - -This directory is intended for project header files. - -A header file is a file containing C declarations and macro definitions -to be shared between several project source files. You request the use of a -header file in your project source file (C, C++, etc) located in `src` folder -by including it, with the C preprocessing directive `#include'. - -```src/main.c - -#include "header.h" - -int main (void) -{ - ... -} -``` - -Including a header file produces the same results as copying the header file -into each source file that needs it. Such copying would be time-consuming -and error-prone. With a header file, the related declarations appear -in only one place. If they need to be changed, they can be changed in one -place, and programs that include the header file will automatically use the -new version when next recompiled. The header file eliminates the labor of -finding and changing all the copies as well as the risk that a failure to -find one copy will result in inconsistencies within a program. - -In C, the convention is to give header files names that end with `.h'. - -Read more about using header files in official GCC documentation: - -* Include Syntax -* Include Operation -* Once-Only Headers -* Computed Includes - -https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/hardware/include/json.hpp b/hardware/include/json.hpp index e69de29..b79a9a3 100644 --- a/hardware/include/json.hpp +++ b/hardware/include/json.hpp @@ -0,0 +1,28 @@ +#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 ( + +); + diff --git a/hardware/include/rest.hpp b/hardware/include/rest.hpp index 2617b4f..41f6c46 100644 --- a/hardware/include/rest.hpp +++ b/hardware/include/rest.hpp @@ -1 +1,4 @@ -#include \ No newline at end of file +#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 cf7ed35..1d7a7f1 100644 --- a/hardware/include/wifi.hpp +++ b/hardware/include/wifi.hpp @@ -1,7 +1,7 @@ #include #include -#define SSID "iPhone de Jose" -#define PASSWORD "bombardeenlaus" +#define SSID "Redmi Note 14 Pro 5G" +#define PASSWORD "aitv5949" int setup_wifi(); \ No newline at end of file diff --git a/hardware/lib/README b/hardware/lib/README deleted file mode 100644 index 9379397..0000000 --- a/hardware/lib/README +++ /dev/null @@ -1,46 +0,0 @@ - -This directory is intended for project specific (private) libraries. -PlatformIO will compile them to static libraries and link into the executable file. - -The source code of each library should be placed in a separate directory -("lib/your_library_name/[Code]"). - -For example, see the structure of the following example libraries `Foo` and `Bar`: - -|--lib -| | -| |--Bar -| | |--docs -| | |--examples -| | |--src -| | |- Bar.c -| | |- Bar.h -| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html -| | -| |--Foo -| | |- Foo.c -| | |- Foo.h -| | -| |- README --> THIS FILE -| -|- platformio.ini -|--src - |- main.c - -Example contents of `src/main.c` using Foo and Bar: -``` -#include -#include - -int main (void) -{ - ... -} - -``` - -The PlatformIO Library Dependency Finder will find automatically dependent -libraries by scanning project source files. - -More information about PlatformIO Library Dependency Finder -- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/hardware/src/json.cpp b/hardware/src/json.cpp index df2ffdf..862f7d2 100644 --- a/hardware/src/json.cpp +++ b/hardware/src/json.cpp @@ -1 +1,38 @@ -#include "json.hpp" \ 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); + + 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 diff --git a/hardware/src/rest.cpp b/hardware/src/rest.cpp index f3b0775..50777ab 100644 --- a/hardware/src/rest.cpp +++ b/hardware/src/rest.cpp @@ -1 +1,4 @@ -#include "rest.hpp" \ No newline at end of file +#include "rest.hpp" + +RestClient client = RestClient(SERVER_IP, SERVER_PORT); + \ No newline at end of file diff --git a/hardware/test/README b/hardware/test/README deleted file mode 100644 index 9b1e87b..0000000 --- a/hardware/test/README +++ /dev/null @@ -1,11 +0,0 @@ - -This directory is intended for PlatformIO Test Runner and project tests. - -Unit Testing is a software testing method by which individual units of -source code, sets of one or more MCU program modules together with associated -control data, usage procedures, and operating procedures, are tested to -determine whether they are fit for use. Unit testing finds problems early -in the development cycle. - -More information about PlatformIO Unit Testing: -- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html From bd7d56b46e7d02f4edc05b2cca4275409a470825 Mon Sep 17 00:00:00 2001 From: Jose Date: Wed, 9 Apr 2025 12:40:03 +0200 Subject: [PATCH 2/5] serializers and deserializers --- hardware/include/json.hpp | 73 ++++++++----- hardware/include/main.hpp | 16 +-- hardware/include/rest.hpp | 6 +- hardware/include/wifi.hpp | 12 +-- hardware/src/json.cpp | 217 +++++++++++++++++++++++++++++++------- hardware/src/main.cpp | 26 ++--- hardware/src/rest.cpp | 6 +- hardware/src/wifi.cpp | 70 ++++++------ 8 files changed, 292 insertions(+), 134 deletions(-) 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; + } +} From 0c03c052b49bb7409f80838a47bd46a316f75d6f Mon Sep 17 00:00:00 2001 From: Jose Date: Wed, 9 Apr 2025 13:14:11 +0200 Subject: [PATCH 3/5] fucking fixes i hate cpp --- hardware/include/json.hpp | 7 ++++--- hardware/include/main.hpp | 2 +- hardware/include/test.hpp | 0 hardware/src/json.cpp | 10 ++++++---- hardware/src/main.cpp | 14 +++++++++++++- hardware/src/rest.cpp | 5 +++-- hardware/src/test.cpp | 1 - hardware/src/wifi.cpp | 3 +-- 8 files changed, 28 insertions(+), 14 deletions(-) delete mode 100644 hardware/include/test.hpp delete mode 100644 hardware/src/test.cpp diff --git a/hardware/include/json.hpp b/hardware/include/json.hpp index b21cff0..374c239 100644 --- a/hardware/include/json.hpp +++ b/hardware/include/json.hpp @@ -1,7 +1,5 @@ #include -#include "HTTPClient.h" - -HTTPClient http; +#include String serializeSensorValue ( int sensorId, @@ -33,13 +31,16 @@ String serializeDevice ( ); void deserializeSensorValue ( + HTTPClient* http, int httpResponseCode ); void deserializeActuatorStatus ( + HTTPClient* http, int httpResponseCode ); void deserializeDevice ( + HTTPClient* http, int httpResponseCode ); \ No newline at end of file diff --git a/hardware/include/main.hpp b/hardware/include/main.hpp index af92752..e32c116 100644 --- a/hardware/include/main.hpp +++ b/hardware/include/main.hpp @@ -6,4 +6,4 @@ #include "test.hpp" #include "mqtt.hpp" -#define LED 2 \ No newline at end of file +uint32_t getChipID(); \ No newline at end of file diff --git a/hardware/include/test.hpp b/hardware/include/test.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/hardware/src/json.cpp b/hardware/src/json.cpp index b8af490..791a100 100644 --- a/hardware/src/json.cpp +++ b/hardware/src/json.cpp @@ -2,7 +2,6 @@ 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); @@ -59,7 +58,8 @@ String serializeDevice(int sensorId, int deviceId, String sensorType, int status return output; } -void deserializeSensorValue (int httpResponseCode){ +void deserializeSensorValue (HTTPClient http, int httpResponseCode) +{ if (httpResponseCode > 0) { @@ -101,7 +101,8 @@ void deserializeSensorValue (int httpResponseCode){ } } -void deserializeActuatorStatus (int httpResponseCode){ +void deserializeActuatorStatus (HTTPClient http, int httpResponseCode) +{ if (httpResponseCode > 0) { @@ -139,7 +140,8 @@ void deserializeActuatorStatus (int httpResponseCode){ } } -void deserializeDevice (int httpResponseCode){ +void deserializeDevice (HTTPClient http, int httpResponseCode) +{ if (httpResponseCode > 0) { diff --git a/hardware/src/main.cpp b/hardware/src/main.cpp index 98d2386..40fb98e 100644 --- a/hardware/src/main.cpp +++ b/hardware/src/main.cpp @@ -1,8 +1,20 @@ #include "main.hpp" - + +const uint32_t deviceId = getChipID(); + +uint32_t getChipID() +{ + uint32_t chipId; + for (int i = 0; i < 17; i = i + 8) { + chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i; + } + return chipId; +} + void setup() { Serial.begin(9600); + // WiFi Connection if(setup_wifi() != 0) { Serial.print("Error connecting to WiFI"); diff --git a/hardware/src/rest.cpp b/hardware/src/rest.cpp index c20cfa8..6208078 100644 --- a/hardware/src/rest.cpp +++ b/hardware/src/rest.cpp @@ -1,4 +1,5 @@ #include "rest.hpp" -RestClient client = RestClient(SERVER_IP, SERVER_PORT); - \ No newline at end of file +HTTPClient httpClient; + + diff --git a/hardware/src/test.cpp b/hardware/src/test.cpp deleted file mode 100644 index ac97f27..0000000 --- a/hardware/src/test.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "test.hpp" \ No newline at end of file diff --git a/hardware/src/wifi.cpp b/hardware/src/wifi.cpp index 7734e63..8b2cd6a 100644 --- a/hardware/src/wifi.cpp +++ b/hardware/src/wifi.cpp @@ -1,7 +1,6 @@ #include -WiFiClient espClient; -PubSubClient client(espClient); +WiFiClient wifiClient; int setup_wifi() { From 245f213ad9329cfd477f5b1ad1093879d7db9dce Mon Sep 17 00:00:00 2001 From: Jose Date: Wed, 9 Apr 2025 14:19:52 +0200 Subject: [PATCH 4/5] http requests from esp32 to api --- hardware/.vscode/settings.json | 11 ++++++++ hardware/include/{json.hpp => JsonTools.hpp} | 6 ++--- hardware/include/{mqtt.hpp => MqttClient.hpp} | 0 hardware/include/RestClient.hpp | 4 +++ hardware/include/WifiConnection.hpp | 7 +++++ hardware/include/main.hpp | 9 +++---- hardware/include/rest.hpp | 4 --- hardware/include/wifi.hpp | 7 ----- hardware/src/{json.cpp => JsonTools.cpp} | 10 +++---- hardware/src/MqttClient.cpp | 1 + hardware/src/RestClient.cpp | 27 +++++++++++++++++++ hardware/src/{wifi.cpp => WifiConnection.cpp} | 5 ++-- hardware/src/main.cpp | 12 ++++++--- hardware/src/mqtt.cpp | 1 - hardware/src/rest.cpp | 5 ---- 15 files changed, 72 insertions(+), 37 deletions(-) create mode 100644 hardware/.vscode/settings.json rename hardware/include/{json.hpp => JsonTools.hpp} (86%) rename hardware/include/{mqtt.hpp => MqttClient.hpp} (100%) create mode 100644 hardware/include/RestClient.hpp create mode 100644 hardware/include/WifiConnection.hpp delete mode 100644 hardware/include/rest.hpp delete mode 100644 hardware/include/wifi.hpp rename hardware/src/{json.cpp => JsonTools.cpp} (92%) create mode 100644 hardware/src/MqttClient.cpp create mode 100644 hardware/src/RestClient.cpp rename hardware/src/{wifi.cpp => WifiConnection.cpp} (86%) delete mode 100644 hardware/src/mqtt.cpp delete mode 100644 hardware/src/rest.cpp diff --git a/hardware/.vscode/settings.json b/hardware/.vscode/settings.json new file mode 100644 index 0000000..cb0b621 --- /dev/null +++ b/hardware/.vscode/settings.json @@ -0,0 +1,11 @@ +{ + "files.associations": { + "array": "cpp", + "*.tcc": "cpp", + "memory": "cpp", + "istream": "cpp", + "functional": "cpp", + "tuple": "cpp", + "utility": "cpp" + } +} \ No newline at end of file diff --git a/hardware/include/json.hpp b/hardware/include/JsonTools.hpp similarity index 86% rename from hardware/include/json.hpp rename to hardware/include/JsonTools.hpp index 374c239..da7c053 100644 --- a/hardware/include/json.hpp +++ b/hardware/include/JsonTools.hpp @@ -31,16 +31,16 @@ String serializeDevice ( ); void deserializeSensorValue ( - HTTPClient* http, + HTTPClient &http, int httpResponseCode ); void deserializeActuatorStatus ( - HTTPClient* http, + HTTPClient &http, int httpResponseCode ); void deserializeDevice ( - HTTPClient* http, + HTTPClient &http, int httpResponseCode ); \ No newline at end of file diff --git a/hardware/include/mqtt.hpp b/hardware/include/MqttClient.hpp similarity index 100% rename from hardware/include/mqtt.hpp rename to hardware/include/MqttClient.hpp diff --git a/hardware/include/RestClient.hpp b/hardware/include/RestClient.hpp new file mode 100644 index 0000000..5304d8c --- /dev/null +++ b/hardware/include/RestClient.hpp @@ -0,0 +1,4 @@ +#include + +void getRequest(HTTPClient &httpClient, const String url, String &response); +void postRequest(HTTPClient &httpClient, const String url, const String &payload, String &response); \ No newline at end of file diff --git a/hardware/include/WifiConnection.hpp b/hardware/include/WifiConnection.hpp new file mode 100644 index 0000000..93ff3b2 --- /dev/null +++ b/hardware/include/WifiConnection.hpp @@ -0,0 +1,7 @@ +#include +#include + +#define SSID "iPhone de Álvaro" +#define PASSWORD "alvarito123" + +int setupWifi(); \ No newline at end of file diff --git a/hardware/include/main.hpp b/hardware/include/main.hpp index e32c116..6d06a4a 100644 --- a/hardware/include/main.hpp +++ b/hardware/include/main.hpp @@ -1,9 +1,8 @@ #include -#include "json.hpp" -#include "rest.hpp" -#include "wifi.hpp" -#include "test.hpp" -#include "mqtt.hpp" +#include "JsonTools.hpp" +#include "RestClient.hpp" +#include "WifiConnection.hpp" +#include "MqttClient.hpp" uint32_t getChipID(); \ No newline at end of file diff --git a/hardware/include/rest.hpp b/hardware/include/rest.hpp deleted file mode 100644 index f422903..0000000 --- a/hardware/include/rest.hpp +++ /dev/null @@ -1,4 +0,0 @@ -#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 deleted file mode 100644 index 000b0db..0000000 --- a/hardware/include/wifi.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#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/JsonTools.cpp similarity index 92% rename from hardware/src/json.cpp rename to hardware/src/JsonTools.cpp index 791a100..02b7989 100644 --- a/hardware/src/json.cpp +++ b/hardware/src/JsonTools.cpp @@ -1,6 +1,4 @@ -#include "json.hpp" - -String response; +#include "JsonTools.hpp" String serializeSensorValue(int sensorId, int deviceId, String sensorType, String unit, int sensorStatus, float temperature, float humidity, float carbonMonoxide, float lat, float lon, long timestamp) { @@ -58,7 +56,7 @@ String serializeDevice(int sensorId, int deviceId, String sensorType, int status return output; } -void deserializeSensorValue (HTTPClient http, int httpResponseCode) +void deserializeSensorValue (HTTPClient &http, int httpResponseCode) { if (httpResponseCode > 0) @@ -101,7 +99,7 @@ void deserializeSensorValue (HTTPClient http, int httpResponseCode) } } -void deserializeActuatorStatus (HTTPClient http, int httpResponseCode) +void deserializeActuatorStatus (HTTPClient &http, int httpResponseCode) { if (httpResponseCode > 0) @@ -140,7 +138,7 @@ void deserializeActuatorStatus (HTTPClient http, int httpResponseCode) } } -void deserializeDevice (HTTPClient http, int httpResponseCode) +void deserializeDevice (HTTPClient &http, int httpResponseCode) { if (httpResponseCode > 0) diff --git a/hardware/src/MqttClient.cpp b/hardware/src/MqttClient.cpp new file mode 100644 index 0000000..c09ca64 --- /dev/null +++ b/hardware/src/MqttClient.cpp @@ -0,0 +1 @@ +#include "MqttClient.hpp" \ No newline at end of file diff --git a/hardware/src/RestClient.cpp b/hardware/src/RestClient.cpp new file mode 100644 index 0000000..171b041 --- /dev/null +++ b/hardware/src/RestClient.cpp @@ -0,0 +1,27 @@ +#include "RestClient.hpp" + + +void getRequest(HTTPClient &httpClient, const String url, String &response) +{ + httpClient.begin(url); + int httpCode = httpClient.GET(); + if (httpCode > 0) { + response = httpClient.getString(); + } else { + response = "Error: " + String(httpCode); + } + httpClient.end(); +} + +void postRequest(HTTPClient &httpClient, const String url, String &payload, String &response) +{ + httpClient.begin(url); + httpClient.addHeader("Content-Type", "application/json"); + int httpCode = httpClient.POST(payload); + if (httpCode > 0) { + response = httpClient.getString(); + } else { + response = "Error: " + String(httpCode); + } + httpClient.end(); +} \ No newline at end of file diff --git a/hardware/src/wifi.cpp b/hardware/src/WifiConnection.cpp similarity index 86% rename from hardware/src/wifi.cpp rename to hardware/src/WifiConnection.cpp index 8b2cd6a..accbbad 100644 --- a/hardware/src/wifi.cpp +++ b/hardware/src/WifiConnection.cpp @@ -1,13 +1,12 @@ -#include +#include WiFiClient wifiClient; -int setup_wifi() +int setupWifi() { Serial.println(); Serial.print("Connecting to "); Serial.println(SSID); - WiFi.mode(WIFI_STA); WiFi.begin(SSID, PASSWORD); diff --git a/hardware/src/main.cpp b/hardware/src/main.cpp index 40fb98e..fb27a34 100644 --- a/hardware/src/main.cpp +++ b/hardware/src/main.cpp @@ -1,6 +1,8 @@ #include "main.hpp" const uint32_t deviceId = getChipID(); +String response; +HTTPClient httpClient; uint32_t getChipID() { @@ -15,12 +17,16 @@ void setup() { Serial.begin(9600); // WiFi Connection - if(setup_wifi() != 0) + if(setupWifi() != 0) { - Serial.print("Error connecting to WiFI"); + Serial.print("Error connecting to WiFi"); } + + // test get + getRequest(httpClient, "http://172.20.10.7:8082/api/v1/sensors/1/values", response); + deserializeSensorValue(httpClient, httpClient.GET()); } void loop() { - + } \ No newline at end of file diff --git a/hardware/src/mqtt.cpp b/hardware/src/mqtt.cpp deleted file mode 100644 index 131e5ea..0000000 --- a/hardware/src/mqtt.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "mqtt.hpp" \ No newline at end of file diff --git a/hardware/src/rest.cpp b/hardware/src/rest.cpp deleted file mode 100644 index 6208078..0000000 --- a/hardware/src/rest.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "rest.hpp" - -HTTPClient httpClient; - - From bf7719bac41b3333602ab1d54660567611e3610a Mon Sep 17 00:00:00 2001 From: Jose Date: Fri, 11 Apr 2025 09:17:08 +0200 Subject: [PATCH 5/5] fin --- .../miarma/contaminus/common/Constants.java | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/backend/src/main/java/net/miarma/contaminus/common/Constants.java b/backend/src/main/java/net/miarma/contaminus/common/Constants.java index 3f26a39..55891fe 100644 --- a/backend/src/main/java/net/miarma/contaminus/common/Constants.java +++ b/backend/src/main/java/net/miarma/contaminus/common/Constants.java @@ -6,34 +6,35 @@ import io.vertx.core.impl.logging.LoggerFactory; public class Constants { public static final String APP_NAME = "ContaminUS"; public static final String API_PREFIX = "/api/v1"; + public static final String RAW_API_PREFIX = "/api/raw/v1"; public static Logger LOGGER = LoggerFactory.getLogger(Constants.APP_NAME); /* API Endpoints */ - public static final String GET_GROUPS = API_PREFIX + "/groups"; - public static final String POST_GROUPS = API_PREFIX + "/groups"; - public static final String PUT_GROUP_BY_ID = API_PREFIX + "/groups/:groupId"; + public static final String GET_GROUPS = RAW_API_PREFIX + "/groups"; + public static final String POST_GROUPS = RAW_API_PREFIX + "/groups"; + public static final String PUT_GROUP_BY_ID = RAW_API_PREFIX + "/groups/:groupId"; - public static final String GET_DEVICES = API_PREFIX + "/devices"; - public static final String POST_DEVICES = API_PREFIX + "/devices"; - public static final String PUT_DEVICE_BY_ID = API_PREFIX + "/devices/:deviceId"; + public static final String GET_DEVICES = RAW_API_PREFIX + "/devices"; + public static final String POST_DEVICES = RAW_API_PREFIX + "/devices"; + public static final String PUT_DEVICE_BY_ID = RAW_API_PREFIX + "/devices/:deviceId"; - public static final String GET_SENSORS = API_PREFIX + "/sensors"; - public static final String POST_SENSORS = API_PREFIX + "/sensors"; - public static final String PUT_SENSOR_BY_ID = API_PREFIX + "/sensors/:sensorId"; + public static final String GET_SENSORS = RAW_API_PREFIX + "/sensors"; + public static final String POST_SENSORS = RAW_API_PREFIX + "/sensors"; + public static final String PUT_SENSOR_BY_ID = RAW_API_PREFIX + "/sensors/:sensorId"; - public static final String GET_ACTUATORS = API_PREFIX + "/actuators"; - public static final String POST_ACTUATORS = API_PREFIX + "/actuators"; - public static final String PUT_ACTUATOR_BY_ID = API_PREFIX + "/actuators/:actuatorId"; + public static final String GET_ACTUATORS = RAW_API_PREFIX + "/actuators"; + public static final String POST_ACTUATORS = RAW_API_PREFIX + "/actuators"; + public static final String PUT_ACTUATOR_BY_ID = RAW_API_PREFIX + "/actuators/:actuatorId"; - public static final String GET_CO_BY_DEVICE_VIEW = API_PREFIX + "/v_co_by_device"; + public static final String GET_CO_BY_DEVICE_VIEW = RAW_API_PREFIX + "/v_co_by_device"; - public static final String GET_GPS_BY_DEVICE_VIEW = API_PREFIX + "/v_gps_by_device"; - public static final String GET_LATEST_VALUES_VIEW = API_PREFIX + "/v_latest_values"; - public static final String GET_POLLUTION_MAP_VIEW = API_PREFIX + "/v_pollution_map"; - public static final String GET_SENSOR_HISTORY_BY_DEVICE_VIEW = API_PREFIX + "/v_sensor_history_by_device"; - public static final String GET_SENSOR_VALUES_VIEW = API_PREFIX + "/v_sensor_values"; - public static final String GET_WEATHER_BY_DEVICE_VIEW = API_PREFIX + "/v_weather_by_device"; + public static final String GET_GPS_BY_DEVICE_VIEW = RAW_API_PREFIX + "/v_gps_by_device"; + public static final String GET_LATEST_VALUES_VIEW = RAW_API_PREFIX + "/v_latest_values"; + public static final String GET_POLLUTION_MAP_VIEW = RAW_API_PREFIX + "/v_pollution_map"; + public static final String GET_SENSOR_HISTORY_BY_DEVICE_VIEW = RAW_API_PREFIX + "/v_sensor_history_by_device"; + public static final String GET_SENSOR_VALUES_VIEW = RAW_API_PREFIX + "/v_sensor_values"; + public static final String GET_WEATHER_BY_DEVICE_VIEW = RAW_API_PREFIX + "/v_weather_by_device"; /* Bussiness Logic API */ public static final String GET_GROUP_BY_ID = API_PREFIX + "/groups/:groupId";