Skip to content

Commit eff28d0

Browse files
committed
Optimize memory with F()
1 parent 8240bef commit eff28d0

10 files changed

Lines changed: 112 additions & 86 deletions

File tree

src/config/Configuration.cpp

Lines changed: 68 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ void handleblinkled() {
140140

141141
//callback notifying us of the need to save WifiManager config
142142
void saveConfigCallback() {
143-
DEBUG_SERIAL.println("Should save config");
143+
DEBUG_SERIAL.println(F("Should save config"));
144144
shouldSaveConfig = true;
145145
}
146146

@@ -279,12 +279,12 @@ void WifiManagerSetup() {
279279
wifiManager.addParameter(&param_tibber_password_show_password);
280280

281281
if (!wifiManager.autoConnect("Energy2Shelly")) {
282-
DEBUG_SERIAL.println("failed to connect and hit timeout");
282+
DEBUG_SERIAL.println(F("failed to connect and hit timeout"));
283283
delay(3000);
284284
ESP.restart();
285285
delay(5000);
286286
}
287-
DEBUG_SERIAL.println("connected");
287+
DEBUG_SERIAL.println(F("connected"));
288288

289289
//read updated parameters
290290
strcpy(reset_password, param_reset_password.getValue());
@@ -319,55 +319,80 @@ void WifiManagerSetup() {
319319

320320
offsetPerPhase = String(power_offset).toDouble() / 3.0; // distribute offset equally across phases
321321

322-
DEBUG_SERIAL.println("The values in the preferences are: ");
323-
DEBUG_SERIAL.println("\treset_password: ********");
324-
DEBUG_SERIAL.println("\tinput_type : " + String(input_type));
325-
DEBUG_SERIAL.println("\tmqtt_server : " + String(mqtt_server));
326-
DEBUG_SERIAL.println("\tmqtt_port : " + String(mqtt_port));
327-
DEBUG_SERIAL.println("\tntp_server: " + String(ntp_server));
328-
DEBUG_SERIAL.println("\ttimezone: " + String(timezone));
329-
DEBUG_SERIAL.println("\tphase_number : " + String(phase_number));
330-
DEBUG_SERIAL.println("\tpower_offset : " + String(power_offset));
331-
DEBUG_SERIAL.println("\tquery_period : " + String(query_period));
332-
DEBUG_SERIAL.println("\tled_gpio : " + String(led_gpio));
333-
DEBUG_SERIAL.println("\tled_gpio_i : " + String(led_gpio_i));
334-
DEBUG_SERIAL.println("\tshelly_mac : " + String(shelly_mac));
335-
DEBUG_SERIAL.println("\tmqtt_topic : " + String(mqtt_topic));
336-
DEBUG_SERIAL.println("\tmqtt_user : " + String(mqtt_user));
337-
DEBUG_SERIAL.println("\tmqtt_passwd : ********");
338-
DEBUG_SERIAL.println("\tmodbus_dev : " + String(modbus_dev));
339-
DEBUG_SERIAL.println("\tpower_path : " + String(power_path));
340-
DEBUG_SERIAL.println("\tpwr_export_path : " + String(pwr_export_path));
341-
DEBUG_SERIAL.println("\tpower_l1_path : " + String(power_l1_path));
342-
DEBUG_SERIAL.println("\tpower_l2_path : " + String(power_l2_path));
343-
DEBUG_SERIAL.println("\tpower_l3_path : " + String(power_l3_path));
344-
DEBUG_SERIAL.println("\tenergy_in_path : " + String(energy_in_path));
345-
DEBUG_SERIAL.println("\tenergy_out_path : " + String(energy_out_path));
346-
DEBUG_SERIAL.println("\tshelly_port : " + String(shelly_port));
347-
DEBUG_SERIAL.println("\tsma_id : " + String(sma_id));
348-
DEBUG_SERIAL.println("\tTibberPulse options:");
349-
DEBUG_SERIAL.println("\t - tibber_url: " + String(tibber_url));
350-
DEBUG_SERIAL.println("\t - tibber_user: " + String(tibber_user));
351-
DEBUG_SERIAL.println("\t - tibber_password: ********");
322+
DEBUG_SERIAL.println(F("The values in the preferences are: "));
323+
DEBUG_SERIAL.println(F("\treset_password: ********"));
324+
DEBUG_SERIAL.print(F("\tinput_type : "));
325+
DEBUG_SERIAL.println(String(input_type));
326+
DEBUG_SERIAL.print(F("\tmqtt_server : "));
327+
DEBUG_SERIAL.println(String(mqtt_server));
328+
DEBUG_SERIAL.print(F("\tmqtt_port : "));
329+
DEBUG_SERIAL.println(String(mqtt_port));
330+
DEBUG_SERIAL.print(F("\tntp_server: "));
331+
DEBUG_SERIAL.println(String(ntp_server));
332+
DEBUG_SERIAL.print(F("\ttimezone: "));
333+
DEBUG_SERIAL.println(String(timezone));
334+
DEBUG_SERIAL.print(F("\tphase_number : "));
335+
DEBUG_SERIAL.println(String(phase_number));
336+
DEBUG_SERIAL.print(F("\tpower_offset : "));
337+
DEBUG_SERIAL.println(String(power_offset));
338+
DEBUG_SERIAL.print(F("\tquery_period : "));
339+
DEBUG_SERIAL.println(String(query_period));
340+
DEBUG_SERIAL.print(F("\tled_gpio : "));
341+
DEBUG_SERIAL.println(String(led_gpio));
342+
DEBUG_SERIAL.print(F("\tled_gpio_i : "));
343+
DEBUG_SERIAL.println(String(led_gpio_i));
344+
DEBUG_SERIAL.print(F("\tshelly_mac : "));
345+
DEBUG_SERIAL.println(String(shelly_mac));
346+
DEBUG_SERIAL.print(F("\tmqtt_topic : "));
347+
DEBUG_SERIAL.println(String(mqtt_topic));
348+
DEBUG_SERIAL.print(F("\tmqtt_user : "));
349+
DEBUG_SERIAL.println(String(mqtt_user));
350+
DEBUG_SERIAL.println(F("\tmqtt_passwd : ********"));
351+
DEBUG_SERIAL.print(F("\tmodbus_dev : "));
352+
DEBUG_SERIAL.println(String(modbus_dev));
353+
DEBUG_SERIAL.print(F("\tpower_path : "));
354+
DEBUG_SERIAL.println(String(power_path));
355+
DEBUG_SERIAL.print(F("\tpwr_export_path : "));
356+
DEBUG_SERIAL.println(String(pwr_export_path));
357+
DEBUG_SERIAL.print(F("\tpower_l1_path : "));
358+
DEBUG_SERIAL.println(String(power_l1_path));
359+
DEBUG_SERIAL.print(F("\tpower_l2_path : "));
360+
DEBUG_SERIAL.println(String(power_l2_path));
361+
DEBUG_SERIAL.print(F("\tpower_l3_path : "));
362+
DEBUG_SERIAL.println(String(power_l3_path));
363+
DEBUG_SERIAL.print(F("\tenergy_in_path : "));
364+
DEBUG_SERIAL.println(String(energy_in_path));
365+
DEBUG_SERIAL.print(F("\tenergy_out_path : "));
366+
DEBUG_SERIAL.println(String(energy_out_path));
367+
DEBUG_SERIAL.print(F("\tshelly_port : "));
368+
DEBUG_SERIAL.println(String(shelly_port));
369+
DEBUG_SERIAL.print(F("\tsma_id : "));
370+
DEBUG_SERIAL.println(String(sma_id));
371+
DEBUG_SERIAL.println(F("\tTibberPulse options:"));
372+
DEBUG_SERIAL.print(F("\t - tibber_url: "));
373+
DEBUG_SERIAL.println(String(tibber_url));
374+
DEBUG_SERIAL.print(F("\t - tibber_user: "));
375+
DEBUG_SERIAL.println(String(tibber_user));
376+
DEBUG_SERIAL.print(F("\t - tibber_password: ********"));
352377

353378
if (strcmp(input_type, "SMA") == 0) {
354379
dataSMA = true;
355-
DEBUG_SERIAL.println("Enabling SMA Multicast data input");
380+
DEBUG_SERIAL.println(F("Enabling SMA Multicast data input"));
356381
} else if (strcmp(input_type, "SHRDZM") == 0) {
357382
dataSHRDZM = true;
358-
DEBUG_SERIAL.println("Enabling SHRDZM UDP data input");
383+
DEBUG_SERIAL.println(F("Enabling SHRDZM UDP data input"));
359384
} else if (strcmp(input_type, "HTTP") == 0) {
360385
dataHTTP = true;
361-
DEBUG_SERIAL.println("Enabling generic HTTP data input");
386+
DEBUG_SERIAL.println(F("Enabling generic HTTP data input"));
362387
} else if (strcmp(input_type, "SUNSPEC") == 0) {
363388
dataSUNSPEC = true;
364-
DEBUG_SERIAL.println("Enabling SUNSPEC data input");
389+
DEBUG_SERIAL.println(F("Enabling SUNSPEC data input"));
365390
} else if (strcmp(input_type, "TIBBERPULSE") == 0) {
366391
dataTIBBERPULSE = true;
367-
DEBUG_SERIAL.println("Enabling TIBBERPULSE data input");
392+
DEBUG_SERIAL.println(F("Enabling TIBBERPULSE data input"));
368393
} else {
369394
dataMQTT = true;
370-
DEBUG_SERIAL.println("Enabling MQTT data input");
395+
DEBUG_SERIAL.println(F("Enabling MQTT data input"));
371396
}
372397

373398
if (strcmp(led_gpio_i, "true") == 0) {
@@ -377,7 +402,7 @@ void WifiManagerSetup() {
377402
}
378403

379404
if (shouldSaveConfig) {
380-
DEBUG_SERIAL.println("saving config");
405+
DEBUG_SERIAL.println(F("saving config"));
381406
preferences.putString("reset_password", reset_password);
382407
preferences.putString("input_type", input_type);
383408
preferences.putString("mqtt_server", mqtt_server);
@@ -408,7 +433,7 @@ void WifiManagerSetup() {
408433
preferences.putString("tibber_password", tibber_password);
409434
wifiManager.reboot();
410435
}
411-
DEBUG_SERIAL.println("local ip");
436+
DEBUG_SERIAL.println(F("local ip"));
412437
DEBUG_SERIAL.println(WiFi.localIP());
413438
}
414439

@@ -417,7 +442,7 @@ void setupMdns() {
417442
strncat(shelly_name, shelly_mac, sizeof(shelly_name) - strlen(shelly_name) - 1);
418443

419444
if (!MDNS.begin(shelly_name)) {
420-
DEBUG_SERIAL.println("Error setting up MDNS responder!");
445+
DEBUG_SERIAL.println(F("Error setting up MDNS responder!"));
421446
}
422447

423448
#ifdef ESP32
@@ -451,5 +476,5 @@ void setupMdns() {
451476
MDNS.addServiceTxt(hMDNSService2, "id", shelly_name);
452477
}
453478
#endif
454-
DEBUG_SERIAL.println("mDNS responder started");
479+
DEBUG_SERIAL.println(F("mDNS responder started"));
455480
}

src/data/DataProcessing.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void setPowerData(double totalPower) {
5656
PhasePower[i].powerFactor = round2(defaultPowerFactor);
5757
PhasePower[i].frequency = defaultFrequency;
5858
}
59-
DEBUG_SERIAL.print("Current total power (with offset): ");
59+
DEBUG_SERIAL.print(F("Current total power (with offset): "));
6060
DEBUG_SERIAL.println(adjustedPower);
6161
}
6262

@@ -81,11 +81,11 @@ void setPowerData(double phase1Power, double phase2Power, double phase3Power) {
8181
PhasePower[i].powerFactor = round2(defaultPowerFactor);
8282
PhasePower[i].frequency = defaultFrequency;
8383
}
84-
DEBUG_SERIAL.print("Current power (with offset) L1: ");
84+
DEBUG_SERIAL.print(F("Current power (with offset) L1: "));
8585
DEBUG_SERIAL.print(phase1Power + offsetPerPhase);
86-
DEBUG_SERIAL.print(" - L2: ");
86+
DEBUG_SERIAL.print(F(" - L2: "));
8787
DEBUG_SERIAL.print(phase2Power + offsetPerPhase);
88-
DEBUG_SERIAL.print(" - L3: ");
88+
DEBUG_SERIAL.print(F(" - L3: "));
8989
DEBUG_SERIAL.println(phase3Power + offsetPerPhase);
9090
}
9191

@@ -105,9 +105,9 @@ void setEnergyData(double totalEnergyGridSupply, double totalEnergyGridFeedIn) {
105105
}
106106
break;
107107
}
108-
DEBUG_SERIAL.print("Total Consumption (Grid Supply): ");
108+
DEBUG_SERIAL.print(F("Total Consumption (Grid Supply): "));
109109
DEBUG_SERIAL.print(totalEnergyGridSupply);
110-
DEBUG_SERIAL.print(" - Total Production (Grid Feed-In): ");
110+
DEBUG_SERIAL.print(F(" - Total Production (Grid Feed-In): "));
111111
DEBUG_SERIAL.println(totalEnergyGridFeedIn);
112112
}
113113

