Skip to content

Commit dd1d7e6

Browse files
Merge pull request #43 from MtheK/main
Fix Marstek compatibility by updating Shelly port and force decimal places
2 parents cfdf09d + 8c6757a commit dd1d7e6

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/main.cpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ char shelly_mac[13];
4949
char shelly_name[26] = "shellypro3em-";
5050
char query_period[10] = "1000";
5151
char modbus_dev[10] = "71"; // default for KSEM
52+
char shelly_port[6] = "2220"; // old: 1010; new (FW>=226): 2220
53+
char force_pwr_decimals[6] = "true"; // to fix Marstek bug
54+
bool forcePwrDecimals = true; // to fix Marstek bug
5255

5356
IPAddress modbus_ip;
5457
ModbusIP modbus1;
@@ -124,7 +127,12 @@ WiFiUDP UdpRPC;
124127
#endif
125128

126129
double round2(double value) {
127-
return (int)(value * 100 + 0.5) / 100.0;
130+
int ivalue = (int)(value * 100.0 + (value > 0.0 ? 0.5 : -0.5));
131+
132+
// fix Marstek bug: make sure to have decimal numbers
133+
if(forcePwrDecimals && (ivalue % 100 == 0)) ivalue++;
134+
135+
return ivalue / 100.0;
128136
}
129137

