-
Notifications
You must be signed in to change notification settings - Fork 159
/
BeaconSensor.cpp
318 lines (268 loc) · 9.54 KB
/
BeaconSensor.cpp
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
Queue.cpp
2012 Copyright (c) Seeed Technology Inc. All right reserved.
Author:Loovee
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "BeaconSensor.h"
#include "BeaconSensorDfs.h"
#include <Wire.h>
/*********************************************************************************************************
** Function name: init
** Descriptions: init sensor id
*********************************************************************************************************/
void BeaconSensor::init(unsigned char id)
{
idSensor = id;
init_io();
}
/*********************************************************************************************************
** Function name: init_io
** Descriptions: init io
*********************************************************************************************************/
void BeaconSensor::init_io()
{
// if idSensor <= SENSORANALOGTOP , then it is an analog(IO) sensor,
// need not to init
if(idSensor == TEMPANDHUMI_TEMP) // temperature&humidity sensor
{
pinMode(IOSENSOR0, OUTPUT);
}
else if(idSensor == BAROMETERSENSOR)
{
Wire.begin();
}
// some others : add code here
}
/*********************************************************************************************************
** Function name: getSensorId
** Descriptions: get sensor id
*********************************************************************************************************/
unsigned char BeaconSensor::getSensorId()
{
return idSensor;
}
/*********************************************************************************************************
** Function name: getSensor
** Descriptions: get sensor data: dta[0]: datalen dta[1]-dta[n], data ;return : dta[0]+1
*********************************************************************************************************/
unsigned char BeaconSensor::getSensor(unsigned char *dta)
{
if(idSensor == TEMPANDHUMI_TEMP) // temperature&humidity sensor
{
unsigned char dtaTmp[5];
if(getTempHumiDta(dtaTmp)) // get bad data
{
dta[0] = 1;
dta[1] = dtaTmp[1];
return 2;
}
else
{
return 0;
}
}
else if(idSensor == TEMPERATURESENSOR) // grove temperature
{
int a=analogRead(0);
float resistance=(float)(1023-a)*10000/a; //get the resistance of the sensor;
dta[1] = 1/(log(resistance/10000)/3975+1/298.15)-273.15;//convert to temperature via datasheet ;
dta[0] = 1;
return 2;
}
else if(idSensor == BAROMETERSENSOR)
{
bmp085GetTempPres(dta);
return 6;
}
else if(idSensor <= SENSORANALOGTOP) // analog & IO
{
unsigned int tmp = 0;
for(int i = 0;i<10; i++)
{
tmp += analogRead(IOSENSOR0);
}
tmp /= 10;
dta[0] = 2;
dta[1] = tmp>>8;
dta[2] = tmp;
return 3;
}
return 0;
}
/*********************************************************************************************************
** Function name: getTempHumiDtaDo1
** Descriptions: use by getTempHumiDta
*********************************************************************************************************/
unsigned char BeaconSensor::getTempHumiDtaDo1()
{
unsigned char i = 0;
unsigned char result=0;
for(i=0; i< 8; i++)
{
while(!digitalRead(IOSENSOR0)); // wait for 50us
delayMicroseconds(30);
if(digitalRead(IOSENSOR0))
result |=(1<<(7-i));
while(digitalRead(IOSENSOR0)); // wait '1' finish
}
return result;
}
/*********************************************************************************************************
** Function name: getTempHumiDta
** Descriptions: get temperature and humi, return 1:ok, return 0, err
*********************************************************************************************************/
unsigned char BeaconSensor::getTempHumiDta(unsigned char *temphumi)
{
unsigned char dht11_dat[5];
unsigned char i;
digitalWrite(IOSENSOR0,LOW);
delay(18);
digitalWrite(IOSENSOR0,HIGH);
delayMicroseconds(40);
pinMode(IOSENSOR0,INPUT);
while(digitalRead(IOSENSOR0)){
return 0;
}
delayMicroseconds(80);
while(!digitalRead(IOSENSOR0)){
return 0;
}
delayMicroseconds(80);
// now ready for data reception
for (i=0; i<5; i++)
dht11_dat[i] = getTempHumiDtaDo1();
pinMode(IOSENSOR0,OUTPUT);
digitalWrite(IOSENSOR0,HIGH);
unsigned char dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
// check check_sum
if(dht11_dat[4] != dht11_check_sum)
{
return 0;
}
temphumi[0] = 4;
temphumi[1] = (dht11_dat[2]*256+dht11_dat[3])/10;
temphumi[2] = (dht11_dat[2]*256+dht11_dat[3])%10;
temphumi[3] = (dht11_dat[0]*256+dht11_dat[1])/10;
temphumi[4] = (dht11_dat[2]*256+dht11_dat[3])%10;
return 1;
}
/*********************************************************************************************************
** Function name: bmp085GetTempPres
** Descriptions: barometer get data
*********************************************************************************************************/
void BeaconSensor::bmp085GetTempPres(unsigned char *dta)
{
dta[0] = 5;
long b5;
long x1, x2;
unsigned int ac5 = bmp085ReadInt(0xB2);
int ac6 = bmp085ReadInt(0xB4);
int mc = bmp085ReadInt(0xBC);
int md = bmp085ReadInt(0xBE);
// make ut
unsigned int ut;
// Write 0x2E into Register 0xF4
// This requests a temperature reading
Wire.beginTransmission(BAROMETERADDRESS);
Wire.write(0xF4);
Wire.write(0x2E);
Wire.endTransmission();
// Wait at least 4.5ms
delay(5);
// Read two bytes from registers 0xF6 and 0xF7
ut = bmp085ReadInt(0xF6);
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long)mc << 11)/(x1 + md);
b5 = (x1 + x2);
//*temp = (float)(( b5+8)>>4)/10.0;
dta[4] = ((b5+8)>>4)/10;
dta[5] = ((b5+8)>>4)%10;
long x3, b3, b6, p;
unsigned long b4, b7;
int ac1 = bmp085ReadInt(0xAA);
int ac2 = bmp085ReadInt(0xAC);
int ac3 = bmp085ReadInt(0xAE);
unsigned int ac4 = bmp085ReadInt(0xB0);
int b1 = bmp085ReadInt(0xB6);
int b2 = bmp085ReadInt(0xB8);
b6 = b5-4000;
// Calculate B3
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)<<0) + 2)>>2;
// Calculate B4
x1 = (ac3 * b6)>>13;
x2 = (b1 * ((b6 * b6)>>12))>>16;
x3 = ((x1 + x2) + 2)>>2;
b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
unsigned char msb, lsb, xlsb;
unsigned long up = 0;
// Request a pressure reading w/ oversampling setting
Wire.beginTransmission(BAROMETERADDRESS);
Wire.write(0xF4);
Wire.write(0x34 + (0<<6));
Wire.endTransmission();
// Wait for conversion, delay time dependent on 0
delay(2 + (3<<0));
// Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
msb = bmp085Read(0xF6);
lsb = bmp085Read(0xF7);
xlsb = bmp085Read(0xF8);
up = (((unsigned long) msb << 16) | ((unsigned long) lsb << 8) | (unsigned long) xlsb) >> (8-0);
b7 = ((unsigned long)(up - b3) * (50000>>0));
p = (b7<0x80000000) ? (b7<<1)/b4 : (b7/b4)<<1;
x1 = (p>>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
p += (x1 + x2 + 3791)>>4;
// *pres = p;
dta[1] = p/10000;
dta[2] = (p%10000)/100;
dta[3] = p%100;
}
/*********************************************************************************************************
** Function name: bmp085Read
** Descriptions: use by bmp085GetTempPres
*********************************************************************************************************/
char BeaconSensor::bmp085Read(unsigned char address)
{
unsigned char data;
Wire.beginTransmission(BAROMETERADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BAROMETERADDRESS, 1);
while(!Wire.available());
return Wire.read();
}
/*********************************************************************************************************
** Function name: bmp085ReadInt
** Descriptions: use by bmp085GetTempPres
*********************************************************************************************************/
int BeaconSensor:: bmp085ReadInt(unsigned char address)
{
unsigned char msb, lsb;
Wire.beginTransmission(BAROMETERADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(BAROMETERADDRESS, 2);
while(Wire.available()<2);
msb = Wire.read();
lsb = Wire.read();
return (int) msb<<8 | lsb;
}
BeaconSensor SENSOR;
/*********************************************************************************************************
END FILE
*********************************************************************************************************/