2023-08-10 02:28:13 +02:00
|
|
|
/*tracerRegisters
|
2023-08-10 02:38:18 +02:00
|
|
|
* https://github.com/alexnathanson/EPSolar_Tracer/blob/master/Arduino/Dec24_modbus_softwareserial/Dec24_modbus_softwareserial.ino
|
|
|
|
*
|
2023-08-10 02:28:13 +02:00
|
|
|
This program is based off of RS485_HalfDuplex.ino found at
|
|
|
|
https://github.com/4-20ma/ModbusMaster/blob/master/examples/RS485_HalfDuplex/RS485_HalfDuplex.ino
|
|
|
|
|
|
|
|
RS485_HalfDuplex.pde - example using ModbusMaster library to communicate
|
|
|
|
with EPSolar LS2024B controller using a half-duplex RS485 transceiver.
|
|
|
|
|
|
|
|
This example is tested against an EPSolar LS2024B solar charge controller.
|
|
|
|
See here for protocol specs:
|
|
|
|
http://www.solar-elektro.cz/data/dokumenty/1733_modbus_protocol.pdf
|
|
|
|
|
|
|
|
Library:: ModbusMaster
|
|
|
|
Author:: Marius Kintel <marius at kintel dot net>
|
|
|
|
|
|
|
|
Copyright:: 2009-2016 Doc Walker
|
|
|
|
|
|
|
|
Modified:: 2023 DeltaLima
|
|
|
|
Notes: I took the register stuff from https://github.com/Bettapro/Solar-Tracer-Blynk-V3
|
|
|
|
Had to hack around e.g. the battOverallCurrent as I only got garbage
|
|
|
|
when using the method from Bettapro.
|
2023-08-10 02:40:56 +02:00
|
|
|
Reading loadPoweredOn does not work atm (10.08.2023)
|
2023-08-10 02:28:13 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
We're using a MAX485-compatible RS485 Transceiver.
|
|
|
|
|
|
|
|
In order to allow for Modbus communication and serial communication with the Arduino Uno,
|
|
|
|
we're using software serial.Rx/Tx is hooked up to the software serial port at RX=10 & TX=11.
|
|
|
|
The Data Enable and Receiver Enable pins are hooked up at DE = 3 & RE = 2.
|
|
|
|
|
|
|
|
Wiring
|
|
|
|
MAX485=>Arduino Uno
|
|
|
|
GND => GND
|
|
|
|
VCC => 5V
|
|
|
|
DI => 11
|
|
|
|
DE => 3
|
|
|
|
RE => 2
|
|
|
|
RO => 10
|
|
|
|
|
2023-08-13 01:24:15 +02:00
|
|
|
MAX4852=>Arduino Uno
|
|
|
|
GND => GND
|
|
|
|
VCC => 5V
|
|
|
|
DI => 9
|
|
|
|
DE => 5
|
|
|
|
RE => 4
|
|
|
|
RO => 6
|
|
|
|
|
2023-08-10 02:28:13 +02:00
|
|
|
TRACER => MAX485
|
|
|
|
blue => B
|
|
|
|
green => A
|
|
|
|
*/
|
|
|
|
//#include <TracerRegisters.h> // empty library for potential future use
|
|
|
|
|
|
|
|
#include <ModbusMaster.h>
|
|
|
|
#include <SoftwareSerial.h>
|
|
|
|
#include "ArduinoJson.h"
|
|
|
|
|
2023-08-13 01:24:15 +02:00
|
|
|
bool twoController = true;
|
|
|
|
|
2023-08-10 02:28:13 +02:00
|
|
|
SoftwareSerial myserial(10, 11); // RX, TX
|
2023-08-13 01:24:15 +02:00
|
|
|
SoftwareSerial myserial2(6, 9); // RX, TX
|
|
|
|
|
|
|
|
|
2023-08-10 02:28:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
#define MAX485_DE 3
|
|
|
|
#define MAX485_RE_NEG 2
|
|
|
|
|
2023-08-13 01:24:15 +02:00
|
|
|
#define MAX485_DE2 5
|
|
|
|
#define MAX485_RE_NEG2 4
|
2023-08-10 02:28:13 +02:00
|
|
|
// instantiate ModbusMaster object
|
|
|
|
ModbusMaster node;
|
2023-08-13 01:24:15 +02:00
|
|
|
ModbusMaster node2;
|
2023-08-10 02:28:13 +02:00
|
|
|
|
|
|
|
// not needed...
|
|
|
|
//float batVolt;
|
|
|
|
//float batPercentage = 0.0;
|
2023-08-10 02:40:56 +02:00
|
|
|
float battChargeCurrent, battDischargeCurrent, battChargePower, battOverallCurrent;
|
2023-08-10 02:28:13 +02:00
|
|
|
float bvoltage, ctemp, btemp, bremaining, lpower, lcurrent, pvvoltage, pvcurrent, pvpower;
|
|
|
|
float stats_today_pv_volt_min, stats_today_pv_volt_max;
|
|
|
|
|
2023-08-13 01:24:15 +02:00
|
|
|
float battChargeCurrent2, battDischargeCurrent2, battChargePower2, battOverallCurrent2;
|
|
|
|
float bvoltage2, ctemp2, btemp2, bremaining2, lpower2, lcurrent2, pvvoltage2, pvcurrent2, pvpower2;
|
|
|
|
float stats_today_pv_volt_min2, stats_today_pv_volt_max2;
|
|
|
|
|
|
|
|
|
2023-08-10 02:28:13 +02:00
|
|
|
bool rs485DataReceived = true;
|
|
|
|
bool loadPoweredOn = true;
|
|
|
|
|
2023-08-10 02:46:02 +02:00
|
|
|
// uint8_t is short hand for a byte or an integer of length 8 bits
|
2023-08-10 02:28:13 +02:00
|
|
|
uint8_t result;
|
|
|
|
uint16_t data[6];
|
|
|
|
|
2023-08-13 01:24:15 +02:00
|
|
|
uint8_t result2;
|
|
|
|
uint16_t data2[6];
|
|
|
|
|
|
|
|
|
2023-08-10 02:28:13 +02:00
|
|
|
void preTransmission()
|
|
|
|
{
|
|
|
|
digitalWrite(MAX485_RE_NEG, 1);
|
|
|
|
digitalWrite(MAX485_DE, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void postTransmission()
|
|
|
|
{
|
|
|
|
digitalWrite(MAX485_RE_NEG, 0);
|
|
|
|
digitalWrite(MAX485_DE, 0);
|
|
|
|
}
|
|
|
|
|
2023-08-13 01:24:15 +02:00
|
|
|
void preTransmission2()
|
|
|
|
{
|
|
|
|
digitalWrite(MAX485_RE_NEG2, 1);
|
|
|
|
digitalWrite(MAX485_DE2, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void postTransmission2()
|
|
|
|
{
|
|
|
|
digitalWrite(MAX485_RE_NEG2, 0);
|
|
|
|
digitalWrite(MAX485_DE2, 0);
|
|
|
|
}
|
|
|
|
|
2023-08-10 02:28:13 +02:00
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
pinMode(MAX485_RE_NEG, OUTPUT);
|
|
|
|
pinMode(MAX485_DE, OUTPUT);
|
|
|
|
// Init in receive mode
|
|
|
|
digitalWrite(MAX485_RE_NEG, 0);
|
|
|
|
digitalWrite(MAX485_DE, 0);
|
|
|
|
|
2023-08-13 01:24:15 +02:00
|
|
|
if(twoController == true) {
|
|
|
|
pinMode(MAX485_RE_NEG2, OUTPUT);
|
|
|
|
pinMode(MAX485_DE2, OUTPUT);
|
|
|
|
// Init in receive mode
|
|
|
|
digitalWrite(MAX485_RE_NEG2, 0);
|
|
|
|
digitalWrite(MAX485_DE2, 0);
|
|
|
|
}
|
2023-08-10 02:28:13 +02:00
|
|
|
// Modbus communication runs at 115200 baud
|
|
|
|
|
|
|
|
//Tracer connection
|
|
|
|
myserial.begin(115200);
|
|
|
|
|
2023-08-13 01:24:15 +02:00
|
|
|
if(twoController == true) {
|
|
|
|
myserial2.begin(115200);
|
|
|
|
}
|
2023-08-10 02:28:13 +02:00
|
|
|
//USB Serial connection
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
|
|
|
// Modbus slave ID 1
|
|
|
|
node.begin(1, myserial);
|
2023-08-13 01:24:15 +02:00
|
|
|
if(twoController == true) {
|
|
|
|
node2.begin(1, myserial2);
|
|
|
|
}
|
2023-08-10 02:28:13 +02:00
|
|
|
// Callbacks allow us to configure the RS485 transceiver correctly
|
|
|
|
node.preTransmission(preTransmission);
|
|
|
|
node.postTransmission(postTransmission);
|
2023-08-13 01:24:15 +02:00
|
|
|
if(twoController == true) {
|
|
|
|
node2.preTransmission(preTransmission2);
|
|
|
|
node2.postTransmission(postTransmission2);
|
|
|
|
}
|
2023-08-10 02:28:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//was true
|
|
|
|
bool state = true;
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
AddressRegistry_3100();
|
|
|
|
AddressRegistry_3106();
|
|
|
|
AddressRegistry_310D();
|
|
|
|
AddressRegistry_311A();
|
|
|
|
AddressRegistry_331B();
|
|
|
|
|
|
|
|
|
2023-08-13 01:24:15 +02:00
|
|
|
StaticJsonDocument<256> jsonOut;
|
|
|
|
jsonOut["1"]["pvpower"] = pvpower;
|
|
|
|
jsonOut["1"]["pvcurrent"] = pvcurrent;
|
|
|
|
jsonOut["1"]["pvvoltage"] = pvvoltage;
|
|
|
|
jsonOut["1"]["lcurrent"] = lcurrent;
|
|
|
|
jsonOut["1"]["pvpower"] = pvpower;
|
|
|
|
jsonOut["1"]["lpower"] = lpower;
|
|
|
|
jsonOut["1"]["pvpower"] = pvpower;
|
|
|
|
jsonOut["1"]["btemp"] = btemp;
|
|
|
|
jsonOut["1"]["bvoltage"] = bvoltage;
|
|
|
|
jsonOut["1"]["bremaining"] = bremaining;
|
|
|
|
jsonOut["1"]["ctemp"] = ctemp;
|
|
|
|
jsonOut["1"]["battChargeCurrent"] = battChargeCurrent;
|
|
|
|
jsonOut["1"]["battChargePower"] = battChargePower;
|
|
|
|
jsonOut["1"]["battOverallCurrent"] = battOverallCurrent;
|
|
|
|
jsonOut["1"]["loadPoweredOn"] = loadPoweredOn;
|
|
|
|
|
|
|
|
if(twoController == true) {
|
|
|
|
jsonOut["2"]["pvpower"] = pvpower;
|
|
|
|
jsonOut["2"]["pvcurrent"] = pvcurrent;
|
|
|
|
jsonOut["2"]["pvvoltage"] = pvvoltage;
|
|
|
|
jsonOut["2"]["lcurrent"] = lcurrent;
|
|
|
|
jsonOut["2"]["pvpower"] = pvpower;
|
|
|
|
jsonOut["2"]["lpower"] = lpower;
|
|
|
|
jsonOut["2"]["pvpower"] = pvpower;
|
|
|
|
jsonOut["2"]["btemp"] = btemp;
|
|
|
|
jsonOut["2"]["bvoltage"] = bvoltage;
|
|
|
|
jsonOut["2"]["bremaining"] = bremaining;
|
|
|
|
jsonOut["2"]["ctemp"] = ctemp;
|
|
|
|
jsonOut["2"]["battChargeCurrent"] = battChargeCurrent;
|
|
|
|
jsonOut["2"]["battChargePower"] = battChargePower;
|
|
|
|
jsonOut["2"]["battOverallCurrent"] = battOverallCurrent;
|
|
|
|
jsonOut["2"]["loadPoweredOn"] = loadPoweredOn;
|
|
|
|
}
|
2023-08-10 02:28:13 +02:00
|
|
|
serializeJson(jsonOut, Serial);
|
|
|
|
Serial.println();
|
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-10 02:46:02 +02:00
|
|
|
// AddressRegistry from https://github.com/Bettapro/Solar-Tracer-Blynk-V3
|
2023-08-10 02:28:13 +02:00
|
|
|
void AddressRegistry_3100() {
|
|
|
|
result = node.readInputRegisters(0x3100, 6);
|
2023-08-13 01:24:15 +02:00
|
|
|
|
|
|
|
if (result == node.ku8MBSuccess) {
|
2023-08-10 02:28:13 +02:00
|
|
|
|
|
|
|
pvvoltage = node.getResponseBuffer(0x00) / 100.0f;
|
2023-08-13 01:24:15 +02:00
|
|
|
|
2023-08-10 02:28:13 +02:00
|
|
|
pvcurrent = node.getResponseBuffer(0x01) / 100.0f;
|
|
|
|
|
|
|
|
pvpower = (node.getResponseBuffer(0x02) | node.getResponseBuffer(0x03) << 16) / 100.0f;
|
|
|
|
|
|
|
|
bvoltage = node.getResponseBuffer(0x04) / 100.0f;
|
2023-08-13 01:24:15 +02:00
|
|
|
|
2023-08-10 02:28:13 +02:00
|
|
|
battChargeCurrent = node.getResponseBuffer(0x05) / 100.0f;
|
2023-08-13 01:24:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(twoController == true) {
|
|
|
|
result2 = node2.readInputRegisters(0x3100, 6);
|
|
|
|
|
|
|
|
if (result2 == node2.ku8MBSuccess) {
|
|
|
|
|
|
|
|
pvvoltage2 = node2.getResponseBuffer(0x00) / 100.0f;
|
|
|
|
|
|
|
|
pvcurrent2 = node2.getResponseBuffer(0x01) / 100.0f;
|
|
|
|
|
|
|
|
pvpower2 = (node2.getResponseBuffer(0x02) | node.getResponseBuffer(0x03) << 16) / 100.0f;
|
|
|
|
|
|
|
|
bvoltage2 = node2.getResponseBuffer(0x04) / 100.0f;
|
|
|
|
|
|
|
|
battChargeCurrent2 = node2.getResponseBuffer(0x05) / 100.0f;
|
|
|
|
|
|
|
|
}
|
2023-08-10 02:28:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddressRegistry_3106()
|
|
|
|
{
|
|
|
|
result = node.readInputRegisters(0x3106, 2);
|
|
|
|
|
|
|
|
if (result == node.ku8MBSuccess) {
|
|
|
|
battChargePower = (node.getResponseBuffer(0x00) | node.getResponseBuffer(0x01) << 16) / 100.0f;
|
2023-08-13 01:24:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(twoController == true) {
|
|
|
|
result2 = node2.readInputRegisters(0x3106, 2);
|
|
|
|
|
|
|
|
if (result2 == node2.ku8MBSuccess) {
|
|
|
|
battChargePower2 = (node2.getResponseBuffer(0x00) | node2.getResponseBuffer(0x01) << 16) / 100.0f;
|
|
|
|
}
|
2023-08-10 02:28:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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!");
|
2023-08-13 01:24:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(twoController == true) {
|
|
|
|
|
|
|
|
result2 = node2.readInputRegisters(0x310D, 3);
|
|
|
|
|
|
|
|
if (result2 == node2.ku8MBSuccess) {
|
|
|
|
lcurrent2 = node2.getResponseBuffer(0x00) / 100.0f;
|
|
|
|
//Serial.print("Load Current: ");
|
|
|
|
//Serial.println(lcurrent);
|
|
|
|
|
|
|
|
lpower2 = (node2.getResponseBuffer(0x01) | node2.getResponseBuffer(0x02) << 16) / 100.0f;
|
|
|
|
//Serial.print("Load Power: ");
|
|
|
|
//Serial.println(lpower);
|
|
|
|
} else {
|
|
|
|
rs485DataReceived = false;
|
|
|
|
//Serial.println("Read register 0x310D failed!");
|
|
|
|
}
|
|
|
|
}
|
2023-08-10 02:28:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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!");
|
|
|
|
}
|
2023-08-13 01:24:15 +02:00
|
|
|
|
|
|
|
if(twoController == true) {
|
|
|
|
result2 = node2.readInputRegisters(0x311A, 2);
|
|
|
|
|
|
|
|
if (result2 == node2.ku8MBSuccess) {
|
|
|
|
bremaining2 = node2.getResponseBuffer(0x00) / 1.0f;
|
|
|
|
//Serial.print("Battery Remaining %: ");
|
|
|
|
//Serial.println(bremaining);
|
|
|
|
|
|
|
|
btemp2 = node2.getResponseBuffer(0x01) / 100.0f;
|
|
|
|
//Serial.print("Battery Temperature: ");
|
|
|
|
//Serial.println(btemp);
|
|
|
|
} else {
|
|
|
|
rs485DataReceived = false;
|
|
|
|
//Serial.println("Read register 0x311A failed!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-10 02:28:13 +02:00
|
|
|
|
|
|
|
void AddressRegistry_331B() {
|
|
|
|
result = node.readInputRegisters(0x331B, 2);
|
|
|
|
|
|
|
|
if (result == node.ku8MBSuccess) {
|
|
|
|
|
|
|
|
//battOverallCurrent = (node.getResponseBuffer(0x00)| node.getResponseBuffer(0x01) << 16) / 100.0f;
|
|
|
|
// idk why this works not with arduino. with esp8266 it worked
|
|
|
|
//battOverallCurrent_war = *((int16_t *)&battOverallCurrent_raw);
|
|
|
|
//battOverallCurrent = 0.001 * battOverallCurrent_war;
|
|
|
|
battOverallCurrent = (battChargeCurrent - lcurrent);
|
|
|
|
|
|
|
|
|
2023-08-10 02:36:44 +02:00
|
|
|
//Serial.print("Battery Discharge Current: ");
|
|
|
|
//Serial.println(battOverallCurrent);
|
2023-08-10 02:28:13 +02:00
|
|
|
} else {
|
|
|
|
rs485DataReceived = false;
|
|
|
|
//Serial.println("Read register 0x331B failed!");
|
|
|
|
}
|
2023-08-13 01:24:15 +02:00
|
|
|
|
|
|
|
if(twoController == true) {
|
|
|
|
result2 = node2.readInputRegisters(0x331B, 2);
|
|
|
|
|
|
|
|
if (result2 == node2.ku8MBSuccess) {
|
|
|
|
|
|
|
|
//battOverallCurrent = (node.getResponseBuffer(0x00)| node.getResponseBuffer(0x01) << 16) / 100.0f;
|
|
|
|
// idk why this works not with arduino. with esp8266 it worked
|
|
|
|
//battOverallCurrent_war = *((int16_t *)&battOverallCurrent_raw);
|
|
|
|
//battOverallCurrent = 0.001 * battOverallCurrent_war;
|
|
|
|
battOverallCurrent2 = (battChargeCurrent2 - lcurrent2);
|
|
|
|
|
|
|
|
|
|
|
|
//Serial.print("Battery Discharge Current: ");
|
|
|
|
//Serial.println(battOverallCurrent);
|
|
|
|
} else {
|
|
|
|
rs485DataReceived = false;
|
|
|
|
//Serial.println("Read register 0x331B failed!");
|
|
|
|
}
|
|
|
|
}
|
2023-08-10 02:28:13 +02:00
|
|
|
}
|