Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Вопрос по коду (CON-1557) #1285

Open
1KALININ629 opened this issue Feb 19, 2025 · 0 comments
Open

Вопрос по коду (CON-1557) #1285

1KALININ629 opened this issue Feb 19, 2025 · 0 comments

Comments

@1KALININ629
Copy link

#include "Matter.h"
#include <app/server/OnboardingCodesUtil.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
#include <esp_matter.h>
#include <esp_matter_core.h>
#include <esp_matter_cluster.h>
#include <esp_matter_endpoint.h>
#include <ESP32Servo.h>
#include <nvs_flash.h> // Добавлено для инициализации NVS
#include <WiFi.h>

using namespace chip;
using namespace chip::app::Clusters;
using namespace esp_matter;
using namespace esp_matter::endpoint;

// Пины для сервоприводов
const int SERVO_PIN_1 = 12;
const int SERVO_PIN_2 = 13;

// Кастомный кластер и атрибуты для управления приводом окна
const uint32_t CUSTOM_WINDOW_COVERING_CLUSTER_ID = 0x1001;
const uint32_t CURRENT_POSITION_ATTRIBUTE_ID = 0x0001;
const uint32_t TARGET_POSITION_ATTRIBUTE_ID = 0x0002;

// Идентификатор конечной точки и атрибуты
uint16_t window_endpoint_id = 0;
attribute_t *current_position_attribute;
attribute_t *target_position_attribute;

// Текущее положение окна
uint16_t current_position = 0;

// Объекты для управления сервоприводами
Servo servo1;
Servo servo2;

// WiFi credentials (replace with your actual credentials)

// Callback функция для событий устройства
static void on_device_event(const ChipDeviceEvent *event, intptr_t arg) {
Serial.print("Device event: ");
Serial.println(event->Type);
}

// Обработчик обновления атрибутов
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) {
Serial.printf("Attribute update: type=%d, endpoint_id=%d, cluster_id=0x%x, attribute_id=0x%x\n",
type, endpoint_id, cluster_id, attribute_id);

if (type == attribute::PRE_UPDATE && endpoint_id == window_endpoint_id &&
    cluster_id == CUSTOM_WINDOW_COVERING_CLUSTER_ID && attribute_id == TARGET_POSITION_ATTRIBUTE_ID) {
    // Получаем целевое положение и перемещаем окно
    uint16_t target_position = val->val.u16;
    Serial.printf("Target position received: %d\n", target_position);
    move_window_to_position(target_position);
}
return ESP_OK;

}

// Функция для перемещения окна в целевое положение
void move_window_to_position(uint16_t target_position) {
// Ограничиваем значение целевого положения от 0 до 100
target_position = std::max(0, std::min(100, (int)target_position));

// Преобразуем положение в угол для сервопривода
uint16_t target_angle = static_cast<uint16_t>(map(target_position, 0, 100, 0, 180));

// Перемещаем окно в целевое положение
Serial.printf("Moving window to position: %d (angle: %d)\n", target_position, target_angle);
if (target_position > current_position) {
    for (int i = current_position; i <= target_position; i++) {
        servo1.write(map(i, 0, 100, 0, 180));
        delay(20);
        Serial.printf("Moving servo1 to angle: %d\n", map(i, 0, 100, 0, 180));
    }
} else if (target_position < current_position) {
    for (int i = current_position; i >= target_position; i--) {
        servo2.write(map(i, 0, 100, 0, 180));
        delay(20);
        Serial.printf("Moving servo2 to angle: %d\n", map(i, 0, 100, 0, 180));
    }
}

// Обновляем текущее положение
current_position = target_position;

// Обновляем атрибут текущего положения
esp_matter_attr_val_t position_val = esp_matter_uint16(current_position);
attribute::update(window_endpoint_id, CUSTOM_WINDOW_COVERING_CLUSTER_ID, CURRENT_POSITION_ATTRIBUTE_ID, &position_val);
Serial.printf("Current position updated to: %d\n", current_position);

}

// Создание кастомного кластера для управления приводом окна
cluster_t *create_custom_window_covering_cluster(endpoint_t *endpoint) {
cluster_t *cluster = cluster::create(endpoint, CUSTOM_WINDOW_COVERING_CLUSTER_ID, CLUSTER_FLAG_SERVER);
current_position_attribute = attribute::create(cluster, CURRENT_POSITION_ATTRIBUTE_ID, ATTRIBUTE_FLAG_NONE, esp_matter_uint16(0));
target_position_attribute = attribute::create(cluster, TARGET_POSITION_ATTRIBUTE_ID, ATTRIBUTE_FLAG_WRITABLE, esp_matter_uint16(0));
return cluster;
}

void setup() {
Serial.begin(115200);

// Initialize NVS
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
    ESP_ERROR_CHECK(nvs_flash_erase());
    err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);


Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

// Инициализация сервоприводов
servo1.attach(SERVO_PIN_1);
servo2.attach(SERVO_PIN_2);
servo1.write(0);
servo2.write(0);
Serial.println("Servos initialized");

// Настройка узла Matter
node::config_t node_config;
node_t *node = node::create(&node_config, on_attribute_update, nullptr);
Serial.println("Matter node created");

// Создание конечной точки
endpoint_t *endpoint = endpoint::create(node, ENDPOINT_FLAG_NONE, nullptr);
Serial.println("Endpoint created");

// Создание кастомного кластера для управления приводом окна
create_custom_window_covering_cluster(endpoint);
Serial.println("Custom cluster created");

// Сохранение идентификатора конечной точки
window_endpoint_id = endpoint::get_id(endpoint);
Serial.printf("Endpoint ID: %d\n", window_endpoint_id);

// Настройка DAC (пример использования)
esp_matter::set_custom_dac_provider(chip::Credentials::Examples::GetExampleDACProvider());
Serial.println("DAC provider set");

// Start Matter device
Serial.println("Starting Matter...");
esp_matter::start(on_device_event);
Serial.println("Matter started");

// Print codes for Matter onboarding
Serial.println("Printing onboarding codes...");
PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE));
Serial.println("Onboarding codes printed");

}

void loop() {
// Основной цикл пуст
}

Всем привет ! Когда работал над проектом, столкнулся с такой проблемой, код компилируется, но не выводит QR-COD для подключения устройства, что может быть не так?

@github-actions github-actions bot changed the title Вопрос по коду Вопрос по коду (CON-1557) Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant