-
Notifications
You must be signed in to change notification settings - Fork 1
/
sleep.ino
196 lines (159 loc) · 5.45 KB
/
sleep.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
#include <Wire.h>
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#define USE_SERIAL Serial
ESP8266WiFiMulti WiFiMulti;
#define DEVICE (0x53) //ADXL345 device address
#define TO_READ (6) //num of bytes we are going to read each time (two bytes for each axis)
byte buff[TO_READ] ; //6 bytes buffer for saving data read from the device
char str[512]; //string buffer to transform data before sending it to the serial port
int regAddress = 0x32; //first axis-acceleration-data register on the ADXL345
int x, y, z; //three axis acceleration data
int xBias, yBias, zBias;
const int BUTTON_PIN = 10;
const int LED_PIN = 9;
bool deviceOn = false;
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 1000;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
Wire.begin(); // join i2c bus (address optional for master)
//Turning on the ADXL345
writeTo(DEVICE, 0x2D, 0);
writeTo(DEVICE, 0x2D, 16);
writeTo(DEVICE, 0x2D, 8);
writeTo(DEVICE, 0x31, 11);
USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println();
WiFiMulti.addAP("AndroidAP", "svfw7168");
xBias = 0;
yBias = 0;
zBias = 0;
for (int i = 0; i < 30; i++) {
calibrateAccelerometer();
}
xBias /= 30;
yBias /= 30;
zBias /= 30;
USE_SERIAL.println("DEVICE READY");
}
void loop() {
if (digitalRead(BUTTON_PIN) == HIGH) {
while (digitalRead(BUTTON_PIN) == HIGH) {
}
deviceOn = !deviceOn;
if (deviceOn) {
USE_SERIAL.println("START");
httpOpenLink("http://192.168.43.82/startSleepRound.php");
} else {
USE_SERIAL.println("END OF ROUND");
USE_SERIAL.println("");
}
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if (deviceOn) {
readAndSendData();
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}
}
void readAndSendData() {
readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
//each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
//thus we are converting both bytes in to one int
x = (((int)buff[1]) << 8) | buff[0];
y = (((int)buff[3]) << 8) | buff[2];
z = (((int)buff[5]) << 8) | buff[4];
short xx = (short)x;
short yy = (short)y;
short zz = (short)z;
String link1 = "http://192.168.43.82/sqlAddData.php?ax=";
String link2 = "&ay=";
String link3 = "&az=";
String ax = (String) (xx - xBias);
String ay = (String) (yy - yBias);
String az = (String) (zz - zBias);
//we send the x y z values as a string to the serial port
USE_SERIAL.print("The acceleration info of x, y, z are:");
USE_SERIAL.print(ax + " " + ay + " " + az);
USE_SERIAL.write(10);
//Roll & Pitch calculate
RP_calculate();
USE_SERIAL.println("");
httpOpenLink(link1 + ax + link2 + ay + link3 + az);
}
void writeTo(int device, byte address, byte val) {
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); //end transmission
}
//reads num bytes starting from address register on device in to buff array
void readFrom(int device, byte address, int num, byte buff[]) {
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); //sends address to read from
Wire.endTransmission(); //end transmission
Wire.beginTransmission(device); //start transmission to device
Wire.requestFrom(device, num); // request 6 bytes from device
int i = 0;
while (Wire.available()) //device may send less than requested (abnormal)
{
buff[i] = Wire.read(); // receive a byte
i++;
}
Wire.endTransmission(); //end transmission
}
//calculate the Roll&Pitch
void RP_calculate() {
double x_Buff = float(x);
double y_Buff = float(y);
double z_Buff = float(z);
}
void calibrateAccelerometer(void) {
//Take a number of readings and average them
//to calculate any bias the accelerometer may have.
readFrom(DEVICE, regAddress, TO_READ, buff);
x = (((int)buff[1]) << 8) | buff[0];
y = (((int)buff[3]) << 8) | buff[2];
z = (((int)buff[5]) << 8) | buff[4];
short xx = (short)x;
short yy = (short)y;
short zz = (short)z;
xBias = xx + xBias;
yBias = yy + yBias;
zBias = zz + zBias;
}
void httpOpenLink(String url) {
if (WiFiMulti.run() == WL_CONNECTED) {
HTTPClient http;
// USE_SERIAL.print("[HTTP] begin " + url + "\n");
http.begin(url); //HTTP
// USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
// USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
int index_start = payload.indexOf("#");
int index_end = payload.lastIndexOf("#");
USE_SERIAL.println(payload.substring(index_start+1, index_end));
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
}