Fix automatic ip adress esp

This commit is contained in:
2024-06-04 14:40:06 +02:00
parent 7539892131
commit 8cd2b00eb0
5 changed files with 27 additions and 14 deletions

View File

@@ -33,8 +33,12 @@ const char* 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) {
// If successful (code 200), read the response body and store the IP address // If successful (code 200), read the response body and parse the IP address
ipAddress = strdup(http.getString().c_str()); String response = http.getString();
StaticJsonDocument<200> doc;
deserializeJson(doc, response);
const char* ip = doc["ip"]; // Extract the IP address
ipAddress = strdup(ip);
} }
} 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());

View File

@@ -11,6 +11,8 @@
#include <ESP8266WiFiSTA.h> #include <ESP8266WiFiSTA.h>
#include <WiFiClient.h> #include <WiFiClient.h>
#include <WiFiClientSecure.h> #include <WiFiClientSecure.h>
#include <ArduinoJson.h>
class Connectivity { class Connectivity {

View File

@@ -4,11 +4,12 @@ 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); Serial.begin(115200);
Serial.println("startup");
} }
void loop() { void loop() {
SensorManager::eulerAngles eulerRotation = sensorManager.getEulerAngles(); SensorManager::RotationQuaternions Rotation = sensorManager.getQuaternions();
// SensorManager::acceleration rotationAcceleration = sensorManager.getAcelleration(); // SensorManager::acceleration rotationAcceleration = sensorManager.getAcelleration();
struct acceleration { struct acceleration {
@@ -18,9 +19,10 @@ struct acceleration {
} accelData; } accelData;
if (!ipAquired) { if (!ipAquired) {
serverIp = connectivity.fetchIPAddress(); serverIp = connectivity.fetchIPAddress(); // Assign the value here
ipAquired = true; ipAquired = true;
} }
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
unsigned long currentTime = millis(); unsigned long currentTime = millis();
if (currentTime - lastTime >= 100) { // 100 ms has passed if (currentTime - lastTime >= 100) { // 100 ms has passed
@@ -29,15 +31,17 @@ struct acceleration {
buffer, buffer,
"{\"deviceId\": %d, \"rotationX\": %f, \"rotationY\": %f, \"rotationZ\": %f, \"accelerationX\": %f, \"accelerationY\": %f, \"accelerationZ\": %f, \"type\": %s}", "{\"deviceId\": %d, \"rotationX\": %f, \"rotationY\": %f, \"rotationZ\": %f, \"accelerationX\": %f, \"accelerationY\": %f, \"accelerationZ\": %f, \"type\": %s}",
DEVICE_ID, DEVICE_ID,
eulerRotation.roll, Rotation.i,
eulerRotation.pitch, Rotation.j,
eulerRotation.yaw, Rotation.k,
accelData.x, Rotation.w,
accelData.y, accelData.y,
accelData.z, accelData.z,
"data"); "data");
// %d = int, %f = floatation, %s = string // %d = int, %f = floatation, %s = string
connectivity.httpPost("192.168.137.30", "/", 3445, buffer, strlen(buffer), "application/json"); connectivity.httpPost(serverIp, "/", 3445, buffer, strlen(buffer), "application/json");
Serial.println(serverIp);
Serial.println(buffer);
lastTime = currentTime; lastTime = currentTime;
} }
} }

View File

@@ -22,14 +22,17 @@ public:
eulerAngles getEulerAngles(); eulerAngles getEulerAngles();
acceleration getAcelleration(); acceleration getAcelleration();
bool sensorTap(); bool sensorTap();
private:
struct RotationQuaternions { struct RotationQuaternions {
float i; float i;
float j; float j;
float k; float k;
float w; float w;
}; };
RotationQuaternions getQuaternions(); RotationQuaternions getQuaternions();
private:
BNO080 myIMU; BNO080 myIMU;
}; };

View File

@@ -8,8 +8,8 @@ Connectivity connectivity;
WebSocketsClient webSocket; WebSocketsClient webSocket;
#define USE_SERIAL Serial #define USE_SERIAL Serial
#define ssid "1235678i" #define ssid "msi 5556"
#define pass "12345678" #define pass "abc12345"
#define BUFFER_SIZE 1024 #define BUFFER_SIZE 1024
#define DEVICE_ID 1 #define DEVICE_ID 1
#define IP_ADDRESS "192.168.137.12" #define IP_ADDRESS "192.168.137.12"