2024-05-31 16:36:58 +02:00
43 changed files with 388 additions and 264 deletions

View File

@@ -8,23 +8,49 @@ void Connectivity::connectWiFi(char* ssid, char* pass){
}
}
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::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);
// 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.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 */
int Connectivity::httpPost(const char *serverAddress, const char *serverSubPath, const unsigned short serverPort,
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("Content-Type: %s\r\n", contentType);
wifi_client.printf("Content-Length: %d\r\n", dataLength);
@@ -35,4 +61,4 @@ int Connectivity::httpPost(const char *serverAddress, const char *serverSubPath,
}
return 1;
}
}

View File

@@ -5,6 +5,7 @@
#include <WebSocketsClient.h>
#include <ArduinoWiFiServer.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFiGeneric.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WiFiSTA.h>
@@ -18,12 +19,12 @@ public:
void websocketSetup(char* ip, uint16_t port, char* adress);
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);
String fetchIPAddress();
private:
ESP8266WiFiMulti wifi;
WiFiClient wifi_client;
WebSocketsClient webSocket;
// WebSocketsClient webSocket;
};

View File

@@ -4,6 +4,7 @@ void setup() {
//connect to internet and start sensor
connectivity.connectWiFi(ssid, pass);
sensorManager.sensorSetup();
Serial.begin(9600);
}
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::acceleration rotationAcceleration = sensorManager.getAcelleration();
struct acceleration {
float x = 9;
float y = 9;
@@ -34,7 +34,7 @@ struct acceleration {
accelData.z,
"data");
// %d = int, %f = floatation, %s = string
connectivity.httpPost(IP_ADDRESS, "/", 3445, buffer, strlen(buffer), "application/json");
connectivity.httpPost(connectivity.fetchIPAddress(), "/", 3445, buffer, strlen(buffer), "application/json");
lastTime = currentTime;
}
}

View File

@@ -1,33 +0,0 @@
#include <BLEDevice.h>
#include <BLEServer.h>
// Define the service UUID
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
// Define the characteristic UUID
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
void setup() {
// Create a BLE server
BLEServer *pServer = BLEDevice::createServer();
// Create a BLE service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE characteristic
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ);
// Set the characteristic value
pCharacteristic->setValue("Hello, Bluetooth!");
// Start the service
pService->start();
// Start advertising the service
pServer->getAdvertising()->start();
}
void loop() {
// Nothing to do here
}