Files
J1B4-Fitbot/code/arduino/Movement-sensor-code/Connectivity.cpp
2024-06-03 12:56:22 +02:00

65 lines
2.0 KiB
C++

#include "Connectivity.h"
void Connectivity::connectWiFi(char* ssid, char* pass){
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
}
// 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);
// }
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)
{
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);
wifi_client.printf("Host: %s\r\n\n", serverAddress);
wifi_client.println(data);
wifi_client.stop();
return 0;
}
return 1;
}