/*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 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 Uno GND => GND VCC => 5V DI => 11 DE => 3 RE => 2 RO => 10 MAX4852=>Arduino Uno GND => GND VCC => 5V DI => 9 DE => 5 RE => 4 RO => 6 TRACER => MAX485 blue => B green => A */ //#include // empty library for potential future use #include #include #include "ArduinoJson.h" bool twoController = true; SoftwareSerial myserial(10, 11); // RX, TX SoftwareSerial myserial2(6, 9); // 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; // 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() { 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 myserial.begin(115200); if(twoController == true) { 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); } } //was true bool state = true; void loop() { 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"] = 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); Serial.println(); delay(1000); } // AddressRegistry from https://github.com/Bettapro/Solar-Tracer-Blynk-V3 void AddressRegistry_3100() { 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) { 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() { result = node.readInputRegisters(0x3106, 2); if (result == node.ku8MBSuccess) { battChargePower = (node.getResponseBuffer(0x00) | node.getResponseBuffer(0x01) << 16) / 100.0f; } if(twoController == true) { result2 = node2.readInputRegisters(0x3106, 2); if (result2 == node2.ku8MBSuccess) { battChargePower2 = (node2.getResponseBuffer(0x00) | node2.getResponseBuffer(0x01) << 16) / 100.0f; } } } 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!"); } 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() { 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) { 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() { 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) { 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!"); } } }