室内检测系统由ESP12E Shield+Arduino UNO R3开发板+DHT11温湿度模块+双色LED灯+有源蜂鸣器+光敏电阻模块+I2CLCD1602液晶显示器所构成。DHT11温湿度模块获取室内温湿度数据通过I2CLCD1602液晶显示器进行显示,另一方面通过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 R3将数据上传至云平台
需要添加俩个设备名称分别为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 displaydht DHT;//create a variable type of dhtconst int DHT11_PIN= 2;//Humiture sensor attach to pin2const 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 OUTPUTpinMode(greenPin, OUTPUT);//set greenPin as OUTPUTlcd.init(); //initialize the lcdlcd.backlight(); //open the backlight
}void loop()
{// READ DATA//Serial.println("DHT11:");D: int chk = DHT.read11(DHT11_PIN);//read the value returned from sensorswitch (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 DATAlcd.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 numberSerial.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 lcdshidu = 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 numberSerial.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 monitorif(outputValue >= 500) //else{digitalWrite(buzzerPin,LOW);analogWrite(redPin, 255); //red value decreaseanalogWrite(greenPin, 0); //green value decreasedelay(100);//Serial.println(val, DEC);}else{digitalWrite(buzzerPin,HIGH);analogWrite(greenPin, 255); //green value decreaseanalogWrite(redPin, 0); //red value decreasedelay(100);}delay(1000); //delay 1s}
所需库下载连接
将下载好的压缩包解压,把文件Dht复制到你的编译器Arduino的libraries文件夹下
五、视频效果演示
基于Arduino UNO R3开发板的安全检测系统的实现---DHT11数据采集上云
视频链接CSDN
视频链接B站