一、MQ-3B酒精气体传感器
在初步了解和学习ESP32-WROOM-32E最小开发板基础知识后,我们开始学习外接传感器设备,网络上关于获取环境数据(如温湿度)的传感器介绍的很多,这里选择拓展MQ-3B酒精气体传感器,这个应用的场景很普遍,交警查酒驾时通过吹气来判断驾驶人是否存在酒驾情况,同时会将数据上传到后台服务器,我们后期围绕这个场景来开发一个简易的酒精气体检测物联网原型,将传感器采集到的数据上传到MQTT服务器并存储到数据库以便进行数据的查询和管理
1.1.MQ-3B酒精气体传感器概述
产品概述
MQ-3B气体传感器所使用的气敏材料是在清洁空气中电 导率较低的二氧化锡(SnO2)。当传感器所处环境中存在酒精 蒸气时,传感器的电导率随空气中酒精蒸气浓度的增加而增 大。使用简单的电路即可将电导率的变化转换为与该气体浓 度相对应的输出信号。 MQ-3气体传感器对酒精的灵敏度高,可以抵抗汽油、 烟雾、水蒸气的干扰。这种传感器可检测多种浓度酒精气氛, 是一款适合多种应用的低成本传感器。
传感器特点
本品在较宽的浓度范围内对酒精有良好的灵敏度。具有长寿命、低成本、驱动电路简单等优点。 主要应用 广泛适用于车用酒精气体报警器、工业用酒精气体报警器以及便携式气体检测器。
主要应用
广泛适用于车用酒精气体报警器、工业用酒精气体报警器以及便携式气体检测器。
拓展排针
下表列出MQ-3B拓展引脚名称和功能
编号 | 名称 | 类型 | 功能 |
---|---|---|---|
VCC | 供电引脚 | P | 5V直流供电 |
AO | 模拟量输出 | O | 模拟量输出 |
DO | 电平输出 | O | 电平输出:高或低 |
GND | 接地 |
注意事项
P: 电源;I:输入;O:输出。
1.2.准备工作
1.2.1.硬件
- ESP32-WROOM-32E最小开发板
- USB 数据线 (Type-C 数据线)
- MQ-3B酒精气体传感器模组
- 杜绑线
- 电脑(Windows 10)
1.2.2.软件
准备软件安装包:
我们在开发资料包中提供的Arduino IDE版本是 2.3.2
如需要安装其它版本,可到Arduino官网去另行下载并安装。
Arduino官网地址: https://www.arduino.cc/en/software
1.2.3.接线示意图
MQ-3B模组需要5V电压供电,将引脚AO用杜绑线连接到最小开发板IO口,用于读取模拟电压,根据电压值换算成PPM数值,以此获取气体中的酒精含量,引脚DO输出为TTL电平,模组带有一个电位器用于调节阈值,当检测到酒精时,模组自带LED灯(绿色)会点亮。
我们这里主要是实现读取PPM值,所以只需要用到引脚AO,DO引脚不用做任何处理。
二、编写程序
打开Arduino IDE,输入程序,命名工程名称为HTKI_MQ_3B
2.1.输入代码
输入以下代码
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <HTKI_Sensor_MQ3B.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//"未检测到酒精"
static const unsigned char bitmap_bytes_0[] = {
0x01, 0x00, 0x10, 0x40, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x10, 0x20,
0x01, 0x00, 0x10, 0x40, 0x27, 0xc4, 0xff, 0x84, 0x4f, 0xfe, 0x10, 0x20,
0x01, 0x00, 0x10, 0xa0, 0x14, 0x44, 0x08, 0x04, 0x20, 0xa0, 0x95, 0xfe,
0x3f, 0xf8, 0x10, 0xa0, 0x14, 0x54, 0x10, 0x24, 0x20, 0xa0, 0x54, 0x20,
0x01, 0x00, 0xfd, 0x10, 0x85, 0x54, 0x22, 0x24, 0x07, 0xfc, 0x58, 0xfc,
0x01, 0x00, 0x12, 0x08, 0x45, 0x54, 0x41, 0x24, 0x84, 0xa4, 0x10, 0x20,
0x01, 0x00, 0x35, 0xf6, 0x45, 0x54, 0xff, 0xa4, 0x44, 0xa4, 0xfd, 0xfe,
0xff, 0xfe, 0x38, 0x00, 0x15, 0x54, 0x08, 0xa4, 0x54, 0xa4, 0x30, 0x00,
0x03, 0x80, 0x54, 0x88, 0x15, 0x54, 0x08, 0x24, 0x15, 0x1c, 0x38, 0xfc,
0x05, 0x40, 0x50, 0x48, 0x25, 0x54, 0x08, 0x24, 0x26, 0x04, 0x54, 0x84,
0x09, 0x20, 0x92, 0x48, 0xe5, 0x54, 0x7f, 0x24, 0xe4, 0x04, 0x54, 0xfc,
0x11, 0x10, 0x11, 0x50, 0x21, 0x04, 0x08, 0x24, 0x27, 0xfc, 0x90, 0x84,
0x21, 0x08, 0x11, 0x10, 0x22, 0x84, 0x08, 0x04, 0x24, 0x04, 0x10, 0xfc,
0xc1, 0x06, 0x10, 0x20, 0x22, 0x44, 0x0f, 0x84, 0x24, 0x04, 0x10, 0x84,
0x01, 0x00, 0x17, 0xfe, 0x24, 0x14, 0xf8, 0x14, 0x27, 0xfc, 0x10, 0x94,
0x01, 0x00, 0x10, 0x00, 0x08, 0x08, 0x40, 0x08, 0x04, 0x04, 0x10, 0x88
};
//"检测到酒精"
static const unsigned char bitmap_bytes_1[] = {
0x10, 0x40, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x10, 0x20,
0x10, 0x40, 0x27, 0xc4, 0xff, 0x84, 0x4f, 0xfe, 0x10, 0x20,
0x10, 0xa0, 0x14, 0x44, 0x08, 0x04, 0x20, 0xa0, 0x95, 0xfe,
0x10, 0xa0, 0x14, 0x54, 0x10, 0x24, 0x20, 0xa0, 0x54, 0x20,
0xfd, 0x10, 0x85, 0x54, 0x22, 0x24, 0x07, 0xfc, 0x58, 0xfc,
0x12, 0x08, 0x45, 0x54, 0x41, 0x24, 0x84, 0xa4, 0x10, 0x20,
0x35, 0xf6, 0x45, 0x54, 0xff, 0xa4, 0x44, 0xa4, 0xfd, 0xfe,
0x38, 0x00, 0x15, 0x54, 0x08, 0xa4, 0x54, 0xa4, 0x30, 0x00,
0x54, 0x88, 0x15, 0x54, 0x08, 0x24, 0x15, 0x1c, 0x38, 0xfc,
0x50, 0x48, 0x25, 0x54, 0x08, 0x24, 0x26, 0x04, 0x54, 0x84,
0x92, 0x48, 0xe5, 0x54, 0x7f, 0x24, 0xe4, 0x04, 0x54, 0xfc,
0x11, 0x50, 0x21, 0x04, 0x08, 0x24, 0x27, 0xfc, 0x90, 0x84,
0x11, 0x10, 0x22, 0x84, 0x08, 0x04, 0x24, 0x04, 0x10, 0xfc,
0x10, 0x20, 0x22, 0x44, 0x0f, 0x84, 0x24, 0x04, 0x10, 0x84,
0x17, 0xfe, 0x24, 0x14, 0xf8, 0x14, 0x27, 0xfc, 0x10, 0x94,
0x10, 0x00, 0x08, 0x08, 0x40, 0x08, 0x04, 0x04, 0x10, 0x88
};
//定义MQ3B传感器
HTKI_Sensor_MQ3B MQ3(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
void setup() {
//初始化串口
Serial.begin(9600);
// 检测OLED屏
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
display.display();
delay(100);
//设置参数据以获取精确数值
MQ3.setRegressionMethod(0);
MQ3.setA(0.3934);
MQ3.setB(-1.504);
//计算PPM值
MQ3.init();
MQ3.setRL(4.7);
Serial.print("Checking please wait.");
float calcR0 = 0;
for (int i = 1; i <= 10; i++) {
MQ3.update();
calcR0 += MQ3.calibrate(RatioMQ3CleanAir);
Serial.print(".");
}
MQ3.setR0(calcR0 / 10);
Serial.println(" done!.");
}
}
void loop() {
MQ3.update();
float concentration = MQ3.readSensor();
float BAC = map(concentration, 20, 500, 7, 184);
BAC = BAC / 1000 *100;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("PPM ");
display.println(concentration, 0);
if (concentration < 20) {
display.drawBitmap(0, 32, bitmap_bytes_0, 96, 16, WHITE);
display.display();
} else {
display.drawBitmap(0, 32, bitmap_bytes_1, 80, 16, WHITE);
display.display();
delay(10000);
}
display.display();
delay(500);
}
2.2.编译并上传程序
输入完成后,执行上传命令即可进行代码的编译并上传至开发板,点击上传
按钮开始上传
看到这样的执行结果就说明程序已经正常上传到开发板,
我们用75%的酒精湿巾靠近传感器, 会提示检测到酒精
注意事项
模组上电后需要预热,传感器表面会发热
这样我们就采集到了传感器的数据。
三、发布传感器数据
我们在读取到MQ-3B数据后,需要将其发布到MQTT服务器。
打开Arduino IDE,输入程序,命名工程名称为HTKI_SendToMqtt_MQ3B
3.1.输入代码
输入以下代码
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <HTKI_Sensor_MQ3B.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
/*=========================== 全局变量及宏定义 =============================*/
int redPin = 25;
// WiFi
const char *ssid = "此处为WIFI网络名称";
const char *password = "此处为WIFI密码";
// MQTT Broker
const char *mqtt_broker = "MQTT服务器IP"; // 服务器地址
const char *clientid = "自定义clientid"; // 自定义clientid
const char *topic = "hantu_iot/testtopic"; // 定义 Topic
const char *mqtt_username = "用户名"; // 用户名
const char *mqtt_password = "密码"; // 密码
const int mqtt_port = 1883; // 端口
//定义客户端
WiFiClient espClient;
PubSubClient client(espClient);
String payload = "";
JsonDocument jsonbuf;
/*****************************Globals***********************************************/
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//定义MQ3B传感器
HTKI_Sensor_MQ3B MQ3(Board, Voltage_Resolution, ADC_Bit_Resolution, Pin, Type);
void setup() {
//初始化串口
Serial.begin(9600);
// 设置LED灯为输出模式
pinMode(redPin, OUTPUT);
// 检测OLED屏
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
display.display();
delay(100);
// 连接WiFi网络并获取WiFi基本信息
WiFi.begin(ssid, password);
Serial.print("正在连接WIFI");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println(".");
}
Serial.println("连接成功");
Serial.print("IP Address");
Serial.println(WiFi.localIP());
Serial.print("MAC Address");
Serial.println(WiFi.macAddress());
Serial.print("SSID");
Serial.println(WiFi.SSID());
Serial.print("RSSI");
Serial.println(WiFi.RSSI());
// 连接到MQTT服务器
client.setServer(mqtt_broker, mqtt_port);
// 设置mqtt保持活跃时间 ms
client.setKeepAlive(60);
// 设置回调函数
client.setCallback(callback);
while (!client.connected()) {
Serial.printf("The client %s connects to the public mqtt broker\n", clientid);
if (client.connect(clientid, mqtt_username, mqtt_password)) {
Serial.println("ESP32开发板连接MQTT服务器成功");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
// 定阅主题
client.subscribe("hantu_iot/led_OnOff", 0);
// 发布消息
client.publish(topic, "Hi EMQX, I'm ESP32 ^^");
//设置参数据以获取精确数值
MQ3.setRegressionMethod(0);
MQ3.setA(0.3934);
MQ3.setB(-1.504);
MQ3.init();
MQ3.setRL(4.7);
Serial.print("Calibrating please wait.");
float calcR0 = 0;
for (int i = 1; i <= 10; i++) {
MQ3.update();
calcR0 += MQ3.calibrate(RatioMQ3CleanAir);
Serial.print(".");
}
MQ3.setR0(calcR0 / 10);
Serial.println(" done!.");
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
//解析payload获取LED灯的开和关的值
JsonDocument doc;
deserializeJson(doc, payload);
int led_logic = doc["LED"];
if (led_logic == 1) {
digitalWrite(redPin, LOW);
Serial.println("LED灯已打开");
}
if (led_logic == 0) {
digitalWrite(redPin, HIGH);
Serial.println("LED灯已关闭");
}
delay(50);
}
void loop() {
MQ3.update();
float concentration = MQ3.readSensor();
float BAC = map(concentration, 20, 500, 7, 184);
BAC = BAC / 1000 * 100;
payload = "";
jsonbuf["LED"] = digitalRead(redPin); //LED灯状态
jsonbuf["SSID"] = WiFi.SSID(); //wifi信息SSID
jsonbuf["IP"] = WiFi.localIP(); //wifi信息IP
jsonbuf["MAC"] = WiFi.macAddress(); //wifi信息MAC
jsonbuf["RSSI"] = WiFi.RSSI(); //wifi信息RSSI
jsonbuf["Status"] = WiFi.status(); //wifi信息Status
jsonbuf["MQ3B"] = int(trunc(concentration)); //传感器数据
serializeJson(jsonbuf, payload);
//发布Payload
if (jsonbuf != nullptr) {
const char *Temp = payload.c_str();
// 发布消息
client.publish(topic, Temp, payload.length());
}
client.loop();
/*Display显示*/
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("PPM ");
display.println(concentration, 0);
display.display();
delay(10000); //Sampling frequency
}
3.2.编译并上传程序
输入完成后,执行上传命令即可进行代码的编译并上传至开发板,点击上传
按钮开始上传
我们打开MQTTX工具,可以看到服务器接收到的数据
至此我们已经成功将传感器及开发板的LED灯状态、WiFi信息以json格式发送给了MQTT服务器。
下一步我们需要将采集到的数据以数据面板的形式展现出来,并存储到Mysql数据库中,这会用到另一个有趣的开发工具Node-RED,它以低代码的方式将硬件设备和在线服务连接到一起。
我们可以先预览一下Node-RED的开发界面及数据面板的运行效果