forked from tekk/Tracer-RS485-Modbus-Blynk-V2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTracer-RS485-Modbus-Blynk-V2.ino
376 lines (300 loc) · 10.6 KB
/
Tracer-RS485-Modbus-Blynk-V2.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// CONNECT THE RS485 MODULE.
// MAX485 module <-> ESP8266
// - DI -> D10 / GPIO1 / TX
// - RO -> D9 / GPIO3 / RX
// - DE and RE are interconnected with a jumper and then connected do eighter pin D1 or D2
// - VCC to +5V / VIN on ESP8266
// - GNDs wired together
// -------------------------------------
// You do not need to disconnect the RS485 while uploading code.
// After first upload you should be able to upload over WiFi
// Tested on NodeMCU + MAX485 module
// RJ 45 cable: Green -> A, Blue -> B, Brown -> GND module + GND ESP8266
// MAX485: DE + RE interconnected with a jumper and connected to D1 or D2
//
// Developed by @jaminNZx
// With modifications by @tekk
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <ModbusMaster.h>
#include "settings.h"
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
const int defaultBaudRate = 115200;
int timerTask1, timerTask2, timerTask3;
float battChargeCurrent, battDischargeCurrent, battOverallCurrent, battChargePower;
float bvoltage, ctemp, btemp, bremaining, lpower, lcurrent, pvvoltage, pvcurrent, pvpower;
float stats_today_pv_volt_min, stats_today_pv_volt_max;
uint8_t result;
bool rs485DataReceived = true;
bool loadPoweredOn = true;
#define MAX485_DE D1
#define MAX485_RE_NEG D2
ModbusMaster node;
SimpleTimer timer;
void preTransmission() {
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission() {
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
// A list of the regisities to query in order
typedef void (*RegistryList[])();
RegistryList Registries = {
AddressRegistry_3100,
AddressRegistry_3106,
AddressRegistry_310D,
AddressRegistry_311A,
AddressRegistry_331B,
};
// keep log of where we are
uint8_t currentRegistryNumber = 0;
// function to switch to next registry
void nextRegistryNumber() {
// better not use modulo, because after overlow it will start reading in incorrect order
currentRegistryNumber++;
if (currentRegistryNumber >= ARRAY_SIZE(Registries)) {
currentRegistryNumber = 0;
}
}
// ****************************************************************************
void setup()
{
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
Serial.begin(defaultBaudRate);
// Modbus slave ID 1
node.begin(1, Serial);
// callbacks to toggle DE + RE on MAX485
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
Serial.println("Connecting to Wifi...");
WiFi.mode(WIFI_STA);
#if defined(USE_LOCAL_SERVER)
Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS, SERVER);
#else
Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS);
#endif
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
Serial.println("Connected.");
Serial.print("Connecting to Blynk...");
while (!Blynk.connect()) {
Serial.print(".");
delay(100);
}
Serial.println();
Serial.println("Connected to Blynk.");
Serial.println("Starting ArduinoOTA...");
ArduinoOTA.setHostname(OTA_HOSTNAME);
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_SPIFFS
type = "filesystem";
}
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd of update");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
Serial.print("ArduinoOTA running. ");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Starting timed actions...");
timerTask1 = timer.setInterval(1000L, executeCurrentRegistryFunction);
timerTask2 = timer.setInterval(1000L, nextRegistryNumber);
timerTask3 = timer.setInterval(1000L, uploadToBlynk);
Serial.println("Setup OK!");
Serial.println("----------------------------");
Serial.println();
}
// --------------------------------------------------------------------------------
// upload values
void uploadToBlynk() {
Blynk.virtualWrite(vPIN_PV_POWER, pvpower);
Blynk.virtualWrite(vPIN_PV_CURRENT, pvcurrent);
Blynk.virtualWrite(vPIN_PV_VOLTAGE, pvvoltage);
Blynk.virtualWrite(vPIN_LOAD_CURRENT, lcurrent);
Blynk.virtualWrite(vPIN_LOAD_POWER, lpower);
Blynk.virtualWrite(vPIN_BATT_TEMP, btemp);
Blynk.virtualWrite(vPIN_BATT_VOLTAGE, bvoltage);
Blynk.virtualWrite(vPIN_BATT_REMAIN, bremaining);
Blynk.virtualWrite(vPIN_CONTROLLER_TEMP, ctemp);
Blynk.virtualWrite(vPIN_BATTERY_CHARGE_CURRENT, battChargeCurrent);
Blynk.virtualWrite(vPIN_BATTERY_CHARGE_POWER, battChargePower);
Blynk.virtualWrite(vPIN_BATTERY_OVERALL_CURRENT, battOverallCurrent);
Blynk.virtualWrite(vPIN_LOAD_ENABLED, loadPoweredOn);
}
// exec a function of registry read (cycles between different addresses)
void executeCurrentRegistryFunction() {
Registries[currentRegistryNumber]();
}
uint8_t setOutputLoadPower(uint8_t state) {
Serial.print("Writing coil 0x0006 value to: ");
Serial.println(state);
delay(10);
// Set coil at address 0x0006 (Force the load on/off)
result = node.writeSingleCoil(0x0006, state);
if (result == node.ku8MBSuccess) {
node.getResponseBuffer(0x00);
Serial.println("Success.");
}
return result;
}
// callback to on/off button state changes from the Blynk app
BLYNK_WRITE(vPIN_LOAD_ENABLED) {
uint8_t newState = (uint8_t)param.asInt();
Serial.print("Setting load state output coil to value: ");
Serial.println(newState);
result = setOutputLoadPower(newState);
//readOutputLoadState();
result &= checkLoadCoilState();
if (result == node.ku8MBSuccess) {
Serial.println("Write & Read suceeded.");
} else {
Serial.println("Write & Read failed.");
}
Serial.print("Output Load state value: ");
Serial.println(loadPoweredOn);
Serial.println();
Serial.println("Uploading results to Blynk.");
uploadToBlynk();
}
uint8_t readOutputLoadState() {
delay(10);
result = node.readHoldingRegisters(0x903D, 1);
if (result == node.ku8MBSuccess) {
loadPoweredOn = (node.getResponseBuffer(0x00) & 0x02) > 0;
Serial.print("Set success. Load: ");
Serial.println(loadPoweredOn);
} else {
// update of status failed
Serial.println("readHoldingRegisters(0x903D, 1) failed!");
}
return result;
}
// reads Load Enable Override coil
uint8_t checkLoadCoilState() {
Serial.print("Reading coil 0x0006... ");
delay(10);
result = node.readCoils(0x0006, 1);
Serial.print("Result: ");
Serial.println(result);
if (result == node.ku8MBSuccess) {
loadPoweredOn = (node.getResponseBuffer(0x00) > 0);
Serial.print(" Value: ");
Serial.println(loadPoweredOn);
} else {
Serial.println("Failed to read coil 0x0006!");
}
return result;
}
// -----------------------------------------------------------------
void AddressRegistry_3100() {
result = node.readInputRegisters(0x3100, 6);
if (result == node.ku8MBSuccess) {
pvvoltage = node.getResponseBuffer(0x00) / 100.0f;
Serial.print("PV Voltage: ");
Serial.println(pvvoltage);
pvcurrent = node.getResponseBuffer(0x01) / 100.0f;
Serial.print("PV Current: ");
Serial.println(pvcurrent);
pvpower = (node.getResponseBuffer(0x02) | node.getResponseBuffer(0x03) << 16) / 100.0f;
Serial.print("PV Power: ");
Serial.println(pvpower);
bvoltage = node.getResponseBuffer(0x04) / 100.0f;
Serial.print("Battery Voltage: ");
Serial.println(bvoltage);
battChargeCurrent = node.getResponseBuffer(0x05) / 100.0f;
Serial.print("Battery Charge Current: ");
Serial.println(battChargeCurrent);
}
}
void AddressRegistry_3106()
{
result = node.readInputRegisters(0x3106, 2);
if (result == node.ku8MBSuccess) {
battChargePower = (node.getResponseBuffer(0x00) | node.getResponseBuffer(0x01) << 16) / 100.0f;
Serial.print("Battery Charge Power: ");
Serial.println(battChargePower);
}
}
void AddressRegistry_310D()
{
result = node.readInputRegisters(0x310D, 3);
if (result == node.ku8MBSuccess) {
lcurrent = node.getResponseBuffer(0x00) / 100.0f;
Serial.print("Load Current: ");
Serial.println(lcurrent);
lpower = (node.getResponseBuffer(0x01) | node.getResponseBuffer(0x02) << 16) / 100.0f;
Serial.print("Load Power: ");
Serial.println(lpower);
} else {
rs485DataReceived = false;
Serial.println("Read register 0x310D failed!");
}
}
void AddressRegistry_311A() {
result = node.readInputRegisters(0x311A, 2);
if (result == node.ku8MBSuccess) {
bremaining = node.getResponseBuffer(0x00) / 1.0f;
Serial.print("Battery Remaining %: ");
Serial.println(bremaining);
btemp = node.getResponseBuffer(0x01) / 100.0f;
Serial.print("Battery Temperature: ");
Serial.println(btemp);
} else {
rs485DataReceived = false;
Serial.println("Read register 0x311A failed!");
}
}
void AddressRegistry_331B() {
result = node.readInputRegisters(0x331B, 2);
if (result == node.ku8MBSuccess) {
battOverallCurrent = (node.getResponseBuffer(0x00) | node.getResponseBuffer(0x01) << 16) / 100.0f;
Serial.print("Battery Discharge Current: ");
Serial.println(battOverallCurrent);
} else {
rs485DataReceived = false;
Serial.println("Read register 0x331B failed!");
}
}
void loop()
{
Blynk.run();
ArduinoOTA.handle();
timer.run();
}