Skip to content

Commit 85e8ab9

Browse files
Merge pull request #46 from TheRealMoeder/sma_serial
Add SMA serial number matching
2 parents f57b6c9 + 358473c commit 85e8ab9

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/main.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Energy2Shelly_ESP v0.5.1
1+
// Energy2Shelly_ESP v0.5.2
22
#include <Arduino.h>
33
#include <Preferences.h>
44
#ifndef ESP32
@@ -52,6 +52,7 @@ char modbus_dev[10] = "71"; // default for KSEM
5252
char shelly_port[6] = "2220"; // old: 1010; new (FW>=226): 2220
5353
char force_pwr_decimals[6] = "true"; // to fix Marstek bug
5454
bool forcePwrDecimals = true; // to fix Marstek bug
55+
char sma_id[17] = "";
5556

5657
IPAddress modbus_ip;
5758
ModbusIP modbus1;
@@ -474,7 +475,13 @@ void parseSMA() {
474475
offset += 2;
475476
// uint16_t susyID = (offset[0] << 8) + offset[1];
476477
offset += 2;
477-
// uint32_t serial = (offset[0] << 24) + (offset[1] << 16) + (offset[2] << 8) + offset[3];
478+
uint32_t serial = (offset[0] << 24) + (offset[1] << 16) + (offset[2] << 8) + offset[3];
479+
DEBUG_SERIAL.print("Received SMA multicast from ");
480+
DEBUG_SERIAL.println(serial);
481+
if ((strcmp(sma_id, "") != 0) && (String(sma_id).toInt() != serial)) {
482+
DEBUG_SERIAL.println("SMA serial not matching - ignoring packet");
483+
break;
484+
}
478485
offset += 4;
479486
// uint32_t timestamp = (offset[0] << 24) + (offset[1] << 16) + (offset[2] << 8) + offset[3];
480487
offset += 4;
@@ -763,7 +770,8 @@ void WifiManagerSetup() {
763770
strcpy(energy_out_path, preferences.getString("energy_out_path", energy_out_path).c_str());
764771
strcpy(shelly_port, preferences.getString("shelly_port", shelly_port).c_str());
765772
strcpy(force_pwr_decimals, preferences.getString("force_pwr_decimals", force_pwr_decimals).c_str());
766-
773+
strcpy(sma_id, preferences.getString("sma_id", sma_id).c_str());
774+
767775
WiFiManagerParameter custom_section1("<h3>General settings</h3>");
768776
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);
769777
WiFiManagerParameter custom_mqtt_server("server", "<b>Server</b><br>MQTT Server IP, query url for generic HTTP or Modbus TCP server IP for SUNSPEC", mqtt_server, 80);
@@ -774,6 +782,7 @@ void WifiManagerSetup() {
774782
WiFiManagerParameter custom_shelly_mac("mac", "<b>Shelly ID</b><br>12 char hexadecimal, defaults to MAC address of ESP", shelly_mac, 13);
775783
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);
776784
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);
785+
WiFiManagerParameter custom_sma_id("sma_id", "<b>SMA serial number</b><br>optional serial number if you have more than one SMA EM/HM in your network", sma_id, 16);
777786
WiFiManagerParameter custom_section2("<hr><h3>MQTT options</h3>");
778787
WiFiManagerParameter custom_mqtt_topic("topic", "<b>MQTT Topic</b>", mqtt_topic, 60);
779788
WiFiManagerParameter custom_mqtt_user("user", "<b>MQTT user</b><br>optional", mqtt_user, 40);
@@ -806,6 +815,7 @@ void WifiManagerSetup() {
806815
wifiManager.addParameter(&custom_shelly_mac);
807816
wifiManager.addParameter(&custom_shelly_port);
808817
wifiManager.addParameter(&custom_force_pwr_decimals);
818+
wifiManager.addParameter(&custom_sma_id);
809819
wifiManager.addParameter(&custom_section2);
810820
wifiManager.addParameter(&custom_mqtt_port);
811821
wifiManager.addParameter(&custom_mqtt_topic);
@@ -852,6 +862,7 @@ void WifiManagerSetup() {
852862
strcpy(energy_out_path, custom_energy_out_path.getValue());
853863
strcpy(shelly_port, custom_shelly_port.getValue());
854864
strcpy(force_pwr_decimals, custom_force_pwr_decimals.getValue());
865+
strcpy(sma_id, custom_sma_id.getValue());
855866

856867
DEBUG_SERIAL.println("The values in the preferences are: ");
857868
DEBUG_SERIAL.println("\tinput_type : " + String(input_type));
@@ -874,6 +885,7 @@ void WifiManagerSetup() {
874885
DEBUG_SERIAL.println("\tenergy_out_path : " + String(energy_out_path));
875886
DEBUG_SERIAL.println("\tshelly_port : " + String(shelly_port));
876887
DEBUG_SERIAL.println("\tforce_pwr_decimals : " + String(force_pwr_decimals));
888+
DEBUG_SERIAL.println("\tsma_id : " + String(sma_id));
877889

878890
if (strcmp(input_type, "SMA") == 0) {
879891
dataSMA = true;
@@ -927,6 +939,7 @@ void WifiManagerSetup() {
927939
preferences.putString("energy_out_path", energy_out_path);
928940
preferences.putString("shelly_port", shelly_port);
929941
preferences.putString("force_pwr_decimals", force_pwr_decimals);
942+
preferences.putString("sma_id", sma_id);
930943
wifiManager.reboot();
931944
}
932945
DEBUG_SERIAL.println("local ip");

0 commit comments

Comments
 (0)