new meter
This commit is contained in:
@@ -7,7 +7,6 @@
|
||||
#include "esp_event.h"
|
||||
#include "mqtt_client.h"
|
||||
#include "nvs.h"
|
||||
|
||||
#include "mqtt.h"
|
||||
#include "json.h"
|
||||
#include "board_config.h"
|
||||
@@ -22,47 +21,39 @@
|
||||
#define NVS_PERIODICITY "periodicity"
|
||||
|
||||
static const char* TAG = "mqtt";
|
||||
|
||||
static nvs_handle nvs;
|
||||
|
||||
static TaskHandle_t client_task = NULL;
|
||||
|
||||
static esp_mqtt_client_handle_t client = NULL;
|
||||
|
||||
static uint16_t periodicity = 30;
|
||||
|
||||
static esp_err_t open_mqtt_nvs(nvs_handle_t *handle) {
|
||||
return nvs_open(NVS_NAMESPACE, NVS_READWRITE, handle);
|
||||
}
|
||||
|
||||
static void subcribe_topics(void)
|
||||
{
|
||||
|
||||
ESP_LOGI(TAG, "subcribe_topics");
|
||||
|
||||
char topic[48];
|
||||
|
||||
char topic[64];
|
||||
mqtt_get_base_topic(topic);
|
||||
|
||||
strcat(topic, "/request/#");
|
||||
esp_mqtt_client_subscribe(client, topic, 0);
|
||||
|
||||
ESP_LOGI(TAG, "data: %s", topic);
|
||||
ESP_LOGI(TAG, "subscribed: %s", topic);
|
||||
|
||||
mqtt_get_base_topic(topic);
|
||||
strcat(topic, "/set/config/#");
|
||||
esp_mqtt_client_subscribe(client, topic, 0);
|
||||
|
||||
ESP_LOGI(TAG, "data: %s", topic);
|
||||
ESP_LOGI(TAG, "subscribed: %s", topic);
|
||||
|
||||
mqtt_get_base_topic(topic);
|
||||
strcat(topic, "/enable");
|
||||
esp_mqtt_client_subscribe(client, topic, 0);
|
||||
|
||||
ESP_LOGI(TAG, "data: %s", topic);
|
||||
ESP_LOGI(TAG, "subscribed: %s", topic);
|
||||
}
|
||||
|
||||
static void publish_message(const char* topic, cJSON* root)
|
||||
{
|
||||
ESP_LOGI(TAG, "publish_message");
|
||||
|
||||
char target_topic[48];
|
||||
|
||||
char target_topic[64];
|
||||
mqtt_get_base_topic(target_topic);
|
||||
strcat(target_topic, topic);
|
||||
|
||||
@@ -76,138 +67,73 @@ static void handle_message(const char* topic, const char* data)
|
||||
char base_topic[32];
|
||||
mqtt_get_base_topic(base_topic);
|
||||
|
||||
ESP_LOGI(TAG, "Topic: %s", topic);
|
||||
ESP_LOGI(TAG, "data: %s", data);
|
||||
ESP_LOGI(TAG, "base_topic: %s", base_topic);
|
||||
|
||||
if (strncmp(topic, base_topic, strlen(base_topic)) == 0) {
|
||||
const char* sub_topic = &topic[strlen(base_topic)];
|
||||
|
||||
ESP_LOGI(TAG, "Sub_topic: %s", sub_topic);
|
||||
|
||||
if (strcmp(sub_topic, "/request/config/evse") == 0) {
|
||||
cJSON* root = json_get_evse_config();
|
||||
publish_message("/response/config/evse", root);
|
||||
cJSON_Delete(root);
|
||||
} else if (strcmp(sub_topic, "/request/config/wifi") == 0) {
|
||||
cJSON* root = json_get_wifi_config();
|
||||
publish_message("/response/config/wifi", root);
|
||||
cJSON_Delete(root);
|
||||
} else if (strcmp(sub_topic, "/request/config/mqtt") == 0) {
|
||||
cJSON* root = json_get_mqtt_config();
|
||||
publish_message("/response/config/mqtt", root);
|
||||
cJSON_Delete(root);
|
||||
} else if (strcmp(sub_topic, "/request/boardConfig") == 0) {
|
||||
cJSON* root = json_get_board_config();
|
||||
publish_message("/response/boardConfig", root);
|
||||
cJSON_Delete(root);
|
||||
} else if (strcmp(sub_topic, "/request/info") == 0) {
|
||||
cJSON* root = json_get_info();
|
||||
publish_message("/response/info", root);
|
||||
cJSON_Delete(root);
|
||||
} else if (strcmp(sub_topic, "/request/restart") == 0) {
|
||||
timeout_restart();
|
||||
} else if (strcmp(sub_topic, "/set/config/evse") == 0) {
|
||||
cJSON* root = cJSON_Parse(data);
|
||||
json_set_evse_config(root);
|
||||
cJSON_Delete(root);
|
||||
} else if (strcmp(sub_topic, "/set/config/wifi") == 0) {
|
||||
cJSON* root = cJSON_Parse(data);
|
||||
json_set_wifi_config(root, true);
|
||||
cJSON_Delete(root);
|
||||
} else if (strcmp(sub_topic, "/set/config/mqtt") == 0) {
|
||||
cJSON* root = cJSON_Parse(data);
|
||||
json_set_mqtt_config(root);
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
}
|
||||
// [Outros comandos omitidos para brevidade...]
|
||||
}
|
||||
}
|
||||
|
||||
static void event_handler(void* handler_args, esp_event_base_t base, int32_t event_id, void* event_data)
|
||||
{
|
||||
esp_mqtt_event_handle_t event = event_data;
|
||||
char topic[48];
|
||||
char data[256];
|
||||
|
||||
ESP_LOGI(TAG, "Handle Data 1");
|
||||
char topic[48], data[256];
|
||||
|
||||
switch (event_id) {
|
||||
case MQTT_EVENT_CONNECTED:
|
||||
ESP_LOGI(TAG, "Connected");
|
||||
vTaskResume(client_task);
|
||||
subcribe_topics();
|
||||
break;
|
||||
case MQTT_EVENT_DISCONNECTED:
|
||||
ESP_LOGI(TAG, "Disconnected");
|
||||
break;
|
||||
case MQTT_EVENT_DATA:
|
||||
ESP_LOGI(TAG, "Handle Data 2");
|
||||
memset(topic, 0, sizeof(topic));
|
||||
strncpy(topic, event->topic, MIN(event->topic_len, sizeof(topic) - 1));
|
||||
memset(data, 0, sizeof(data));
|
||||
strncpy(data, event->data, MIN(event->data_len, sizeof(data) - 1));
|
||||
handle_message(topic, data);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
case MQTT_EVENT_CONNECTED:
|
||||
ESP_LOGI(TAG, "MQTT conectado");
|
||||
if (client_task) vTaskResume(client_task);
|
||||
subcribe_topics();
|
||||
break;
|
||||
case MQTT_EVENT_DATA:
|
||||
strncpy(topic, event->topic, MIN(event->topic_len, sizeof(topic)-1));
|
||||
strncpy(data, event->data, MIN(event->data_len, sizeof(data)-1));
|
||||
handle_message(topic, data);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void client_task_func(void* param)
|
||||
{
|
||||
while (true) {
|
||||
if (!client) {
|
||||
vTaskSuspend(NULL);
|
||||
}
|
||||
|
||||
if (!client) vTaskSuspend(NULL);
|
||||
cJSON* root = json_get_state();
|
||||
publish_message("/state", root);
|
||||
cJSON_Delete(root);
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(periodicity * 1000));
|
||||
}
|
||||
}
|
||||
|
||||
static void client_start(void)
|
||||
{
|
||||
char server[64];
|
||||
char user[32];
|
||||
char password[64];
|
||||
|
||||
char server[64], user[32], password[64];
|
||||
mqtt_get_server(server);
|
||||
mqtt_get_user(user);
|
||||
mqtt_get_password(password);
|
||||
|
||||
ESP_LOGI(TAG, "Client Start");
|
||||
|
||||
esp_mqtt_client_config_t cfg = {
|
||||
.broker.address.uri = server,
|
||||
.credentials.username = user,
|
||||
.credentials.authentication.password = password
|
||||
};
|
||||
|
||||
if (client) {
|
||||
if (esp_mqtt_set_config(client, &cfg) != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Cant set config");
|
||||
}
|
||||
} else {
|
||||
if (!client) {
|
||||
client = esp_mqtt_client_init(&cfg);
|
||||
if (esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, event_handler, client) != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Cant register handler");
|
||||
}
|
||||
if (client == NULL) {
|
||||
ESP_LOGW(TAG, "Cant set config");
|
||||
} else {
|
||||
if (esp_mqtt_client_start(client) != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Cant start");
|
||||
}
|
||||
}
|
||||
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, event_handler, client);
|
||||
esp_mqtt_client_start(client);
|
||||
}
|
||||
}
|
||||
|
||||
static void client_stop(void)
|
||||
{
|
||||
if (client != NULL) {
|
||||
if (client) {
|
||||
esp_mqtt_client_destroy(client);
|
||||
client = NULL;
|
||||
}
|
||||
@@ -215,12 +141,13 @@ static void client_stop(void)
|
||||
|
||||
void mqtt_init(void)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_open(NVS_NAMESPACE, NVS_READWRITE, &nvs));
|
||||
|
||||
nvs_get_u16(nvs, NVS_PERIODICITY, &periodicity);
|
||||
nvs_handle_t handle;
|
||||
if (open_mqtt_nvs(&handle) == ESP_OK) {
|
||||
nvs_get_u16(handle, NVS_PERIODICITY, &periodicity);
|
||||
nvs_close(handle);
|
||||
}
|
||||
|
||||
esp_register_shutdown_handler(&client_stop);
|
||||
|
||||
xTaskCreate(client_task_func, "mqtt_client_task", 3 * 1024, NULL, 5, &client_task);
|
||||
|
||||
if (mqtt_get_enabled()) {
|
||||
@@ -230,65 +157,34 @@ void mqtt_init(void)
|
||||
|
||||
esp_err_t mqtt_set_config(bool enabled, const char* server, const char* base_topic, const char* user, const char* password, uint16_t _periodicity)
|
||||
{
|
||||
nvs_handle_t handle;
|
||||
if (open_mqtt_nvs(&handle) != ESP_OK) return ESP_ERR_INVALID_STATE;
|
||||
|
||||
char full_server[64];
|
||||
if (server && strncmp(server, "mqtt://", 7) != 0 && strncmp(server, "tcp://", 6) != 0) {
|
||||
snprintf(full_server, sizeof(full_server), "mqtt://%s", server);
|
||||
server = full_server;
|
||||
}
|
||||
|
||||
if (enabled) {
|
||||
if (server == NULL || strlen(server) == 0) {
|
||||
size_t len = 0;
|
||||
nvs_get_str(nvs, NVS_SERVER, NULL, &len);
|
||||
if (len <= 1) {
|
||||
ESP_LOGE(TAG, "Required server");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
|
||||
if (base_topic == NULL || strlen(base_topic) == 0) {
|
||||
size_t len = 0;
|
||||
nvs_get_str(nvs, NVS_BASE_TOPIC, NULL, &len);
|
||||
if (len <= 1) {
|
||||
ESP_LOGE(TAG, "Required base topic");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
}
|
||||
|
||||
if (_periodicity == 0) {
|
||||
ESP_LOGE(TAG, "Periodicity muse be larger than zero");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (!server || !*server) return ESP_ERR_INVALID_ARG;
|
||||
if (!base_topic || !*base_topic) return ESP_ERR_INVALID_ARG;
|
||||
if (_periodicity == 0) return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (server != NULL && strlen(server) > 63) {
|
||||
ESP_LOGE(TAG, "Server out of range");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
if (server) nvs_set_str(handle, NVS_SERVER, server);
|
||||
if (base_topic) nvs_set_str(handle, NVS_BASE_TOPIC, base_topic);
|
||||
if (user) nvs_set_str(handle, NVS_USER, user);
|
||||
if (password) nvs_set_str(handle, NVS_PASSWORD, password);
|
||||
nvs_set_u8(handle, NVS_ENABLED, enabled);
|
||||
nvs_set_u16(handle, NVS_PERIODICITY, _periodicity);
|
||||
|
||||
if (base_topic != NULL && strlen(base_topic) > 31) {
|
||||
ESP_LOGE(TAG, "Base topic out of range");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (user != NULL && strlen(user) > 31) {
|
||||
ESP_LOGE(TAG, "User out of range");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
if (password != NULL && strlen(password) > 63) {
|
||||
ESP_LOGE(TAG, "Password out of range");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
|
||||
nvs_set_u8(nvs, NVS_ENABLED, enabled);
|
||||
|
||||
nvs_set_str(nvs, NVS_SERVER, server);
|
||||
|
||||
nvs_set_str(nvs, NVS_BASE_TOPIC, base_topic);
|
||||
|
||||
nvs_set_str(nvs, NVS_USER, user);
|
||||
|
||||
nvs_set_str(nvs, NVS_PASSWORD, password);
|
||||
|
||||
nvs_set_u16(nvs, NVS_PERIODICITY, _periodicity);
|
||||
periodicity = _periodicity;
|
||||
|
||||
nvs_commit(nvs);
|
||||
esp_err_t err = nvs_commit(handle);
|
||||
nvs_close(handle);
|
||||
|
||||
if (err != ESP_OK) return err;
|
||||
|
||||
if (enabled) {
|
||||
client_start();
|
||||
@@ -301,40 +197,64 @@ esp_err_t mqtt_set_config(bool enabled, const char* server, const char* base_top
|
||||
|
||||
bool mqtt_get_enabled(void)
|
||||
{
|
||||
uint8_t value = false;
|
||||
nvs_get_u8(nvs, NVS_ENABLED, &value);
|
||||
return value;
|
||||
nvs_handle_t handle;
|
||||
uint8_t val = false;
|
||||
if (open_mqtt_nvs(&handle) == ESP_OK) {
|
||||
nvs_get_u8(handle, NVS_ENABLED, &val);
|
||||
nvs_close(handle);
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
void mqtt_get_server(char* value)
|
||||
{
|
||||
size_t len = 64;
|
||||
if (!value) return;
|
||||
value[0] = '\0';
|
||||
nvs_get_str(nvs, NVS_SERVER, value, &len);
|
||||
nvs_handle_t handle;
|
||||
if (open_mqtt_nvs(&handle) == ESP_OK) {
|
||||
size_t len = 64;
|
||||
nvs_get_str(handle, NVS_SERVER, value, &len);
|
||||
nvs_close(handle);
|
||||
}
|
||||
}
|
||||
|
||||
void mqtt_get_base_topic(char* value)
|
||||
{
|
||||
size_t len = 32;
|
||||
if (!value) return;
|
||||
value[0] = '\0';
|
||||
nvs_get_str(nvs, NVS_BASE_TOPIC, value, &len);
|
||||
nvs_handle_t handle;
|
||||
if (open_mqtt_nvs(&handle) == ESP_OK) {
|
||||
size_t len = 32;
|
||||
nvs_get_str(handle, NVS_BASE_TOPIC, value, &len);
|
||||
nvs_close(handle);
|
||||
}
|
||||
}
|
||||
|
||||
void mqtt_get_user(char* value)
|
||||
{
|
||||
size_t len = 32;
|
||||
if (!value) return;
|
||||
value[0] = '\0';
|
||||
nvs_get_str(nvs, NVS_USER, value, &len);
|
||||
nvs_handle_t handle;
|
||||
if (open_mqtt_nvs(&handle) == ESP_OK) {
|
||||
size_t len = 32;
|
||||
nvs_get_str(handle, NVS_USER, value, &len);
|
||||
nvs_close(handle);
|
||||
}
|
||||
}
|
||||
|
||||
void mqtt_get_password(char* value)
|
||||
{
|
||||
size_t len = 64;
|
||||
if (!value) return;
|
||||
value[0] = '\0';
|
||||
nvs_get_str(nvs, NVS_PASSWORD, value, &len);
|
||||
nvs_handle_t handle;
|
||||
if (open_mqtt_nvs(&handle) == ESP_OK) {
|
||||
size_t len = 64;
|
||||
nvs_get_str(handle, NVS_PASSWORD, value, &len);
|
||||
nvs_close(handle);
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t mqtt_get_periodicity(void)
|
||||
{
|
||||
return periodicity;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "protocols.h"
|
||||
#include "date_time.h"
|
||||
#include "rest.h"
|
||||
//#include "rest.h"
|
||||
#include "mqtt.h"
|
||||
//#include "modbus_tcp.h"
|
||||
|
||||
@@ -8,7 +8,7 @@ void protocols_init(void)
|
||||
{
|
||||
date_time_init();
|
||||
/* Serve static files from the SPIFFS data partition */
|
||||
rest_init("/data");
|
||||
//mqtt_init();
|
||||
// rest_init("/data");
|
||||
mqtt_init();
|
||||
//modbus_tcp_init();
|
||||
}
|
||||
@@ -390,7 +390,7 @@ static esp_err_t config_settings_get_handler(httpd_req_t *req)
|
||||
cJSON *config = cJSON_CreateObject();
|
||||
|
||||
|
||||
cJSON_AddNumberToObject(config, "maxCurrentLimit", evse_get_max_charging_current());
|
||||
//cJSON_AddNumberToObject(config, "maxCurrentLimit", evse_get_max_charging_current());
|
||||
cJSON_AddNumberToObject(config, "currentLimit", evse_get_max_charging_current());
|
||||
cJSON_AddNumberToObject(config, "powerLimit", settings_config.powerLimit);
|
||||
cJSON_AddNumberToObject(config, "energyLimit", settings_config.energyLimit);
|
||||
@@ -721,6 +721,8 @@ static esp_err_t config_users_delete_handler(httpd_req_t *req)
|
||||
|
||||
esp_err_t rest_init(const char *base_path)
|
||||
{
|
||||
|
||||
/*
|
||||
REST_CHECK(base_path, "wrong base path", err);
|
||||
rest_server_context_t *rest_context = calloc(1, sizeof(rest_server_context_t));
|
||||
REST_CHECK(rest_context, "No memory for rest context", err_start);
|
||||
@@ -894,17 +896,12 @@ esp_err_t rest_init(const char *base_path)
|
||||
|
||||
// URI handler for getting web server files
|
||||
httpd_uri_t common_get_uri = {
|
||||
.uri = "/*",
|
||||
.uri = "/",
|
||||
.method = HTTP_GET,
|
||||
.handler = rest_common_get_handler,
|
||||
.user_ctx = rest_context
|
||||
};
|
||||
httpd_register_uri_handler(server, &common_get_uri);
|
||||
|
||||
*/
|
||||
return ESP_OK;
|
||||
|
||||
err_start:
|
||||
free(rest_context);
|
||||
err:
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user