为了让智能空气监测系统检测信息更加全面可靠,在室内加入多合一空气质量传感器SEN44,传感器是Sensrion上一代产品,该产品目前已经更新到SEN5X系列了,其检测有温度、湿度、PM2.5、VOC等参数,整个智能空气监测系统如图:
一、默认官网SEN44参考程序
程序源码:
#include <Arduino.h>
#include <SensirionUartSen44.h>
// Adjust as needed for you Arduino board.
// [Serial, Serial1, Serial2, etc.]
#define SENSOR_SERIAL_INTERFACE Serial1
SensirionUartSen44 sen44;
void printModuleVersions() {
uint16_t error;
char errorMessage[256];
uint8_t firmwareMajor;
uint8_t firmwareMinor;
bool firmwareDebug;
uint8_t hardwareMajor;
uint8_t hardwareMinor;
uint8_t protocolMajor;
uint8_t protocolMinor;
error = sen44.getVersion(firmwareMajor, firmwareMinor, firmwareDebug,
hardwareMajor, hardwareMinor, protocolMajor,
protocolMinor);
if (error) {
Serial.print("Error trying to execute getVersion(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
} else {
if (firmwareDebug) {
printf("Development firmware version: ");
}
Serial.print("Firmware: ");
Serial.print(firmwareMajor);
Serial.print(".");
Serial.print(firmwareMinor);
Serial.print(", ");
Serial.print("Hardware: ");
Serial.print(hardwareMajor);
Serial.print(".");
Serial.print(hardwareMinor);
Serial.print(", ");
Serial.print("Protocol: ");
Serial.print(protocolMajor);
Serial.print(".");
Serial.println(protocolMinor);
}
}
void printSerialNumber() {
uint16_t error;
char errorMessage[256];
unsigned char serialNumber[32];
uint8_t serialNumberSize = 32;
error = sen44.getSerialNumber(serialNumber, serialNumberSize);
if (error) {
Serial.print("Error trying to execute getSerialNumber(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
} else {
Serial.print("Serial number: ");
Serial.println((char*)serialNumber);
}
}
void setup() {
uint16_t error;
char errorMessage[256];
Serial.begin(115200);
while (!Serial) {
delay(100);
}
SENSOR_SERIAL_INTERFACE.begin(115200);
while (!SENSOR_SERIAL_INTERFACE) {
delay(100);
}
sen44.begin(SENSOR_SERIAL_INTERFACE);
error = sen44.deviceReset();
if (error) {
Serial.print("Error trying to execute getSerialNumber(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}
// Print SEN44 module information
printSerialNumber();
printModuleVersions();
// Start Measurement
error = sen44.startMeasurement();
if (error) {
Serial.print("Error trying to execute startMeasurement(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}
}
void loop() {
uint16_t error;
char errorMessage[256];
delay(1000);
// Read Measurement
uint16_t massConcentrationPm1p0;
uint16_t massConcentrationPm2p5;
uint16_t massConcentrationPm4p0;
uint16_t massConcentrationPm10p0;
float vocIndex;
float ambientHumidity;
float ambientTemperature;
error = sen44.readMeasuredMassConcentrationAndAmbientValues(
massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0,
massConcentrationPm10p0, vocIndex, ambientHumidity, ambientTemperature);
if (error) {
Serial.print("Error trying to execute "
"readMeasuredMassConcentrationAndAmbientValues(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
} else {
Serial.print("MassConcentrationPm1p0:");
Serial.print(massConcentrationPm1p0);
Serial.print("\t");
Serial.print("MassConcentrationPm2p5:");
Serial.print(massConcentrationPm2p5);
Serial.print("\t");
Serial.print("MassConcentrationPm4p0:");
Serial.print(massConcentrationPm4p0);
Serial.print("\t");
Serial.print("MassConcentrationPm10p0:");
Serial.print(massConcentrationPm10p0);
Serial.print("\t");
Serial.print("VocIndex:");
Serial.print(vocIndex);
Serial.print("\t");
Serial.print("AmbientHumidity:");
Serial.print(ambientHumidity);
Serial.print("\t");
Serial.print("AmbientTemperature:");
Serial.println(ambientTemperature);
}
}
成功获取空气质量参数: MassConcentrationPm4p0:8 MassConcentrationPm10p0:8 VocIndex:209.00 AmbientHumidity:71.35 AmbientTemperature:11.42
二、修改软件串口
因硬件串口1给Xbee使用,需要另外串口给SENN44传感器进行通信,官网Leonardo开发板中SoftwareSerial案例指出并不是所有I/O都能当作软件串口:
Not all pins on the Leonardo and Micro support change interrupts, so only the following can be used for RX:8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
那就直接用8、9当作软件串口
修改后程序代码:
#include <Arduino.h>
#include <SensirionUartSen44.h>
#include <SoftwareSerial.h>
// Adjust as needed for you Arduino board.
// [Serial, Serial1, Serial2, etc.]
SoftwareSerial Serial3(8, 9);
#define SENSOR_SERIAL_INTERFACE Serial3
SensirionUartSen44 sen44;
void printModuleVersions() {
uint16_t error;
char errorMessage[256];
uint8_t firmwareMajor;
uint8_t firmwareMinor;
bool firmwareDebug;
uint8_t hardwareMajor;
uint8_t hardwareMinor;
uint8_t protocolMajor;
uint8_t protocolMinor;
error = sen44.getVersion(firmwareMajor, firmwareMinor, firmwareDebug,
hardwareMajor, hardwareMinor, protocolMajor,
protocolMinor);
if (error) {
Serial.print("Error trying to execute getVersion(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
} else {
if (firmwareDebug) {
printf("Development firmware version: ");
}
Serial.print("Firmware: ");
Serial.print(firmwareMajor);
Serial.print(".");
Serial.print(firmwareMinor);
Serial.print(", ");
Serial.print("Hardware: ");
Serial.print(hardwareMajor);
Serial.print(".");
Serial.print(hardwareMinor);
Serial.print(", ");
Serial.print("Protocol: ");
Serial.print(protocolMajor);
Serial.print(".");
Serial.println(protocolMinor);
}
}
void printSerialNumber() {
uint16_t error;
char errorMessage[256];
unsigned char serialNumber[32];
uint8_t serialNumberSize = 32;
error = sen44.getSerialNumber(serialNumber, serialNumberSize);
if (error) {
Serial.print("Error trying to execute getSerialNumber(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
} else {
Serial.print("Serial number: ");
Serial.println((char*)serialNumber);
}
}
void setup() {
uint16_t error;
char errorMessage[256];
Serial.begin(115200);
while (!Serial) {
delay(100);
}
SENSOR_SERIAL_INTERFACE.begin(115200);
while (!SENSOR_SERIAL_INTERFACE) {
delay(100);
}
sen44.begin(SENSOR_SERIAL_INTERFACE);
error = sen44.deviceReset();
if (error) {
Serial.print("Error trying to execute getSerialNumber(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}
// Print SEN44 module information
printSerialNumber();
printModuleVersions();
// Start Measurement
error = sen44.startMeasurement();
if (error) {
Serial.print("Error trying to execute startMeasurement(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}
}
void loop() {
uint16_t error;
char errorMessage[256];
delay(1000);
// Read Measurement
uint16_t massConcentrationPm1p0;
uint16_t massConcentrationPm2p5;
uint16_t massConcentrationPm4p0;
uint16_t massConcentrationPm10p0;
float vocIndex;
float ambientHumidity;
float ambientTemperature;
error = sen44.readMeasuredMassConcentrationAndAmbientValues(
massConcentrationPm1p0, massConcentrationPm2p5, massConcentrationPm4p0,
massConcentrationPm10p0, vocIndex, ambientHumidity, ambientTemperature);
if (error) {
Serial.print("Error trying to execute "
"readMeasuredMassConcentrationAndAmbientValues(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
} else {
Serial.print("MassConcentrationPm1p0:");
Serial.print(massConcentrationPm1p0);
Serial.print("\t");
Serial.print("MassConcentrationPm2p5:");
Serial.print(massConcentrationPm2p5);
Serial.print("\t");
Serial.print("MassConcentrationPm4p0:");
Serial.print(massConcentrationPm4p0);
Serial.print("\t");
Serial.print("MassConcentrationPm10p0:");
Serial.print(massConcentrationPm10p0);
Serial.print("\t");
Serial.print("VocIndex:");
Serial.print(vocIndex);
Serial.print("\t");
Serial.print("AmbientHumidity:");
Serial.print(ambientHumidity);
Serial.print("\t");
Serial.print("AmbientTemperature:");
Serial.println(ambientTemperature);
}
}
正常获取传感器SEN44数据,串口打印出获取空气质量数据:
硬件接法如图实物图:
本帖最后由 eew_TKwwQ7 于 2023-12-17 23:09 编辑