@@ -116,7 +116,7 @@ void setJsonPathPower(JsonDocument json) {
116116
double offsetPerPhase = String(power_offset).toDouble() / 3.0; // distribute offset equally across phases
117117

118118
if (json["a_current"].is<JsonVariant>() || json["a_act_power"].is<JsonVariant>()) {
119-
DEBUG_SERIAL.println("Parsing direct Shelly 3EM payload");
119+
DEBUG_SERIAL.println(F("Parsing direct Shelly 3EM payload"));
120120
PhasePower[0].current = round2((double)json["a_current"].as<double>());
121121
PhasePower[0].voltage = round2((double)json["a_voltage"].as<double>());
122122
PhasePower[0].power = round2((double)json["a_act_power"].as<double>() + offsetPerPhase);
@@ -142,13 +142,13 @@ void setJsonPathPower(JsonDocument json) {
142142
if (json["total_act_power"].is<JsonVariant>()) {
143143
double total = json["total_act_power"].as<double>() + offsetPerPhase * 3;
144144
// distribute if individual phases missing or for logging
145-
DEBUG_SERIAL.print("Total power from payload: ");
145+
DEBUG_SERIAL.print(F("Total power from payload: "));
146146
DEBUG_SERIAL.println(total);
147147
}
148148
return;
149149
}
150150
if (strcmp(power_path, "TRIPHASE") == 0) {
151-
DEBUG_SERIAL.println("resolving triphase");
151+
DEBUG_SERIAL.println(F("resolving triphase"));
152152
double power1 = resolveJsonPath(json, power_l1_path);
153153
double power2 = resolveJsonPath(json, power_l2_path);
154154
double power3 = resolveJsonPath(json, power_l3_path);
@@ -157,15 +157,15 @@ void setJsonPathPower(JsonDocument json) {
157157
} else {
158158
// Check if BOTH paths (Import = power_path, Export = pwr_export_path) are defined
159159
if ((strcmp(power_path, "") != 0) && (strcmp(pwr_export_path, "") != 0)) {
160-
DEBUG_SERIAL.println("Resolving net power (import - export)");
160+
DEBUG_SERIAL.println(F("Resolving net power (import - export)"));
161161
double importPower = resolveJsonPath(json, power_path).as<double>();
162162
double exportPower = resolveJsonPath(json, pwr_export_path).as<double>();
163163
double netPower = importPower - exportPower;
164164
setPowerData(netPower);
165165
}
166166
// (FALLBACK): Only the normal power_path (import path) is defined (old logic)
167167
else if (strcmp(power_path, "") != 0) {
168-
DEBUG_SERIAL.println("Resolving monophase (single path only)");
168+
DEBUG_SERIAL.println(F("Resolving monophase (single path only)"));
169169
double power = resolveJsonPath(json, power_path).as<double>();
170170
setPowerData(power);
171171
}
@@ -182,7 +182,7 @@ void parseShellyString(const char *jsonStr) {
182182
JsonDocument doc;
183183
DeserializationError err = deserializeJson(doc, jsonStr);
184184
if (err) {
185-
DEBUG_SERIAL.print("deserializeJson failed: ");
185+
DEBUG_SERIAL.print(F("deserializeJson failed: "));
186186
DEBUG_SERIAL.println(err.c_str());
187187
return;
188188
}

src/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ void setup(void) {
3232
#endif
3333
while (!getLocalTime(&timeinfo))
3434
{
35-
DEBUG_SERIAL.println("Waiting for NTP time...");
35+
DEBUG_SERIAL.println(F("Waiting for NTP time..."));
3636
delay(500);
3737
}
38-
DEBUG_SERIAL.print("Current time: ");
38+
DEBUG_SERIAL.print(F("Current time: "));
3939
char time_buffer[20];
4040
strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", &timeinfo);
4141
DEBUG_SERIAL.println(time_buffer);
@@ -208,7 +208,7 @@ void setup(void) {
208208
modbus1.client();
209209
modbus_ip.fromString(mqtt_server);
210210
if (!modbus1.isConnected(modbus_ip)) { // reuse mqtt server adresss for modbus adress
211-
Serial.println("Trying to connect SUNSPEC powermeter data");
211+
Serial.println(F("Trying to connect SUNSPEC powermeter data"));
212212
modbus1.connect(modbus_ip, String(mqtt_port).toInt());
213213
}
214214
}

src/parsers/HttpParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
// Query a generic HTTP endpoint and parse JSON response
55
void queryHTTP() {
66
JsonDocument json;
7-
DEBUG_SERIAL.println("Querying HTTP source");
7+
DEBUG_SERIAL.println(F("Querying HTTP source"));
88
http.begin(wifi_client, mqtt_server);
99
http.GET();
1010
deserializeJson(json, http.getStream());
1111
if (strcmp(power_path, "") == 0) {
12-
DEBUG_SERIAL.println("HTTP query: no JSONPath for power data provided");
12+
DEBUG_SERIAL.println(F("HTTP query: no JSONPath for power data provided"));
1313
} else {
1414
setJsonPathPower(json);
1515
}

src/parsers/MqttParser.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ void mqtt_callback(char *topic, byte *payload, unsigned int length) {
1717
float rawValue = payloadStr.toFloat();
1818
setPowerData(rawValue);
1919
} else {
20-
DEBUG_SERIAL.print("Error parsing MQTT payload");
20+
DEBUG_SERIAL.print(F("Error parsing MQTT payload"));
2121
}
2222
} else {
2323
// Successfully parsed as JSON
@@ -26,14 +26,14 @@ void mqtt_callback(char *topic, byte *payload, unsigned int length) {
2626
}
2727

2828
void mqtt_reconnect() {
29-
DEBUG_SERIAL.print("Attempting MQTT connection...");
29+
DEBUG_SERIAL.print(F("Attempting MQTT connection..."));
3030
if (mqtt_client.connect(shelly_name, String(mqtt_user).c_str(), String(mqtt_passwd).c_str())) {
31-
DEBUG_SERIAL.println("connected");
31+
DEBUG_SERIAL.println(F("connected"));
3232
mqtt_client.subscribe(mqtt_topic);
3333
} else {
34-
DEBUG_SERIAL.print("failed, rc=");
34+
DEBUG_SERIAL.print(F("failed, rc="));
3535
DEBUG_SERIAL.print(mqtt_client.state());
36-
DEBUG_SERIAL.println(" try again in 5 seconds");
36+
DEBUG_SERIAL.println(F(" try again in 5 seconds"));
3737
delay(5000);
3838
}
3939
}

src/parsers/SmaParser.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ void parseSMA() {
88
if (packetSize) {
99
int rSize = Udp.read(buffer, 1024);
1010
if (buffer[0] != 'S' || buffer[1] != 'M' || buffer[2] != 'A') {
11-
DEBUG_SERIAL.println("Not an SMA packet?");
11+
DEBUG_SERIAL.println(F("Not an SMA packet?"));
1212
return;
1313
}
1414
uint16_t grouplen;
@@ -28,10 +28,10 @@ void parseSMA() {
2828
// uint16_t susyID = (offset[0] << 8) + offset[1];
2929
offset += 2;
3030
uint32_t serial = (offset[0] << 24) + (offset[1] << 16) + (offset[2] << 8) + offset[3];
31-
DEBUG_SERIAL.print("Received SMA multicast from ");
31+
DEBUG_SERIAL.print(F("Received SMA multicast from "));
3232
DEBUG_SERIAL.println(serial);
3333
if (sma_id[0] != '\0' && strtoul(sma_id, nullptr, 10) != serial) {
34-
DEBUG_SERIAL.println("SMA serial not matching - ignoring packet");
34+
DEBUG_SERIAL.println(F("SMA serial not matching - ignoring packet"));
3535
break;
3636
}
3737
offset += 4;
@@ -150,16 +150,16 @@ void parseSMA() {
150150
offset += 4;
151151
} else {
152152
offset += type;
153-
DEBUG_SERIAL.println("Unknown measurement");
153+
DEBUG_SERIAL.println(F("Unknown measurement"));
154154
}
155155
}
156156
} else if (grouptag == 0) {
157157
// end marker
158158
offset += grouplen;
159159
} else {
160-
DEBUG_SERIAL.print("unhandled group ");
160+
DEBUG_SERIAL.print(F("unhandled group "));
161161
DEBUG_SERIAL.print(grouptag);
162-
DEBUG_SERIAL.print(" with len=");
162+
DEBUG_SERIAL.print(F(" with len="));
163163
DEBUG_SERIAL.println(grouplen);
164164
offset += grouplen;
165165
}

src/parsers/SunspecParser.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void parseSUNSPEC() {
4646
delay(10);
4747
t++;
4848
if (t > 50) {
49-
DEBUG_SERIAL.println("Timeout SUNSPEC");
49+
DEBUG_SERIAL.println(F("Timeout SUNSPEC"));
5050
//prolong=10;
5151
modbus1.disconnect(modbus_ip);
5252
break;
@@ -92,6 +92,7 @@ void parseSUNSPEC() {
9292
PhaseEnergy[n].gridfeedin = -p/1000.0*scale_real_energy;
9393
}
9494
}
95-
DEBUG_SERIAL.printf("SUNSPEC power: %d,%d\n\r", t, power);
95+
DEBUG_SERIAL.print(F("SUNSPEC power: "));
96+
DEBUG_SERIAL.printf("%d,%d\n\r", t, power);
9697
}
9798
}

src/parsers/TibberPulseParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ byte smlpayload[SMLPAYLOADMAXSIZE]{0};
3939
bool parseTibberPulse() {
4040
bool ret = true;
4141
int getlength = 0;
42-
DEBUG_SERIAL.print("Querying TibberPulse raw SML: ");
42+
DEBUG_SERIAL.print(F("Querying TibberPulse raw SML: "));
4343
String url = "http://";
4444
url += String(tibber_url);
4545
url += String(tibber_rpc);

0 commit comments

Comments
 (0)