chore: Update Connectivity class with fetchIPAddress method
This commit is contained in:
@@ -8,23 +8,49 @@ void Connectivity::connectWiFi(char* ssid, char* pass){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connectivity::websocketSetup(char* ip, uint16_t port, char* adress){
|
// void Connectivity::websocketSetup(char* ip, uint16_t port, char* adress){
|
||||||
//ws server address, port and URL
|
// //ws server address, port and URL
|
||||||
webSocket.begin(ip , port, adress);
|
// webSocket.begin(ip , port, adress);
|
||||||
// try every 500 again if connection has failed
|
// // try every 500 again if connection has failed
|
||||||
webSocket.setReconnectInterval(500);
|
// webSocket.setReconnectInterval(500);
|
||||||
}
|
// }
|
||||||
|
|
||||||
void Connectivity::sendData(float roll, float pitch, float yaw){
|
// void Connectivity::sendData(float roll, float pitch, float yaw){
|
||||||
String message = "{\"Sensor\": 1, \"roll\":\"" + String(roll) + "\",\"pitch\":\"" + String(pitch) + "\",\"yaw\":\"" + String(yaw) + "\"}";
|
// String message = "{\"Sensor\": 1, \"roll\":\"" + String(roll) + "\",\"pitch\":\"" + String(pitch) + "\",\"yaw\":\"" + String(yaw) + "\"}";
|
||||||
webSocket.sendTXT(message);
|
// 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 */
|
/** Send a POST request to a server with provided data */
|
||||||
int Connectivity::httpPost(const char *serverAddress, const char *serverSubPath, const unsigned short serverPort,
|
int Connectivity::httpPost(const char *serverAddress, const char *serverSubPath, const unsigned short serverPort,
|
||||||
const char *data, const size_t dataLength, const char *contentType)
|
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("POST %s HTTP/1.1\r\n", serverSubPath);
|
||||||
wifi_client.printf("Content-Type: %s\r\n", contentType);
|
wifi_client.printf("Content-Type: %s\r\n", contentType);
|
||||||
wifi_client.printf("Content-Length: %d\r\n", dataLength);
|
wifi_client.printf("Content-Length: %d\r\n", dataLength);
|
||||||
|
@@ -5,6 +5,7 @@
|
|||||||
#include <WebSocketsClient.h>
|
#include <WebSocketsClient.h>
|
||||||
#include <ArduinoWiFiServer.h>
|
#include <ArduinoWiFiServer.h>
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ESP8266HTTPClient.h>
|
||||||
#include <ESP8266WiFiGeneric.h>
|
#include <ESP8266WiFiGeneric.h>
|
||||||
#include <ESP8266WiFiMulti.h>
|
#include <ESP8266WiFiMulti.h>
|
||||||
#include <ESP8266WiFiSTA.h>
|
#include <ESP8266WiFiSTA.h>
|
||||||
@@ -18,12 +19,12 @@ public:
|
|||||||
void websocketSetup(char* ip, uint16_t port, char* adress);
|
void websocketSetup(char* ip, uint16_t port, char* adress);
|
||||||
void sendData(float roll, float pitch, float yaw);
|
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);
|
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:
|
private:
|
||||||
ESP8266WiFiMulti wifi;
|
ESP8266WiFiMulti wifi;
|
||||||
WiFiClient wifi_client;
|
WiFiClient wifi_client;
|
||||||
WebSocketsClient webSocket;
|
// WebSocketsClient webSocket;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -4,6 +4,7 @@ void setup() {
|
|||||||
//connect to internet and start sensor
|
//connect to internet and start sensor
|
||||||
connectivity.connectWiFi(ssid, pass);
|
connectivity.connectWiFi(ssid, pass);
|
||||||
sensorManager.sensorSetup();
|
sensorManager.sensorSetup();
|
||||||
|
Serial.begin(9600);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long lastTime = 0; // will store the last time the code was run
|
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::eulerAngles eulerRotation = sensorManager.getEulerAngles();
|
||||||
// SensorManager::acceleration rotationAcceleration = sensorManager.getAcelleration();
|
// SensorManager::acceleration rotationAcceleration = sensorManager.getAcelleration();
|
||||||
|
|
||||||
|
|
||||||
struct acceleration {
|
struct acceleration {
|
||||||
float x = 9;
|
float x = 9;
|
||||||
float y = 9;
|
float y = 9;
|
||||||
@@ -34,7 +34,7 @@ struct acceleration {
|
|||||||
accelData.z,
|
accelData.z,
|
||||||
"data");
|
"data");
|
||||||
// %d = int, %f = floatation, %s = string
|
// %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;
|
lastTime = currentTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user