基于ESP8266的温湿度、可燃气监测系统

1.硬件准备
esp8266开发板
DHT11温湿度传感器
无源蜂鸣器
OLED显示屏
面包板
若干杜邦线
2.线路连接
DHT11 温湿度传感器
传感器开发板VCC3.3VGNDGNDOUTD4
MQ-5液化气传感器模块
传感器开发板AOA0DOD5GNDGNDVCC3.3V
无源蜂鸣器
注:无源蜂鸣器翻转过来有正负极
传感器开发板+D6HXVCC
OLED显示屏
显示屏开发板GNDGNDVDDVCCSCK/SCLD1SDAD2
实物连接好的图片

#include <dht11.h> // 引入DHT11库,温湿度传感器
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> //需要自行下载, OLED显示
#define DHT11PIN 2 // 温湿度传感器引脚,2对应D4
#define Sensor_DO 14
#define MQ5PIN A0
/*************定义类型****************/
dht11 DHT11;
Adafruit_SSD1306 oled(128, 64, &Wire,-1); //实例化
/*************定义全局变量****************/
float sensorValue; // MQ
float ad_co_f = 0;
int pinBuzzer = 12;
float temp = 0; // 温度
float hum = 0; // 湿度
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
OLED_Init();
// MQ-5初始化
pinMode(Sensor_DO, INPUT);
// 蜂鸣器初始化
pinMode(pinBuzzer, OUTPUT);
// 等待
delay(5000);
}
void loop() {
// put your main code here, to run repeatedly:
// 获取温湿度
Serial.println("获取温湿度");
DHT11.read(DHT11PIN); // 更新传感器上的数据
temp = getTemperature();
hum = getHumidity();
Serial.print("当前湿度(%): ");
Serial.println(hum, 2);
Serial.print("当前温度(℃):");
Serial.println(temp, 2);
// 获取MQ-5
Serial.println("获取MQ-5数据");
sensorValue = getSensorValue();
ad_co_f = sensorValue * (3.3/1024);
// 显示到OLED
Serial.println("显示到OLED屏");
oled.clearDisplay(); // 清屏
OLED_Show_Temperature(temp);
OLED_Show_Humidity(hum);
OLED_Show_MQ5(ad_co_f);
oled.display();
// 判断条件
if(digitalRead(Sensor_DO) == LOW)
{
// 蜂鸣器响
Serial.println("蜂鸣器响了");
activateVoice();
} else {
// 蜂鸣器停
Serial.println("蜂鸣器停了");
deactivateVoice();
}
// 等待
delay(1000);
}
// OLED显示函数
void OLED_Init()
{
oled.begin(SSD1306_SWITCHCAPVCC, 0x3c);
oled.setTextColor(WHITE);
oled.clearDisplay(); // 清屏
}
void OLED_Show_Temperature(float temp)
{
// 显示温度
oled.setTextSize(1.9);
oled.setCursor(5, 1);
oled.print("Temp: ");
oled.print(temp);
oled.println("C");
}
void OLED_Show_Humidity(float hum)
{
// 显示湿度
oled.setTextSize(1.9);
oled.setCursor(5, 12);
oled.print("Humidity:");
oled.print(hum);
oled.println("%");
}
void OLED_Show_MQ5(float alarm)
{
oled.setTextSize(1.9);
oled.setCursor(5, 24);
oled.print("CO alarm: ");
oled.println(alarm);
}
// DHT11温湿度传感器
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32; //摄氏温度度转化为华氏温度
}
double Kelvin(double celsius)
{
return celsius + 273.15; //摄氏温度转化为开氏温度
}
float getHumidity()
{
return (float)DHT11.humidity;
}
float getTemperature()
{
return (float)DHT11.temperature;
}
// 蜂鸣器函数
void activateVoice()
{
tone(pinBuzzer, 300, 500);
}
void deactivateVoice()
{
noTone(pinBuzzer);
}
// 获取MQ-5传感器参数
float getSensorValue()
{
return analogRead(MQ5PIN);
}
效果

4.参考资料
MQ2气体/烟雾传感器如何工作及其与Arduino接口,https://zhuanlan.zhihu.com/p/340072270
ESP8266如此简单-入门之驱动蜂鸣器,https://www.bilibili.com/video/BV1zv4y137YN
Arduino esp8266接OLED亮屏,https://blog.csdn.net/dddexter/article/details/116461972
基于arduino的oled显示屏的使用,https://blog.csdn.net/jiayan0428/article/details/105254403
ESP8266驱动OLED显示屏(附源码),https://www.bilibili.com/video/BV1QM4y1W7kN
基于ESP8266芯片的实时温湿度传感器,https://blog.csdn.net/m0_57035925/article/details/121971844
基于esp8266、dht11、MQ2、oled的CO可燃气检测系统设计,https://www.bilibili.com/video/BV1AT4y1a7i4
ESP8266传感器开发参考资料(太极创客),