add ha ok.

This commit is contained in:
amass 2024-06-01 23:47:21 +08:00
parent 1b628e631c
commit ac6a5433c4
3 changed files with 66 additions and 21 deletions

View File

@ -1,5 +1,5 @@
idf_component_register(SRCS idf_component_register(SRCS
MqttClient.h MqttClient.cpp MqttClient.h MqttClient.cpp
INCLUDE_DIRS . INCLUDE_DIRS .
REQUIRES mqtt REQUIRES mqtt json LedController
) )

View File

@ -1,35 +1,69 @@
#include "MqttClient.h" #include "MqttClient.h"
#include "LedController.h"
#include <cJSON.h>
#include <esp_log.h> #include <esp_log.h>
#include <iostream> #include <iostream>
#include <mqtt_client.h> #include <string_view>
static const char *TAG = "mqtt"; static const char *TAG = "mqtt";
void MqttClient::onConnected(struct esp_mqtt_client *client) {
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
cJSON *root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "~", "homeassistant/light/bedroom");
cJSON_AddStringToObject(root, "name", "Bedroom");
cJSON_AddStringToObject(root, "uniq_id", "bedroom_light");
cJSON_AddStringToObject(root, "cmd_t", "~/set");
cJSON_AddStringToObject(root, "stat_t", "~/state");
cJSON_AddStringToObject(root, "schema", "json");
cJSON_AddNumberToObject(root, "brightness_scale", 100);
const char *modes[] = {
"color_temp",
};
cJSON *supported_color_modes = cJSON_CreateStringArray(modes, sizeof(modes) / sizeof(modes[0]));
cJSON_AddItemToObject(root, "supported_color_modes", supported_color_modes);
cJSON_AddTrueToObject(root, "brightness");
auto json = cJSON_PrintUnformatted(root);
int msg_id = esp_mqtt_client_publish(client, "homeassistant/light/bedroom/config", json, strlen(json), 1, 0);
cJSON_free(json);
cJSON_Delete(root);
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
m_setStatusTopic = "homeassistant/light/bedroom/set";
msg_id = esp_mqtt_client_subscribe(client, m_setStatusTopic.c_str(), 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
}
void MqttClient::onSetStatus(const char *data, int size) {
ESP_LOGI(TAG, "set: %s", data);
cJSON *root = cJSON_Parse(data);
cJSON *brightness = cJSON_GetObjectItem(root, "brightness");
if (brightness != nullptr) {
ESP_LOGI(TAG, "brightness: %d", brightness->valueint);
LedController::instance()->setDuty(1, brightness->valueint);
}
cJSON_Delete(root);
}
static void log_error_if_nonzero(const char *message, int error_code) { static void log_error_if_nonzero(const char *message, int error_code) {
if (error_code != 0) { if (error_code != 0) {
ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code); ESP_LOGE(TAG, "Last error %s: 0x%x", message, error_code);
} }
} }
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) { void MqttClient::eventHandler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
auto self = reinterpret_cast<MqttClient *>(handler_args);
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32 "", base, event_id); ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32 "", base, event_id);
auto event = reinterpret_cast<esp_mqtt_event_handle_t>(event_data); auto event = reinterpret_cast<esp_mqtt_event_handle_t>(event_data);
esp_mqtt_client_handle_t client = event->client; esp_mqtt_client_handle_t client = event->client;
int msg_id; int msg_id;
switch ((esp_mqtt_event_id_t)event_id) { switch ((esp_mqtt_event_id_t)event_id) {
case MQTT_EVENT_CONNECTED: case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED"); self->onConnected(client);
msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0);
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1");
ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id);
break; break;
case MQTT_EVENT_DISCONNECTED: case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED"); ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
@ -47,9 +81,13 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id); ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
break; break;
case MQTT_EVENT_DATA: case MQTT_EVENT_DATA:
ESP_LOGI(TAG, "MQTT_EVENT_DATA"); ESP_LOGI(TAG, "MQTT_EVENT_DATA %s", self->m_setStatusTopic.c_str());
if (self->m_setStatusTopic == std::string_view(event->topic, event->topic_len)) {
self->onSetStatus(event->data, event->data_len);
} else {
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic); printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
printf("DATA=%.*s\r\n", event->data_len, event->data); printf("DATA=%.*s\r\n", event->data_len, event->data);
}
break; break;
case MQTT_EVENT_ERROR: case MQTT_EVENT_ERROR:
ESP_LOGI(TAG, "MQTT_EVENT_ERROR"); ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
@ -74,8 +112,8 @@ bool MqttClient::initialize(const std::string &username, const std::string &pass
config.credentials.authentication.password = password.c_str(); config.credentials.authentication.password = password.c_str();
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&config); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&config);
esp_mqtt_client_register_event(client, static_cast<esp_mqtt_event_id_t>(ESP_EVENT_ANY_ID), mqtt_event_handler, esp_mqtt_client_register_event(client, static_cast<esp_mqtt_event_id_t>(ESP_EVENT_ANY_ID),
NULL); &MqttClient::eventHandler, this);
esp_mqtt_client_start(client); esp_mqtt_client_start(client);
ESP_LOGI(TAG, "connect to %s, username: %s, password: %s", config.broker.address.uri, config.credentials.username, ESP_LOGI(TAG, "connect to %s, username: %s, password: %s", config.broker.address.uri, config.credentials.username,
config.credentials.authentication.password); config.credentials.authentication.password);

View File

@ -1,6 +1,7 @@
#ifndef __MQTTCLIENT_H__ #ifndef __MQTTCLIENT_H__
#define __MQTTCLIENT_H__ #define __MQTTCLIENT_H__
#include <mqtt_client.h>
#include <string> #include <string>
class MqttClient { class MqttClient {
@ -9,6 +10,12 @@ public:
bool initialize(const std::string &username, const std::string &password); bool initialize(const std::string &username, const std::string &password);
protected: protected:
static void eventHandler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data);
void onConnected(struct esp_mqtt_client *client);
void onSetStatus(const char *data, int size);
MqttClient() = default; MqttClient() = default;
private:
std::string m_setStatusTopic;
}; };
#endif // __MQTTCLIENT_H__ #endif // __MQTTCLIENT_H__