add connect wifi mqtt auto.
This commit is contained in:
@ -64,4 +64,13 @@ void Application::initializeWifi() {
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
bool Application::contains(const std::string &key) {
|
||||
nvs_handle_t hanlde;
|
||||
esp_err_t error = nvs_open(Namespace, NVS_READWRITE, &hanlde);
|
||||
nvs_type_t type;
|
||||
error = nvs_find_key(hanlde, key.c_str(), &type);
|
||||
nvs_close(hanlde);
|
||||
return error == ESP_OK;
|
||||
}
|
@ -26,11 +26,12 @@ public:
|
||||
if (error != ESP_OK) {
|
||||
ESP_LOGI("App", "nvs_open() failed.");
|
||||
}
|
||||
|
||||
if (std::is_same_v<char *, std::decay_t<T>>) {
|
||||
if constexpr (std::is_same_v<char *, std::decay_t<T>>) {
|
||||
error = nvs_set_str(hanlde, key.c_str(), value);
|
||||
} else if constexpr (std::is_same_v<std::string, T>) {
|
||||
error = nvs_set_str(hanlde, key.c_str(), value.c_str());
|
||||
} else if constexpr (std::is_same_v<int32_t, T> || std::is_same_v<int, T>) {
|
||||
error = nvs_set_i32(hanlde, key.c_str(), value);
|
||||
} else {
|
||||
ESP_LOGW("App", "unknown data");
|
||||
}
|
||||
@ -43,8 +44,25 @@ public:
|
||||
|
||||
template <typename T>
|
||||
T field(const std::string &key) {
|
||||
T ret{};
|
||||
nvs_handle_t hanlde;
|
||||
nvs_open(Namespace, NVS_READWRITE, &hanlde);
|
||||
if constexpr (std::is_same_v<char *, std::decay_t<T>> || std::is_same_v<std::string, T>) {
|
||||
size_t requiredSize;
|
||||
nvs_get_str(hanlde, key.c_str(), nullptr, &requiredSize);
|
||||
ret.resize(requiredSize);
|
||||
nvs_get_str(hanlde, key.c_str(), ret.data(), &requiredSize);
|
||||
} else if constexpr (std::is_same_v<int, T>) {
|
||||
ret = field<int32_t>(key);
|
||||
} else if constexpr (std::is_same_v<int32_t, T>) {
|
||||
nvs_get_i32(hanlde, key.c_str(), &ret);
|
||||
}
|
||||
nvs_close(hanlde);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool contains(const std::string &key);
|
||||
|
||||
protected:
|
||||
static void eventHandler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
||||
void initializeWifi();
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "MqttClient.h"
|
||||
#include "Application.h"
|
||||
#include "LedController.h"
|
||||
#include <cJSON.h>
|
||||
#include <esp_log.h>
|
||||
@ -17,7 +18,7 @@ void MqttClient::onConnected(struct esp_mqtt_client *client) {
|
||||
cJSON_AddStringToObject(root, "cmd_t", "~/set");
|
||||
cJSON_AddStringToObject(root, "stat_t", "~/state");
|
||||
cJSON_AddStringToObject(root, "schema", "json");
|
||||
cJSON_AddNumberToObject(root, "brightness_scale", 100);
|
||||
cJSON_AddNumberToObject(root, "brightness_scale", LedController::Resolution);
|
||||
|
||||
const char *modes[] = {
|
||||
"color_temp",
|
||||
@ -33,20 +34,62 @@ void MqttClient::onConnected(struct esp_mqtt_client *client) {
|
||||
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
|
||||
|
||||
m_setStatusTopic = "homeassistant/light/bedroom/set";
|
||||
m_statusTopic = "homeassistant/light/bedroom/state";
|
||||
msg_id = esp_mqtt_client_subscribe(client, m_setStatusTopic.c_str(), 0);
|
||||
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
|
||||
|
||||
reportLedState();
|
||||
}
|
||||
|
||||
void MqttClient::onSetStatus(const char *data, int size) {
|
||||
ESP_LOGI(TAG, "set: %s", data);
|
||||
ESP_LOGI(TAG, "set: %.*s", size, 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);
|
||||
LedController::instance()->setBrightness(brightness->valueint);
|
||||
Application::instance()->setField("brightness", brightness->valueint);
|
||||
}
|
||||
|
||||
cJSON *color_temp = cJSON_GetObjectItem(root, "color_temp");
|
||||
if (color_temp != nullptr) {
|
||||
ESP_LOGI(TAG, "color_temp: %d", color_temp->valueint);
|
||||
LedController::instance()->setColorTemperature(color_temp->valueint);
|
||||
Application::instance()->setField("color_temp", color_temp->valueint);
|
||||
}
|
||||
|
||||
cJSON *state = cJSON_GetObjectItem(root, "state");
|
||||
if (state != nullptr) {
|
||||
if (std::string_view(state->valuestring) == "ON") {
|
||||
LedController::instance()->setEnabled(true);
|
||||
} else {
|
||||
LedController::instance()->setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
reportLedState();
|
||||
}
|
||||
|
||||
void MqttClient::reportLedState() {
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(root, "state", LedController::instance()->enabled() ? "ON" : "OFF");
|
||||
cJSON_AddNumberToObject(root, "brightness", LedController::instance()->brightness());
|
||||
cJSON_AddNumberToObject(root, "color_temp", LedController::instance()->colorTemperature());
|
||||
|
||||
auto text = cJSON_PrintUnformatted(root);
|
||||
if (text != nullptr) {
|
||||
auto messageId = esp_mqtt_client_publish(m_client, m_statusTopic.c_str(), text, strlen(text), 0, 0);
|
||||
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", messageId);
|
||||
free(text);
|
||||
}
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
|
||||
MqttClient::~MqttClient() {
|
||||
if (m_client != nullptr) {
|
||||
esp_mqtt_client_destroy(m_client);
|
||||
}
|
||||
}
|
||||
|
||||
static void log_error_if_nonzero(const char *message, int error_code) {
|
||||
@ -60,7 +103,6 @@ void MqttClient::eventHandler(void *handler_args, esp_event_base_t base, int32_t
|
||||
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);
|
||||
esp_mqtt_client_handle_t client = event->client;
|
||||
int msg_id;
|
||||
switch ((esp_mqtt_event_id_t)event_id) {
|
||||
case MQTT_EVENT_CONNECTED:
|
||||
self->onConnected(client);
|
||||
@ -71,8 +113,6 @@ void MqttClient::eventHandler(void *handler_args, esp_event_base_t base, int32_t
|
||||
|
||||
case MQTT_EVENT_SUBSCRIBED:
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
|
||||
msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "data", 0, 0, 0);
|
||||
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
|
||||
break;
|
||||
case MQTT_EVENT_UNSUBSCRIBED:
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
|
||||
@ -111,10 +151,10 @@ bool MqttClient::initialize(const std::string &username, const std::string &pass
|
||||
config.credentials.username = username.c_str();
|
||||
config.credentials.authentication.password = password.c_str();
|
||||
|
||||
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),
|
||||
m_client = esp_mqtt_client_init(&config);
|
||||
esp_mqtt_client_register_event(m_client, static_cast<esp_mqtt_event_id_t>(ESP_EVENT_ANY_ID),
|
||||
&MqttClient::eventHandler, this);
|
||||
esp_mqtt_client_start(client);
|
||||
esp_mqtt_client_start(m_client);
|
||||
ESP_LOGI(TAG, "connect to %s, username: %s, password: %s", config.broker.address.uri, config.credentials.username,
|
||||
config.credentials.authentication.password);
|
||||
return true;
|
||||
|
@ -9,6 +9,9 @@ public:
|
||||
static MqttClient *instance();
|
||||
bool initialize(const std::string &username, const std::string &password);
|
||||
|
||||
void reportLedState();
|
||||
~MqttClient();
|
||||
|
||||
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);
|
||||
@ -17,5 +20,7 @@ protected:
|
||||
|
||||
private:
|
||||
std::string m_setStatusTopic;
|
||||
std::string m_statusTopic;
|
||||
esp_mqtt_client_handle_t m_client = nullptr;
|
||||
};
|
||||
#endif // __MQTTCLIENT_H__
|
Reference in New Issue
Block a user