-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhardware.ino
159 lines (131 loc) · 3.1 KB
/
hardware.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
#include <dht11.h>
// Pins
#define dustPin 7
#define dht11Pin 5
#define ledPowerPin 2
#define breathPin 4
#define basePin 7
// DelayTimes
#define delayTime 280
#define delayTime2 40
#define offTime 9680
#define intervalTime 10
#define ConstK 0.5
float getDust()
{
static long value = 200;
static float Voc = 0.6;
int dustVal = 0;
// ledPowerPin is any digital pin on the arduino connected to Pin 3 on the sensor
digitalWrite(ledPowerPin, LOW);
delayMicroseconds(delayTime);
dustVal = analogRead(dustPin);
delayMicroseconds(delayTime2);
digitalWrite(ledPowerPin, HIGH);
delayMicroseconds(offTime);
// 因为这个传感器貌似有问题,所以滤波只能这样
if (dustVal != 0)
if (((float)abs(value - dustVal) / (float)value < 2.0) || value == 0)
value = (value * 99 + dustVal) / 100;
else
value = (value * 199 + dustVal) / 200;
float Vo = value / 1024.0 * 5.0;
float dV = Vo - Voc;
if ( dV < 0 )
{
dV = 0;
Voc = Vo;
}
float dustDensity = dV / ConstK * 100.0;
return dustDensity;
}
int getHumidity()
{
static int value = 0;
dht11 DHT11;
DHT11.read(dht11Pin);
//滤波
if (DHT11.humidity > 20 && DHT11.humidity < 100)
{
if (value == 0)
{
value = DHT11.humidity;
return value;
}
if ((float)abs(value - DHT11.humidity) / (float)value < 0.1)
value = (value * 3 + DHT11.humidity) / 4;
else
value = (value * 9 + DHT11.humidity) / 10;
}
return value;
}
int getTemperature()
{
static int value = 0;
dht11 DHT11;
DHT11.read(dht11Pin);
//滤波
if (DHT11.temperature > 5 && DHT11.temperature < 35)
{
if (value == 0)
{
value = DHT11.temperature;
return value;
}
if ((float)abs(value - DHT11.temperature) / (float)value < 0.1)
value = (value * 3 + DHT11.temperature) / 4;
else
value = (value * 9 + DHT11.temperature) / 10;
}
return value;
}
// 表示前100次检测中的呼气频率
float getBreath()
{
static float value = 0.5;
value = (value * 99.0 + digitalRead(breathPin)) / 100.0;
return value;
}
void setup()
{
Serial.begin(9600);
pinMode(ledPowerPin, OUTPUT);
pinMode(dustPin, INPUT);
pinMode(dht11Pin, INPUT);
pinMode(breathPin, INPUT);
pinMode(basePin, OUTPUT);
digitalWrite(basePin, LOW);
}
void loop()
{
int count = 0;
int humidity, temperature;
float dust, breath;
String toSend;
while (1)
{
dust = getDust();
delay(intervalTime);
humidity = getHumidity();
delay(intervalTime);
temperature = getTemperature();
delay(intervalTime);
breath = getBreath();
delay(intervalTime);
count ++;
if (count == 30)
{
toSend = "{\"dust\":";
toSend += dust;
toSend += ",\"humidity\":";
toSend += humidity;
toSend += ",\"temperature\":";
toSend += temperature;
toSend += ",\"breath\":";
toSend += breath;
toSend += "}";
Serial.println(toSend);
count = 0;
}
}
}