Skip to content

Commit e2285de

Browse files
Merge pull request #104 from oldboys92/feature_phase-number
Feature `phase_number` configuration option
2 parents 962379d + 6ef26fd commit e2285de

4 files changed

Lines changed: 59 additions & 12 deletions

File tree

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ SMA Multicast code is based on https://www.mikrocontroller.net/topic/559607
6868

6969
The Shelly ID defaults to the ESP's MAC address, you may change this if you want to substitute an existing uni-meter configuration without reconnecting the battery to a new shelly device.
7070

71+
### How the <code>phase_number</code> option can be used:
72+
- If you have a monophase setup, set <code>phase_number</code> to <code>1</code>. This asigns all power and energy data to phase 1.
73+
- If you have a triphase setup, set <code>phase_number</code> to <code>3</code>. This distributes power and energy data across all three phases.
74+
75+
> [!NOTE]
76+
> This option controls how energy2shelly firmware outputs energy data, it does not change the input data. So if you have a triphase setup but want to assign all data to phase 1, set <code>phase_number</code> to <code>1</code>. The TRIPAHSE option in JSONPATH settings only controls how the firmware reads the input data, not how it outputs this data.
77+
7178
4) Check if your device is visible in the WLAN. <code>http://IP-address</code><br>
7279
5) Check the current power data at <code>http://IP-address/status</code><br>
7380
- [ ] \(Optional) If you want to reset you Wifi-Configuration and/or reconfigure other settings go to <code>http://IP-address/reset</code> and reconnect to the Energy2Shelly hotspot.

