-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsleep_iot.ino
220 lines (185 loc) · 6.54 KB
/
sleep_iot.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
217
218
219
220
#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 LED_PIN = 9;
const int BUTTON_PIN = 10;
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();
USE_SERIAL.println();
USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
WiFiMulti.addAP("McDonan", "21091996");
xBias = 0;
yBias=0;
zBias=0;
for(int i = 0; i<30; i++){
calibrateAccelerometer();
}
xBias /= 30;
yBias /= 30;
zBias /= 30;
if((WiFiMulti.run() == WL_CONNECTED)) {
HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
//http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
http.begin("http://172.20.10.3/startSleepRound.php"); //HTTP
USE_SERIAL.print("[HTTP] GET startSleepRound...\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();
USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... startSleepRound failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
}
void loop() {
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
for(int i = 0; i < 6; i++){
USE_SERIAL.print(buff[i],BIN);
USE_SERIAL.print(" ");
}
USE_SERIAL.println("");
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;
//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:");
sprintf(str, "%d %d %d", x, y, z);
USE_SERIAL.print(str);
USE_SERIAL.write(10);
//Roll & Pitch calculate
RP_calculate();
USE_SERIAL.println("");
USE_SERIAL.print(xx);
USE_SERIAL.print(" ");
USE_SERIAL.print(yy);
USE_SERIAL.print(" ");
USE_SERIAL.print(zz);
//It appears that delay is needed in order not to clog the port
USE_SERIAL.print(" ");
USE_SERIAL.print(xx,BIN);
USE_SERIAL.print(" ");
USE_SERIAL.print(yy,BIN);
USE_SERIAL.print(" ");
USE_SERIAL.print(zz,BIN);
USE_SERIAL.println("");
USE_SERIAL.print("two's complements are : ");
sprintf(str, "%d %d %d", xx, yy, zz);
USE_SERIAL.print(str);
USE_SERIAL.write(10);
//Roll & Pitch calculate
RP_calculate();
USE_SERIAL.println("");
delay(1000);
// wait for WiFi connection
String link1 = "http://172.20.10.3/sqlAddData.php?ax=";
String link2 = "&ay=";
String link3 = "&az=";
String ax = (String) (xx-xBias);
String ay = (String) (yy-yBias);
String az = (String) (zz-zBias);
if((WiFiMulti.run() == WL_CONNECTED)) {
HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
//http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
http.begin(link1+ax+link2+ay+link3+az); //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();
USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
delay(1000);
}
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() {
//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;
}