基于ESP8266的溫濕度、可燃?xì)獗O(jiān)測(cè)系統(tǒng)

1.硬件準(zhǔn)備
esp8266開(kāi)發(fā)板
DHT11溫濕度傳感器
無(wú)源蜂鳴器
OLED顯示屏
面包板
若干杜邦線(xiàn)
2.線(xiàn)路連接
DHT11 溫濕度傳感器
傳感器開(kāi)發(fā)板VCC3.3VGNDGNDOUTD4
MQ-5液化氣傳感器模塊
傳感器開(kāi)發(fā)板AOA0DOD5GNDGNDVCC3.3V
無(wú)源蜂鳴器
注:無(wú)源蜂鳴器翻轉(zhuǎn)過(guò)來(lái)有正負(fù)極
傳感器開(kāi)發(fā)板+D6HXVCC
OLED顯示屏
顯示屏開(kāi)發(fā)板GNDGNDVDDVCCSCK/SCLD1SDAD2
實(shí)物連接好的圖片

#include <dht11.h> ? // 引入DHT11庫(kù),溫濕度傳感器
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> //需要自行下載, OLED顯示
#define DHT11PIN 2 ? // 溫濕度傳感器引腳,2對(duì)應(yīng)D4
#define Sensor_DO 14
#define MQ5PIN A0
/*************定義類(lèi)型****************/
dht11 DHT11;
Adafruit_SSD1306 oled(128, 64, &Wire,-1); ? //實(shí)例化
/*************定義全局變量****************/
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); ?// 更新傳感器上的數(shù)據(jù)
?temp = getTemperature();
?hum = getHumidity();
?Serial.print("當(dāng)前濕度(%): ");
?Serial.println(hum, 2);
?Serial.print("當(dāng)前溫度(℃):");
?Serial.println(temp, 2);
?// 獲取MQ-5
?Serial.println("獲取MQ-5數(shù)據(jù)");
?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顯示函數(shù)
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; //攝氏溫度度轉(zhuǎn)化為華氏溫度
}
double Kelvin(double celsius)
{
?return celsius + 273.15; //攝氏溫度轉(zhuǎn)化為開(kāi)氏溫度
}
float getHumidity()
{
?return (float)DHT11.humidity;
}
float getTemperature()
{
?return (float)DHT11.temperature;
}
// 蜂鳴器函數(shù)
void activateVoice()
{
?tone(pinBuzzer, 300, 500);
}
void deactivateVoice()
{
?noTone(pinBuzzer);
}
// 獲取MQ-5傳感器參數(shù)
float getSensorValue()
{
?return analogRead(MQ5PIN);
}
效果

4.參考資料
MQ2氣體/煙霧傳感器如何工作及其與Arduino接口,https://zhuanlan.zhihu.com/p/340072270
ESP8266如此簡(jiǎn)單-入門(mén)之驅(qū)動(dòng)蜂鳴器,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驅(qū)動(dòng)OLED顯示屏(附源碼),https://www.bilibili.com/video/BV1QM4y1W7kN
基于ESP8266芯片的實(shí)時(shí)溫濕度傳感器,https://blog.csdn.net/m0_57035925/article/details/121971844
基于esp8266、dht11、MQ2、oled的CO可燃?xì)鈾z測(cè)系統(tǒng)設(shè)計(jì),https://www.bilibili.com/video/BV1AT4y1a7i4
ESP8266傳感器開(kāi)發(fā)參考資料(太極創(chuàng)客),