【DFRobot 云雀气象仪评测】Arduino无线智能空气监测系统搭建-06室内空气质量传感....
本帖最后由 eew_TKwwQ7 于 2023-12-17 23:09 编辑<p>为了让智能空气监测系统检测信息更加全面可靠,在室内加入多合一空气质量传感器SEN44,传感器是Sensrion上一代产品,该产品目前已经更新到SEN5X系列了,其检测有温度、湿度、PM2.5、VOC等参数,整个智能空气监测系统如图:</p>
<p> </p>
<p>SEN44:<br />
</p>
<p></p>
<p> </p>
<p><strong><span style="font-size:18px;">一、默认官网SEN44参考程序</span></strong></p>
<p> </p>
<p><span style="color:#e74c3c;"><span style="background-color:#1abc9c;">程序源码:</span></span></p>
<pre>
<code>#include <Arduino.h>
#include <SensirionUartSen44.h>
// Adjust as needed for you Arduino board.
//
#define SENSOR_SERIAL_INTERFACE Serial1
SensirionUartSen44 sen44;
void printModuleVersions() {
uint16_t error;
char errorMessage;
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;
unsigned char serialNumber;
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;
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;
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);
}
}
</code></pre>
<p>成功获取空气质量参数: <strong><span style="color:#e74c3c;">MassConcentrationPm4p0:8 MassConcentrationPm10p0:8 VocIndex:209.00 AmbientHumidity:71.35 AmbientTemperature:11.42</span></strong></p>
<p> </p>
<p> </p>
<p><strong><span style="font-size:18px;">二、修改软件串口</span></strong></p>
<p>因硬件串口1给Xbee使用,需要另外串口给SENN44传感器进行通信,官网Leonardo开发板中SoftwareSerial案例指出并不是所有I/O都能当作软件串口:</p>
<p> 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).</p>
<p> </p>
<p>那就直接用8、9当作软件串口</p>
<p> </p>
<p><span style="color:#e74c3c;"><span style="background-color:#1abc9c;">修改后程序代码:</span></span></p>
<pre>
<code>
#include <Arduino.h>
#include <SensirionUartSen44.h>
#include <SoftwareSerial.h>
// Adjust as needed for you Arduino board.
//
SoftwareSerial Serial3(8, 9);
#define SENSOR_SERIAL_INTERFACE Serial3
SensirionUartSen44 sen44;
void printModuleVersions() {
uint16_t error;
char errorMessage;
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;
unsigned char serialNumber;
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;
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;
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);
}
}
</code></pre>
<p>正常获取传感器SEN44数据,串口打印出获取空气质量数据:</p>
<p> </p>
<p>硬件接法如图实物图:</p>
<p> </p>
<p> </p>
<p>楼主费心了,提供的技术内容非常详实,实用价值很高,受教了,感谢楼主的无私</p>
页:
[1]