it is now working with two seperate solar charge controllers with two mx485 software serial ports :)

This commit is contained in:
Marcus 2023-08-13 02:46:36 +02:00
parent 95e1ea053a
commit 28f49e981d

View file

@ -59,7 +59,7 @@
DI => 9
DE => 5
RE => 4
RO => 6
RO => 8
TRACER => MAX485
blue => B
@ -77,7 +77,7 @@
bool twoController = true;
SoftwareSerial myserial(10, 11); // RX, TX
SoftwareSerial myserial2(6, 9); // RX, TX
SoftwareSerial myserial2(8, 9); // RX, TX
@ -105,6 +105,7 @@ 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;
@ -166,23 +167,30 @@ void setup()
// 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);
}
}
//was true
bool state = true;
int countRound = 0;
void loop()
{
myserial.listen();
// I have some issues sometimes getting values and maybe some delay helps IDK
AddressRegistry_3100();
AddressRegistry_3106();
AddressRegistry_310D();
@ -208,30 +216,37 @@ void loop()
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;
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.println();
//Serial.write( '\r' ); // Carriage Return
Serial.write( '\n' ); // Carriage Return
Serial.println(countRound);
countRound++;
delay(1000);
}
// 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) {
@ -249,6 +264,8 @@ void loop()
}
if(twoController == true) {
// listen to the first software serial port
myserial2.listen();
result2 = node2.readInputRegisters(0x3100, 6);
if (result2 == node2.ku8MBSuccess) {
@ -265,10 +282,13 @@ void loop()
}
}
}
void AddressRegistry_3106()
{
// listen to the first software serial port
myserial.listen();
result = node.readInputRegisters(0x3106, 2);
if (result == node.ku8MBSuccess) {
@ -276,16 +296,21 @@ void loop()
}
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) {
@ -302,7 +327,8 @@ void loop()
}
if(twoController == true) {
// listen to the first software serial port
myserial2.listen();
result2 = node2.readInputRegisters(0x310D, 3);
if (result2 == node2.ku8MBSuccess) {
@ -318,9 +344,12 @@ void loop()
//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) {
@ -337,6 +366,8 @@ void loop()
}
if(twoController == true) {
// listen to the first software serial port
myserial2.listen();
result2 = node2.readInputRegisters(0x311A, 2);
if (result2 == node2.ku8MBSuccess) {
@ -352,9 +383,12 @@ void loop()
//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) {
@ -374,6 +408,8 @@ void loop()
}
if(twoController == true) {
// listen to the first software serial port
myserial2.listen();
result2 = node2.readInputRegisters(0x331B, 2);
if (result2 == node2.ku8MBSuccess) {
@ -392,4 +428,5 @@ void loop()
//Serial.println("Read register 0x331B failed!");
}
}
}