reworked topic structure with added GROUP subtopic
This commit is contained in:
parent
f9576486d1
commit
a813d33168
20
Kconfig
20
Kconfig
|
|
@ -578,7 +578,7 @@ menu "webguiapp configuration"
|
|||
config WEBGUIAPP_MQTT_MAX_TOPIC_LENGTH
|
||||
int "Max topic length"
|
||||
range 32 512
|
||||
default 64
|
||||
default 128
|
||||
|
||||
config WEBGUIAPP_MQTT_SERVER_URL
|
||||
string "MQTT server URL"
|
||||
|
|
@ -590,18 +590,22 @@ menu "webguiapp configuration"
|
|||
default 1883
|
||||
|
||||
config WEBGUIAPP_MQTT_CLIENT_ID_1
|
||||
string "MQTT_1 client ID"
|
||||
default "DEVID1"
|
||||
string "MQTT_1 client ID prefix"
|
||||
default "DEV1"
|
||||
|
||||
if WEBGUIAPP_MQTT_CLIENTS_NUM > 1
|
||||
config WEBGUIAPP_MQTT_CLIENT_ID_2
|
||||
string "MQTT_2 client ID"
|
||||
default "DEVID2"
|
||||
string "MQTT_2 client ID prefix"
|
||||
default "DEV2"
|
||||
endif
|
||||
|
||||
config WEBGUIAPP_MQTT_ROOT_TOPIC
|
||||
string "MQTT root topic"
|
||||
default "ROOTTOPIC"
|
||||
config WEBGUIAPP_MQTT_SYSTEM_NAME
|
||||
string "MQTT global system name"
|
||||
default "SYSTEMNAME"
|
||||
|
||||
config WEBGUIAPP_MQTT_GROUP_NAME
|
||||
string "MQTT group name"
|
||||
default "GROUPNAME"
|
||||
|
||||
config WEBGUIAPP_MQTT_USERNAME
|
||||
string "MQTT user name"
|
||||
|
|
|
|||
152
include/MQTT.h
152
include/MQTT.h
|
|
@ -1,76 +1,76 @@
|
|||
/* Copyright 2022 Bogdan Pilyugin
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* File name: MQTT.h
|
||||
* Project: ChargePointMainboard
|
||||
* Created on: 2022-07-21
|
||||
* Author: Bogdan Pilyugin
|
||||
* Description:
|
||||
*/
|
||||
|
||||
#ifndef MAIN_INCLUDE_MQTT_H_
|
||||
#define MAIN_INCLUDE_MQTT_H_
|
||||
|
||||
#include "mqtt_client.h"
|
||||
|
||||
|
||||
typedef int mqtt_app_err_t;
|
||||
|
||||
#define API_OK 0 /*!< success (no error) */
|
||||
#define API_INTERNAL_ERR 1
|
||||
#define API_WRONG_JSON_ERR 2
|
||||
#define API_NO_ID_ERR 3
|
||||
#define API_ID_OVERSIZE_ERR 4
|
||||
#define API_NO_API_ERR 5
|
||||
#define API_VERSION_ERR 6
|
||||
#define API_NO_REQUEST_ERR 7
|
||||
#define API_UNSUPPORTED_METHOD_ERR 8
|
||||
#define API_NO_URL_ERR 9
|
||||
#define API_URL_OVERSIZE_ERR 10
|
||||
|
||||
#define API_URL_NOT_FOUND_ERR 11
|
||||
|
||||
#define API_NO_POSTDATA_ERR 12
|
||||
#define API_POSTDATA_OVERSIZE_ERR 13
|
||||
#define API_FILE_OVERSIZE_ERR 14
|
||||
#define API_FILE_EMPTY_ERR 15
|
||||
#define API_UNKNOWN_ERR 16
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char topic[CONFIG_WEBGUIAPP_MQTT_MAX_TOPIC_LENGTH];
|
||||
char *raw_data_ptr;
|
||||
int data_length;
|
||||
}DATA_SEND_STRUCT;
|
||||
|
||||
/**
|
||||
* @brief wrapper around esp_mqtt_client_handle_t with additional info
|
||||
*
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
int mqtt_index; /// numerical index of mqtt client
|
||||
esp_mqtt_client_handle_t mqtt; /// mqtt client handle
|
||||
QueueHandle_t mqtt_queue; /// queue for data sending over current mqtt client
|
||||
int wait_delivery_bit; /// is message delivered before timeout flag
|
||||
bool is_connected; /// is client connected flag
|
||||
} mqtt_client_t;
|
||||
|
||||
mqtt_client_t* GetMQTTHandlesPool(int idx);
|
||||
QueueHandle_t GetMQTTSendQueue(int idx);
|
||||
void ComposeTopic(char *topic, char *system_name, char *direct, char *client_name, char *service_name);
|
||||
void regUserEventHandler(void (*event_handler)(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data));
|
||||
void SystemDataHandler(char *data, uint32_t len, int idx);
|
||||
|
||||
#endif /* MAIN_INCLUDE_MQTT_H_ */
|
||||
/* Copyright 2022 Bogdan Pilyugin
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* File name: MQTT.h
|
||||
* Project: ChargePointMainboard
|
||||
* Created on: 2022-07-21
|
||||
* Author: Bogdan Pilyugin
|
||||
* Description:
|
||||
*/
|
||||
|
||||
#ifndef MAIN_INCLUDE_MQTT_H_
|
||||
#define MAIN_INCLUDE_MQTT_H_
|
||||
|
||||
#include "mqtt_client.h"
|
||||
|
||||
|
||||
typedef int mqtt_app_err_t;
|
||||
|
||||
#define API_OK 0 /*!< success (no error) */
|
||||
#define API_INTERNAL_ERR 1
|
||||
#define API_WRONG_JSON_ERR 2
|
||||
#define API_NO_ID_ERR 3
|
||||
#define API_ID_OVERSIZE_ERR 4
|
||||
#define API_NO_API_ERR 5
|
||||
#define API_VERSION_ERR 6
|
||||
#define API_NO_REQUEST_ERR 7
|
||||
#define API_UNSUPPORTED_METHOD_ERR 8
|
||||
#define API_NO_URL_ERR 9
|
||||
#define API_URL_OVERSIZE_ERR 10
|
||||
|
||||
#define API_URL_NOT_FOUND_ERR 11
|
||||
|
||||
#define API_NO_POSTDATA_ERR 12
|
||||
#define API_POSTDATA_OVERSIZE_ERR 13
|
||||
#define API_FILE_OVERSIZE_ERR 14
|
||||
#define API_FILE_EMPTY_ERR 15
|
||||
#define API_UNKNOWN_ERR 16
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char topic[CONFIG_WEBGUIAPP_MQTT_MAX_TOPIC_LENGTH];
|
||||
char *raw_data_ptr;
|
||||
int data_length;
|
||||
}DATA_SEND_STRUCT;
|
||||
|
||||
/**
|
||||
* @brief wrapper around esp_mqtt_client_handle_t with additional info
|
||||
*
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
int mqtt_index; /// numerical index of mqtt client
|
||||
esp_mqtt_client_handle_t mqtt; /// mqtt client handle
|
||||
QueueHandle_t mqtt_queue; /// queue for data sending over current mqtt client
|
||||
int wait_delivery_bit; /// is message delivered before timeout flag
|
||||
bool is_connected; /// is client connected flag
|
||||
} mqtt_client_t;
|
||||
|
||||
mqtt_client_t* GetMQTTHandlesPool(int idx);
|
||||
QueueHandle_t GetMQTTSendQueue(int idx);
|
||||
void ComposeTopic(char *topic, int idx, char *service_name, char *direct);
|
||||
void regUserEventHandler(void (*event_handler)(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data));
|
||||
void SystemDataHandler(char *data, uint32_t len, int idx);
|
||||
|
||||
#endif /* MAIN_INCLUDE_MQTT_H_ */
|
||||
|
|
|
|||
|
|
@ -136,8 +136,9 @@
|
|||
{
|
||||
char ServerAddr[32];
|
||||
uint16_t ServerPort;
|
||||
char SystemName[32];
|
||||
char GroupName[32];
|
||||
char ClientID[32];
|
||||
char RootTopic[32];
|
||||
char UserName[32];
|
||||
char UserPass[32];
|
||||
struct
|
||||
|
|
|
|||
|
|
@ -104,26 +104,26 @@ static HTTP_IO_RESULT HTTPPostAdaptersSettings(httpd_req_t *req, char *PostData)
|
|||
if (httpd_query_key_value(PostData, "ethen", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "1"))
|
||||
TempIsETHEnabled = true;
|
||||
TempIsETHEnabled = true;
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "dhcp", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "1"))
|
||||
TempIsETHDHCPEnabled = true;
|
||||
TempIsETHDHCPEnabled = true;
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "ipa", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.IPAddr);
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.IPAddr);
|
||||
if (httpd_query_key_value(PostData, "mas", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Mask);
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Mask);
|
||||
if (httpd_query_key_value(PostData, "gte", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Gateway);
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.Gateway);
|
||||
if (httpd_query_key_value(PostData, "dns1", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr1);
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr1);
|
||||
if (httpd_query_key_value(PostData, "dns2", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr2);
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr2);
|
||||
if (httpd_query_key_value(PostData, "dns3", tmp, 15) == ESP_OK)
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr3);
|
||||
esp_netif_str_to_ip4(tmp, (esp_ip4_addr_t*) &GetSysConf()->ethSettings.DNSAddr3);
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -261,62 +261,66 @@ static HTTP_IO_RESULT HTTPPostServicesSettings(httpd_req_t *req, char *PostData)
|
|||
char tmp[33];
|
||||
#if CONFIG_WEBGUIAPP_MQTT_ENABLE
|
||||
bool TempIsMQTT1Enabled = false;
|
||||
#if CONFIG_MQTT_CLIENTS_NUM == 2
|
||||
#if CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM == 2
|
||||
bool TempIsMQTT2Enabled = false;
|
||||
#endif
|
||||
httpd_query_key_value(PostData, "cld1", GetSysConf()->mqttStation[0].ServerAddr,
|
||||
httpd_query_key_value(PostData, "mqurl1", GetSysConf()->mqttStation[0].ServerAddr,
|
||||
sizeof(GetSysConf()->mqttStation[0].ServerAddr));
|
||||
httpd_query_key_value(PostData, "idd1", GetSysConf()->mqttStation[0].ClientID,
|
||||
httpd_query_key_value(PostData, "mqid1", GetSysConf()->mqttStation[0].ClientID,
|
||||
sizeof(GetSysConf()->mqttStation[0].ClientID));
|
||||
httpd_query_key_value(PostData, "top1", GetSysConf()->mqttStation[0].RootTopic,
|
||||
sizeof(GetSysConf()->mqttStation[0].RootTopic));
|
||||
httpd_query_key_value(PostData, "clnm1", GetSysConf()->mqttStation[0].UserName,
|
||||
httpd_query_key_value(PostData, "mqsys1", GetSysConf()->mqttStation[0].SystemName,
|
||||
sizeof(GetSysConf()->mqttStation[0].SystemName));
|
||||
httpd_query_key_value(PostData, "mqgrp1", GetSysConf()->mqttStation[0].GroupName,
|
||||
sizeof(GetSysConf()->mqttStation[0].GroupName));
|
||||
httpd_query_key_value(PostData, "mqname1", GetSysConf()->mqttStation[0].UserName,
|
||||
sizeof(GetSysConf()->mqttStation[0].UserName));
|
||||
|
||||
if (httpd_query_key_value(PostData, "mqttenb1", tmp, sizeof(tmp)) == ESP_OK)
|
||||
if (httpd_query_key_value(PostData, "mqen1", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "1"))
|
||||
TempIsMQTT1Enabled = true;
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "mprt1", tmp, sizeof(tmp)) == ESP_OK)
|
||||
if (httpd_query_key_value(PostData, "mprt1", tmp, sizeof(tmp)) == ESP_OK)
|
||||
if (httpd_query_key_value(PostData, "mqport1", tmp, sizeof(tmp)) == ESP_OK)
|
||||
if (httpd_query_key_value(PostData, "mqport1", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
uint16_t tp = atoi((const char*) tmp);
|
||||
if (tp < 65535 && tp >= 1000)
|
||||
GetSysConf()->mqttStation[0].ServerPort = tp;
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "clps1", tmp, sizeof(tmp)) == ESP_OK &&
|
||||
if (httpd_query_key_value(PostData, "mqpass1", tmp, sizeof(tmp)) == ESP_OK &&
|
||||
strcmp(tmp, (const char*) "******"))
|
||||
{
|
||||
strcpy(GetSysConf()->mqttStation[0].UserPass, tmp);
|
||||
}
|
||||
|
||||
#if CONFIG_MQTT_CLIENTS_NUM == 2
|
||||
httpd_query_key_value(PostData, "cld2", GetSysConf()->mqttStation[1].ServerAddr,
|
||||
sizeof(GetSysConf()->mqttStation[1].ServerAddr));
|
||||
httpd_query_key_value(PostData, "idd2", GetSysConf()->mqttStation[1].ClientID,
|
||||
sizeof(GetSysConf()->mqttStation[1].ClientID));
|
||||
httpd_query_key_value(PostData, "top2", GetSysConf()->mqttStation[1].RootTopic,
|
||||
sizeof(GetSysConf()->mqttStation[1].RootTopic));
|
||||
httpd_query_key_value(PostData, "clnm2", GetSysConf()->mqttStation[1].UserName,
|
||||
sizeof(GetSysConf()->mqttStation[1].UserName));
|
||||
#if CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM == 2
|
||||
httpd_query_key_value(PostData, "mqurl2", GetSysConf()->mqttStation[1].ServerAddr,
|
||||
sizeof(GetSysConf()->mqttStation[1].ServerAddr));
|
||||
httpd_query_key_value(PostData, "mqid2", GetSysConf()->mqttStation[1].ClientID,
|
||||
sizeof(GetSysConf()->mqttStation[1].ClientID));
|
||||
httpd_query_key_value(PostData, "mqsys2", GetSysConf()->mqttStation[1].SystemName,
|
||||
sizeof(GetSysConf()->mqttStation[1].SystemName));
|
||||
httpd_query_key_value(PostData, "mqgrp2", GetSysConf()->mqttStation[1].GroupName,
|
||||
sizeof(GetSysConf()->mqttStation[1].GroupName));
|
||||
httpd_query_key_value(PostData, "mqname2", GetSysConf()->mqttStation[1].UserName,
|
||||
sizeof(GetSysConf()->mqttStation[1].UserName));
|
||||
|
||||
if (httpd_query_key_value(PostData, "mqttenb2", tmp, sizeof(tmp)) == ESP_OK)
|
||||
if (httpd_query_key_value(PostData, "mqen2", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
if (!strcmp((const char*) tmp, (const char*) "1"))
|
||||
TempIsMQTT2Enabled = true;
|
||||
TempIsMQTT2Enabled = true;
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "mprt2", tmp, sizeof(tmp)) == ESP_OK)
|
||||
if (httpd_query_key_value(PostData, "mprt2", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
uint16_t tp = atoi((const char*) tmp);
|
||||
if (tp < 65535 && tp >= 1000)
|
||||
GetSysConf()->mqttStation[1].ServerPort = tp;
|
||||
}
|
||||
if (httpd_query_key_value(PostData, "mqport2", tmp, sizeof(tmp)) == ESP_OK)
|
||||
if (httpd_query_key_value(PostData, "mqport2", tmp, sizeof(tmp)) == ESP_OK)
|
||||
{
|
||||
uint16_t tp = atoi((const char*) tmp);
|
||||
if (tp < 65535 && tp >= 1000)
|
||||
GetSysConf()->mqttStation[1].ServerPort = tp;
|
||||
}
|
||||
|
||||
if (httpd_query_key_value(PostData, "clps2", tmp, sizeof(tmp)) == ESP_OK &&
|
||||
if (httpd_query_key_value(PostData, "mqpass2", tmp, sizeof(tmp)) == ESP_OK &&
|
||||
strcmp(tmp, (const char*) "******"))
|
||||
{
|
||||
strcpy(GetSysConf()->mqttStation[1].UserPass, tmp);
|
||||
|
|
@ -340,7 +344,7 @@ static HTTP_IO_RESULT HTTPPostServicesSettings(httpd_req_t *req, char *PostData)
|
|||
if (!strcmp(tmp, (const char*) "mqtt"))
|
||||
{
|
||||
GetSysConf()->mqttStation[0].Flags1.bIsGlobalEnabled = TempIsMQTT1Enabled;
|
||||
#if CONFIG_MQTT_CLIENTS_NUM == 2
|
||||
#if CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM == 2
|
||||
GetSysConf()->mqttStation[1].Flags1.bIsGlobalEnabled = TempIsMQTT2Enabled;
|
||||
#endif
|
||||
}
|
||||
|
|
|
|||
|
|
@ -457,61 +457,69 @@ void HTTPPrint_gsmmac(char *VarData, void *arg)
|
|||
#endif
|
||||
|
||||
#if CONFIG_WEBGUIAPP_MQTT_ENABLE
|
||||
void HTTPPrint_mqtten1(char *VarData, void *arg)
|
||||
void HTTPPrint_mqen1(char *VarData, void *arg)
|
||||
{
|
||||
PrintCheckbox(VarData, arg, GetSysConf()->mqttStation[0].Flags1.bIsGlobalEnabled);
|
||||
}
|
||||
void HTTPPrint_ipcld1(char *VarData, void *arg)
|
||||
void HTTPPrint_mqurl1(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", GetSysConf()->mqttStation[0].ServerAddr);
|
||||
}
|
||||
void HTTPPrint_mport1(char *VarData, void *arg)
|
||||
void HTTPPrint_mqport1(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%d", GetSysConf()->mqttStation[0].ServerPort);
|
||||
}
|
||||
void HTTPPrint_idcld1(char *VarData, void *arg)
|
||||
void HTTPPrint_mqid1(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", GetSysConf()->mqttStation[0].ClientID);
|
||||
}
|
||||
void HTTPPrint_topic1(char *VarData, void *arg)
|
||||
void HTTPPrint_mqsys1(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", GetSysConf()->mqttStation[0].RootTopic);
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", GetSysConf()->mqttStation[0].SystemName);
|
||||
}
|
||||
void HTTPPrint_clname1(char *VarData, void *arg)
|
||||
void HTTPPrint_mqgrp1(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", GetSysConf()->mqttStation[0].GroupName);
|
||||
}
|
||||
void HTTPPrint_mqname1(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", GetSysConf()->mqttStation[0].UserName);
|
||||
}
|
||||
void HTTPPrint_clpass1(char *VarData, void *arg)
|
||||
void HTTPPrint_mqpass1(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", "******");
|
||||
}
|
||||
|
||||
#if CONFIG_MQTT_CLIENTS_NUM == 2
|
||||
void HTTPPrint_mqtten2(char *VarData, void *arg)
|
||||
#if CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM == 2
|
||||
void HTTPPrint_mqen2(char *VarData, void *arg)
|
||||
{
|
||||
PrintCheckbox(VarData, arg, GetSysConf()->mqttStation[1].Flags1.bIsGlobalEnabled);
|
||||
}
|
||||
void HTTPPrint_ipcld2(char *VarData, void *arg)
|
||||
void HTTPPrint_mqurl2(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", GetSysConf()->mqttStation[1].ServerAddr);
|
||||
}
|
||||
void HTTPPrint_mport2(char *VarData, void *arg)
|
||||
void HTTPPrint_mqport2(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%d", GetSysConf()->mqttStation[1].ServerPort);
|
||||
}
|
||||
void HTTPPrint_idcld2(char *VarData, void *arg)
|
||||
void HTTPPrint_mqid2(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", GetSysConf()->mqttStation[1].ClientID);
|
||||
}
|
||||
void HTTPPrint_topic2(char *VarData, void *arg)
|
||||
void HTTPPrint_mqsys2(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", GetSysConf()->mqttStation[1].RootTopic);
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", GetSysConf()->mqttStation[1].SystemName);
|
||||
}
|
||||
void HTTPPrint_clname2(char *VarData, void *arg)
|
||||
void HTTPPrint_mqgrp2(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", GetSysConf()->mqttStation[1].GroupName);
|
||||
}
|
||||
void HTTPPrint_mqname2(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", GetSysConf()->mqttStation[1].UserName);
|
||||
}
|
||||
void HTTPPrint_clpass2(char *VarData, void *arg)
|
||||
void HTTPPrint_mqpass2(char *VarData, void *arg)
|
||||
{
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", "******");
|
||||
}
|
||||
|
|
@ -564,7 +572,7 @@ static void HTTPPrint_ifc_gprs(char *VarData, void *arg)
|
|||
|
||||
static void HTTPPrint_ifc_mq2(char *VarData, void *arg)
|
||||
{
|
||||
#if CONFIG_MQTT_CLIENTS_NUM == 2
|
||||
#if CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM == 2
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "1");
|
||||
#else
|
||||
snprintf(VarData, MAX_DYNVAR_LENGTH, "0");
|
||||
|
|
@ -656,21 +664,23 @@ dyn_var_handler_t HANDLERS_ARRAY[] = {
|
|||
|
||||
#if CONFIG_WEBGUIAPP_MQTT_ENABLE
|
||||
/*MQTT*/
|
||||
{ "mqtten1", sizeof("mqtten1") - 1, &HTTPPrint_mqtten1 },
|
||||
{ "ipcld1", sizeof("ipcld1") - 1, &HTTPPrint_ipcld1 },
|
||||
{ "mport1", sizeof("mport1") - 1, &HTTPPrint_mport1 },
|
||||
{ "idcld1", sizeof("idcld1") - 1, &HTTPPrint_idcld1 },
|
||||
{ "topic1", sizeof("topic1") - 1, &HTTPPrint_topic1 },
|
||||
{ "clname1", sizeof("clname1") - 1, &HTTPPrint_clname1 },
|
||||
{ "clpass1", sizeof("clpass1") - 1, &HTTPPrint_clpass1 },
|
||||
#if CONFIG_MQTT_CLIENTS_NUM == 2
|
||||
{ "mqtten2", sizeof("mqtten2") - 1, &HTTPPrint_mqtten2 },
|
||||
{ "ipcld2", sizeof("ipcld2") - 1, &HTTPPrint_ipcld2 },
|
||||
{ "mport2", sizeof("mport2") - 1, &HTTPPrint_mport2 },
|
||||
{ "idcld2", sizeof("idcld2") - 1, &HTTPPrint_idcld2 },
|
||||
{ "topic2", sizeof("topic2") - 1, &HTTPPrint_topic2 },
|
||||
{ "clname2", sizeof("clname2") - 1, &HTTPPrint_clname2 },
|
||||
{ "clpass2", sizeof("clpass2") - 1, &HTTPPrint_clpass2 },
|
||||
{ "mqen1", sizeof("mqen1") - 1, &HTTPPrint_mqen1 },
|
||||
{ "mqurl1", sizeof("mqurl1") - 1, &HTTPPrint_mqurl1 },
|
||||
{ "mqport1", sizeof("mqport1") - 1, &HTTPPrint_mqport1 },
|
||||
{ "mqsys1", sizeof("mqsys1") - 1, &HTTPPrint_mqsys1 },
|
||||
{ "mqgrp1", sizeof("mqgrp1") - 1, &HTTPPrint_mqgrp1 },
|
||||
{ "mqid1", sizeof("mqid1") - 1, &HTTPPrint_mqid1 },
|
||||
{ "mqname1", sizeof("mqname1") - 1, &HTTPPrint_mqname1 },
|
||||
{ "mqpass1", sizeof("mqpass1") - 1, &HTTPPrint_mqpass1 },
|
||||
#if CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM == 2
|
||||
{ "mqen2", sizeof("mqen2") - 1, &HTTPPrint_mqen2 },
|
||||
{ "mqurl2", sizeof("mqurl2") - 1, &HTTPPrint_mqurl2 },
|
||||
{ "mqport2", sizeof("mqport2") - 1, &HTTPPrint_mqport2 },
|
||||
{ "mqid2", sizeof("mqid2") - 1, &HTTPPrint_mqid2 },
|
||||
{ "mqsys2", sizeof("mqsys2") - 1, &HTTPPrint_mqsys2 },
|
||||
{ "mqgrp2", sizeof("mqgrp2") - 1, &HTTPPrint_mqgrp2 },
|
||||
{ "mqname2", sizeof("mqname2") - 1, &HTTPPrint_mqname2 },
|
||||
{ "mqpass2", sizeof("mqpass2") - 1, &HTTPPrint_mqpass2 },
|
||||
#endif
|
||||
#endif
|
||||
/*SNTP*/
|
||||
|
|
|
|||
38
src/MQTT.c
38
src/MQTT.c
|
|
@ -44,7 +44,8 @@ mqtt_client_t mqtt[CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM] = { 0 };
|
|||
static void mqtt_system_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data);
|
||||
|
||||
void (*UserEventHandler)(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data);
|
||||
void regUserEventHandler(void (*event_handler)(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data))
|
||||
void regUserEventHandler(
|
||||
void (*event_handler)(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data))
|
||||
{
|
||||
UserEventHandler = event_handler;
|
||||
}
|
||||
|
|
@ -77,25 +78,21 @@ static void log_error_if_nonzero(const char *message, int error_code)
|
|||
}
|
||||
}
|
||||
|
||||
void ComposeTopic(char *topic, char *system_name, char *direct, char *client_name, char *service_name)
|
||||
void ComposeTopic(char *topic, int idx, char *service_name, char *direct)
|
||||
{
|
||||
char tmp[4];
|
||||
char dev_rom_id[8];
|
||||
GetChipId((uint8_t*) tmp);
|
||||
BytesToStr((unsigned char*) tmp, (unsigned char*) dev_rom_id, 4);
|
||||
strcpy((char*) topic, system_name); // Global system name
|
||||
strcpy((char*) topic, GetSysConf()->mqttStation[idx].SystemName); // Global system name
|
||||
strcat((char*) topic, "/");
|
||||
strcat((char*) topic, (const char*) dev_rom_id); // Unique device ID (based on ROM chip id)
|
||||
strcat((char*) topic, GetSysConf()->mqttStation[idx].GroupName); // Global system name
|
||||
strcat((char*) topic, "/");
|
||||
strcat((char*) topic, client_name); // Device client name (for multiclient devices)
|
||||
strcat((char*) topic, GetSysConf()->mqttStation[idx].ClientID); // Device client name (for multiclient devices)
|
||||
strcat((char*) topic, "-");
|
||||
strcat((char*) topic, GetSysConf()->ID); //
|
||||
strcat((char*) topic, "/");
|
||||
strcat((char*) topic, (const char*) service_name); // Device service name
|
||||
strcat((char*) topic, "/");
|
||||
strcat((char*) topic, direct); // Data direction UPLINK or DOWNLINK
|
||||
|
||||
}
|
||||
|
||||
|
||||
static void mqtt_system_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
|
||||
{
|
||||
xSemaphoreTake(xSemaphoreMQTTHandle, pdMS_TO_TICKS(1000));
|
||||
|
|
@ -113,12 +110,9 @@ static void mqtt_system_event_handler(void *handler_args, esp_event_base_t base,
|
|||
ctx->is_connected = true;
|
||||
MQTTReconnectCounter = 0;
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED client %d", ctx->mqtt_index);
|
||||
ComposeTopic(topic,
|
||||
GetSysConf()->mqttStation[ctx->mqtt_index].RootTopic,
|
||||
"DWLINK",
|
||||
GetSysConf()->mqttStation[ctx->mqtt_index].ClientID,
|
||||
"SYSTEM");
|
||||
ComposeTopic(topic, ctx->mqtt_index, "SYSTEM", "DWLINK");
|
||||
msg_id = esp_mqtt_client_subscribe(client, (const char*) topic, 0);
|
||||
ESP_LOGI(TAG, "Subscribe to %s", topic);
|
||||
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
|
||||
break;
|
||||
case MQTT_EVENT_DISCONNECTED:
|
||||
|
|
@ -142,11 +136,7 @@ static void mqtt_system_event_handler(void *handler_args, esp_event_base_t base,
|
|||
case MQTT_EVENT_DATA:
|
||||
ESP_LOGI(TAG, "MQTT_EVENT_DATA, client %d", ctx->mqtt_index);
|
||||
//Check if topic is SYSTEM and pass data to handler
|
||||
ComposeTopic(topic,
|
||||
GetSysConf()->mqttStation[ctx->mqtt_index].RootTopic,
|
||||
"DWLINK",
|
||||
GetSysConf()->mqttStation[ctx->mqtt_index].ClientID,
|
||||
"SYSTEM");
|
||||
ComposeTopic(topic, ctx->mqtt_index, "SYSTEM", "DWLINK");
|
||||
if (!memcmp(topic, event->topic, event->topic_len))
|
||||
{
|
||||
SystemDataHandler(event->data, event->data_len, ctx->mqtt_index);
|
||||
|
|
@ -231,7 +221,8 @@ void MQTTTaskTransmit(void *pvParameter)
|
|||
esp_mqtt_client_publish(mqtt[idx].mqtt,
|
||||
(const char*) DSS.topic,
|
||||
(const char*) DSS.raw_data_ptr,
|
||||
DSS.data_length, 0, 0);
|
||||
DSS.data_length,
|
||||
0, 0);
|
||||
}
|
||||
else
|
||||
ESP_LOGE(TAG, "MQTT client not initialized");
|
||||
|
|
@ -268,7 +259,7 @@ static void start_mqtt()
|
|||
mqtt_cfg.username = GetSysConf()->mqttStation[i].UserName;
|
||||
mqtt_cfg.password = GetSysConf()->mqttStation[i].UserPass;
|
||||
strcpy(tmp, GetSysConf()->mqttStation[i].ClientID);
|
||||
strcat(tmp, "_");
|
||||
strcat(tmp, "-");
|
||||
strcat(tmp, GetSysConf()->ID);
|
||||
mqtt_cfg.client_id = tmp;
|
||||
mqtt[i].is_connected = false;
|
||||
|
|
@ -298,7 +289,6 @@ void MQTTRun(void)
|
|||
&xStaticMQTT1MessagesQueue);
|
||||
mqtt[0].mqtt_queue = MQTT1MessagesQueueHandle;
|
||||
|
||||
|
||||
#if CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM == 2
|
||||
if (GetSysConf()->mqttStation[1].Flags1.bIsGlobalEnabled)
|
||||
MQTT2MessagesQueueHandle = xQueueCreateStatic(CH_MESSAGE_BUFER_LENTH,
|
||||
|
|
|
|||
|
|
@ -131,11 +131,7 @@ static mqtt_app_err_t ResponceWithError(int idx,
|
|||
{
|
||||
memcpy(buf, JSONErrorMess, strlen(JSONErrorMess));
|
||||
DATA_SEND_STRUCT DSS;
|
||||
ComposeTopic(DSS.topic,
|
||||
GetSysConf()->mqttStation[idx].RootTopic,
|
||||
"UPLINK",
|
||||
GetSysConf()->mqttStation[idx].ClientID,
|
||||
"SYSTEM");
|
||||
ComposeTopic(DSS.topic, idx, "SYSTEM", "UPLINK");
|
||||
DSS.raw_data_ptr = buf;
|
||||
DSS.data_length = strlen(JSONErrorMess);
|
||||
if (xQueueSend(GetMQTTHandlesPool(idx)->mqtt_queue, &DSS, pdMS_TO_TICKS(1000)) == pdPASS)
|
||||
|
|
@ -222,11 +218,7 @@ static mqtt_app_err_t ResponceWithFile(int idx, espfs_file_t *file,
|
|||
strcat((fdata + readBytes), tail);
|
||||
free(filebuf);
|
||||
DATA_SEND_STRUCT DSS;
|
||||
ComposeTopic(DSS.topic,
|
||||
GetSysConf()->mqttStation[idx].RootTopic,
|
||||
"UPLINK",
|
||||
GetSysConf()->mqttStation[idx].ClientID,
|
||||
"SYSTEM");
|
||||
ComposeTopic(DSS.topic, idx, "SYSTEM", "UPLINK");
|
||||
DSS.raw_data_ptr = outbuf;
|
||||
DSS.data_length = (fdata - outbuf) + readBytes + strlen(tail);
|
||||
if (xQueueSend(GetMQTTHandlesPool(idx)->mqtt_queue, &DSS, pdMS_TO_TICKS(1000)) == pdPASS)
|
||||
|
|
|
|||
|
|
@ -304,16 +304,19 @@ static void ResetSysConfig(SYS_CONFIG *Conf)
|
|||
Conf->mqttStation[0].Flags1.bIsGlobalEnabled = CONFIG_WEBGUIAPP_MQTT_ON;
|
||||
memcpy(Conf->mqttStation[0].ServerAddr, CONFIG_WEBGUIAPP_MQTT_SERVER_URL, sizeof(CONFIG_WEBGUIAPP_MQTT_SERVER_URL));
|
||||
Conf->mqttStation[0].ServerPort = CONFIG_WEBGUIAPP_MQTT_SERVER_PORT;
|
||||
|
||||
memcpy(Conf->mqttStation[0].SystemName, CONFIG_WEBGUIAPP_MQTT_SYSTEM_NAME, sizeof(CONFIG_WEBGUIAPP_MQTT_SYSTEM_NAME));
|
||||
memcpy(Conf->mqttStation[0].GroupName, CONFIG_WEBGUIAPP_MQTT_GROUP_NAME, sizeof(CONFIG_WEBGUIAPP_MQTT_GROUP_NAME));
|
||||
memcpy(Conf->mqttStation[0].ClientID, CONFIG_WEBGUIAPP_MQTT_CLIENT_ID_1, sizeof(CONFIG_WEBGUIAPP_MQTT_CLIENT_ID_1));
|
||||
memcpy(Conf->mqttStation[0].RootTopic, CONFIG_WEBGUIAPP_MQTT_ROOT_TOPIC, sizeof(CONFIG_WEBGUIAPP_MQTT_ROOT_TOPIC));
|
||||
memcpy(Conf->mqttStation[0].UserName, CONFIG_WEBGUIAPP_MQTT_USERNAME, sizeof(CONFIG_WEBGUIAPP_MQTT_USERNAME));
|
||||
memcpy(Conf->mqttStation[0].UserPass, CONFIG_WEBGUIAPP_MQTT_PASSWORD, sizeof(CONFIG_WEBGUIAPP_MQTT_PASSWORD));
|
||||
#if CONFIG_MQTT_CLIENTS_NUM == 2
|
||||
#if CONFIG_WEBGUIAPP_MQTT_CLIENTS_NUM == 2
|
||||
Conf->mqttStation[1].Flags1.bIsGlobalEnabled = CONFIG_WEBGUIAPP_MQTT_ON;
|
||||
memcpy(Conf->mqttStation[1].ServerAddr, CONFIG_WEBGUIAPP_MQTT_SERVER_URL, sizeof(CONFIG_WEBGUIAPP_MQTT_SERVER_URL));
|
||||
Conf->mqttStation[1].ServerPort = CONFIG_WEBGUIAPP_MQTT_SERVER_PORT;
|
||||
memcpy(Conf->mqttStation[1].SystemName, CONFIG_WEBGUIAPP_MQTT_SYSTEM_NAME, sizeof(CONFIG_WEBGUIAPP_MQTT_SYSTEM_NAME));
|
||||
memcpy(Conf->mqttStation[1].GroupName, CONFIG_WEBGUIAPP_MQTT_GROUP_NAME, sizeof(CONFIG_WEBGUIAPP_MQTT_GROUP_NAME));
|
||||
memcpy(Conf->mqttStation[1].ClientID, CONFIG_WEBGUIAPP_MQTT_CLIENT_ID_2, sizeof(CONFIG_WEBGUIAPP_MQTT_CLIENT_ID_2));
|
||||
memcpy(Conf->mqttStation[1].RootTopic, CONFIG_WEBGUIAPP_MQTT_ROOT_TOPIC, sizeof(CONFIG_WEBGUIAPP_MQTT_ROOT_TOPIC));
|
||||
memcpy(Conf->mqttStation[1].UserName, CONFIG_WEBGUIAPP_MQTT_USERNAME, sizeof(CONFIG_WEBGUIAPP_MQTT_USERNAME));
|
||||
memcpy(Conf->mqttStation[1].UserPass, CONFIG_WEBGUIAPP_MQTT_PASSWORD, sizeof(CONFIG_WEBGUIAPP_MQTT_PASSWORD));
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user