Reducing stack mem footprint

This commit is contained in:
Gunnar Skjold
2022-02-10 10:35:57 +01:00
parent e2fb5e2673
commit 29524c2123
13 changed files with 59 additions and 66 deletions

View File

@@ -10,8 +10,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState) {
if(energy > 0.0) {
char val[16];
snprintf(val, 16, "%.1f;%.1f", (data->getActiveImportPower()/1.0), energy*1000.0);
char json[192];
snprintf_P(json, sizeof(json), DOMOTICZ_JSON,
snprintf_P(json, BufferSize, DOMOTICZ_JSON,
config.elidx,
val
);
@@ -25,8 +24,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState) {
if (config.vl1idx > 0){
char val[16];
snprintf(val, 16, "%.2f", data->getL1Voltage());
char json[192];
snprintf_P(json, sizeof(json), DOMOTICZ_JSON,
snprintf_P(json, BufferSize, DOMOTICZ_JSON,
config.vl1idx,
val
);
@@ -36,8 +34,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState) {
if (config.vl2idx > 0){
char val[16];
snprintf(val, 16, "%.2f", data->getL2Voltage());
char json[192];
snprintf_P(json, sizeof(json), DOMOTICZ_JSON,
snprintf_P(json, BufferSize, DOMOTICZ_JSON,
config.vl2idx,
val
);
@@ -47,8 +44,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState) {
if (config.vl3idx > 0){
char val[16];
snprintf(val, 16, "%.2f", data->getL3Voltage());
char json[192];
snprintf_P(json, sizeof(json), DOMOTICZ_JSON,
snprintf_P(json, BufferSize, DOMOTICZ_JSON,
config.vl3idx,
val
);
@@ -58,8 +54,7 @@ bool DomoticzMqttHandler::publish(AmsData* data, AmsData* previousState) {
if (config.cl1idx > 0){
char val[16];
snprintf(val, 16, "%.1f;%.1f;%.1f", data->getL1Current(), data->getL2Current(), data->getL3Current());
char json[192];
snprintf_P(json, sizeof(json), DOMOTICZ_JSON,
snprintf_P(json, BufferSize, DOMOTICZ_JSON,
config.cl1idx,
val
);

View File

@@ -8,14 +8,19 @@ class DomoticzMqttHandler : public AmsMqttHandler {
public:
DomoticzMqttHandler(MQTTClient* mqtt, DomoticzConfig config) : AmsMqttHandler(mqtt) {
this->config = config;
this->json = (char*) malloc(BufferSize);
};
bool publish(AmsData* data, AmsData* previousState);
bool publishTemperatures(AmsConfiguration*, HwTools*);
bool publishPrices(EntsoeApi*);
bool publishSystem(HwTools*);
static const uint16_t BufferSize = 192;
private:
DomoticzConfig config;
int energy = 0.0;
char* json;
};
#endif

View File

@@ -13,8 +13,7 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState) {
return false;
if(data->getListType() == 1) {
char json[256];
snprintf_P(json, sizeof(json), JSON1_JSON,
snprintf_P(json, BufferSize, JSON1_JSON,
WiFi.macAddress().c_str(),
clientId.c_str(),
(uint32_t) (millis64()/1000),
@@ -26,8 +25,7 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState) {
);
return mqtt->publish(topic, json);
} else if(data->getListType() == 2) {
char json[512];
snprintf_P(json, sizeof(json), JSON2_JSON,
snprintf_P(json, BufferSize, JSON2_JSON,
WiFi.macAddress().c_str(),
clientId.c_str(),
(uint32_t) (millis64()/1000),
@@ -52,8 +50,7 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState) {
return mqtt->publish(topic, json);
} else if(data->getListType() == 3) {
if(data->getPowerFactor() == 0) {
char json[768];
snprintf_P(json, sizeof(json), JSON3_JSON,
snprintf_P(json, BufferSize, JSON3_JSON,
WiFi.macAddress().c_str(),
clientId.c_str(),
(uint32_t) (millis64()/1000),
@@ -82,8 +79,7 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState) {
);
return mqtt->publish(topic, json);
} else {
char json[768];
snprintf_P(json, sizeof(json), JSON3PF_JSON,
snprintf_P(json, BufferSize, JSON3PF_JSON,
WiFi.macAddress().c_str(),
clientId.c_str(),
(uint32_t) (millis64()/1000),
@@ -229,8 +225,7 @@ bool JsonMqttHandler::publishPrices(EntsoeApi* eapi) {
sprintf(ts6hr, "%04d-%02d-%02dT%02d:00:00Z", tm.Year+1970, tm.Month, tm.Day, tm.Hour);
}
char json[384];
snprintf_P(json, sizeof(json), JSONPRICES_JSON,
snprintf_P(json, BufferSize, JSONPRICES_JSON,
WiFi.macAddress().c_str(),
values[0],
values[1],
@@ -257,8 +252,7 @@ bool JsonMqttHandler::publishSystem(HwTools* hw) {
if(init || topic.isEmpty() || !mqtt->connected())
return false;
char json[192];
snprintf_P(json, sizeof(json), JSONSYS_JSON,
snprintf_P(json, BufferSize, JSONSYS_JSON,
WiFi.macAddress().c_str(),
clientId.c_str(),
(uint32_t) (millis64()/1000),

View File

@@ -9,16 +9,20 @@ public:
this->clientId = clientId;
this->topic = String(topic);
this->hw = hw;
this->json = (char*) malloc(BufferSize);
};
bool publish(AmsData* data, AmsData* previousState);
bool publishTemperatures(AmsConfiguration*, HwTools*);
bool publishPrices(EntsoeApi*);
bool publishSystem(HwTools*);
static const uint16_t BufferSize = 768;
private:
String clientId;
String topic;
HwTools* hw;
bool init = false;
char* json;
};
#endif

View File

@@ -14,6 +14,8 @@ public:
bool publishPrices(EntsoeApi*);
bool publishSystem(HwTools*);
static const uint16_t BufferSize = 128;
private:
String topic;
bool full;