This repository was archived by the owner on Aug 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmqtt.cpp
More file actions
executable file
·286 lines (225 loc) · 9.3 KB
/
mqtt.cpp
File metadata and controls
executable file
·286 lines (225 loc) · 9.3 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
TWC Manager for ESP32
Copyright (C) 2020 Jarl Nicolson
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "mqtt.h"
#include "config.h"
#include "twc_protocol.h"
TeslaMqttIO::TeslaMqttIO(PangolinMQTT &mqttClient) :
mqttClient_(&mqttClient)
{};
void TeslaMqttIO::Begin(TWCConfig &twc_config) {
Serial.print(F("Starting MQTT Client... "));
// TODO: Fix this
mqttClient_->setServer(twc_config.mqtt.server, twc_config.mqtt.port);
mqttClient_->onConnect([this](bool sessionPresent){ this->onMqttConnect(sessionPresent); });
mqttClient_->onMessage([this](const char* topic, const uint8_t* payload, size_t len, uint8_t qos, bool retain, bool dup) { this->onMqttMessage(topic, payload, len, qos, retain, dup); });
mqttClient_->onDisconnect([this](int8_t reason){ this->onMqttDisconnect(reason); });
mqttClient_->connect();
Serial.println(F("Done!"));
}
void TeslaMqttIO::onMqttMessage(const char* topic, const uint8_t* payload, size_t len, uint8_t qos, bool retain, bool dup) {
if (std::string(topic) == "twcController/debug/packetSend") {
if (onRawMessageCallback_) onRawMessageCallback_(payload, len);
} else if (std::string(topic) == "twcController/availableCurrent") {
uint8_t returnCurrent;
char *endPtr;
errno = 0;
long result = strtol((const char*)payload, &endPtr, 10);
if ((uint8_t*)endPtr == payload) {
Serial.printf_P(PSTR("Error converting MQTT current to number!\r\n"));
returnCurrent = 0;
}
else if (errno != 0 && result == 0) {
Serial.printf_P(PSTR("An unspecified error occured converting MQTT current\r\n"));
}
else if (errno == 0) {
if (result > UCHAR_MAX || result < CHAR_MIN) {
Serial.printf_P(PSTR("Number %d out of range (range is 0-255)\r\n"), result);
returnCurrent = 0;
} else {
returnCurrent = (uint8_t)result;
}
} else {
Serial.println(F("An error occured converting the MQTT current"));
}
if(onCurrentMessageCallback_) onCurrentMessageCallback_(returnCurrent);
} else if (std::string(topic) == "twcController/mode") {
if (std::string((const char*)payload) == "charge") {
// Just charge at the
} else if (std::string((const char*)payload) == "stop") {
// stop charging
} else if (std::string((const char*)payload) == "follow") {
// follow the avaialble current
}
}
else if (std::string(topic) == "twcController/debugEnabled") {
if ((char)payload[0] == '1') {
if(onDebugMessageCallback_) onDebugMessageCallback_(true);
} else {
if(onDebugMessageCallback_) onDebugMessageCallback_(false);
}
}
else {
Serial.println(F("Unknown MQTT Message Received"));
}
}
void TeslaMqttIO::onMqttConnect(bool sessionPresent) {
Serial.print(F("Connected to MQTT! Subscribing to topics... "));
mqttClient_->subscribe("twcController/debugEnabled", 2);
mqttClient_->subscribe("twcController/availableCurrent", 2);
mqttClient_->subscribe("twcController/mode", 2);
mqttClient_->subscribe("twcController/debug/packetSend", 2);
Serial.println(F("Done!"));
}
void TeslaMqttIO::onMqttDisconnect(int8_t reason) {
Serial.println(F("Disconnected from MQTT. Attempting to reconnect..."));
mqttClient_->connect();
}
void TeslaMqttIO::onRawMessage(std::function<void(const uint8_t*, size_t)> callback) {
onRawMessageCallback_ = callback;
}
void TeslaMqttIO::onChargeChangeMessage() {
}
void TeslaMqttIO::writeStopCharging(uint16_t twcid) {
char buffer[10];
snprintf(buffer, sizeof(buffer), "%04x", twcid);
mqttClient_->publish("twcController/stopCharging", (uint8_t *)buffer, strlen(buffer), 2, false);
}
void TeslaMqttIO::writeStartCharging(uint16_t twcid) {
char buffer[10];
snprintf(buffer, sizeof(buffer), "%04x", twcid);
mqttClient_->publish("twcController/startCharging", (uint8_t *)buffer, strlen(buffer), 2, false);
}
void TeslaMqttIO::onCurrentMessage(std::function<void(uint8_t)> callback) {
onCurrentMessageCallback_ = callback;
}
void TeslaMqttIO::onDebugMessage(std::function<void(bool)> callback) {
onDebugMessageCallback_ = callback;
}
void TeslaMqttIO::writeRaw(uint8_t *data, size_t length) {
char buffer[length * 2];
char *target = buffer;
for (uint8_t i = 0; i < length; i++) {
target += sprintf(target, "%02x", data[i]);
}
mqttClient_->publish("twcController/debug/raw", (uint8_t *)buffer, strlen(buffer), 2, false);
}
void TeslaMqttIO::writeRawPacket(uint8_t *data, size_t length) {
char buffer[length * 2];
char *target = buffer;
for (uint8_t i = 0; i < length; i++) {
target += sprintf(target, "%02x", data[i]);
}
mqttClient_->publish("twcController/debug/packetReceive", (uint8_t *)buffer, strlen(buffer), 2, false);
}
void TeslaMqttIO::writeActualCurrent(uint8_t actualCurrent) {
char buffer[10];
snprintf(buffer, 10, "%d", actualCurrent);
mqttClient_->publish("twcController/total/current_draw", (uint8_t *)buffer, strlen(buffer), 2, true);
}
void TeslaMqttIO::writeCharger(uint16_t twcid, uint8_t max_allowable_current) {
char topic[50];
snprintf(topic, 50, "twcController/twcs/%04x/max_allowable_current", twcid);
char buffer[10];
snprintf(buffer, 10, "%d", max_allowable_current);
mqttClient_->publish(topic, (uint8_t *)buffer, strlen(buffer), 2, true);
}
void TeslaMqttIO::writeChargerTotalKwh(uint16_t twcid, uint32_t total_kwh) {
char topic[50];
snprintf(topic, 50, "twcController/twcs/%04x/total_kwh_delivered", twcid);
char buffer[10];
snprintf(buffer, 10, "%d", total_kwh);
mqttClient_->publish(topic, (uint8_t *)buffer, strlen(buffer), 2, true);
}
void TeslaMqttIO::writeChargerSerial(uint16_t twcid, uint8_t* serial, size_t length) {
char topic[50];
snprintf(topic, 50, "twcController/twcs/%04x/serial", twcid);
mqttClient_->publish(topic, serial, length, 2, true);
}
void TeslaMqttIO::writeChargerVoltage(uint16_t twcid, uint16_t voltage, uint8_t phase) {
if (phase > 3) {
Serial.println(F("Phase Should be 3 or less!"));
return;
}
char topic[50];
snprintf(topic, 50, "twcController/twcs/%04x/phase_%d_voltage", twcid, phase);
char buffer[10];
snprintf(buffer, 10, "%d", voltage);
mqttClient_->publish(topic, (uint8_t *)buffer, strlen(buffer), 2, true);
}
void TeslaMqttIO::writeChargerCurrent(uint16_t twcid, uint8_t current, uint8_t phase) {
if (phase > 3) {
Serial.println(F("Phase should be 3 or less"));
return;
}
char topic[50];
snprintf(topic, 50, "twcController/twcs/%04x/phase_%d_current", twcid, phase);
char buffer[10];
snprintf(buffer, 10, "%d", current);
mqttClient_->publish(topic, (uint8_t *)buffer, strlen(buffer), 2, true);
};
void TeslaMqttIO::writeTotalConnectedChargers(uint8_t connected_chargers) {
char buffer[10];
snprintf(buffer, 10, "%d", connected_chargers);
mqttClient_->publish("twcController/total/connected_chargers", (uint8_t *)buffer, strlen(buffer), 2, true);
};
void TeslaMqttIO::writeChargerFirmware(uint16_t twcid, EXT_FIRMWARE_PAYLOAD_T *firmware_payload) {
char topic[50];
snprintf(topic, 50, "twcController/twcs/%04x/firmware_version", twcid);
char buffer[10];
snprintf(buffer, 10, "%d.%d.%d.%d",
firmware_payload->major,
firmware_payload->minor,
firmware_payload->revision,
firmware_payload->extended
);
mqttClient_->publish(topic, (uint8_t *)buffer, strlen(buffer), 2, true);
};
void TeslaMqttIO::writeChargerActualCurrent(uint16_t twcid, uint8_t current) {
char topic[50];
snprintf(topic, 50, "twcController/twcs/%04x/actual_current", twcid);
char buffer[10];
snprintf(buffer, 10, "%d", current);
mqttClient_->publish(topic, (uint8_t *)buffer, strlen(buffer), 2, true);
}
void TeslaMqttIO::writeChargerTotalPhaseCurrent(uint8_t current, uint8_t phase) {
char topic[50];
snprintf(topic, 50, "twcController/total/phase_%d_current", phase);
char buffer[10];
snprintf(buffer, 10, "%d", current);
mqttClient_->publish(topic, (uint8_t *)buffer, strlen(buffer), 2, true);
}
void TeslaMqttIO::writeChargerConnectedVin(uint16_t twcid, uint8_t* vin) {
char topic[50];
snprintf(topic, 50, "twcController/twcs/%04x/connected_vin", twcid);
mqttClient_->publish(topic, vin, strlen((const char*)vin), 2, true);
}
void TeslaMqttIO::writeChargerState(uint16_t twcid, uint8_t state) {
char topic[50];
snprintf(topic, 50, "twcController/twcs/%04x/state", twcid);
char buffer[10];
snprintf(buffer, 10, "%d", state);
mqttClient_->publish(topic, (uint8_t *)buffer, strlen(buffer), 2, true);
}
void TeslaMqttIO::writeTotalConnectedCars(uint8_t connected_cars) {
char buffer[10];
snprintf(buffer, 10, "%d", connected_cars);
mqttClient_->publish("twcController/total/connected_cars", (uint8_t *)buffer, strlen(buffer), 2, true);
}
void TeslaMqttIO::writeState() {
//mqttClient.publish("topic", 2, true, payload)
/*
twc/total/connected_cars
*/
}