44#include " divert.h"
55#include " input.h"
66#include " hal.h"
7+ #include " net_manager.h"
78
89#include < Arduino.h>
10+ #include < ArduinoJson.h>
911#include < MongooseMqttClient.h>
1012
1113MongooseMqttClient mqttclient;
@@ -16,6 +18,8 @@ int i = 0;
1618String payload_str = " " ;
1719bool connecting = false ;
1820
21+ String lastWill = " " ;
22+
1923// -------------------------------------------------------------------
2024// MQTT msg Received callback function:
2125// Function to be called when msg is received on MQTT subscribed topic
@@ -68,9 +72,9 @@ void mqttmsg_callback(MongooseString topic, MongooseString payload) {
6872 cmd += " " +payload_str;
6973 }
7074
71- rapiSender.sendCmd (cmd, [](int ret)
75+ rapiSender.sendCmd (cmd, [](int ret)
7276 {
73- if (RAPI_RESPONSE_OK == ret || RAPI_RESPONSE_NK == ret)
77+ if (RAPI_RESPONSE_OK == ret || RAPI_RESPONSE_NK == ret)
7478 {
7579 String rapiString = rapiSender.getResponse ();
7680 String mqtt_data = rapiString;
@@ -95,28 +99,55 @@ mqtt_connect()
9599
96100 mqttclient.onMessage (mqttmsg_callback); // function to be called when mqtt msg is received on subscribed topic
97101 mqttclient.onError ([](uint8_t err) {
98- DBUGF (" Got error %u" , err);
102+ DBUGF (" MQTT error %u" , err);
99103 connecting = false ;
100104 });
101105
102106 DBUG (" MQTT Connecting to..." );
103107 DBUGLN (mqtt_server);
104108
105- String strID = " openevse-" +HAL.getShortId ();
109+ // Build the last will message
110+ DynamicJsonDocument willDoc (JSON_OBJECT_SIZE (3 ) + 60 );
111+
112+ willDoc[" state" ] = " disconnected" ;
113+ willDoc[" id" ] = HAL.getLongId ();
114+ willDoc[" name" ] = esp_hostname;
106115
107- mqttclient.connect (mqtt_server + " :1883" , mqtt_user, mqtt_pass, []()
116+ lastWill = " " ;
117+ serializeJson (willDoc, lastWill);
118+ DBUGVAR (lastWill);
119+
120+ mqttclient.setCredentials (mqtt_user, mqtt_pass);
121+ mqttclient.setLastWillAndTestimment (mqtt_announce_topic, lastWill, true );
122+ mqttclient.connect (mqtt_server + " :1883" , esp_hostname, []()
108123 {
109124 DBUGLN (" MQTT connected" );
110125
111- mqttclient.publish (mqtt_topic, " connected" ); // Once connected, publish an announcement..
112- String mqtt_sub_topic = mqtt_topic + " /rapi/in/#" ; // MQTT Topic to subscribe to receive RAPI commands via MQTT
113- // e.g to set current to 13A: <base-topic>/rapi/in/$SC 13
126+ DynamicJsonDocument doc (JSON_OBJECT_SIZE (5 ) + 200 );
127+
128+ doc[" state" ] = " connected" ;
129+ doc[" id" ] = HAL.getLongId ();
130+ doc[" name" ] = esp_hostname;
131+ doc[" mqtt" ] = mqtt_topic;
132+ doc[" http" ] = " http://" +ipaddress+" /" ;
133+
134+ // Once connected, publish an announcement..
135+ String announce = " " ;
136+ serializeJson (doc, announce);
137+ DBUGVAR (announce);
138+ mqttclient.publish (mqtt_announce_topic, announce, true );
139+
140+ // MQTT Topic to subscribe to receive RAPI commands via MQTT
141+ String mqtt_sub_topic = mqtt_topic + " /rapi/in/#" ;
142+
143+ // e.g to set current to 13A: <base-topic>/rapi/in/$SC 13
114144 mqttclient.subscribe (mqtt_sub_topic);
145+
115146 // subscribe to solar PV / grid_ie MQTT feeds
116- if (mqtt_solar!=" " ){
147+ if (mqtt_solar!=" " ) {
117148 mqttclient.subscribe (mqtt_solar);
118149 }
119- if (mqtt_grid_ie!=" " ){
150+ if (mqtt_grid_ie!=" " ) {
120151 mqttclient.subscribe (mqtt_grid_ie);
121152 }
122153 mqtt_sub_topic = mqtt_topic + " /divertmode/set" ; // MQTT Topic to change divert mode
@@ -125,29 +156,6 @@ mqtt_connect()
125156 connecting = false ;
126157 });
127158
128- /*
129- if (mqttclient.connect( strID.c_str(), mqtt_user.c_str(), mqtt_pass.c_str(),mqtt_topic.c_str(),1,0,(char*)"disconnected")) { // Attempt to connect
130- DEBUG.println("MQTT connected");
131- mqttclient.publish(mqtt_topic.c_str(), "connected"); // Once connected, publish an announcement..
132- String mqtt_sub_topic = mqtt_topic + "/rapi/in/#"; // MQTT Topic to subscribe to receive RAPI commands via MQTT
133- //e.g to set current to 13A: <base-topic>/rapi/in/$SC 13
134- mqttclient.subscribe(mqtt_sub_topic.c_str());
135- // subscribe to solar PV / grid_ie MQTT feeds
136- if (mqtt_solar!=""){
137- mqttclient.subscribe(mqtt_solar.c_str());
138- }
139- if (mqtt_grid_ie!=""){
140- mqttclient.subscribe(mqtt_grid_ie.c_str());
141- }
142- mqtt_sub_topic = mqtt_topic + "/divertmode/set"; // MQTT Topic to change divert mode
143- mqttclient.subscribe(mqtt_sub_topic.c_str());
144-
145- } else {
146- DEBUG.print("MQTT failed: ");
147- DEBUG.println(mqttclient.state());
148- return (0);
149- }
150- */
151159 return true ;
152160}
153161
@@ -160,6 +168,10 @@ void
160168mqtt_publish (String data) {
161169 Profile_Start (mqtt_publish);
162170
171+ if (!mqttclient.connected ()) {
172+ return ;
173+ }
174+
163175 String mqtt_data = " " ;
164176 String topic = mqtt_topic + " /" ;
165177
0 commit comments