mirror of
https://github.com/jomjol/AI-on-the-edge-device.git
synced 2025-12-07 20:16:55 +03:00
.
This commit is contained in:
@@ -14,7 +14,10 @@
|
||||
#define __HIDE_PASSWORD
|
||||
|
||||
static const char *TAG = "class_flow_MQTT";
|
||||
#define LWT_TOPIC "connection"
|
||||
|
||||
#define LWT_TOPIC "connection"
|
||||
#define LWT_CONNECTED "connected"
|
||||
#define LWT_DISCONNECTED "connection lost"
|
||||
|
||||
extern const char* libfive_git_version(void);
|
||||
extern const char* libfive_git_revision(void);
|
||||
@@ -51,6 +54,7 @@ void sendHomeAssistantDiscoveryTopic(std::string maintopic, std::string group, s
|
||||
|
||||
topic = "homeassistant/sensor/" + maintopic + "-" + topicT + "/config";
|
||||
|
||||
/* See https://www.home-assistant.io/docs/mqtt/discovery/ */
|
||||
payload = "{" + nl +
|
||||
"\"~\": \"" + maintopic + "\"," + nl +
|
||||
"\"unique_id\": \"" + maintopic + "-" +topicT + "\"," + nl +
|
||||
@@ -66,17 +70,19 @@ void sendHomeAssistantDiscoveryTopic(std::string maintopic, std::string group, s
|
||||
}
|
||||
|
||||
payload +=
|
||||
"\"availability_topic\": \"~/connection\"," + nl +
|
||||
"\"payload_available\": \"connected\"," + nl +
|
||||
"\"payload_not_available\": \"connection lost\"," + nl;
|
||||
"\"avty_t\": \"~/" + std::string(LWT_TOPIC) + "\"," + nl +
|
||||
"\"pl_avail\": \"" + LWT_CONNECTED + "\"," + nl +
|
||||
"\"pl_not_avail\": \"" + LWT_DISCONNECTED + "\"," + nl;
|
||||
|
||||
payload +=
|
||||
"\"device\": {" + nl +
|
||||
"\"identifiers\": [\"" + maintopic + "\"]," + nl +
|
||||
"\"ids\": [\"" + maintopic + "\"]," + nl +
|
||||
"\"name\": \"" + maintopic + "\"," + nl +
|
||||
"\"model\": \"HomeAssistant Discovery for AI on the Edge Device\"," + nl +
|
||||
"\"manufacturer\": \"AI on the Edge Device\"," + nl +
|
||||
"\"sw_version\": \"" + version + "\"" + nl +
|
||||
"\"model\": \"Meter Digitizer\"," + nl +
|
||||
"\"mf\": \"AI on the Edge Device\"," + nl +
|
||||
"\"sw\": \"" + version + "\"" + nl +
|
||||
"\"hw\": \"ESP32-CAM\"" + nl +
|
||||
"\"cu\": \"https://" + *getIPAddress() + "\"" + nl +
|
||||
"}" + nl +
|
||||
"}" + nl;
|
||||
|
||||
@@ -103,9 +109,6 @@ void MQTThomeassistantDiscovery(std::string maintopic) {
|
||||
sendHomeAssistantDiscoveryTopic(maintopic, (*NUMBERS)[i]->name, "timestamp", "clock-time-eight-outline", "");
|
||||
sendHomeAssistantDiscoveryTopic(maintopic, (*NUMBERS)[i]->name, "json", "code-json", "");
|
||||
}
|
||||
|
||||
// Send LWT once more to indicate that we are online
|
||||
//MQTTPublish(LWT_TOPIC, "connected", true);
|
||||
}
|
||||
|
||||
void publishRuntimeData(std::string maintopic, int SetRetainFlag) {
|
||||
@@ -254,7 +257,7 @@ bool ClassFlowMQTT::ReadParameter(FILE* pfile, string& aktparamgraph)
|
||||
}
|
||||
}
|
||||
|
||||
MQTT_Configure(uri, clientname, user, password, maintopic, LWT_TOPIC, keepAlive, SetRetainFlag, (void *)&GotConnected);
|
||||
MQTT_Configure(uri, clientname, user, password, maintopic, LWT_TOPIC, LWT_CONNECTED, LWT_DISCONNECTED, keepAlive, SetRetainFlag, (void *)&GotConnected);
|
||||
|
||||
if (!MQTT_Init()) {
|
||||
if (!MQTT_Init()) { // Retry
|
||||
|
||||
@@ -18,7 +18,7 @@ esp_mqtt_event_id_t esp_mmqtt_ID = MQTT_EVENT_ANY;
|
||||
|
||||
bool mqtt_connected = false;
|
||||
esp_mqtt_client_handle_t client = NULL;
|
||||
std::string uri, client_id, lwt_topic, user, password, maintopic;
|
||||
std::string uri, client_id, lwt_topic, lwt_connected, lwt_disconnected, user, password, maintopic;
|
||||
int keepalive, SetRetainFlag;
|
||||
void (*callbackOnConnected)(std::string, int) = NULL;
|
||||
|
||||
@@ -116,7 +116,8 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
|
||||
|
||||
|
||||
void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password,
|
||||
std::string _maintopic, std::string _lwt, int _keepalive, int _SetRetainFlag, void *_callbackOnConnected){
|
||||
std::string _maintopic, std::string _lwt, std::string _lwt_connected, std::string _lwt_disconnected,
|
||||
int _keepalive, int _SetRetainFlag, void *_callbackOnConnected){
|
||||
#ifdef __HIDE_PASSWORD
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, "MQTT Configuration: uri: " + _mqttURI + ", clientname: " + _clientid +
|
||||
", user: " + _user + ", password: XXXXXXXX, maintopic: " + _maintopic + ", last-will-topic: " + _maintopic + "/" + _lwt + ", keepAlive: " + std::to_string(_keepalive));
|
||||
@@ -128,6 +129,8 @@ void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _us
|
||||
uri = _mqttURI;
|
||||
client_id = _clientid;
|
||||
lwt_topic = _maintopic + "/" + _lwt;
|
||||
lwt_connected = _lwt_connected;
|
||||
lwt_disconnected = _lwt_disconnected;
|
||||
keepalive = _keepalive;
|
||||
SetRetainFlag = _SetRetainFlag;
|
||||
maintopic = _maintopic;
|
||||
@@ -145,7 +148,7 @@ bool MQTT_Init() {
|
||||
|
||||
MQTTdestroy_client();
|
||||
|
||||
std::string lw = "connection lost";
|
||||
std::string lw = lwt_disconnected;
|
||||
|
||||
esp_mqtt_client_config_t mqtt_cfg = {
|
||||
.uri = uri.c_str(),
|
||||
@@ -254,7 +257,7 @@ void MQTTconnected(){
|
||||
if (mqtt_connected) {
|
||||
LogFile.WriteToFile(ESP_LOG_INFO, "MQTT - Connected");
|
||||
|
||||
MQTTPublish(lwt_topic, "connected", true);
|
||||
MQTTPublish(lwt_topic, lwt_connected, true);
|
||||
|
||||
if (connectFunktionMap != NULL) {
|
||||
for(std::map<std::string, std::function<void()>>::iterator it = connectFunktionMap->begin(); it != connectFunktionMap->end(); ++it) {
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
#include <functional>
|
||||
|
||||
void MQTT_Configure(std::string _mqttURI, std::string _clientid, std::string _user, std::string _password,
|
||||
std::string _maintopic, std::string _lwt, int _keepalive, int SetRetainFlag, void *callbackOnConnected);
|
||||
std::string _maintopic, std::string _lwt, std::string _lwt_connected, std::string _lwt_disconnected,
|
||||
int _keepalive, int SetRetainFlag, void *callbackOnConnected);
|
||||
bool MQTT_Init();
|
||||
void MQTTdestroy_client();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user