From b9b4a9ca9b488b6c99c988bbd48af4d046878415 Mon Sep 17 00:00:00 2001 From: Niels Gras Date: Fri, 31 May 2024 16:32:06 +0200 Subject: [PATCH] chore: Update Connectivity class with fetchIPAddress method --- .../Movement-sensor-code/Connectivity.cpp | 48 ++++++++++++++----- .../Movement-sensor-code/Connectivity.h | 5 +- .../Movement-sensor-code.ino | 4 +- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/code/arduino/Movement-sensor-code/Connectivity.cpp b/code/arduino/Movement-sensor-code/Connectivity.cpp index 6e96ca3..e56767e 100644 --- a/code/arduino/Movement-sensor-code/Connectivity.cpp +++ b/code/arduino/Movement-sensor-code/Connectivity.cpp @@ -8,23 +8,49 @@ void Connectivity::connectWiFi(char* ssid, char* pass){ } } -void Connectivity::websocketSetup(char* ip, uint16_t port, char* adress){ - //ws server address, port and URL - webSocket.begin(ip , port, adress); - // try every 500 again if connection has failed - webSocket.setReconnectInterval(500); -} +// void Connectivity::websocketSetup(char* ip, uint16_t port, char* adress){ +// //ws server address, port and URL +// webSocket.begin(ip , port, adress); +// // try every 500 again if connection has failed +// webSocket.setReconnectInterval(500); +// } -void Connectivity::sendData(float roll, float pitch, float yaw){ - String message = "{\"Sensor\": 1, \"roll\":\"" + String(roll) + "\",\"pitch\":\"" + String(pitch) + "\",\"yaw\":\"" + String(yaw) + "\"}"; - webSocket.sendTXT(message); +// void Connectivity::sendData(float roll, float pitch, float yaw){ +// String message = "{\"Sensor\": 1, \"roll\":\"" + String(roll) + "\",\"pitch\":\"" + String(pitch) + "\",\"yaw\":\"" + String(yaw) + "\"}"; +// webSocket.sendTXT(message); +// } + +const char* getServerURL = "http://145.109.171.85:42069/get-ip"; +String ipAddress = ""; + +String Connectivity::fetchIPAddress() { + if (WiFi.status() == WL_CONNECTED) { + HTTPClient http; + WiFiClient client; + http.begin(client, getServerURL); + + int httpCode = http.GET(); + if (httpCode > 0) { + if (httpCode == HTTP_CODE_OK) { + ipAddress = http.getString(); + } + } else { + Serial.printf("GET request failed, error: %s\n", http.errorToString(httpCode).c_str()); + } + + http.end(); + } else { + Serial.println("WiFi not connected"); + } + return ipAddress; // Add this return statement } /** Send a POST request to a server with provided data */ int Connectivity::httpPost(const char *serverAddress, const char *serverSubPath, const unsigned short serverPort, const char *data, const size_t dataLength, const char *contentType) { - if ( wifi_client.connect(serverAddress, serverPort)) { + WiFiClient wifi_client; // Ensure WiFiClient is declared and initialized + if (wifi_client.connect(serverAddress, serverPort)) { wifi_client.printf("POST %s HTTP/1.1\r\n", serverSubPath); wifi_client.printf("Content-Type: %s\r\n", contentType); wifi_client.printf("Content-Length: %d\r\n", dataLength); @@ -35,4 +61,4 @@ int Connectivity::httpPost(const char *serverAddress, const char *serverSubPath, } return 1; -} \ No newline at end of file +} diff --git a/code/arduino/Movement-sensor-code/Connectivity.h b/code/arduino/Movement-sensor-code/Connectivity.h index 90742aa..6de0109 100644 --- a/code/arduino/Movement-sensor-code/Connectivity.h +++ b/code/arduino/Movement-sensor-code/Connectivity.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -18,12 +19,12 @@ public: void websocketSetup(char* ip, uint16_t port, char* adress); void sendData(float roll, float pitch, float yaw); int httpPost(const char *serverAddress, const char *serverSubPath, const unsigned short serverPort, const char *data, const size_t dataLength, const char *contentType); - + String fetchIPAddress(); private: ESP8266WiFiMulti wifi; WiFiClient wifi_client; - WebSocketsClient webSocket; + // WebSocketsClient webSocket; }; diff --git a/code/arduino/Movement-sensor-code/Movement-sensor-code.ino b/code/arduino/Movement-sensor-code/Movement-sensor-code.ino index 3bb3e92..061f12d 100644 --- a/code/arduino/Movement-sensor-code/Movement-sensor-code.ino +++ b/code/arduino/Movement-sensor-code/Movement-sensor-code.ino @@ -4,6 +4,7 @@ void setup() { //connect to internet and start sensor connectivity.connectWiFi(ssid, pass); sensorManager.sensorSetup(); + Serial.begin(9600); } unsigned long lastTime = 0; // will store the last time the code was run @@ -12,7 +13,6 @@ void loop() { SensorManager::eulerAngles eulerRotation = sensorManager.getEulerAngles(); // SensorManager::acceleration rotationAcceleration = sensorManager.getAcelleration(); - struct acceleration { float x = 9; float y = 9; @@ -34,7 +34,7 @@ struct acceleration { accelData.z, "data"); // %d = int, %f = floatation, %s = string - connectivity.httpPost("192.168.137.243", "/", 3445, buffer, strlen(buffer), "application/json"); + connectivity.httpPost(connectivity.fetchIPAddress(), "/", 3445, buffer, strlen(buffer), "application/json"); lastTime = currentTime; } }