src/config/Configuration.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ tm timeinfo;
1414
char input_type[40];
1515
char ntp_server[40] = "de.pool.ntp.org";
1616
char timezone[64] = "CET-1CEST,M3.5.0/2,M10.5.0/3"; // Central European Time
17+
char phase_number[2] = "3"; // number of phases: 1 for monophase or 3 for triphase
1718
char mqtt_server[160];
1819
char mqtt_port[6] = "1883";
1920
char mqtt_topic[90] = "tele/meter/SENSOR";
@@ -146,6 +147,7 @@ void WifiManagerSetup() {
146147
strcpy(mqtt_server, preferences.getString("mqtt_server", mqtt_server).c_str());
147148
strcpy(ntp_server, preferences.getString("ntp_server", ntp_server).c_str());
148149
strcpy(timezone, preferences.getString("timezone", timezone).c_str());
150+
strcpy(phase_number, preferences.getString("phase_number", phase_number).c_str());
149151
strcpy(query_period, preferences.getString("query_period", query_period).c_str());
150152
strcpy(led_gpio, preferences.getString("led_gpio", led_gpio).c_str());
151153
strcpy(led_gpio_i, preferences.getString("led_gpio_i", led_gpio_i).c_str());
@@ -172,6 +174,7 @@ void WifiManagerSetup() {
172174
WiFiManagerParameter custom_mqtt_port("port", "<b>Port</b><br> for MQTT or Modbus TCP (SUNSPEC)", mqtt_port, 6);
173175
WiFiManagerParameter param_ntp_server("ntp_server", "NTP server <span title=\"for time synchronization\" style=\"cursor: help;\" aria-label=\"Help\" tabindex=\"0\">(?)</span>", ntp_server, 40);
174176
WiFiManagerParameter param_timezone("timezone", "Timezone <span title=\"e.g. UTC0, UTC+1, UTC-3, UTC+1CET-1CEST,M3.5.0/02:00:00,M10.5.0/03:00:00\" style=\"cursor: help;\" aria-label=\"Help\" tabindex=\"0\">(?)</span>", timezone, 64);
177+
WiFiManagerParameter param_phase_number("phase_number", "Number of phases <span title=\"Number of phases (e.g. 1 or 3)\" style=\"cursor: help;\" aria-label=\"Help\" tabindex=\"0\">(?)</span>", phase_number, 1);
175178
WiFiManagerParameter custom_query_period("query_period", "<b>Query period</b><br>for generic HTTP and SUNSPEC, in milliseconds", query_period, 10);
176179
WiFiManagerParameter custom_led_gpio("led_gpio", "<b>GPIO</b><br>of internal LED", led_gpio, 3);
177180
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);
@@ -189,8 +192,8 @@ void WifiManagerSetup() {
189192
WiFiManagerParameter custom_power_path("power_path", "<b>Total power JSON path</b><br>e.g. <code>ENERGY.Power</code> or <code>TRIPHASE</code> for tri-phase data", power_path, 60);
190193
WiFiManagerParameter custom_pwr_export_path("pwr_export_path", "<b>Export power JSON path</b><br>Optional, for net calc (e.g. \"i-e\"", pwr_export_path, 60);
191194
WiFiManagerParameter custom_power_l1_path("power_l1_path", "<b>Phase 1 power JSON path</b><br>optional", power_l1_path, 60);
192-
WiFiManagerParameter custom_power_l2_path("power_l2_path", "<b>Phase 2 power JSON path</b><br>Phase 2 power JSON path<br>optional", power_l2_path, 60);
193-
WiFiManagerParameter custom_power_l3_path("power_l3_path", "<b>Phase 3 power JSON path</b><br>Phase 3 power JSON path<br>optional", power_l3_path, 60);
195+
WiFiManagerParameter custom_power_l2_path("power_l2_path", "<b>Phase 2 power JSON path</b><br>optional", power_l2_path, 60);
196+
WiFiManagerParameter custom_power_l3_path("power_l3_path", "<b>Phase 3 power JSON path</b><br>optional", power_l3_path, 60);
194197
WiFiManagerParameter custom_energy_in_path("energy_in_path", "<b>Energy from grid JSON path</b><br>e.g. <code>ENERGY.Grid</code>", energy_in_path, 60);
195198
WiFiManagerParameter custom_energy_out_path("energy_out_path", "<b>Energy to grid JSON path</b><br>e.g. <code>ENERGY.FeedIn</code>", energy_out_path, 60);
196199

@@ -207,6 +210,7 @@ void WifiManagerSetup() {
207210
wifiManager.addParameter(&custom_mqtt_server);
208211
wifiManager.addParameter(&param_ntp_server);
209212
wifiManager.addParameter(&param_timezone);
213+
wifiManager.addParameter(&param_phase_number);
210214
wifiManager.addParameter(&custom_query_period);
211215
wifiManager.addParameter(&custom_led_gpio);
212216
wifiManager.addParameter(&custom_led_gpio_i);
@@ -245,6 +249,7 @@ void WifiManagerSetup() {
245249
strcpy(mqtt_port, custom_mqtt_port.getValue());
246250
strcpy(ntp_server, param_ntp_server.getValue());
247251
strcpy(timezone, param_timezone.getValue());
252+
strcpy(phase_number, param_phase_number.getValue());
248253
strcpy(query_period, custom_query_period.getValue());
249254
strcpy(led_gpio, custom_led_gpio.getValue());
250255
strcpy(led_gpio_i, custom_led_gpio_i.getValue());
@@ -270,6 +275,7 @@ void WifiManagerSetup() {
270275
DEBUG_SERIAL.println("\tmqtt_port : " + String(mqtt_port));
271276
DEBUG_SERIAL.println("\tntp_server: " + String(ntp_server));
272277
DEBUG_SERIAL.println("\ttimezone: " + String(timezone));
278+
DEBUG_SERIAL.println("\tphase_number : " + String(phase_number));
273279
DEBUG_SERIAL.println("\tquery_period : " + String(query_period));
274280
DEBUG_SERIAL.println("\tled_gpio : " + String(led_gpio));
275281
DEBUG_SERIAL.println("\tled_gpio_i : " + String(led_gpio_i));
@@ -288,7 +294,6 @@ void WifiManagerSetup() {
288294
DEBUG_SERIAL.println("\tshelly_port : " + String(shelly_port));
289295
DEBUG_SERIAL.println("\tforce_pwr_decimals : " + String(force_pwr_decimals));
290296
DEBUG_SERIAL.println("\tsma_id : " + String(sma_id));
291-
292297
if (strcmp(input_type, "SMA") == 0) {
293298
dataSMA = true;
294299
DEBUG_SERIAL.println("Enabling SMA Multicast data input");
@@ -326,6 +331,7 @@ void WifiManagerSetup() {
326331
preferences.putString("mqtt_port", mqtt_port);
327332
preferences.putString("ntp_server", ntp_server);
328333
preferences.putString("timezone", timezone);
334+
preferences.putString("phase_number", phase_number);
329335
preferences.putString("query_period", query_period);
330336
preferences.putString("led_gpio", led_gpio);
331337
preferences.putString("led_gpio_i", led_gpio_i);

src/config/Configuration.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ extern tm timeinfo;
5151
extern char input_type[40];
5252
extern char ntp_server[40];
5353
extern char timezone[64];
54+
extern char phase_number[2];
5455
extern char mqtt_server[160];
5556
extern char mqtt_port[6];
5657
extern char mqtt_topic[90];

src/data/DataProcessing.cpp

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,20 @@ JsonVariant resolveJsonPath(JsonVariant variant, const char *path) {
3939
}
4040

4141
void setPowerData(double totalPower) {
42+
switch(phase_number[0]) {
43+
case '1': // monophase
44+
PhasePower[0].power = round2(totalPower);
45+
PhasePower[1].power = 0.0;
46+
PhasePower[2].power = 0.0;
47+
break;
48+
case '3': // triphase
49+
default:
50+
PhasePower[0].power = round2(totalPower * 0.3333);
51+
PhasePower[1].power = round2(totalPower * 0.3333);
52+
PhasePower[2].power = round2(totalPower * 0.3333);
53+
break;
54+
}
4255
for (int i = 0; i <= 2; i++) {
43-
PhasePower[i].power = round2(totalPower * 0.3333);
4456
PhasePower[i].voltage = round2(defaultVoltage);
4557
PhasePower[i].current = round2(PhasePower[i].power / PhasePower[i].voltage);
4658
PhasePower[i].apparentPower = round2(PhasePower[i].power);
@@ -52,9 +64,19 @@ void setPowerData(double totalPower) {
5264
}
5365

5466
void setPowerData(double phase1Power, double phase2Power, double phase3Power) {
55-
PhasePower[0].power = round2(phase1Power);
56-
PhasePower[1].power = round2(phase2Power);
57-
PhasePower[2].power = round2(phase3Power);
67+
switch(phase_number[0]) {
68+
case '1': // monophase
69+
PhasePower[0].power = round2(phase1Power) + round2(phase2Power) + round2(phase3Power);
70+
PhasePower[1].power = 0.0;
71+
PhasePower[2].power = 0.0;
72+
break;
73+
case '3': // triphase
74+
default:
75+
PhasePower[0].power = round2(phase1Power);
76+
PhasePower[1].power = round2(phase2Power);
77+
PhasePower[2].power = round2(phase3Power);
78+
break;
79+
}
5880
for (int i = 0; i <= 2; i++) {
5981
PhasePower[i].voltage = round2(defaultVoltage);
6082
PhasePower[i].current = round2(PhasePower[i].power / PhasePower[i].voltage);
@@ -71,13 +93,24 @@ void setPowerData(double phase1Power, double phase2Power, double phase3Power) {
7193
}
7294

7395
void setEnergyData(double totalEnergyGridSupply, double totalEnergyGridFeedIn) {
74-
for (int i = 0; i <= 2; i++) {
75-
PhaseEnergy[i].consumption = round2(totalEnergyGridSupply * 0.3333);
76-
PhaseEnergy[i].gridfeedin = round2(totalEnergyGridFeedIn * 0.3333);
96+
switch (phase_number[0]) {
97+
case '1': // monophase
98+
for (int i = 0; i <= 2; i++) {
99+
PhaseEnergy[i].consumption = (i == 0) ? round2(totalEnergyGridSupply) : 0.0;
100+
PhaseEnergy[i].gridfeedin = (i == 0) ? round2(totalEnergyGridFeedIn) : 0.0;
101+
}
102+
break;
103+
case '3': // triphase
104+
default:
105+
for (int i = 0; i <= 2; i++){
106+
PhaseEnergy[i].consumption = round2(totalEnergyGridSupply * 0.3333);
107+
PhaseEnergy[i].gridfeedin = round2(totalEnergyGridFeedIn * 0.3333);
108+
}
109+
break;
77110
}
78-
DEBUG_SERIAL.print("Total consumption: ");
111+
DEBUG_SERIAL.print("Total Consumption (Grid Supply): ");
79112
DEBUG_SERIAL.print(totalEnergyGridSupply);
80-
DEBUG_SERIAL.print(" - Total Grid Feed-In: ");
113+
DEBUG_SERIAL.print(" - Total Production (Grid Feed-In): ");
81114
DEBUG_SERIAL.println(totalEnergyGridFeedIn);
82115
}
83116

0 commit comments

Comments
 (0)