-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathesp32
75 lines (63 loc) · 1.77 KB
/
esp32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <Wire.h>
#include <MPU6050.h>
#include <WiFi.h>
MPU6050 mpu;
const char *ssid = "YourWiFiNetwork";
const char *password = "YourWiFiPassword";
const int port = 1234;
WiFiServer server(port);
WiFiClient client;
const float dt = 0.01; // Time step in seconds (adjust as needed)
float velocity[3] = {0}; // Velocity vector (x, y, z)
void setup() {
Serial.begin(115200);
delay(1000);
// Connect to WiFi network
Serial.println("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
server.begin();
}
void loop() {
client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char command = client.read();
if (command == 'START') {
// Start measuring acceleration
measureAcceleration();
// Send speed data back to the client
client.println(getSpeed());
}
}
}
client.stop();
Serial.println("Client disconnected");
}
}
void measureAcceleration() {
mpu.initialize();
delay(1000); // Allow sensor to stabilize
int16_t ax, ay, az;
while (true) {
mpu.getAcceleration(&ax, &ay, &az);
float accelX = ax / 16384.0;
float accelY = ay / 16384.0;
float accelZ = az / 16384.0;
// Integrate acceleration to obtain velocity
velocity[0] += accelX * dt;
velocity[1] += accelY * dt;
velocity[2] += accelZ * dt;
// Wait for a short duration (adjust as needed)
delay(10);
}
}
String getSpeed() {
float speed = sqrt(velocity[0] * velocity[0] + velocity[1] * velocity[1] + velocity[2] * velocity[2]);
return String(speed);
}