室内检测系统由ESP12E Shield Arduino UNO R3开发板 DHT11温湿度模块 双色LED灯 有源蜂鸣器 光敏电阻模块 I2CLCD由1602液晶显示器组成。DHT通过温湿度模块获取室内温湿度数据I2CLCD另一方面,通过1602液晶显示器显示,ESP12E Shield将数据上传到云平台。光敏电阻捕获室内光强。如果光强太弱,即太阳太弱,周围环境太暗,低于设定阈值,则有源蜂鸣器报警和双色LED灯变红,正常情况下LED灯为绿色。
一、设备准备
Arduino UNO R3 I2CLCD1602液晶显示器 ESP12E Shield DHT11温湿度模块 有源蜂鸣器 光敏电阻模块 双色LED灯
二、设备连接
ESP12E Shield直接覆盖Arduino UNO R3开发板 DHT11模块
DHT11模块 | Arduino UNO R3 |
---|---|
5V | |
- | GND |
OUT | P2 |
I2CLCD1602液晶显示器
I2CLCD1602液晶显示器 | Arduino UNO R3 |
---|---|
GDN | GND |
VCC | 5V |
SDA | A4 |
SCL | A5 |
光敏电阻模块
光敏电阻模块 | Arduino UNO R3 |
---|---|
VCC | 5V |
GND | GND |
AO | A0 |
有源蜂鸣器
有源蜂鸣器 | Arduino UNO R3 |
---|---|
GND | GND |
I/O | P7 |
VCC | 5V |
双色LED灯
双色LED灯 | Arduino UNO R3 |
---|---|
GND | GND |
R | P11 |
G | P10 |
三、云平台设置
上传到服务器的配置可参考本博客 一、Arduino UNO R将数据上传到云平台 需要添加两个设备名称humi和temp用于存储温湿度信息
四、完整代码
#include <dht.h> #include <LiquidCrystal_I2C.h> #include <Wire.h> LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 and 0x3F for a 16 chars and 2 line display dht DHT;//create a variable type of dht const int DHT11_PIN= 2;//Humiture sensor attach to pin2 const int greenPin = 11; // R petal on RGB LED module connected to digital pin 11 const int redPin= 10; // G petal on RGB LED module connected to digital pin 10 const int photocellPin = A0; //photoresistor module attach to A0
int outputValue = 0;
const int buzzerPin=7; //buzzer attach to digital 7
int val = 0;
int wendu=0;
int shidu=0;
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(buzzerPin,OUTPUT);
pinMode(redPin, OUTPUT); //set redPin as OUTPUT
pinMode(greenPin, OUTPUT);//set greenPin as OUTPUT
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
}
void loop()
{
// READ DATA
//Serial.println("DHT11:");
D: int chk = DHT.read11(DHT11_PIN);//read the value returned from sensor
switch (chk)
{
case DHTLIB_OK:
//Serial.println("OK!");
break;
case DHTLIB_ERROR_CHECKSUM:
//goto D;
//Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
//goto D;
//Serial.print("Time out error,\t");
break;
default:
// goto D;
//Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
lcd.setCursor(0, 0);
lcd.print("Tem:");
//Serial.print("Tem:");
lcd.print(DHT.temperature,1); //print the temperature on lcd
wendu = DHT.temperature;
/* Serial.print("wendu:"); Serial.print(wendu); Serial.print("\n"); */
Serial.print("cmd=upload&device_name=temp&data=");
Serial.print(wendu);//send a random number
Serial.println("&uid=beyondyy&key=6d2cc6e3e19efa259a302fcf4166e2ce");
lcd.print(char(223));//print the unit" ℃ "
lcd.print("C");
// Serial.println(" C");
lcd.setCursor(0, 1);
lcd.print("Hum:");
//Serial.print("Hum:");
lcd.print(DHT.humidity,1); //print the humidity on lcd
shidu = DHT.humidity;
/* Serial.print("shidu:"); Serial.print(shidu); Serial.print("\n"); */
Serial.print("cmd=upload&device_name=humi&data=");
Serial.print(shidu);//send a random number
Serial.println("&uid=beyondyy&key=6d2cc6e3e19efa259a302fcf4166e2ce");
lcd.print(" %");
//Serial.println(" %");
delay(200); //wait a while
outputValue = analogRead(photocellPin);//read the value of photoresistor
//Serial.println(outputValue); //print it in serial monitor
if(outputValue >= 500) //else
{
digitalWrite(buzzerPin,LOW);
analogWrite(redPin, 255); //red value decrease
analogWrite(greenPin, 0); //green value decrease
delay(100);
//Serial.println(val, DEC);
}
else
{
digitalWrite(buzzerPin,HIGH);
analogWrite(greenPin, 255); //green value decrease
analogWrite(redPin, 0); //red value decrease
delay(100);
}
delay(1000); //delay 1s
}
所需库下载连接 将下载好的压缩包解压,把文件Dht复制到你的编译器Arduino的libraries文件夹下
五、视频效果演示
基于Arduino UNO R3开发板的安全检测系统的实现---DHT11数据采集上云
视频链接CSDN 视频链接B站