-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmqtt_utils.cpp
More file actions
238 lines (189 loc) · 7.62 KB
/
mqtt_utils.cpp
File metadata and controls
238 lines (189 loc) · 7.62 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include "mqtt_utils.h"
void init_mqtt() {
client.setServer(MQTT_SERVER, MQTT_PORT);
client.setCallback(mqtt_callback);
client.setBufferSize(MAX_MQTT_PACKET);
delay(100);
}
void mqtt_connect() {
// Loop until we're connected
while (!client.connected()) {
SERIAL_DEBUG_MQTT &&Serial.println("Attempting MQTT connection...");
if ((WiFi.status() != WL_CONNECTED)) {
SERIAL_DEBUG && Serial.println("WiFi not connected, aborting MQTT connection");
return;
}
// Attempt to connect
if (client.connect(derivator_id.c_str(), MQTT_USER, MQTT_PASS, mqtt_avtyTopic.c_str(), 1, true, "Offline")) {
mqtt_status = 1;
client.publish(mqtt_avtyTopic.c_str(), "Online", true);
SERIAL_DEBUG_MQTT &&Serial.println("MQTT connected");
sendMQTTStatusDiscoveryMsg();
// ... and resubscribe
client.subscribe(MQTT_GRID_POWER);
if (MQTT_SOLAR_POWER_1 != NULL) {
client.subscribe(MQTT_SOLAR_POWER_1);
}
if (MQTT_SOLAR_POWER_2 != NULL) {
client.subscribe(MQTT_SOLAR_POWER_2);
}
//Listen button
client.subscribe(mqtt_btnPassthroughtTopic.c_str());
} else {
SERIAL_DEBUG_MQTT &&Serial.printf("MQTT connection failed, rc=%d\n", client.state());
delay(5000);
}
}
Serial.println("MQTT connect finish ok, sending defaults");
client.publish(mqtt_stateTopic.c_str(), "Online", true);
client.publish(mqtt_derivationTopic.c_str(), "0", false);
client.publish(mqtt_rssiTopic.c_str(), "0", false);
char mqttrec_cstr[4];
itoa(mqtt_reconnections, mqttrec_cstr, 10);
client.publish(mqtt_reconnectTopic.c_str(), mqttrec_cstr, false);
char wifirec_cstr[4];
itoa(wifi_disconnections, wifirec_cstr, 10);
client.publish(wifi_disconnectTopic.c_str(), wifirec_cstr, false);
//client.publish(mqtt_btnPassthroughtTopic.c_str(), "OFF", false);
//client.publish(mqtt_btnPassthroughtTopic_status.c_str(), "OFF", true);
}
void mqtt_callback(char *topic, byte *payload, unsigned int length) {
if (topic) {
SERIAL_DEBUG_MQTT &&Serial.printf("Callback called for topic: %s\n", topic);
} else {
SERIAL_DEBUG_MQTT &&Serial.println("Empty MQTT topic");
return;
}
char power[12] = { '\0' };
char solar[12] = { '\0' };
char passbtn[12] = { '\0' };
int solar_update = 0;
payload[length] = '\0';
if (strcmp(topic, MQTT_GRID_POWER) == 0 && payload) {
strncpy(power, (char *)payload, length);
SERIAL_DEBUG_MQTT &&SERIAL_DEBUG_MQTT_VALUES &&Serial.printf("Power: %s, Payload: %s, Length %i\n", power, payload, length);
if (power) {
float fpower = atof(power);
grid_power = floor(fpower);
}
SERIAL_DEBUG_MQTT &&SERIAL_DEBUG_MQTT_VALUES &&Serial.printf("MQTT Received grid power: %i\n", grid_power);
}
if (MQTT_SOLAR_POWER_1 != NULL && (strcmp(topic, MQTT_SOLAR_POWER_1) == 0)) {
if (payload) {
strncpy(solar, (char *)payload, length);
float fsolar = atof(solar);
solar_power_1 = ceil(fsolar);
solar_update = 1;
}
}
if (MQTT_SOLAR_POWER_2 != NULL && (strcmp(topic, MQTT_SOLAR_POWER_2) == 0)) {
strncpy(solar, (char *)payload, length);
float fsolar = atof(solar);
solar_power_2 = static_cast<int>(fsolar);
solar_update = 1;
}
if (mqtt_btnPassthroughtTopic != NULL && (strcmp(topic, mqtt_btnPassthroughtTopic.c_str()) == 0)) {
strncpy(passbtn, (char *)payload, length);
if (strcmp(passbtn, "ON") == 0) {
passBtn = 1;
client.publish(mqtt_btnPassthroughtTopic_status.c_str(), "ON", true);
}
if (strcmp(passbtn, "OFF") == 0) {
passBtn = 0;
client.publish(mqtt_btnPassthroughtTopic_status.c_str(), "OFF", true);
}
}
if (mqtt_btnPassthroughtTopic_status != NULL && (strcmp(topic, mqtt_btnPassthroughtTopic_status.c_str()) == 0)) {
strncpy(passbtn, (char *)payload, length);
}
if (solar_update) {
solar_power_total = solar_power_1 + solar_power_2;
if (SERIAL_DEBUG_MQTT && SERIAL_DEBUG_MQTT_VALUES) {
Serial.printf("MQTT Received solar power: %d\n", solar_power_total);
}
}
SERIAL_DEBUG_MQTT && SERIAL_DEBUG_MQTT_VALUES &&Serial.printf("Grid Power: %i, SolarPowerTotal: %i\n", grid_power, solar_power_total);
if (OLED) {
draw_screen(grid_power, solar_power_total, derivation, mqtt_status, mqtt_reconnections);
}
}
void sendMQTTStatusDiscoveryMsg() {
SERIAL_DEBUG_MQTT &&Serial.println("Sending MQTT Discovery message");
String discoveryTopic = "homeassistant/sensor/" + String(derivator_id) + "/config";
DynamicJsonDocument doc(MAX_MQTT_PACKET);
char buffer[4096];
size_t n;
doc["name"] = "Status";
doc["state_topic"] = mqtt_stateTopic;
doc["availability_topic"] = mqtt_avtyTopic;
doc["uniq_id"] = String(derivator_id) + "_status";
doc["frc_upd"] = true;
doc["pl_avail"] = "Online";
doc["pl_not_avail"] = "Offline";
//doc["mac"] = mac; Where?
doc["ret"] = true;
JsonObject dev = doc.createNestedObject("dev");
JsonArray ids = dev.createNestedArray("ids");
ids.add(String(derivator_id));
dev["name"] = "SimpleDerivator";
dev["mdl"] = "SimpleDerivator";
dev["sw"] = VERSION;
dev["mf"] = "DieGarGon";
//Esto falla y deja de funcionar el autodiscovery
//JsonObject cns = dev.createNestedObject("cns");
//cns["mac"] = mac;
n = serializeJson(doc, buffer);
client.publish(discoveryTopic.c_str(), (uint8_t *)buffer, n);
discoveryTopic = "homeassistant/sensor/" + String(derivator_id) + "/derivation/config";
doc["name"] = "Derivation";
doc["state_topic"] = mqtt_derivationTopic;
doc["uniq_id"] = String(derivator_id) + "_derivion";
doc["ic"] = "mdi:information-outline",
doc["unit_of_meas"] = "%";
doc["dev_cla"] = "power_factor";
doc["frc_upd"] = true;
//doc["val_tpl"] = "{{ value_json.derivation|default(0) }}";
n = serializeJson(doc, buffer);
client.publish(discoveryTopic.c_str(), (uint8_t *)buffer, n), true;
discoveryTopic = "homeassistant/sensor/" + String(derivator_id) + "/rssi/config";
doc["name"] = "RSSI";
doc["state_topic"] = mqtt_rssiTopic;
doc["ic"] = "mdi:information-outline",
doc["unit_of_meas"] = "dBm";
doc["uniq_id"] = String(derivator_id) + "_rssi";
doc["dev_cla"] = "signal_strength";
doc["frc_upd"] = true;
n = serializeJson(doc, buffer);
client.publish(discoveryTopic.c_str(), (uint8_t *)buffer, n, true);
discoveryTopic = "homeassistant/switch/" + String(derivator_id) + "/switch_1/config";
doc["name"] = "Passthrought";
doc["state_topic"] = mqtt_btnPassthroughtTopic_status;
doc["cmd_t"] = mqtt_btnPassthroughtTopic;
doc["uniq_id"] = String(derivator_id) + "_passthr";
doc["pl_on"] = "ON";
doc["pl_off"] = "OFF";
doc["dev_cla"] = "switch";
doc["frc_upd"] = true;
n = serializeJson(doc, buffer);
client.publish(discoveryTopic.c_str(), (uint8_t *)buffer, n, true);
discoveryTopic = "homeassistant/sensor/" + String(derivator_id) + "/mqtt_rec/config";
doc["name"] = "MQTT Reconnect";
doc["state_topic"] = mqtt_reconnectTopic;
doc["ic"] = "mdi:information-outline",
doc["unit_of_meas"] = "";
doc["uniq_id"] = String(derivator_id) + "_mqtt_rec";
doc["dev_cla"] = "signal_strength";
doc["frc_upd"] = true;
n = serializeJson(doc, buffer);
client.publish(discoveryTopic.c_str(), (uint8_t *)buffer, n, true);
discoveryTopic = "homeassistant/sensor/" + String(derivator_id) + "/wifi_disc/config";
doc["name"] = "Wifi Disconnections";
doc["state_topic"] = wifi_disconnectTopic;
doc["ic"] = "mdi:information-outline",
doc["unit_of_meas"] = "";
doc["uniq_id"] = String(derivator_id) + "_wifi_dis";
doc["dev_cla"] = "signal_strength";
doc["frc_upd"] = true;
n = serializeJson(doc, buffer);
client.publish(discoveryTopic.c_str(), (uint8_t *)buffer, n, true);
}