diff --git a/code/arduino/Movement-sensor-code/Connectivity.cpp b/code/arduino/Movement-sensor-code/Connectivity.cpp index 6e96ca3..bfc3c04 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.92.8.132:443/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; } } diff --git a/code/arduino/Movement-sensor-code/headerFIle.h b/code/arduino/Movement-sensor-code/headerFIle.h index b065b3e..8e3480e 100644 --- a/code/arduino/Movement-sensor-code/headerFIle.h +++ b/code/arduino/Movement-sensor-code/headerFIle.h @@ -12,4 +12,6 @@ WebSocketsClient webSocket; #define pass "12345678" #define BUFFER_SIZE 1024 #define DEVICE_ID 1 +#define IP_ADDRESS "192.168.137.12" + char *buffer = (char *)malloc(sizeof(char) * BUFFER_SIZE); diff --git a/code/server/ipSender/index.js b/code/server/ipSender/index.js new file mode 100644 index 0000000..dc68c2d --- /dev/null +++ b/code/server/ipSender/index.js @@ -0,0 +1,22 @@ +const express = require('express'); +const app = express(); + +app.use(express.json()); // for parsing application/json + +let ipAddress = ''; // to store the received IP address + +// endpoint to receive an IP address from an external source +app.post('/set-ip', (req, res) => { + ipAddress = req.body.ip; + console.log('IP address received:', ipAddress); +}); + +// endpoint for the ESP32 to fetch the IP address +app.get('/get-ip', (req, res) => { + res.json({ ip: ipAddress }); + console.log('IP address sent to ESP32'); +}); + +app.listen(42069, () => { + console.log('Server is running on port 42069'); +}); \ No newline at end of file diff --git a/code/server/server.js b/code/server/server.js index 886846e..c31eded 100644 --- a/code/server/server.js +++ b/code/server/server.js @@ -21,6 +21,20 @@ const pool = mariadb.createPool(databaseCredentials); // Register incoming HTTP request handlers require('./incoming_request_handlers')(app, pool); +let ipAddress = ''; // to store the received IP address + +// endpoint to receive an IP address from an external source +app.post('/set-ip', (req, res) => { + ipAddress = req.body.ip; + console.log('IP address received:', ipAddress); +}); + +// endpoint for the ESP32 to fetch the IP address +app.get('/get-ip', (req, res) => { + res.json({ ip: ipAddress }); + console.log('IP address sent to ESP32'); +}); + // Start server app.listen(serverPort, () => { console.log(`Server running on port ${serverPort}`); diff --git a/code/src/Fitbot/app/src/main/AndroidManifest.xml b/code/src/Fitbot/app/src/main/AndroidManifest.xml index f6c0983..32bdaaf 100644 --- a/code/src/Fitbot/app/src/main/AndroidManifest.xml +++ b/code/src/Fitbot/app/src/main/AndroidManifest.xml @@ -10,13 +10,15 @@ + + + + - - () { + @Override + protected Void doInBackground(Void... params) { + String ipAddress = getIPAddress(context); + String jsonInputString = "{\"ip\":\"" + + ipAddress + + "\"}"; + + byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8); + HttpURLConnection conn = null; + try { + URL url = new URL("http://145.92.8.132:443/set-ip"); // Replace with your Node server URL + conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("POST"); + conn.setRequestProperty("Content-Type", "application/json"); + conn.setDoOutput(true); + + OutputStream os = conn.getOutputStream(); + os.write(input, 0, input.length); + os.close(); + + int responseCode = conn.getResponseCode(); + Log.i("NetworkUtils", "Response Code: " + responseCode); + } catch (Exception e) { + Log.e("NetworkUtils", "Error sending IP address", e); + } finally { + if (conn != null) { + conn.disconnect(); + } + } + return null; + } + }.execute(); + } + + + private static String getIPAddress(Context context) { + WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); + int ipAddress = wifiManager.getConnectionInfo().getIpAddress(); + + // Convert little-endian to big-endian if needed + if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) { + ipAddress = Integer.reverseBytes(ipAddress); + } + + byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray(); + + String ip = ""; + try { + ip = InetAddress.getByAddress(ipByteArray).getHostAddress(); + } catch (UnknownHostException ex) { + Log.e("WIFIIP", "Unable to get host address."); + } + + return ip; + } +} \ No newline at end of file diff --git a/code/src/Fitbot/app/src/main/res/drawable/ic_baseline_info_24.xml b/code/src/Fitbot/app/src/main/res/drawable/ic_baseline_info_24.xml new file mode 100644 index 0000000..17255b7 --- /dev/null +++ b/code/src/Fitbot/app/src/main/res/drawable/ic_baseline_info_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/code/src/Fitbot/app/src/main/res/drawable/ic_baseline_info_40.xml b/code/src/Fitbot/app/src/main/res/drawable/ic_baseline_info_40.xml new file mode 100644 index 0000000..df12462 --- /dev/null +++ b/code/src/Fitbot/app/src/main/res/drawable/ic_baseline_info_40.xml @@ -0,0 +1,5 @@ + + + diff --git a/code/src/Fitbot/app/src/main/res/drawable/ic_baseline_info_48.xml b/code/src/Fitbot/app/src/main/res/drawable/ic_baseline_info_48.xml new file mode 100644 index 0000000..15875d1 --- /dev/null +++ b/code/src/Fitbot/app/src/main/res/drawable/ic_baseline_info_48.xml @@ -0,0 +1,5 @@ + + + diff --git a/code/src/Fitbot/app/src/main/res/drawable/red_button_gradient.xml b/code/src/Fitbot/app/src/main/res/drawable/red_button_gradient.xml index 6983a91..a087a43 100644 --- a/code/src/Fitbot/app/src/main/res/drawable/red_button_gradient.xml +++ b/code/src/Fitbot/app/src/main/res/drawable/red_button_gradient.xml @@ -2,7 +2,7 @@ + android:layout_gravity="center"> + +