Skip to content

Matter carbon dioxide concentration measurement sensor (ESP32 + SCD41) example

License

Notifications You must be signed in to change notification settings

YOGYUI/matter-esp32-scd41

Repository files navigation

Matter CO2 Sensor Example (ESP32 + SCD41)

Matter 이산화탄소 농도 측정 센서 예제 프로젝트
다음 Matter 클러스터들에 대한 코드 구현 방법을 알아본다

  • Carbon Dioxide Concentration Measurement (Cluster Id: 0x040D)
  • Temperature Measurement (Cluster ID: 0x0402)
  • Relative Humidity Measurement (Cluster ID: 0x0405)

2024년 2월 18일 기준 Apple Home 및 Google Home 모두 Air Quality Device Type 지원하지 않음

Software (Matter)

1개의 Endpoint가 아래와 같이 생성된다. (센서 모듈이 CO2 농도 뿐만 아니라 온도와 상대 습도 데이터도 제공함)

  1. Endpoint ID 1
    Device Type: Air Quality Sensor (Classification ID: 0x002C)
    [Clusters]
    • Air Quality (Cluster ID: 0x005B)
    • Carbon Dioxide Concentration Measurement (Cluster ID: 0x040D)
      [Attributes]
      • Measured Value (Attribute ID: 0x0000)
      • Min Measured Value (Attribute ID: 0x0001)
      • Max Measured Value (Attribute ID: 0x0002)
      • Measurement Unit (Attribute ID: 0x0008)
    • Temperature Measurement (Cluster ID: 0x0402)
      [Attributes]
      • Measured Value (Attribute ID: 0x0000)
      • Min Measured Value (Attribute ID: 0x0001)
      • Max Measured Value (Attribute ID: 0x0002)
    • Relative Humidity Measurement (Cluster ID: 0x0405)
      [Attributes]
      • Measured Value (Attribute ID: 0x0000)
      • Min Measured Value (Attribute ID: 0x0001)
      • Max Measured Value (Attribute ID: 0x0002)

Hardware

SCD41: I2C 통신 방식의 CO2 센서 IC 사용


I2C GPIO 핀번호 변경은 /main/include/definition.h에서 아래 항목을 수정
default: SDA = GPIO18 / SCL = GPIO19

#define GPIO_PIN_I2C_SCL 19
#define GPIO_PIN_I2C_SDA 18

SDK Version

SDK Source Code Modification

  • esp_matter_cluster.h
    namespace carbon_dioxide_concentration_measurement {
    typedef struct config {
        uint16_t cluster_revision;
        config() : cluster_revision(3) {}
    } config_t;
    
    // 아래 한줄 추가
    cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags);
    } /* carbon_dioxide_concentration_measurement */
  • esp_matter_attribute.cpp
    static esp_err_t get_attr_val_from_data(esp_matter_attr_val_t *val, EmberAfAttributeType attribute_type,
                                        uint16_t attribute_size, uint8_t *value,
                                        const EmberAfAttributeMetadata * attribute_metadata)
    {
        switch (attribute_type) {
        /*
        * 기존 코드 유지
        */
        
        // 아래 블록 추가
        case ZCL_SINGLE_ATTRIBUTE_TYPE: {
            using Traits = chip::app::NumericAttributeTraits<float>;
            Traits::StorageType attribute_value;
            memcpy((float *)&attribute_value, value, sizeof(Traits::StorageType));
            if (attribute_metadata->IsNullable()) {
                if (Traits::IsNullValue(attribute_value)) {
                    *val = esp_matter_nullable_float(nullable<float>());
                } else {
                    *val = esp_matter_nullable_float(attribute_value);
                }
            } else {
                *val = esp_matter_float(attribute_value);
            }
            break;
        }
        
        default:
            *val = esp_matter_invalid(NULL);
            break;
        }
    
        return ESP_OK;
    }

Helper Scripts

SDK 클론 및 설치

$ source ./scripts/install_sdk.sh

SDK (idf.py) 준비

$ source ./scripts/prepare_sdk.sh

Build & Flash Firmware

  1. Factory Partition (Matter DAC)
    $ source ./scripts/flash_factory_dac_provider.sh
  2. Configure project
    $ idf.py set-target esp32
  3. Build Firmware
    $ idf.py build
  4. Flash Firmware
    $ idf.py -p ${seiral_port} flash monitor

QR Code for commisioning

qrcode.png

Read Data using CHIP-TOOL

이산화탄소 농도 측정값 읽기

$ chip-tool carbondioxideconcentrationmeasurement read measured-value {pairing_node_id} 1

온도 측정값 읽기

$ chip-tool temperaturemeasurement read measured-value {pairing_node_id} 1

습도 측정값 읽기

$ chip-tool relativehumiditymeasurement read measured-value {pairing_node_id} 1

References

Matter 이산화탄소 농도 측정 클러스터 개발 예제 (ESP32)

About

Matter carbon dioxide concentration measurement sensor (ESP32 + SCD41) example

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published