本文提供了基于ESP32开发板快速完成甲醛传感器数据读取的方式,可以用于开发甲醛检测仪。
准备
- 传感器:达特WZ-S甲醛传感器
- 开发板:安信可ESP32 NodeMCU-32S
- 开发环境:Arduino
- 依赖库: WZ (https://github.com/leonlucc/WZ)
引脚连接
ESP32 | WZ-S |
---|---|
5V | 5V |
GND | GND |
GPIO17 | RXD |
GPIO16 | TXD |
代码
主动模式读数
#include "WZ.h"
/////////////////////////// ESP32-S NodeMCU Board
/*
* There are three serial ports on the ESP32 known as UART0, UART1 and UART2.
*
* UART0 (GPIO1 - TX0, GPIO3 - RX0) is used to communicate with the ESP32 for programming and during reset/boot.
* UART1 (GPIO10 - TX1, GPIO9 - RX1) is unused and can be used for your projects. Some boards use this port for SPI Flash access though
* UART2 (GPIO17 - TX2, GPIO16 - RX2) is unused and can be used for your projects.
*
*/
WZ wz(Serial2); // UART2
WZ::DATA hcho_data;
void setup()
{
Serial.begin(115200);
Serial2.begin(9600);
// wz.activeMode();
}
void loop()
{
if (wz.read(hcho_data))
{
Serial.print("HCHO (ppd): ");
Serial.println(hcho_data.HCHO_PPB);
Serial.print("HCHO (ug/m3): ");
Serial.println(hcho_data.HCHO_UGM3); // no data here, 0 returned
Serial.println();
}
// Do other stuff...
}
被动模式读数
#include "WZ.h"
/////////////////////////// ESP32-S NodeMCU Board
/*
* There are three serial ports on the ESP32 known as UART0, UART1 and UART2.
*
* UART0 (GPIO1 - TX0, GPIO3 - RX0) is used to communicate with the ESP32 for programming and during reset/boot.
* UART1 (GPIO10 - TX1, GPIO9 - RX1) is unused and can be used for your projects. Some boards use this port for SPI Flash access though
* UART2 (GPIO17 - TX2, GPIO16 - RX2) is unused and can be used for your projects.
*
*/
WZ wz(Serial2); // UART2
WZ::DATA hcho_data;
void setup()
{
Serial.begin(115200);
Serial2.begin(9600);
wz.passiveMode();
}
void loop()
{
wz.requestRead();
if (wz.readUntil(hcho_data))
{
Serial.print("HCHO (ppd): ");
Serial.println(hcho_data.HCHO_PPB);
Serial.print("HCHO (ug/m3): ");
Serial.println(hcho_data.HCHO_UGM3);
Serial.println();
}
delay(1000);
// Do other stuff...
}