1
+ // 导入需要用到的液晶显示库、dht11库、json库与串口通讯库
2
+ #include < LiquidCrystal.h>
3
+ #include < dht11.h>
4
+ #include < ArduinoJson.h>
5
+ #include < SoftwareSerial.h>
6
+
7
+ // 预编译各个引脚
8
+ #define DHTPIN 9
9
+ #define INFOUT 7
10
+ #define MQPIN 6
11
+
12
+ // 初始化各种对象
13
+ /*
14
+ lcd的各接口:
15
+ rs:12
16
+ e:11
17
+ d4:5
18
+ d5:4
19
+ d6:3
20
+ d7:2
21
+ */
22
+ LiquidCrystal lcd (12 , 13 , 5 , 4 , 3 , 2 );
23
+ SoftwareSerial ArduinoSerial (11 , 10 );
24
+ dht11 DHT11;
25
+
26
+ // PM2.5传感器处理函数
27
+ float PM ()
28
+ {
29
+ // 设置PM2.5传感器相关引脚与变量
30
+ int measurePin = A0;
31
+ int ledPower = 8 ;
32
+
33
+ unsigned int samplingTime = 280 ; // 3脚直接由单片机给脉冲波10ms周期,高电平0.32ms
34
+ unsigned int deltaTime = 40 ;
35
+ unsigned int sleepTime = 9680 ;
36
+
37
+ float voMeasured = 0 ;
38
+ float calcVoltage = 0 ;
39
+ float dustDensity = 0 ;
40
+
41
+ digitalWrite (ledPower, LOW);
42
+ delayMicroseconds (samplingTime);
43
+
44
+ voMeasured = analogRead (measurePin);
45
+
46
+ delayMicroseconds (deltaTime);
47
+ digitalWrite (ledPower, HIGH);
48
+ delayMicroseconds (sleepTime);
49
+
50
+ calcVoltage = voMeasured * 3.0 / 1023.0 ;
51
+ dustDensity = 0.172 * calcVoltage - 0.1 ;
52
+
53
+ if (dustDensity < 0 )
54
+ {
55
+ dustDensity = 0.00 ;
56
+ }
57
+
58
+ Serial.println ();
59
+ Serial.print (" Raw Signal Value (0-1023):" );
60
+ Serial.println (voMeasured);
61
+
62
+ Serial.print (" - Voltage: " );
63
+ Serial.println (calcVoltage);
64
+
65
+ Serial.print (" - Dust Density: " );
66
+ Serial.print (dustDensity * 1000 );
67
+ Serial.println (" ug/m3" );
68
+
69
+ delay (1000 );
70
+
71
+ // 返回实际的浓度值,单位ug/m3
72
+ return dustDensity * 1000 ;
73
+ }
74
+
75
+ // 初始化各种设置
76
+ void setup () {
77
+ // put your setup code here, to run once:
78
+ // 启动串口通讯
79
+ ArduinoSerial.begin (9600 );
80
+ Serial.begin (115200 );
81
+ // 初始化液晶屏各种设置
82
+ lcd.begin (16 , 2 );
83
+ lcd.print (" Welcome!" );
84
+ delay (1000 );
85
+ lcd.clear ();
86
+ // 设置小灯泡的引脚
87
+ pinMode (INFOUT, OUTPUT);
88
+ }
89
+ /* *******************与DHT11传感器相关的数据获取********************/
90
+ float getTemp ()
91
+ {
92
+ int chk = DHT11.read (DHTPIN);
93
+ return (float )DHT11.temperature ;
94
+ }
95
+
96
+ float getHumi ()
97
+ {
98
+ int chk = DHT11.read (DHTPIN);
99
+ return (float )DHT11.humidity ;
100
+ }
101
+ /* ***************************************************************/
102
+
103
+ /* *****************分别在液晶屏和串口显示数据的函数*****************/
104
+ void printOnLCD (float t, float h, int val, float d)
105
+ {
106
+ lcd.print (" tempareture:" );
107
+ lcd.setCursor (0 , 1 );
108
+ lcd.print (t);
109
+ delay (2000 );
110
+ lcd.clear ();
111
+ lcd.print (" humidity:" );
112
+ lcd.setCursor (0 , 1 );
113
+ lcd.print (h);
114
+ delay (2000 );
115
+ lcd.clear ();
116
+ lcd.print (" mq9:" );
117
+ lcd.setCursor (0 , 1 );
118
+ lcd.print (val);
119
+ delay (2000 );
120
+ lcd.clear ();
121
+ lcd.print (" Dust Density:" );
122
+ lcd.setCursor (0 , 1 );
123
+ lcd.print (d);
124
+ delay (2000 );
125
+ lcd.clear ();
126
+ }
127
+
128
+ void printOnSerial (float t, float h, int val)
129
+ {
130
+ Serial.println (" current temperature(`C):" );
131
+ Serial.println (t);
132
+ Serial.println (" current humidity(%):" );
133
+ Serial.println (h);
134
+ Serial.println (" data from MQ-9:" );
135
+ Serial.println (val);
136
+ }
137
+ /* *******************************************************/
138
+
139
+ /* **********************警报函数*************************/
140
+ void warning (float d)
141
+ {
142
+ if (d > 700 )// 如果PM2.5浓度大于700ug/m3就输出高电平,使蜂鸣器振动,LED灯亮
143
+ {
144
+ int i;
145
+ for (i = 0 ; i < 20 ; i++)
146
+ {
147
+ digitalWrite (INFOUT, HIGH);
148
+ delay (200 );
149
+ digitalWrite (INFOUT, LOW);
150
+ delay (200 );
151
+ }
152
+ }
153
+ }
154
+ /* ******************************************************/
155
+
156
+ // void infoTrans(float t,float h,float v,float d)
157
+ // {
158
+ // StaticJsonDocument<500> doc;
159
+ // JsonObject root = doc.to<JsonObject>();
160
+
161
+ // root["temp"] = t;
162
+ // root["humi"] = h;
163
+ // root["val"] = v;
164
+ // root["dens"] = d;
165
+
166
+ // serializeJson(doc,ArduinoSerial);
167
+ // }
168
+ // 测试中去掉的数据传输函数
169
+
170
+ void loop () {
171
+ // put your main code here, to run repeatedly:
172
+
173
+ /* ***********将数据转换成json格式并发送出去**************/
174
+ const size_t CAPACITY = JSON_OBJECT_SIZE (20 );
175
+ /*
176
+ *t:温度
177
+ *h:湿度
178
+ *d:PM2.5浓度
179
+ *val:CO浓度
180
+ */
181
+ float t = getTemp ();
182
+ float h = getHumi ();
183
+ float d = PM ();
184
+ float val = analogRead (MQPIN);
185
+
186
+ // 将数据通过串口通信传输给nodeMCU.
187
+ StaticJsonDocument<CAPACITY> doc;
188
+ JsonObject root = doc.to <JsonObject>();
189
+
190
+ root[" temp" ] = t;
191
+ root[" humi" ] = h;
192
+ root[" val" ] = val;
193
+ root[" dens" ] = d;
194
+
195
+ serializeJson (doc, ArduinoSerial);
196
+ /* ****************************************************/
197
+
198
+ printOnLCD (t, h, val, d);
199
+ printOnSerial (t, h, val);
200
+ warning (d);
201
+ }
0 commit comments