chore: Update Connectivity class with fetchIPAddress method

This commit is contained in:
Niels Gras
2024-05-31 16:32:06 +02:00
parent 3db278f92c
commit b9b4a9ca9b
3 changed files with 42 additions and 15 deletions

View File

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

View File

@@ -5,6 +5,7 @@
#include <WebSocketsClient.h>
#include <ArduinoWiFiServer.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFiGeneric.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WiFiSTA.h>
@@ -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;
};

View File

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