-
Notifications
You must be signed in to change notification settings - Fork 4
/
sketch2.ino
217 lines (172 loc) · 4.98 KB
/
sketch2.ino
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <WiFi.h>
#include <WebServer.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
// Replace with your network credentials
//const char* ssid = "STARLINK v2";
const char* ssid = "get of my LAN";
const char* password = "P@$$w0rD";
//onboard led
const int onboardLED = 2;
// Variables to store acceleration and gyroscope data
float ax, ay, az, gx, gy, gz;
WebServer server(80);
Adafruit_MPU6050 mpu;
int vSpeed = 50;
int i = 0;
void setup() {
// Start serial communication
Serial.begin(115200);
// Connect to Wi-Fi network
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Start the web server
server.on("/", handleRoot);
server.begin();
Serial.println("Web server started");
Serial.println("Adafruit MPU6050 test!");
// Try to initialize!
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
Serial.print("Accelerometer range set to: ");
switch (mpu.getAccelerometerRange()) {
case MPU6050_RANGE_2_G:
Serial.println("+-2G");
break;
case MPU6050_RANGE_4_G:
Serial.println("+-4G");
break;
case MPU6050_RANGE_8_G:
Serial.println("+-8G");
break;
case MPU6050_RANGE_16_G:
Serial.println("+-16G");
break;
}
mpu.setGyroRange(MPU6050_RANGE_500_DEG);
Serial.print("Gyro range set to: ");
switch (mpu.getGyroRange()) {
case MPU6050_RANGE_250_DEG:
Serial.println("+- 250 deg/s");
break;
case MPU6050_RANGE_500_DEG:
Serial.println("+- 500 deg/s");
break;
case MPU6050_RANGE_1000_DEG:
Serial.println("+- 1000 deg/s");
break;
case MPU6050_RANGE_2000_DEG:
Serial.println("+- 2000 deg/s");
break;
}
mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);
Serial.print("Filter bandwidth set to: ");
switch (mpu.getFilterBandwidth()) {
case MPU6050_BAND_260_HZ:
Serial.println("260 Hz");
break;
case MPU6050_BAND_184_HZ:
Serial.println("184 Hz");
break;
case MPU6050_BAND_94_HZ:
Serial.println("94 Hz");
break;
case MPU6050_BAND_44_HZ:
Serial.println("44 Hz");
break;
case MPU6050_BAND_21_HZ:
Serial.println("21 Hz");
break;
case MPU6050_BAND_10_HZ:
Serial.println("10 Hz");
break;
case MPU6050_BAND_5_HZ:
Serial.println("5 Hz");
break;
}
}
void loop() {
// Check if Wi-Fi connection is still active
checkWifiConnection();
// Read MPU6050 data
readMPU6050();
// Serial.print("ax: "); Serial.print(ax);
// Serial.print(" ay: "); Serial.print(ay);
// Serial.print(" az: "); Serial.print(az);
// Serial.print(" gx: "); Serial.print(gx);
// Serial.print(" gy: "); Serial.print(gy);
// Serial.print(" gz: "); Serial.println(gz);
// Handle incoming client requests
server.handleClient();
}
// Handler for the root URL
void handleRoot() {
String html = "<html><head><meta http-equiv='refresh' content='0.2'></head><body><h1 style='text-align:center;'></h1>";
html += "<table style='margin:auto;'>";
html += "<tr><td colspan='3' style='text-align:center;'><b>Accelerometer Values: <span id='accelerometerValues'>[" + String(ax) + "," + String(ay) + "," + String(az) + "]</span></b></td></tr>";
html += "<tr><td colspan='3' style='text-align:center;'><b>Gyrometer Values: <span id='gyrometerValues'>[" + String(gx) + "," + String(gy) + "," + String(gz) + "]</span></td></b></tr>";
html += "</table>";
html += "</body></html>";
server.send(200, "text/html", html);
}
void BlinkLed() {
digitalWrite(onboardLED, HIGH);
delay(75);
digitalWrite(onboardLED, LOW);
}
void checkWifiConnection() {
if (WiFi.status() != WL_CONNECTED) {
for (int i = 0; i < 3; i++) {
digitalWrite(onboardLED, HIGH); // turn on the LED
delay(50);
digitalWrite(onboardLED, LOW); // turn off the LED
delay(50);
}
digitalWrite(onboardLED, LOW); // turn off the LED
delay(1200);
} else {
digitalWrite(onboardLED, LOW); // turn off the LED
}
}
// Function to read accelerometer and gyroscope data from MPU6050
void readMPU6050() {
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
// Print the accelerometer data
Serial.print("Acceleration (m/s^2): ");
Serial.print(a.acceleration.x);
Serial.print(", ");
Serial.print(a.acceleration.y);
Serial.print(", ");
Serial.print(a.acceleration.z);
// Print the gyroscope data
Serial.print(" | Gyroscope (rad/s): ");
Serial.print(g.gyro.x);
Serial.print(", ");
Serial.print(g.gyro.y);
Serial.print(", ");
Serial.print(g.gyro.z);
// Update global variables with sensor data
ax = a.acceleration.x;
ay = a.acceleration.y;
az = a.acceleration.z;
gx = g.gyro.x;
gy = g.gyro.y;
gz = g.gyro.z;
Serial.println();
}