-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.ino
89 lines (72 loc) · 2.2 KB
/
sensor.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
#include <Wire.h>
#include <MPU6050.h>
#include <WiFi.h>
MPU6050 accelgyro;
const char *ssid = "123456789";
const char *password = "123456789";
WiFiServer server(80);
int16_t ax, ay, az;
int16_t gx, gy, gz;
const int historySize = 5;
int16_t pre_gx[historySize];
int16_t pre_gy[historySize];
int16_t pre_gz[historySize];
int historyIndex = 0;
void setup() {
Serial.begin(115200);
Wire.begin();
delay(1000);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("WiFi connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
accelgyro.initialize();
if (accelgyro.testConnection()) {
Serial.println("MPU6050 connection successful.");
} else {
Serial.println("MPU6050 connection failed.");
}
for (int i = 0; i < historySize; i++) {
pre_gx[i] = 0;
pre_gy[i] = 0;
pre_gz[i] = 0;
}
}
void loop() {
WiFiClient client = server.available();
if (client) {
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
int16_t diff_gx = gx - pre_gx[historyIndex];
int16_t diff_gy = gy - pre_gy[historyIndex];
int16_t diff_gz = gz - pre_gz[historyIndex];
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<html><body>");
client.print("GX: ");
client.print(diff_gx);
client.print(", GY: ");
client.print(diff_gy);
client.print(", GZ: ");
client.println(diff_gz);
client.println("</body></html>");
pre_gx[historyIndex] = gx;
pre_gy[historyIndex] = gy;
pre_gz[historyIndex] = gz;
historyIndex = (historyIndex + 1) % historySize;
Serial.print("GX: ");
Serial.print(diff_gx);
Serial.print(", GY: ");
Serial.print(diff_gy);
Serial.print(", GZ: ");
Serial.println(diff_gz);
delay(500); // 보드 및 통신 과부화 방지 추후에 고치기 -> 최종적으로 끝나면
}
}