esp32-arduino-matter
とりあえず動いた
を使っています
code:platformio.ini
platform = espressif32 @ 6.6.0
board = m5stack-atom
framework = arduino
build_unflags = -std=gnu++11
build_flags =
-std=gnu++17
lib_deps =
crankyoldgit/IRremoteESP8266@^2.8.6
board_build.partitions=min_spiffs.csv
monitor_speed = 115200
Matterで送受信します 実際のリモコンと同期します
code:main.cpp
#include <app/server/OnboardingCodesUtil.h> #include <credentials/examples/DeviceAttestationCredsExample.h> using namespace chip;
using namespace chip::app::Clusters;
using namespace esp_matter;
using namespace esp_matter::endpoint;
// ピン設定
const uint16_t kRecvPin = 32; // IR受信機を接続するGPIOピン
const uint16_t kIrLedPin = 26; // IR LEDを接続するGPIOピン
// IR送受信オブジェクトの作成
IRsend irsend(kIrLedPin);
IRrecv irrecv(kRecvPin);
decode_results results;
// Matter関連の変数
const uint32_t CLUSTER_ID = OnOff::Id;
const uint32_t ATTRIBUTE_ID = OnOff::Attributes::OnOff::Id;
uint16_t light_endpoint_id = 0;
attribute_t *attribute_ref;
// デバイスイベントのコールバック(必要に応じて実装)
static void on_device_event(const ChipDeviceEvent *event, intptr_t arg) {}
static esp_err_t on_identification(identification::callback_type_t type, uint16_t endpoint_id,
uint8_t effect_id, uint8_t effect_variant, void *priv_data) {
return ESP_OK;
}
// IR信号を送信する関数
void sendIRSignal(bool isOn) {
irrecv.disableIRIn(); // 受信を一時停止
if (isOn) {
// "on" 信号を送信
Serial.println("Sending ON IR Signal...");
irsend.sendPanasonic64(0x344A90B424, 40);
Serial.println("Sent: 0x344A90B424 (ON)");
} else {
// "off" 信号を送信
Serial.println("Sending OFF IR Signal...");
irsend.sendPanasonic64(0x344A90F464, 40);
Serial.println("Sent: 0x344A90F464 (OFF)");
}
delay(50); // 送信完了を待機
irrecv.enableIRIn(); // 受信を再開
}
// 属性更新要求のリスナー
static esp_err_t on_attribute_update(attribute::callback_type_t type, uint16_t endpoint_id, uint32_t cluster_id,
uint32_t attribute_id, esp_matter_attr_val_t *val, void *priv_data) {
if (type == attribute::PRE_UPDATE && endpoint_id == light_endpoint_id &&
cluster_id == CLUSTER_ID && attribute_id == ATTRIBUTE_ID) {
// OnOff属性が更新されたときの処理
bool new_state = val->val.b;
Serial.printf("Received Matter command: %s\n", new_state ? "ON" : "OFF");
sendIRSignal(new_state);
}
return ESP_OK;
}
// IR信号を受信する関数
void receiveIRSignal() {
if (irrecv.decode(&results)) {
Serial.println("IR Signal Received");
Serial.print("Protocol: ");
Serial.println(typeToString(results.decode_type));
Serial.print("Data: 0x");
Serial.println(results.value, HEX);
// 受信したIR信号に応じてMatterデバイスの状態を更新
if (results.value == 0x344A90B424) {
// "ON" 信号を受信
Serial.println("Received ON IR Signal");
esp_matter_attr_val_t val = esp_matter_bool(true);
// 修正点: 属性を更新する正しい方法を使用
attribute::update(light_endpoint_id, CLUSTER_ID, ATTRIBUTE_ID, &val);
} else if (results.value == 0x344A90F464) {
// "OFF" 信号を受信
Serial.println("Received OFF IR Signal");
esp_matter_attr_val_t val = esp_matter_bool(false);
// 修正点: 属性を更新する正しい方法を使用
attribute::update(light_endpoint_id, CLUSTER_ID, ATTRIBUTE_ID, &val);
} else {
Serial.println("Unknown IR Signal");
}
irrecv.resume(); // 次の受信に備える
}
}
// 初期設定
void setup() {
Serial.begin(115200);
irsend.begin(); // IR送信を開始
irrecv.enableIRIn(); // IR受信を開始
Serial.println("IR Receiver & Sender Ready");
// デバッグログを有効化
esp_log_level_set("*", ESP_LOG_DEBUG);
// Matterノードの設定
node::config_t node_config;
node_t *node = node::create(&node_config, on_attribute_update, on_identification);
// Lightエンドポイントとクラスタの設定
on_off_light::config_t light_config;
light_config.on_off.on_off = false;
light_config.on_off.lighting.start_up_on_off = false;
endpoint_t *endpoint = on_off_light::create(node, &light_config, ENDPOINT_FLAG_NONE, NULL);
// OnOff属性の参照を保存
attribute_ref = attribute::get(cluster::get(endpoint, CLUSTER_ID), ATTRIBUTE_ID);
// エンドポイントIDを保存
light_endpoint_id = endpoint::get_id(endpoint);
// DACの設定
esp_matter::set_custom_dac_provider(chip::Credentials::Examples::GetExampleDACProvider());
// Matterデバイスの起動
esp_matter::start(on_device_event);
// オンボーディングコードの表示
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
}
// メインループ
void loop() {
// IR信号の受信を処理
receiveIRSignal();
delay(100); // 少し待機
}
esp32-s3って違うんですね
monitor_speed = 115200
build_flags = -DARDUINO_USB_CDC_ON_BOOT=1