[分享] 【DFRobot 云雀气象仪评测】Arduino无线智能空气监测系统搭建-06室内空气质量传感....

eew_TKwwQ7   2023-12-13 20:41 楼主

为了让智能空气监测系统检测信息更加全面可靠,在室内加入多合一空气质量传感器SEN44,传感器是Sensrion上一代产品,该产品目前已经更新到SEN5X系列了,其检测有温度、湿度、PM2.5、VOC等参数,整个智能空气监测系统如图:

image.png  

SEN44:
image.png  

wKgZomTnNYaAT49WAADayiflSDU848.png

 

一、默认官网SEN44参考程序

image.png    

程序源码:

#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

1702822917757.jpg  

 

二、修改软件串口

因硬件串口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).

image.png  

那就直接用8、9当作软件串口

image.png  

修改后程序代码:


#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数据,串口打印出获取空气质量数据:

image.png  

硬件接法如图实物图:

697cdb20040e64dea8f571deec632d8.jpg  

 

本帖最后由 eew_TKwwQ7 于 2023-12-17 23:09 编辑

回复评论 (1)

楼主费心了,提供的技术内容非常详实,实用价值很高,受教了,感谢楼主的无私

点赞  2023-12-27 15:22
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复