130138
JsonVariant resolveJsonPath(JsonVariant variant, const char *path) {
@@ -149,7 +157,7 @@ void setPowerData(double totalPower) {
149157
PhasePower[i].power = round2(totalPower * 0.3333);
150158
PhasePower[i].voltage = defaultVoltage;
151159
PhasePower[i].current = round2(PhasePower[i].power / PhasePower[i].voltage);
152-
PhasePower[i].apparentPower = PhasePower[i].power;
160+
PhasePower[i].apparentPower = round2(PhasePower[i].power);
153161
PhasePower[i].powerFactor = defaultPowerFactor;
154162
PhasePower[i].frequency = defaultFrequency;
155163
}
@@ -164,7 +172,7 @@ void setPowerData(double phase1Power, double phase2Power, double phase3Power) {
164172
for (int i = 0; i <= 2; i++) {
165173
PhasePower[i].voltage = defaultVoltage;
166174
PhasePower[i].current = round2(PhasePower[i].power / PhasePower[i].voltage);
167-
PhasePower[i].apparentPower = PhasePower[i].power;
175+
PhasePower[i].apparentPower = round2(PhasePower[i].power);
168176
PhasePower[i].powerFactor = defaultPowerFactor;
169177
PhasePower[i].frequency = defaultFrequency;
170178
}
@@ -753,6 +761,8 @@ void WifiManagerSetup() {
753761
strcpy(power_l3_path, preferences.getString("power_l3_path", power_l3_path).c_str());
754762
strcpy(energy_in_path, preferences.getString("energy_in_path", energy_in_path).c_str());
755763
strcpy(energy_out_path, preferences.getString("energy_out_path", energy_out_path).c_str());
764+
strcpy(shelly_port, preferences.getString("shelly_port", shelly_port).c_str());
765+
strcpy(force_pwr_decimals, preferences.getString("force_pwr_decimals", force_pwr_decimals).c_str());
756766

757767
WiFiManagerParameter custom_section1("<h3>General settings</h3>");
758768
WiFiManagerParameter custom_input_type("type", "<b>Data source</b><br><code>MQTT</code> for MQTT<br><code>HTTP</code> for generic HTTP<br><code>SMA</code> for SMA EM/HM multicast<br><code>SHRDZM</code> for SHRDZM UDP data<br><code>SUNSPEC</code> for Modbus TCP SUNSPEC data", input_type, 40);
@@ -762,6 +772,8 @@ void WifiManagerSetup() {
762772
WiFiManagerParameter custom_led_gpio("led_gpio", "<b>GPIO</b><br>of internal LED", led_gpio, 3);
763773
WiFiManagerParameter custom_led_gpio_i("led_gpio_i", "<b>GPIO is inverted</b><br><code>true</code> or <code>false</code>", led_gpio_i, 6);
764774
WiFiManagerParameter custom_shelly_mac("mac", "<b>Shelly ID</b><br>12 char hexadecimal, defaults to MAC address of ESP", shelly_mac, 13);
775+
WiFiManagerParameter custom_shelly_port("shelly_port", "<b>Shelly UDP port</b><br><code>1010</code> for old Marstek FW, <code>2220</code> for new Marstek FW v226+/v108+", shelly_port, 6);
776+
WiFiManagerParameter custom_force_pwr_decimals("force_pwr_decimals", "<b>Force decimals numbers for Power values</b><br><code>true</code> to fix Marstek bug", force_pwr_decimals, 6);
765777
WiFiManagerParameter custom_section2("<hr><h3>MQTT options</h3>");
766778
WiFiManagerParameter custom_mqtt_topic("topic", "<b>MQTT Topic</b>", mqtt_topic, 60);
767779
WiFiManagerParameter custom_mqtt_user("user", "<b>MQTT user</b><br>optional", mqtt_user, 40);
@@ -792,6 +804,8 @@ void WifiManagerSetup() {
792804
wifiManager.addParameter(&custom_led_gpio);
793805
wifiManager.addParameter(&custom_led_gpio_i);
794806
wifiManager.addParameter(&custom_shelly_mac);
807+
wifiManager.addParameter(&custom_shelly_port);
808+
wifiManager.addParameter(&custom_force_pwr_decimals);
795809
wifiManager.addParameter(&custom_section2);
796810
wifiManager.addParameter(&custom_mqtt_port);
797811
wifiManager.addParameter(&custom_mqtt_topic);
@@ -807,7 +821,7 @@ void WifiManagerSetup() {
807821
wifiManager.addParameter(&custom_power_l3_path);
808822
wifiManager.addParameter(&custom_energy_in_path);
809823
wifiManager.addParameter(&custom_energy_out_path);
810-
824+
811825

812826
if (!wifiManager.autoConnect("Energy2Shelly")) {
813827
DEBUG_SERIAL.println("failed to connect and hit timeout");
@@ -836,6 +850,8 @@ void WifiManagerSetup() {
836850
strcpy(power_l3_path, custom_power_l3_path.getValue());
837851
strcpy(energy_in_path, custom_energy_in_path.getValue());
838852
strcpy(energy_out_path, custom_energy_out_path.getValue());
853+
strcpy(shelly_port, custom_shelly_port.getValue());
854+
strcpy(force_pwr_decimals, custom_force_pwr_decimals.getValue());
839855

840856
DEBUG_SERIAL.println("The values in the preferences are: ");
841857
DEBUG_SERIAL.println("\tinput_type : " + String(input_type));
@@ -856,7 +872,8 @@ void WifiManagerSetup() {
856872
DEBUG_SERIAL.println("\tpower_l3_path : " + String(power_l3_path));
857873
DEBUG_SERIAL.println("\tenergy_in_path : " + String(energy_in_path));
858874
DEBUG_SERIAL.println("\tenergy_out_path : " + String(energy_out_path));
859-
875+
DEBUG_SERIAL.println("\tshelly_port : " + String(shelly_port));
876+
DEBUG_SERIAL.println("\tforce_pwr_decimals : " + String(force_pwr_decimals));
860877

861878
if (strcmp(input_type, "SMA") == 0) {
862879
dataSMA = true;
@@ -882,6 +899,12 @@ void WifiManagerSetup() {
882899
led_i = false;
883900
}
884901

902+
if (strcmp(force_pwr_decimals, "true") == 0) {
903+
forcePwrDecimals = true;
904+
} else {
905+
forcePwrDecimals = false;
906+
}
907+
885908
if (shouldSaveConfig) {
886909
DEBUG_SERIAL.println("saving config");
887910
preferences.putString("input_type", input_type);
@@ -902,6 +925,8 @@ void WifiManagerSetup() {
902925
preferences.putString("power_l3_path", power_l3_path);
903926
preferences.putString("energy_in_path", energy_in_path);
904927
preferences.putString("energy_out_path", energy_out_path);
928+
preferences.putString("shelly_port", shelly_port);
929+
preferences.putString("force_pwr_decimals", force_pwr_decimals);
905930
wifiManager.reboot();
906931
}
907932
DEBUG_SERIAL.println("local ip");
@@ -970,7 +995,7 @@ void setup(void) {
970995
server.begin();
971996

972997
// Set up RPC over UDP for Marstek users
973-
UdpRPC.begin(1010);
998+
UdpRPC.begin(String(shelly_port).toInt());
974999

9751000
// Set up MQTT
9761001
if (dataMQTT) {

0 commit comments

Comments
 (0)