/
mqtt.cpp
218 lines (191 loc) · 6.6 KB
/
mqtt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "emonesp.h"
#include "mqtt.h"
#include "app_config.h"
#include "divert.h"
#include "input.h"
#include "espal.h"
#include "openevse.h"
#include <Arduino.h>
#include <PubSubClient.h> // MQTT https://github.com/knolleary/pubsubclient PlatformIO lib: 89
#include <WiFiClient.h>
WiFiClient espClient; // Create client for MQTT
PubSubClient mqttclient(espClient); // Create client for MQTT
static long nextMqttReconnectAttempt = 0;
static unsigned long mqttRestartTime = 0;
int clientTimeout = 0;
int i = 0;
#ifndef MQTT_CONNECT_TIMEOUT
#define MQTT_CONNECT_TIMEOUT (5 * 1000)
#endif // !MQTT_CONNECT_TIMEOUT
// -------------------------------------------------------------------
// MQTT msg Received callback function:
// Function to be called when msg is received on MQTT subscribed topic
// Used to receive RAPI commands via MQTT
// //e.g to set current to 13A: <base-topic>/rapi/$SC 13
// -------------------------------------------------------------------
void mqttmsg_callback(char *topic, byte * payload, unsigned int length) {
String topic_string = String(topic);
String payload_str = "";
// print received MQTT to debug
DBUGLN("MQTT received:");
DBUGF("Topic: %s", topic);
for (int i = 0; i < length; i++) {
payload_str += (char) payload[i];
}
DEBUG.println("Payload: " + payload_str);
// If MQTT message is solar PV
if (topic_string == mqtt_solar){
solar = payload_str.toInt();
DBUGF("solar:%dW", solar);
divert_update_state();
}
else if (topic_string == mqtt_grid_ie){
grid_ie = payload_str.toInt();
DBUGF("grid:%dW", grid_ie);
divert_update_state();
}
else if (topic_string == mqtt_vrms){
voltage = payload_str.toFloat();
DBUGF("voltage: %.2f", voltage);
OpenEVSE.setVoltage(voltage, [](int ret) {
// Only gives better power calculations so not critical if this fails
});
}
// If MQTT message to set divert mode is received
else if (topic_string == mqtt_topic + "/divertmode/set"){
byte newdivert = payload_str.toInt();
if ((newdivert==1) || (newdivert==2)){
divertmode_update(newdivert);
}
}
else
{
// If MQTT message is RAPI command
// Detect if MQTT message is a RAPI command e.g to set 13A <base-topic>/rapi/$SC 13
// Locate '$' character in the MQTT message to identify RAPI command
int rapi_character_index = topic_string.indexOf('$');
DBUGVAR(rapi_character_index);
if (rapi_character_index > 1) {
DBUGF("Processing as RAPI");
// Print RAPI command from mqtt-sub topic e.g $SC
// ASSUME RAPI COMMANDS ARE ALWAYS PREFIX BY $ AND TWO CHARACTERS LONG)
String cmd = String(topic + rapi_character_index);
if (payload[0] != 0); { // If MQTT msg contains a payload e.g $SC 13. Not all rapi commands have a payload e.g. $GC
cmd += " ";
// print RAPI value received via MQTT serial
for (unsigned int i = 0; i < length; i++) {
cmd += (char)payload[i];
}
}
rapiSender.sendCmd(cmd, [](int ret)
{
if (RAPI_RESPONSE_OK == ret || RAPI_RESPONSE_NK == ret)
{
String rapiString = rapiSender.getResponse();
String mqtt_data = rapiString;
String mqtt_sub_topic = mqtt_topic + "/rapi/out";
mqttclient.publish(mqtt_sub_topic.c_str(), mqtt_data.c_str());
}
});
}
}
} //end call back
// -------------------------------------------------------------------
// MQTT Connect
// -------------------------------------------------------------------
boolean
mqtt_connect() {
mqttclient.setServer(mqtt_server.c_str(), mqtt_port);
mqttclient.setCallback(mqttmsg_callback); //function to be called when mqtt msg is received on subscribed topic
DEBUG.print("MQTT Connecting to...");
DEBUG.println(mqtt_user.c_str());
String strID = String(ESP.getChipId());
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
DEBUG.println("MQTT connected");
mqttclient.publish(mqtt_topic.c_str(), "connected"); // Once connected, publish an announcement..
String mqtt_sub_topic = mqtt_topic + "/rapi/in/#"; // MQTT Topic to subscribe to receive RAPI commands via MQTT
//e.g to set current to 13A: <base-topic>/rapi/in/$SC 13
mqttclient.subscribe(mqtt_sub_topic.c_str());
// subscribe to solar PV / grid_ie MQTT feeds
if(config_divert_enabled())
{
if (mqtt_solar!="") {
mqttclient.subscribe(mqtt_solar.c_str());
}
if (mqtt_grid_ie!="") {
mqttclient.subscribe(mqtt_grid_ie.c_str());
}
}
if (mqtt_vrms!="") {
mqttclient.subscribe(mqtt_vrms.c_str());
}
mqtt_sub_topic = mqtt_topic + "/divertmode/set"; // MQTT Topic to change divert mode
mqttclient.subscribe(mqtt_sub_topic.c_str());
} else {
DEBUG.print("MQTT failed: ");
DEBUG.println(mqttclient.state());
return (0);
}
return (1);
}
// -------------------------------------------------------------------
// Publish status to MQTT
// -------------------------------------------------------------------
void
mqtt_publish(JsonDocument &data) {
Profile_Start(mqtt_publish);
if(!config_mqtt_enabled() || !mqttclient.connected()) {
return;
}
JsonObject root = data.as<JsonObject>();
for (JsonPair kv : root) {
String topic = mqtt_topic + "/";
topic += kv.key().c_str();
String val = kv.value().as<String>();
mqttclient.publish(topic.c_str(), val.c_str());
}
Profile_End(mqtt_publish, 5);
}
// -------------------------------------------------------------------
// MQTT state management
//
// Call every time around loop() if connected to the WiFi
// -------------------------------------------------------------------
void
mqtt_loop() {
Profile_Start(mqtt_loop);
// Do we need to restart MQTT?
if(mqttRestartTime > 0 && millis() > mqttRestartTime)
{
mqttRestartTime = 0;
if (mqttclient.connected()) {
DBUGF("Disconnecting MQTT");
mqttclient.disconnect();
}
nextMqttReconnectAttempt = 0;
}
if(config_mqtt_enabled())
{
if (!mqttclient.connected()) {
long now = millis();
// try and reconnect every x seconds
if (now > nextMqttReconnectAttempt) {
nextMqttReconnectAttempt = now + MQTT_CONNECT_TIMEOUT;
mqtt_connect(); // Attempt to reconnect
}
} else {
// if MQTT connected
mqttclient.loop();
}
}
Profile_End(mqtt_loop, 5);
}
void
mqtt_restart() {
// If connected disconnect MQTT to trigger re-connect with new details
mqttRestartTime = millis();
}
boolean
mqtt_connected() {
return mqttclient.connected();
}