Fixed it so it only pulls the ip once
This commit is contained in:
@@ -21,9 +21,9 @@ void Connectivity::connectWiFi(char* ssid, char* pass){
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
const char* getServerURL = "http://145.92.8.132:443/get-ip";
|
const char* getServerURL = "http://145.92.8.132:443/get-ip";
|
||||||
String ipAddress = "";
|
char* ipAddress = "";
|
||||||
|
|
||||||
String Connectivity::fetchIPAddress() {
|
const char* Connectivity::fetchIPAddress() {
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
HTTPClient http;
|
HTTPClient http;
|
||||||
WiFiClient client;
|
WiFiClient client;
|
||||||
@@ -32,7 +32,7 @@ String Connectivity::fetchIPAddress() {
|
|||||||
int httpCode = http.GET();
|
int httpCode = http.GET();
|
||||||
if (httpCode > 0) {
|
if (httpCode > 0) {
|
||||||
if (httpCode == HTTP_CODE_OK) {
|
if (httpCode == HTTP_CODE_OK) {
|
||||||
ipAddress = http.getString();
|
ipAddress = strdup(http.getString().c_str());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Serial.printf("GET request failed, error: %s\n", http.errorToString(httpCode).c_str());
|
Serial.printf("GET request failed, error: %s\n", http.errorToString(httpCode).c_str());
|
||||||
|
@@ -19,7 +19,7 @@ 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();
|
const char* fetchIPAddress();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ESP8266WiFiMulti wifi;
|
ESP8266WiFiMulti wifi;
|
||||||
|
@@ -7,8 +7,6 @@ void setup() {
|
|||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long lastTime = 0; // will store the last time the code was run
|
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
SensorManager::eulerAngles eulerRotation = sensorManager.getEulerAngles();
|
SensorManager::eulerAngles eulerRotation = sensorManager.getEulerAngles();
|
||||||
// SensorManager::acceleration rotationAcceleration = sensorManager.getAcelleration();
|
// SensorManager::acceleration rotationAcceleration = sensorManager.getAcelleration();
|
||||||
@@ -19,6 +17,11 @@ struct acceleration {
|
|||||||
float z = 9;
|
float z = 9;
|
||||||
} accelData;
|
} accelData;
|
||||||
|
|
||||||
|
if (!ipAquired) {
|
||||||
|
serverIp = connectivity.fetchIPAddress();
|
||||||
|
ipAquired = true;
|
||||||
|
}
|
||||||
|
unsigned long lastTime = 0; // will store the last time the code was run
|
||||||
unsigned long currentTime = millis();
|
unsigned long currentTime = millis();
|
||||||
if (currentTime - lastTime >= 100) { // 100 ms has passed
|
if (currentTime - lastTime >= 100) { // 100 ms has passed
|
||||||
memset(buffer, 0, BUFFER_SIZE);
|
memset(buffer, 0, BUFFER_SIZE);
|
||||||
@@ -34,7 +37,7 @@ struct acceleration {
|
|||||||
accelData.z,
|
accelData.z,
|
||||||
"data");
|
"data");
|
||||||
// %d = int, %f = floatation, %s = string
|
// %d = int, %f = floatation, %s = string
|
||||||
connectivity.httpPost(connectivity.fetchIPAddress(), "/", 3445, buffer, strlen(buffer), "application/json");
|
connectivity.httpPost(serverIp, "/", 3445, buffer, strlen(buffer), "application/json");
|
||||||
lastTime = currentTime;
|
lastTime = currentTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -15,3 +15,5 @@ WebSocketsClient webSocket;
|
|||||||
#define IP_ADDRESS "192.168.137.12"
|
#define IP_ADDRESS "192.168.137.12"
|
||||||
|
|
||||||
char *buffer = (char *)malloc(sizeof(char) * BUFFER_SIZE);
|
char *buffer = (char *)malloc(sizeof(char) * BUFFER_SIZE);
|
||||||
|
const char* serverIp = NULL; // Declare serverIp here
|
||||||
|
bool ipAquired = false;
|
Reference in New Issue
Block a user