From 9750bf26bb80a6a530d93ffd8435b66a2238d6bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sat, 19 Aug 2017 18:24:07 +0200 Subject: [PATCH] Moved strings to PROGMEM, 160 bytes saved in memory --- code/espurna/analog.ino | 2 +- code/espurna/button.ino | 2 +- code/espurna/config/arduino.h | 1 - code/espurna/config/general.h | 4 +++- code/espurna/config/hardware.h | 2 ++ code/espurna/dht.ino | 4 ++-- code/espurna/domoticz.ino | 4 ++-- code/espurna/emon.ino | 8 ++++---- code/espurna/espurna.ino | 2 +- code/espurna/hlw8012.ino | 12 ++++++------ code/espurna/influxdb.ino | 8 ++++---- code/espurna/led.ino | 2 +- code/espurna/light.ino | 16 ++++++++-------- code/espurna/mqtt.ino | 2 +- code/espurna/ntp.ino | 2 +- code/espurna/relay.ino | 16 ++++++++-------- code/espurna/rfbridge.ino | 10 +++++----- code/espurna/web.ino | 10 +++++----- 18 files changed, 55 insertions(+), 52 deletions(-) diff --git a/code/espurna/analog.ino b/code/espurna/analog.ino index 18c66438..1c8f1280 100644 --- a/code/espurna/analog.ino +++ b/code/espurna/analog.ino @@ -21,7 +21,7 @@ void analogSetup() { pinMode(ANALOG_PIN, INPUT); apiRegister(ANALOG_TOPIC, ANALOG_TOPIC, [](char * buffer, size_t len) { - snprintf(buffer, len, "%d", getAnalog()); + snprintf_P(buffer, len, PSTR("%d"), getAnalog()); }); } diff --git a/code/espurna/button.ino b/code/espurna/button.ino index d0a8efcc..36eefada 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -25,7 +25,7 @@ std::vector _buttons; void buttonMQTT(unsigned char id, uint8_t event) { if (id >= _buttons.size()) return; char payload[2]; - sprintf(payload, "%d", event); + sprintf_P(payload, PSTR("%d"), event); mqttSend(MQTT_TOPIC_BUTTON, id, payload); } #endif diff --git a/code/espurna/config/arduino.h b/code/espurna/config/arduino.h index 11bcea82..fe1b830c 100644 --- a/code/espurna/config/arduino.h +++ b/code/espurna/config/arduino.h @@ -49,7 +49,6 @@ //#define ENABLE_DOMOTICZ 0 //#define ENABLE_DS18B20 1 //#define ENABLE_EMON 1 -//#define ENABLE_HLW8012 1 //#define ENABLE_I2C 1 //#define ENABLE_INFLUXDB 0 //#define ENABLE_FAUXMO 0 diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index b22449a7..bb9334f2 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -33,7 +33,7 @@ // General debug options and macros #define DEBUG_MESSAGE_MAX_LENGTH 80 -#if ENABLE_SERIAL_DEBUG | ENABLE_UDP_DEBUG +#if (ENABLE_SERIAL_DEBUG==1) || (ENABLE_UDP_DEBUG==1) #define DEBUG_MSG(...) debugSend(__VA_ARGS__) #define DEBUG_MSG_P(...) debugSend_P(__VA_ARGS__) #endif @@ -321,7 +321,9 @@ PROGMEM const char* const custom_reset_string[] = { // I2C // ----------------------------------------------------------------------------- +#ifndef ENABLE_I2C #define ENABLE_I2C 0 // I2C enabled +#endif #define I2C_SDA_PIN 4 // SDA GPIO #define I2C_SCL_PIN 14 // SCL GPIO #define I2C_CLOCK_STRETCH_TIME 200 // BRZO clock stretch time diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index 8ebaa4fe..cd389e2e 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -84,6 +84,7 @@ #define LED1_PIN_INVERSE 0 // HLW8012 + #define ENABLE_HLW8012 1 #define HLW8012_SEL_PIN 2 #define HLW8012_CF1_PIN 13 #define HLW8012_CF_PIN 14 @@ -246,6 +247,7 @@ #define LED1_PIN_INVERSE 0 // HLW8012 + #define ENABLE_HLW8012 1 #define HLW8012_SEL_PIN 5 #define HLW8012_CF1_PIN 13 #define HLW8012_CF_PIN 14 diff --git a/code/espurna/dht.ino b/code/espurna/dht.ino index 67927bf9..9c455f4a 100644 --- a/code/espurna/dht.ino +++ b/code/espurna/dht.ino @@ -34,7 +34,7 @@ void dhtSetup() { dtostrf(_dhtTemperature, len-1, 1, buffer); }); apiRegister(DHT_HUMIDITY_TOPIC, DHT_HUMIDITY_TOPIC, [](char * buffer, size_t len) { - snprintf(buffer, len, "%d", _dhtHumidity); + snprintf_P(buffer, len, PSTR("%d"), _dhtHumidity); }); } @@ -88,7 +88,7 @@ void dhtLoop() { status = HUMIDITY_DRY; } char buffer[2]; - sprintf(buffer, "%d", status); + sprintf_P(buffer, PSTR("%d"), status); domoticzSend("dczHumIdx", humidity, buffer); } #endif diff --git a/code/espurna/domoticz.ino b/code/espurna/domoticz.ino index a093a72f..cd870ae6 100644 --- a/code/espurna/domoticz.ino +++ b/code/espurna/domoticz.ino @@ -84,13 +84,13 @@ template void domoticzSend(const char * key, T nvalue) { void domoticzSendRelay(unsigned int relayID) { if (!_dczEnabled) return; char buffer[15]; - sprintf(buffer, "dczRelayIdx%d", relayID); + sprintf_P(buffer, PSTR("dczRelayIdx%d"), relayID); domoticzSend(buffer, relayStatus(relayID) ? "1" : "0"); } int domoticzIdx(unsigned int relayID) { char buffer[15]; - sprintf(buffer, "dczRelayIdx%d", relayID); + sprintf_P(buffer, PSTR("dczRelayIdx%d"), relayID); return getSetting(buffer).toInt(); } diff --git a/code/espurna/emon.ino b/code/espurna/emon.ino index 94f042d1..dd5348c8 100644 --- a/code/espurna/emon.ino +++ b/code/espurna/emon.ino @@ -113,7 +113,7 @@ void powerMonitorSetup() { apiRegister(EMON_APOWER_TOPIC, EMON_APOWER_TOPIC, [](char * buffer, size_t len) { if (_emonReady) { - snprintf(buffer, len, "%d", _emonPower); + snprintf_P(buffer, len, PSTR("%d"), _emonPower); } else { buffer = NULL; } @@ -191,11 +191,11 @@ void powerMonitorLoop() { #if ENABLE_DOMOTICZ { char buffer[20]; - snprintf(buffer, 20, "%d;%s", _emonPower, String(energy_delta, 3).c_str()); + snprintf_P(buffer, 20, PSTR("%d;%s"), _emonPower, String(energy_delta, 3).c_str()); domoticzSend("dczPowIdx", 0, buffer); - snprintf(buffer, 20, "%s", String(energy_delta, 3).c_str()); + snprintf_P(buffer, 20, PSTR("%s"), String(energy_delta, 3).c_str()); domoticzSend("dczEnergyIdx", 0, buffer); - snprintf(buffer, 20, "%s", String(_emonCurrent, 3).c_str()); + snprintf_P(buffer, 20, PSTR("%s"), String(_emonCurrent, 3).c_str()); domoticzSend("dczCurrentIdx", 0, buffer); } #endif diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index 1143e031..acaec099 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -28,7 +28,7 @@ along with this program. If not, see . String getIdentifier() { char identifier[20]; - sprintf(identifier, "%s_%06X", DEVICE, ESP.getChipId()); + sprintf_P(identifier, PSTR("%s_%06X"), DEVICE, ESP.getChipId()); return String(identifier); } diff --git a/code/espurna/hlw8012.ino b/code/espurna/hlw8012.ino index 0a23926f..d3faf45a 100644 --- a/code/espurna/hlw8012.ino +++ b/code/espurna/hlw8012.ino @@ -160,7 +160,7 @@ void hlw8012Setup() { // API definitions apiRegister(HLW8012_POWER_TOPIC, HLW8012_POWER_TOPIC, [](char * buffer, size_t len) { if (_hlwReady) { - snprintf(buffer, len, "%d", _hlwPower); + snprintf_P(buffer, len, PSTR("%d"), _hlwPower); } else { buffer = NULL; } @@ -174,7 +174,7 @@ void hlw8012Setup() { }); apiRegister(HLW8012_VOLTAGE_TOPIC, HLW8012_VOLTAGE_TOPIC, [](char * buffer, size_t len) { if (_hlwReady) { - snprintf(buffer, len, "%d", _hlwVoltage); + snprintf_P(buffer, len, PSTR("%d"), _hlwVoltage); } else { buffer = NULL; } @@ -295,13 +295,13 @@ void hlw8012Loop() { #if ENABLE_DOMOTICZ { char buffer[20]; - snprintf(buffer, 20, "%d;%s", _hlwPower, String(energy_delta, 3).c_str()); + snprintf_P(buffer, 20, PSTR("%d;%s"), _hlwPower, String(energy_delta, 3).c_str()); domoticzSend("dczPowIdx", 0, buffer); - snprintf(buffer, 20, "%s", String(energy_delta, 3).c_str()); + snprintf_P(buffer, 20, PSTR("%s"), String(energy_delta, 3).c_str()); domoticzSend("dczEnergyIdx", 0, buffer); - snprintf(buffer, 20, "%d", _hlwVoltage); + snprintf_P(buffer, 20, PSTR("%d"), _hlwVoltage); domoticzSend("dczVoltIdx", 0, buffer); - snprintf(buffer, 20, "%s", String(_hlwCurrent).c_str()); + snprintf_P(buffer, 20, PSTR("%s"), String(_hlwCurrent).c_str()); domoticzSend("dczCurrentIdx", 0, buffer); } #endif diff --git a/code/espurna/influxdb.ino b/code/espurna/influxdb.ino index f19e5add..82ed09fb 100644 --- a/code/espurna/influxdb.ino +++ b/code/espurna/influxdb.ino @@ -19,17 +19,17 @@ template bool influxDBSend(const char * topic, T payload) { if (!influxDBEnabled) return true; if (!wifiConnected() || (WiFi.getMode() != WIFI_STA)) return true; - DEBUG_MSG_P(("[INFLUXDB] Sending\n")); + DEBUG_MSG("[INFLUXDB] Sending\n"); _influxClient.setTimeout(2); if (!_influxClient.connect(getSetting("idbHost").c_str(), getSetting("idbPort", INFLUXDB_PORT).toInt())) { - DEBUG_MSG_P(("[INFLUXDB] Connection failed\n")); + DEBUG_MSG("[INFLUXDB] Connection failed\n"); return false; } char data[128]; sprintf(data, "%s,device=%s value=%s", topic, getSetting("hostname").c_str(), String(payload).c_str()); - DEBUG_MSG_P(("[INFLUXDB] Data: %s\n"), data); + DEBUG_MSG("[INFLUXDB] Data: %s\n", data); char request[256]; sprintf(request, "POST /write?db=%s&u=%s&p=%s HTTP/1.1\r\nHost: %s:%d\r\nContent-Length: %d\r\n\r\n%s", @@ -45,7 +45,7 @@ template bool influxDBSend(const char * topic, T payload) { } _influxClient.stop(); - DEBUG_MSG_P(("[INFLUXDB] Sent failed\n")); + DEBUG_MSG("[INFLUXDB] Sent failed\n"); while (_influxClient.connected()) delay(0); return false; diff --git a/code/espurna/led.ino b/code/espurna/led.ino index cb922779..b687cf12 100644 --- a/code/espurna/led.ino +++ b/code/espurna/led.ino @@ -64,7 +64,7 @@ void ledMQTTCallback(unsigned int type, const char * topic, const char * payload if (type == MQTT_CONNECT_EVENT) { char buffer[strlen(MQTT_TOPIC_LED) + 3]; - sprintf(buffer, "%s/+", MQTT_TOPIC_LED); + sprintf_P(buffer, PSTR("%s/+"), MQTT_TOPIC_LED); mqttSubscribe(buffer); } diff --git a/code/espurna/light.ino b/code/espurna/light.ino index 2ceb850d..18e1df1e 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -133,7 +133,7 @@ void _toRGB(char * rgb, size_t len, bool applyBrightness) { value <<= 8; value += _channels[2].value * b; - snprintf(rgb, len, "#%06X", value); + snprintf_P(rgb, len, PSTR("#%06X"), value); } @@ -293,7 +293,7 @@ void _lightMQTTCallback(unsigned int type, const char * topic, const char * payl } char buffer[strlen(MQTT_TOPIC_CHANNEL) + 3]; - sprintf(buffer, "%s/+", MQTT_TOPIC_CHANNEL); + sprintf_P(buffer, PSTR("%s/+"), MQTT_TOPIC_CHANNEL); mqttSubscribe(buffer); } @@ -370,14 +370,14 @@ void lightMQTT() { mqttSend(MQTT_TOPIC_COLOR, buffer); // Brightness - sprintf(buffer, "%d", _brightness); + sprintf_P(buffer, PSTR("%d"), _brightness); mqttSend(MQTT_TOPIC_BRIGHTNESS, buffer); } // Channels for (unsigned int i=0; i < _channels.size(); i++) { - sprintf(buffer, "%d", _channels[i].value); + sprintf_P(buffer, PSTR("%d"), _channels[i].value); mqttSend(MQTT_TOPIC_CHANNEL, i, buffer); } @@ -476,7 +476,7 @@ void _lightAPISetup() { apiRegister(MQTT_TOPIC_BRIGHTNESS, MQTT_TOPIC_BRIGHTNESS, [](char * buffer, size_t len) { - snprintf(buffer, len, "%d", _brightness); + snprintf_P(buffer, len, PSTR("%d"), _brightness); }, [](const char * payload) { lightBrightness(atoi(payload)); @@ -505,14 +505,14 @@ void _lightAPISetup() { for (unsigned int id=0; id= _relays.size()) return; char buffer[10]; - sprintf(buffer, "%s,id=%d", MQTT_TOPIC_RELAY, id); + sprintf_P(buffer, PSTR("%s,id=%d"), MQTT_TOPIC_RELAY, id); influxDBSend(buffer, relayStatus(id) ? "1" : "0"); } #endif diff --git a/code/espurna/rfbridge.ino b/code/espurna/rfbridge.ino index 4976a6af..8128e180 100644 --- a/code/espurna/rfbridge.ino +++ b/code/espurna/rfbridge.ino @@ -201,7 +201,7 @@ From a byte array to an hexa char array ("A220EE...", double the size) */ bool _rfbToChar(byte * in, char * out) { for (unsigned char p = 0; p '%s'\n"), id, status ? "ON" : "OFF", code); char key[8] = {0}; - sprintf(key, "rfb%d%s", id, status ? "on" : "off"); + sprintf_P(key, PSTR("rfb%d%s"), id, status ? "on" : "off"); setSetting(key, code); } String rfbRetrieve(unsigned char id, bool status) { char key[8] = {0}; - sprintf(key, "rfb%d%s", id, status ? "on" : "off"); + sprintf_P(key, PSTR("rfb%d%s"), id, status ? "on" : "off"); return getSetting(key); } @@ -282,7 +282,7 @@ void rfbLearn(unsigned char id, bool status) { void rfbForget(unsigned char id, bool status) { char key[8] = {0}; - sprintf(key, "rfb%d%s", id, status ? "on" : "off"); + sprintf_P(key, PSTR("rfb%d%s"), id, status ? "on" : "off"); delSetting(key); // Websocket update diff --git a/code/espurna/web.ino b/code/espurna/web.ino index e42cd27d..32d49b5f 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -430,7 +430,7 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { void _wsStart(uint32_t client_id) { char chipid[6]; - sprintf(chipid, "%06X", ESP.getChipId()); + sprintf_P(chipid, PSTR("%06X"), ESP.getChipId()); DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); @@ -776,7 +776,7 @@ ArRequestHandlerFunction _bindAPI(unsigned int apiID) { char *p = ltrim(value); // The response will be a 404 NOT FOUND if the resource is not available - if (*value == NULL) { + if (!value) { DEBUG_MSG_P(PSTR("[API] Sending 404 response\n")); request->send(404); return; @@ -801,7 +801,7 @@ void apiRegister(const char * url, const char * key, apiGetCallbackFunction getF // Store it web_api_t api; char buffer[40]; - snprintf(buffer, 39, "/api/%s", url); + snprintf_P(buffer, 39, PSTR("/api/%s"), url); api.url = strdup(buffer); api.key = strdup(key); api.getFn = getFn; @@ -913,7 +913,7 @@ void _onGetConfig(AsyncWebServerRequest *request) { } char buffer[100]; - sprintf(buffer, "attachment; filename=\"%s-backup.json\"", (char *) getSetting("hostname").c_str()); + sprintf_P(buffer, PSTR("attachment; filename=\"%s-backup.json\""), (char *) getSetting("hostname").c_str()); response->addHeader("Content-Disposition", buffer); response->setLength(); request->send(response); @@ -993,7 +993,7 @@ void webSetup() { mqttRegister(wsMQTTCallback); // Cache the Last-Modifier header value - sprintf(_last_modified, "%s %s GMT", __DATE__, __TIME__); + sprintf_P(_last_modified, PSTR("%s %s GMT"), __DATE__, __TIME__); // Setup webserver _server->addHandler(&ws);