EPEVER Solar Controller RS485 MODBUS test

From Public Wiki
Jump to navigation Jump to search

Synopsis

Notes

This was developed to test communication to the EPEVER AN Solar controller from the Arduino


Code

  1 //Power is not currently working for battery or panel
  2 
  3 #include <ModbusMaster.h>
  4 
  5 
  6 #define MAX485_DE   49
  7 #define MAX485_RE   48
  8 
  9 #define PANEL_VOLTS     0x00
 10 #define PANEL_AMPS      0x01
 11 #define PANEL_POWER_L   0x02
 12 #define PANEL_POWER_H   0x03
 13 #define BATT_VOLTS      0x04
 14 #define BATT_AMPS       0x05
 15 #define BATT_POWER_L    0x06
 16 #define BATT_POWER_H    0x07
 17 #define OVERVOLT_DISC   0x00
 18 #define CHARGING_LIMIT  0x01
 19 #define OVERVOLT_RECON  0x02
 20 #define EQUILIBRIUM_CV  0x03
 21 #define BOOST_CV        0x04
 22 #define FLOAT_CV        0x05
 23 #define BOOST_RECON     0x06
 24 
 25 
 26 // instantiate ModbusMaster object
 27 ModbusMaster node;
 28 
 29 //#include <SoftwareSerial.h>
 30 
 31 //SoftwareSerial mySerial(10, 11); // RX, TX
 32 
 33 void setup()
 34 {
 35   Serial.begin(115200);
 36   while (!Serial) {
 37     ;
 38   }
 39   Serial.println("Moooooo!");
 40 
 41   pinMode(MAX485_RE, OUTPUT);
 42   pinMode(MAX485_DE, OUTPUT);
 43   // Init in receive mode
 44   digitalWrite(MAX485_RE, 0);
 45   digitalWrite(MAX485_DE, 0);
 46 
 47   // Modbus at 115200 baud
 48   Serial1.begin(115200);
 49 
 50   // EPEver Device ID 1
 51   node.begin(1, Serial1);
 52 
 53   // Callbacks
 54   node.preTransmission(preTransmission);
 55   node.postTransmission(postTransmission);
 56 }
 57 
 58 void loop()
 59 {
 60   uint8_t result;
 61 
 62   // Read 8 registers starting at 0x3100)
 63   node.clearResponseBuffer();
 64   result = node.readInputRegisters(0x3100, 8);
 65 
 66   if (result == node.ku8MBSuccess)
 67   {
 68     Serial.print("VPanel: ");
 69     Serial.println(node.getResponseBuffer(PANEL_VOLTS)/100.0f);
 70     Serial.print("IPanel: ");
 71     Serial.println(node.getResponseBuffer(PANEL_AMPS)/100.0f);
 72     Serial.print("PPanel: ");
 73     Serial.println((node.getResponseBuffer(PANEL_POWER_L) +
 74                     node.getResponseBuffer(PANEL_POWER_H) << 16)/100.0f);
 75    
 76     Serial.print("VBatt: ");
 77     Serial.println(node.getResponseBuffer(BATT_VOLTS)/100.0f);
 78     Serial.print("Ibatt: ");
 79     Serial.println(node.getResponseBuffer(BATT_AMPS)/100.0f);
 80     Serial.print("PBatt: ");
 81     Serial.println((node.getResponseBuffer(BATT_POWER_L) +
 82                     node.getResponseBuffer(BATT_POWER_H) << 16)/100.0f);                   
 83     Serial.println();
 84     Serial.println();
 85   } else {
 86     Serial.print("Miss read, ret val:");
 87     Serial.println(result);
 88   }
 89   //delay(1000);
 90 
 91   // Read 6 registers starting at 0x9003)
 92   node.clearResponseBuffer();
 93   result = node.readHoldingRegisters(0x9003, 7);
 94 
 95   if (result == node.ku8MBSuccess)
 96   {
 97     Serial.print("Overvolt Disconnect Voltage: ");
 98     Serial.println(node.getResponseBuffer(OVERVOLT_DISC)/100.0f);
 99     Serial.print("Charging Limit Voltage: ");
100     Serial.println(node.getResponseBuffer(CHARGING_LIMIT)/100.0f);
101     Serial.print("Overvolt Disconnect Voltage: ");
102     Serial.println(node.getResponseBuffer(OVERVOLT_DISC)/100.0f);
103     Serial.print("Overvolt Reconnect Voltage: ");
104     Serial.println(node.getResponseBuffer(OVERVOLT_RECON)/100.0f);
105     Serial.print("Equilibrium Charge Voltage: ");
106     Serial.println(node.getResponseBuffer(EQUILIBRIUM_CV)/100.0f);
107     Serial.print("Boost Charging Voltage:  ");
108     Serial.println(node.getResponseBuffer(BOOST_CV)/100.0f);
109     Serial.print("Float Charging Voltage: ");
110     Serial.println(node.getResponseBuffer(FLOAT_CV)/100.0f);
111     Serial.print("Boost Reconnect Voltage: ");
112     Serial.println(node.getResponseBuffer(BOOST_RECON)/100.0f);
113                   
114     Serial.println();
115     Serial.println();
116   } else {
117     Serial.print("Miss read, ret val:");
118     Serial.println(result);
119   }
120 
121   delay(2000);
122 }
123 
124 void preTransmission()
125 {
126   digitalWrite(MAX485_RE, 1);
127   digitalWrite(MAX485_DE, 1);
128 }
129 
130 void postTransmission()
131 {
132   digitalWrite(MAX485_RE, 0);
133   digitalWrite(MAX485_DE, 0);
134 }