arduino-max485-epever-json-.../arduino-max485-epever-json-...

440 lines
13 KiB
C++

/*tracerRegisters
* https://github.com/alexnathanson/EPSolar_Tracer/blob/master/Arduino/Dec24_modbus_softwareserial/Dec24_modbus_softwareserial.ino
*
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.
Reading loadPoweredOn does not work atm (10.08.2023)
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
GND => GND
VCC => 5V
DI => 9
DE => 3
RE => 2
RO => 8
MAX485 Nr 2 =>Arduino
GND => GND
VCC => 5V
DI => 11
DE => 5
RE => 4
RO => 10
TRACER => MAX485
blue => B
green => A
Put a 10K Resistor from D7 to GND.
To enable two Controller Mode, add a Jumper from 5V to D7.
*/
//#include <TracerRegisters.h> // empty library for potential future use
#include <ModbusMaster.h>
#include <SoftwareSerial.h>
#include "ArduinoJson.h"
// when having two solar controllers, set it to true, otherwise to false
// it is important to set it to false when only having one solar controller
// to avoid waiting for the timeout of the second controller
bool twoController = false;
int twoControllerBoolPin = 7;
SoftwareSerial myserial(8, 9); // RX, TX
SoftwareSerial myserial2(10, 11); // RX, TX
#define MAX485_DE 3
#define MAX485_RE_NEG 2
#define MAX485_DE2 5
#define MAX485_RE_NEG2 4
// instantiate ModbusMaster object
ModbusMaster node;
ModbusMaster node2;
// not needed...
//float batVolt;
//float batPercentage = 0.0;
float battChargeCurrent, battDischargeCurrent, battChargePower, battOverallCurrent;
float bvoltage, ctemp, btemp, bremaining, lpower, lcurrent, pvvoltage, pvcurrent, pvpower;
float stats_today_pv_volt_min, stats_today_pv_volt_max;
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;
bool rs485DataReceived = true;
bool loadPoweredOn = true;
bool loadPoweredOn2 = true;
// uint8_t is short hand for a byte or an integer of length 8 bits
uint8_t result;
uint16_t data[6];
uint8_t result2;
uint16_t data2[6];
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
void preTransmission2()
{
digitalWrite(MAX485_RE_NEG2, 1);
digitalWrite(MAX485_DE2, 1);
}
void postTransmission2()
{
digitalWrite(MAX485_RE_NEG2, 0);
digitalWrite(MAX485_DE2, 0);
}
void setup()
{
// set twoControllerBoolPin to Input
pinMode(twoControllerBoolPin, INPUT);
// check if twoController Mode is set (HIGH)
if(digitalRead(twoControllerBoolPin) == HIGH ) {
twoController = true;
}
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
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);
}
// Modbus communication runs at 115200 baud
//Tracer connection 1
myserial.begin(115200);
if(twoController == true) {
//Tracer connection 2
myserial2.begin(115200);
}
//USB Serial connection
Serial.begin(115200);
// Modbus slave ID 1
node.begin(1, myserial);
if(twoController == true) {
node2.begin(1, myserial2);
}
// Callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
if(twoController == true) {
node2.preTransmission(preTransmission2);
node2.postTransmission(postTransmission2);
}
}
void loop()
{
myserial.listen();
// I have some issues sometimes getting values and maybe some delay helps IDK
AddressRegistry_3100();
AddressRegistry_3106();
AddressRegistry_310D();
AddressRegistry_311A();
AddressRegistry_331B();
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"] = pvpower2;
jsonOut["2"]["pvcurrent"] = pvcurrent2;
jsonOut["2"]["pvvoltage"] = pvvoltage2;
jsonOut["2"]["lcurrent"] = lcurrent2;
jsonOut["2"]["pvpower"] = pvpower2;
jsonOut["2"]["lpower"] = lpower2;
jsonOut["2"]["pvpower"] = pvpower2;
jsonOut["2"]["btemp"] = btemp2;
jsonOut["2"]["bvoltage"] = bvoltage2;
jsonOut["2"]["bremaining"] = bremaining2;
jsonOut["2"]["ctemp"] = ctemp2;
jsonOut["2"]["battChargeCurrent"] = battChargeCurrent2;
jsonOut["2"]["battChargePower"] = battChargePower2;
jsonOut["2"]["battOverallCurrent"] = battOverallCurrent2;
jsonOut["2"]["loadPoweredOn"] = loadPoweredOn2;
}
serializeJson(jsonOut, Serial);
Serial.println();
//Serial.write( '\r' ); // Carriage Return
//Serial.write( '\n' ); // EOL
delay(3000);
}
// AddressRegistry from https://github.com/Bettapro/Solar-Tracer-Blynk-V3
void AddressRegistry_3100() {
// listen to the first software serial port
myserial.listen();
result = node.readInputRegisters(0x3100, 6);
if (result == node.ku8MBSuccess) {
pvvoltage = node.getResponseBuffer(0x00) / 100.0f;
pvcurrent = node.getResponseBuffer(0x01) / 100.0f;
pvpower = (node.getResponseBuffer(0x02) | node.getResponseBuffer(0x03) << 16) / 100.0f;
bvoltage = node.getResponseBuffer(0x04) / 100.0f;
battChargeCurrent = node.getResponseBuffer(0x05) / 100.0f;
}
if(twoController == true) {
// listen to the first software serial port
myserial2.listen();
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;
}
}
}
void AddressRegistry_3106()
{
// listen to the first software serial port
myserial.listen();
result = node.readInputRegisters(0x3106, 2);
if (result == node.ku8MBSuccess) {
battChargePower = (node.getResponseBuffer(0x00) | node.getResponseBuffer(0x01) << 16) / 100.0f;
}
if(twoController == true) {
// listen to the first software serial port
myserial2.listen();
result2 = node2.readInputRegisters(0x3106, 2);
if (result2 == node2.ku8MBSuccess) {
battChargePower2 = (node2.getResponseBuffer(0x00) | node2.getResponseBuffer(0x01) << 16) / 100.0f;
}
}
}
void AddressRegistry_310D()
{
// listen to the first software serial port
myserial.listen();
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!");
}
if(twoController == true) {
// listen to the first software serial port
myserial2.listen();
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!");
}
}
}
void AddressRegistry_311A() {
// listen to the first software serial port
myserial.listen();
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!");
}
if(twoController == true) {
// listen to the first software serial port
myserial2.listen();
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!");
}
}
}
void AddressRegistry_331B() {
// listen to the first software serial port
myserial.listen();
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);
//Serial.print("Battery Discharge Current: ");
//Serial.println(battOverallCurrent);
} else {
rs485DataReceived = false;
//Serial.println("Read register 0x331B failed!");
}
if(twoController == true) {
// listen to the first software serial port
myserial2.listen();
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!");
}
}
}