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; - -