-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVictron-MQTT.ino
304 lines (285 loc) · 8.99 KB
/
Victron-MQTT.ino
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//this code is for ESP32, its take UART telemetry from Victron Solar chargers and relay the data to MQTT server.
//this code is based on project: http://www.svpartyoffive.com/2018/02/28/victron-monitors-technical/
//Be advide Victron UART port use 5V logic levels, ESP32 is designed for 3.3V logic levels
//use logic level converter to avoid damage your ESP32!!!
// define UART2 at pins 16 and 17
/*
PID 0xA043 -- Product ID for BlueSolar MPPT 100/15
FW 119 -- Firmware version of controller, v1.19
SER# HQXXXXXXXXX -- Serial number
V 13790 -- Battery voltage, mV
I -10 -- Battery current, mA
VPV 15950 -- Panel voltage, mV
PPV 0 -- Panel power, W
CS 5 -- Charge state, 0 to 9
ERR 0 -- Error code, 0 to 119
LOAD ON -- Load output state, ON/OFF
IL 0 -- Load current, mA
H19 0 -- Yield total, kWh
H20 0 -- Yield today, kWh
H21 397 -- Maximum power today, W
H22 0 -- Yield yesterday, kWh
H23 0 -- Maximum power yesterday, W
HSDS 0 -- Day sequence number, 0 to 365
Checksum l:A0002000148 -- Message checksum
MQTT topics
victron/mode reports the charger mode 0=OFF, 1=LOW_Power, 2=fault, 3=BULK, 4=Absorption, 5=FLOAT, 6=Inverter
victron/solar/w reports the power of the solar panels
victron/solar/v reports the voltage of the solar panels
victron/batery/c reports the charging current
victron/batery/v reports the battery voltage
TODO
1. to make JSON format of the reported data to MQTT
2. to add H19 H20 H21 H22 H23
*/
#include <WiFi.h>
#include <PubSubClient.h>
#define RXD2 16
#define TXD2 17
#define DEBUG
//Cnange this section
const char* ssid = "SSID";
const char* password = "yourpass";
const char* mqtt_server = "mqtt_server_ip";
WiFiClient espClient11;
PubSubClient client(espClient11);
long lastMsg = 0;
char msg[50];
int value = 0;
int CS1;
float V2, I2, VPV2;
const byte numChars = 30;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
const char * labelsOfInterest[] = {"V", "I", "VPV", "PPV", "CS"};
const unsigned int maxLabelsOfInterest = sizeof(labelsOfInterest) / sizeof(*labelsOfInterest);
boolean sleep1 = false;
void setup() {
Serial.begin(19200);
delay(500);
Serial.println("initial....");
Serial2.begin(19200, SERIAL_8N1, RXD2, TXD2);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
delay(1000);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
#ifdef DEBUG
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
#endif
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
#ifdef DEBUG
Serial.print(".");
#endif
}
#ifdef DEBUG
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
#endif
}
void reconnect() {
// Loop until we're reconnected
// while (!client.connected()) {
if (!client.connected()) {
#ifdef DEBUG
Serial.print("Attempting MQTT connection...");
#endif
// Attempt to connect
if (client.connect("ESP32Client")) {
#ifdef DEBUG
Serial.println("connected");
#endif
// Subscribe
client.subscribe("b_node1/sorce");
} else {
#ifdef DEBUG
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
#endif
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
recvWithEndMarker();
if (newData) { // we have received a ful line, deal with it
parseNewData(); // this messes up receivedChars[]
newData = false;
}
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial2.available() > 0 && newData == false) {
rc = Serial2.read();
if (rc != '\r') { // we ignore that character
if (rc != endMarker) {
if (rc == '\t') rc = ' '; // change tabs into just a space
receivedChars[ndx] = rc;
ndx++; // we go to the next space in our buffer
if (ndx >= numChars) { // and check bounds.
ndx = numChars - 1; // that means we will loose the end of long strings
}
} else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0; // ndx is static, so we prepare for next turn
newData = true;
}
}
}
}
void parseNewData()
{
const char *delim = " ";
char * item;
boolean labelFound;
long int labelValue;
int labelIndex;
item = strtok (receivedChars, delim);
labelFound = false;
for (int i = 0; i < maxLabelsOfInterest; ++i) {
if (!strcmp(labelsOfInterest[i], item)) {
item = strtok(NULL, delim);
if (item != NULL) {
labelFound = true;
labelIndex = i;
labelValue = atol(item);
break; // ends the for loop as we found a label
} // end if we had a value for the label
} // end string compare
} // end for
if (labelFound) {
switch (labelIndex) {
case 0:
V2 = labelValue / 1000.00f;
Serial.print("Battery Volate = ");
Serial.println(V2);
char V2_String[8];
dtostrf(V2, 1, 2, V2_String);
client.publish("victron/batery/v", V2_String);
break;
case 1:
I2 = labelValue / 1000.00f;
if (I2 < 0.05) {
Serial.println("Charger Sleeping ");
char I2_String[8];
dtostrf(I2, 1, 2, I2_String);
client.publish("victron/batery/c", I2_String);
sleep1=true;
}
else {
Serial.print("Current Into Batts = ");
Serial.println(I2);
char I2_String[8];
dtostrf(I2, 1, 2, I2_String);
client.publish("victron/batery/c", I2_String);
sleep1=false;
}
break;
case 2:
VPV2 = labelValue / 1000.0f;
if (sleep1 == true) {
Serial.println("Panels Sleeping ");
char VPV2_String[8];
dtostrf(VPV2, 1, 2, VPV2_String);
client.publish("victron/solar/v", VPV2_String);
}
else {
Serial.print("Panel Voltage = ");
Serial.println(VPV2);
char VPV2_String[8];
dtostrf(VPV2, 1, 2, VPV2_String);
client.publish("victron/solar/v", VPV2_String);
}
break;
case 3:
Serial.print("Panel Wattage = ");
Serial.println(labelValue);
char labelValue_String[8];
dtostrf(labelValue, 1, 2, labelValue_String);
client.publish("victron/solar/w", labelValue_String);
break;
case 4:
CS1=labelValue;
switch (CS1) {
case 0:
Serial.println("Charger is OFF");
client.publish("victron/mode", "0");
CS1 = NULL;
break;
case 1:
Serial.println("Charger is in low power mode");
client.publish("victron/mode", "1");
CS1 = NULL;
break;
case 2:
Serial.println("Charger has a fault");
client.publish("victron/mode", "2");
CS1 = NULL;
break;
case 3:
Serial.println("Charger is in BULK mode");
client.publish("victron/mode", "3");
CS1 = NULL;
break;
case 4:
Serial.println("Charger is in ABSORB mode");
client.publish("victron/mode", "4");
CS1 = NULL;
break;
case 5:
Serial.println("Charger is in FLOAT mode");
client.publish("victron/mode", "5");
CS1 = NULL;
break;
case 9:
Serial.println("Charger is in INVERT mode");
client.publish("victron/mode", "9");
CS1 = NULL;
break;
}
Serial.print("Charger State = ");
Serial.println(labelValue);
Serial.print("\n");
delay(3000);
break;
}
} else {
}
}
void callback(char* topic, byte* message, unsigned int length) {
#ifdef DEBUG
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
#endif
String messageTemp;
for (int i = 0; i < length; i++) {
#ifdef DEBUG
Serial.print((char)message[i]);
#endif
messageTemp += (char)message[i];
}
#ifdef DEBUG
Serial.println();
#endif
// Feel free to add more if statements to control more GPIOs with MQTT
// If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
// Changes the output state according to the message
}