#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 that will hold the server's IP address /** Fetch the IP address of pepper from the server */ const char* Connectivity::fetchIPAddress() { char* ipAddress = NULL; // Declare ipAddress as a char* 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) { // If successful (code 200), read the response body and parse the IP address String response = http.getString(); StaticJsonDocument<200> doc; deserializeJson(doc, response); const char* ip = doc["ip"]; // Extract the IP address ipAddress = strdup(ip); } } else { Serial.printf("GET request failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } else { Serial.println("WiFi not connected"); } return ipAddress; } /** 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; }