first (dirty) protoype of having two solar controllers with two seperate mx485

This commit is contained in:
Marcus 2023-08-13 01:24:15 +02:00
parent 0c2ac86c32
commit d491b335e4

View file

@ -53,6 +53,14 @@
RE => 2 RE => 2
RO => 10 RO => 10
MAX4852=>Arduino Uno
GND => GND
VCC => 5V
DI => 9
DE => 5
RE => 4
RO => 6
TRACER => MAX485 TRACER => MAX485
blue => B blue => B
green => A green => A
@ -63,14 +71,22 @@
#include <SoftwareSerial.h> #include <SoftwareSerial.h>
#include "ArduinoJson.h" #include "ArduinoJson.h"
bool twoController = true;
SoftwareSerial myserial(10, 11); // RX, TX SoftwareSerial myserial(10, 11); // RX, TX
SoftwareSerial myserial2(6, 9); // RX, TX
#define MAX485_DE 3 #define MAX485_DE 3
#define MAX485_RE_NEG 2 #define MAX485_RE_NEG 2
#define MAX485_DE2 5
#define MAX485_RE_NEG2 4
// instantiate ModbusMaster object // instantiate ModbusMaster object
ModbusMaster node; ModbusMaster node;
ModbusMaster node2;
// not needed... // not needed...
//float batVolt; //float batVolt;
@ -79,6 +95,11 @@ float battChargeCurrent, battDischargeCurrent, battChargePower, battOverallCurre
float bvoltage, ctemp, btemp, bremaining, lpower, lcurrent, pvvoltage, pvcurrent, pvpower; float bvoltage, ctemp, btemp, bremaining, lpower, lcurrent, pvvoltage, pvcurrent, pvpower;
float stats_today_pv_volt_min, stats_today_pv_volt_max; 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 rs485DataReceived = true;
bool loadPoweredOn = true; bool loadPoweredOn = true;
@ -86,6 +107,10 @@ bool loadPoweredOn = true;
uint8_t result; uint8_t result;
uint16_t data[6]; uint16_t data[6];
uint8_t result2;
uint16_t data2[6];
void preTransmission() void preTransmission()
{ {
digitalWrite(MAX485_RE_NEG, 1); digitalWrite(MAX485_RE_NEG, 1);
@ -98,6 +123,18 @@ void postTransmission()
digitalWrite(MAX485_DE, 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() void setup()
{ {
pinMode(MAX485_RE_NEG, OUTPUT); pinMode(MAX485_RE_NEG, OUTPUT);
@ -106,21 +143,36 @@ void setup()
digitalWrite(MAX485_RE_NEG, 0); digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 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 // Modbus communication runs at 115200 baud
//Tracer connection //Tracer connection
myserial.begin(115200); myserial.begin(115200);
if(twoController == true) {
myserial2.begin(115200);
}
//USB Serial connection //USB Serial connection
Serial.begin(115200); Serial.begin(115200);
// Modbus slave ID 1 // Modbus slave ID 1
node.begin(1, myserial); node.begin(1, myserial);
if(twoController == true) {
node2.begin(1, myserial2);
}
// Callbacks allow us to configure the RS485 transceiver correctly // Callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(preTransmission); node.preTransmission(preTransmission);
node.postTransmission(postTransmission); node.postTransmission(postTransmission);
if(twoController == true) {
node2.preTransmission(preTransmission2);
node2.postTransmission(postTransmission2);
}
} }
//was true //was true
@ -135,22 +187,40 @@ void loop()
AddressRegistry_331B(); AddressRegistry_331B();
StaticJsonDocument<128> jsonOut; StaticJsonDocument<256> jsonOut;
jsonOut["pvpower"] = pvpower; jsonOut["1"]["pvpower"] = pvpower;
jsonOut["pvcurrent"] = pvcurrent; jsonOut["1"]["pvcurrent"] = pvcurrent;
jsonOut["pvvoltage"] = pvvoltage; jsonOut["1"]["pvvoltage"] = pvvoltage;
jsonOut["lcurrent"] = lcurrent; jsonOut["1"]["lcurrent"] = lcurrent;
jsonOut["pvpower"] = pvpower; jsonOut["1"]["pvpower"] = pvpower;
jsonOut["lpower"] = lpower; jsonOut["1"]["lpower"] = lpower;
jsonOut["pvpower"] = pvpower; jsonOut["1"]["pvpower"] = pvpower;
jsonOut["btemp"] = btemp; jsonOut["1"]["btemp"] = btemp;
jsonOut["bvoltage"] = bvoltage; jsonOut["1"]["bvoltage"] = bvoltage;
jsonOut["bremaining"] = bremaining; jsonOut["1"]["bremaining"] = bremaining;
jsonOut["ctemp"] = ctemp; jsonOut["1"]["ctemp"] = ctemp;
jsonOut["battChargeCurrent"] = battChargeCurrent; jsonOut["1"]["battChargeCurrent"] = battChargeCurrent;
jsonOut["battChargePower"] = battChargePower; jsonOut["1"]["battChargePower"] = battChargePower;
jsonOut["battOverallCurrent"] = battOverallCurrent; jsonOut["1"]["battOverallCurrent"] = battOverallCurrent;
jsonOut["loadPoweredOn"] = loadPoweredOn; 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;
}
serializeJson(jsonOut, Serial); serializeJson(jsonOut, Serial);
Serial.println(); Serial.println();
delay(1000); delay(1000);
@ -160,28 +230,37 @@ void loop()
// AddressRegistry from https://github.com/Bettapro/Solar-Tracer-Blynk-V3 // AddressRegistry from https://github.com/Bettapro/Solar-Tracer-Blynk-V3
void AddressRegistry_3100() { void AddressRegistry_3100() {
result = node.readInputRegisters(0x3100, 6); result = node.readInputRegisters(0x3100, 6);
if (result == node.ku8MBSuccess) { if (result == node.ku8MBSuccess) {
pvvoltage = node.getResponseBuffer(0x00) / 100.0f; pvvoltage = node.getResponseBuffer(0x00) / 100.0f;
//Serial.print("PV Voltage: ");
//Serial.println(pvvoltage);
pvcurrent = node.getResponseBuffer(0x01) / 100.0f; pvcurrent = node.getResponseBuffer(0x01) / 100.0f;
//Serial.print("PV Current: ");
//Serial.println(pvcurrent);
pvpower = (node.getResponseBuffer(0x02) | node.getResponseBuffer(0x03) << 16) / 100.0f; pvpower = (node.getResponseBuffer(0x02) | node.getResponseBuffer(0x03) << 16) / 100.0f;
//Serial.print("PV Power: ");
//Serial.println(pvpower);
bvoltage = node.getResponseBuffer(0x04) / 100.0f; bvoltage = node.getResponseBuffer(0x04) / 100.0f;
//Serial.print("Battery Voltage: ");
//Serial.println(bvoltage);
battChargeCurrent = node.getResponseBuffer(0x05) / 100.0f; battChargeCurrent = node.getResponseBuffer(0x05) / 100.0f;
//Serial.print("Battery Charge Current: ");
//Serial.println(battChargeCurrent); }
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;
}
} }
} }
@ -191,8 +270,14 @@ void loop()
if (result == node.ku8MBSuccess) { if (result == node.ku8MBSuccess) {
battChargePower = (node.getResponseBuffer(0x00) | node.getResponseBuffer(0x01) << 16) / 100.0f; battChargePower = (node.getResponseBuffer(0x00) | node.getResponseBuffer(0x01) << 16) / 100.0f;
//Serial.print("Battery Charge Power: "); }
//Serial.println(battChargePower);
if(twoController == true) {
result2 = node2.readInputRegisters(0x3106, 2);
if (result2 == node2.ku8MBSuccess) {
battChargePower2 = (node2.getResponseBuffer(0x00) | node2.getResponseBuffer(0x01) << 16) / 100.0f;
}
} }
} }
@ -211,7 +296,25 @@ void loop()
} else { } else {
rs485DataReceived = false; rs485DataReceived = false;
//Serial.println("Read register 0x310D failed!"); //Serial.println("Read register 0x310D failed!");
} }
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!");
}
}
} }
void AddressRegistry_311A() { void AddressRegistry_311A() {
@ -229,7 +332,24 @@ void loop()
rs485DataReceived = false; rs485DataReceived = false;
//Serial.println("Read register 0x311A failed!"); //Serial.println("Read register 0x311A failed!");
} }
}
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!");
}
}
}
void AddressRegistry_331B() { void AddressRegistry_331B() {
result = node.readInputRegisters(0x331B, 2); result = node.readInputRegisters(0x331B, 2);
@ -249,4 +369,24 @@ void loop()
rs485DataReceived = false; rs485DataReceived = false;
//Serial.println("Read register 0x331B failed!"); //Serial.println("Read register 0x331B failed!");
} }
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!");
}
}
} }