Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f4f46c8

Browse files
authoredJul 15, 2021
Add files via upload
1 parent 89fb7f5 commit f4f46c8

File tree

4 files changed

+441
-0
lines changed

4 files changed

+441
-0
lines changed
 

‎release3.0-Uno.ino

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
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+
}

‎release3.0-nodeMCU.ino

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
//导入相关的库
2+
#include<ArduinoJson.h>
3+
#include<SoftwareSerial.h>
4+
#include<ESP8266WiFi.h>
5+
#include<Adafruit_MQTT.h>
6+
#include<Adafruit_MQTT_Client.h>
7+
8+
/************************* WiFi Access Point *********************************/
9+
10+
#define WLAN_SSID "iQOO 7" //你的WI-FI的SSID,注意把你的 WIFI 的 AP 设置成 2.4GHz 频段。
11+
#define WLAN_PASS "1234567899" //你的WI-FI的密码
12+
13+
/************************* Adafruit.io Setup *********************************/
14+
15+
#define AIO_SERVER "io.adafruit.com"
16+
#define AIO_SERVERPORT 1883 //use 8883 for SSL
17+
#define AIO_USERNAME "coderzzx" //你在io.adafruit.com上注册的用户名
18+
#define AIO_KEY "aio_FVAz59rsIgM6vRHWNTbX3h6ceAch" //你在io.adafruit.com所获得的AIO
19+
20+
/*
21+
定义nodeMCU引脚编号
22+
*/
23+
#define D1 5
24+
#define D2 4
25+
#define D3 0
26+
#define D4 2
27+
#define D5 14
28+
#define D6 12
29+
#define D7 13
30+
31+
#define LED_PIN D4
32+
33+
/************************************************实例化串口通信的对象****************************************************/
34+
SoftwareSerial nodemcuSerial(D6, D5);
35+
36+
/**********************************************用于将数据发布到服务器上***************************************************/
37+
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
38+
WiFiClient client;
39+
40+
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
41+
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
42+
43+
/****************************** Feeds ***************************************/
44+
45+
// Setup a feed called 'photocell' for publishing.
46+
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
47+
Adafruit_MQTT_Publish t_rel = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Temperature");
48+
Adafruit_MQTT_Publish h_rel = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Humidity");
49+
Adafruit_MQTT_Publish c_rel = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/CO Density");
50+
Adafruit_MQTT_Publish p_rel = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/PM2.5");
51+
52+
// Setup a feed called 'onoff' for subscribing to changes.
53+
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/Switch");
54+
55+
// Function to connect and reconnect as necessary to the MQTT server.
56+
// Should be called in the loop function and it will take care if connecting.
57+
void MQTT_connect() {
58+
int8_t ret;
59+
int8_t retries = 10;
60+
61+
// Stop if already connected.
62+
if ( mqtt.connected() )
63+
{
64+
return;
65+
}
66+
67+
Serial.println("Connecting to MQTT... ");
68+
69+
while ( (ret = mqtt.connect()) != 0 ) // connect will return 0 for connected
70+
{
71+
Serial.println( mqtt.connectErrorString(ret) );
72+
Serial.println("Retrying MQTT connection in 5 seconds...");
73+
74+
mqtt.disconnect();
75+
delay(5000);
76+
77+
retries--;
78+
if (retries == 0) {
79+
// basically die and wait for WDT to reset me
80+
while (1);
81+
}
82+
}
83+
84+
Serial.println("MQTT Connected!");
85+
}
86+
87+
/***************************************************数据发布函数*********************************************************/
88+
void infoRelea(float x[])
89+
{
90+
// this is our 'wait for incoming subscription packets' busy subloop
91+
// try to spend your time here
92+
Adafruit_MQTT_Subscribe *subscription;
93+
94+
// Ensure the connection to the MQTT server is alive (this will make the first
95+
// connection and automatically reconnect when disconnected). See the MQTT_connect
96+
// function definition further below.
97+
MQTT_connect();
98+
99+
int flag;
100+
while ((subscription = mqtt.readSubscription(5000)))
101+
{
102+
if (subscription == &onoffbutton)
103+
{
104+
Serial.print(F("Got: "));
105+
String value = (char *)onoffbutton.lastread;
106+
107+
Serial.println(value);
108+
if (!value.compareTo("1"))
109+
{
110+
digitalWrite(LED_PIN, HIGH);
111+
flag = 1;
112+
}
113+
if (!value.compareTo("0"))
114+
{
115+
digitalWrite(LED_PIN, LOW);
116+
flag = 0;
117+
return;
118+
}
119+
}
120+
}
121+
122+
// Now we can publish stuff!
123+
int i;
124+
for (i = 0; i < 4; i++)
125+
{
126+
switch (i)
127+
{
128+
case 0:
129+
if (! t_rel.publish(x[0]))
130+
{
131+
Serial.println(F("Failed"));
132+
}
133+
else
134+
{
135+
Serial.println(F("OK!"));
136+
}
137+
break;
138+
139+
case 1:
140+
if (! h_rel.publish(x[1]))
141+
{
142+
Serial.println(F("Failed"));
143+
}
144+
else
145+
{
146+
Serial.println(F("OK!"));
147+
}
148+
break;
149+
150+
case 2:
151+
if (! c_rel.publish(x[2]))
152+
{
153+
Serial.println(F("Failed"));
154+
}
155+
else
156+
{
157+
Serial.println(F("OK!"));
158+
}
159+
break;
160+
161+
case 3:
162+
if (! p_rel.publish(x[3]))
163+
{
164+
Serial.println(F("Failed"));
165+
}
166+
else
167+
{
168+
Serial.println(F("OK!"));
169+
}
170+
}
171+
}
172+
}
173+
174+
//各个初始化操作
175+
void setup()
176+
{
177+
Serial.begin(115200);
178+
while (!Serial) continue;
179+
180+
nodemcuSerial.begin(9600);
181+
182+
Serial.println(F("Adafruit MQTT demo"));
183+
184+
// Connect to WiFi access point.
185+
Serial.print("Connecting to ");
186+
Serial.println(WLAN_SSID);
187+
188+
WiFi.begin(WLAN_SSID, WLAN_PASS);
189+
190+
while (WiFi.status() != WL_CONNECTED)
191+
{
192+
delay(1000);
193+
Serial.print(".");
194+
}
195+
196+
Serial.println();
197+
Serial.println("WiFi connected");
198+
Serial.print("IP address: ");
199+
Serial.println(WiFi.localIP());
200+
201+
// Setup MQTT subscription for onoff feed.
202+
mqtt.subscribe(&onoffbutton);
203+
204+
pinMode(LED_PIN, OUTPUT);
205+
}
206+
207+
void loop()
208+
{
209+
/************************************************用于从串口中获取数据****************************************************/
210+
const size_t CAPACITY = JSON_OBJECT_SIZE(20);
211+
StaticJsonDocument<CAPACITY> doc;
212+
213+
DeserializationError error = deserializeJson(doc, nodemcuSerial);
214+
215+
// if (error)
216+
// {
217+
// Serial.print(F("deserializeJson() failed: "));
218+
// Serial.println(error.f_str());
219+
// return;
220+
// }
221+
222+
JsonObject root = doc.as<JsonObject>();
223+
//获取数据
224+
/*
225+
t:温度
226+
h:湿度
227+
val:一氧化碳浓度
228+
d:PM2.5浓度
229+
*/
230+
float temp = (float)root["temp"];
231+
float humi = (float)root["humi"];
232+
float val = (float)root["val"];
233+
float dens = (float)root["dens"];
234+
235+
float data[4] = {temp, humi, val, dens};
236+
// for (int i = 0; i < 4; i++)
237+
// Serial.println(data[i]);
238+
/***********************************************************************************************************************/
239+
infoRelea(data);
240+
}

‎大作业电路图.fzz

28.4 KB
Binary file not shown.

‎电路连接图final-version.png

214 KB
Loading

0 commit comments

Comments
 (0)
Please sign in to comment.