Browse Source

Moved strings to PROGMEM, 160 bytes saved in memory

fastled
Xose Pérez 7 years ago
parent
commit
9750bf26bb
18 changed files with 55 additions and 52 deletions
  1. +1
    -1
      code/espurna/analog.ino
  2. +1
    -1
      code/espurna/button.ino
  3. +0
    -1
      code/espurna/config/arduino.h
  4. +3
    -1
      code/espurna/config/general.h
  5. +2
    -0
      code/espurna/config/hardware.h
  6. +2
    -2
      code/espurna/dht.ino
  7. +2
    -2
      code/espurna/domoticz.ino
  8. +4
    -4
      code/espurna/emon.ino
  9. +1
    -1
      code/espurna/espurna.ino
  10. +6
    -6
      code/espurna/hlw8012.ino
  11. +4
    -4
      code/espurna/influxdb.ino
  12. +1
    -1
      code/espurna/led.ino
  13. +8
    -8
      code/espurna/light.ino
  14. +1
    -1
      code/espurna/mqtt.ino
  15. +1
    -1
      code/espurna/ntp.ino
  16. +8
    -8
      code/espurna/relay.ino
  17. +5
    -5
      code/espurna/rfbridge.ino
  18. +5
    -5
      code/espurna/web.ino

+ 1
- 1
code/espurna/analog.ino View File

@ -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());
});
}


+ 1
- 1
code/espurna/button.ino View File

@ -25,7 +25,7 @@ std::vector<button_t> _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


+ 0
- 1
code/espurna/config/arduino.h View File

@ -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


+ 3
- 1
code/espurna/config/general.h View File

@ -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


+ 2
- 0
code/espurna/config/hardware.h View File

@ -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


+ 2
- 2
code/espurna/dht.ino View File

@ -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


+ 2
- 2
code/espurna/domoticz.ino View File

@ -84,13 +84,13 @@ template<typename T> 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();
}


+ 4
- 4
code/espurna/emon.ino View File

@ -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


+ 1
- 1
code/espurna/espurna.ino View File

@ -28,7 +28,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
String getIdentifier() {
char identifier[20];
sprintf(identifier, "%s_%06X", DEVICE, ESP.getChipId());
sprintf_P(identifier, PSTR("%s_%06X"), DEVICE, ESP.getChipId());
return String(identifier);
}


+ 6
- 6
code/espurna/hlw8012.ino View File

@ -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


+ 4
- 4
code/espurna/influxdb.ino View File

@ -19,17 +19,17 @@ template<typename T> 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<typename T> 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;


+ 1
- 1
code/espurna/led.ino View File

@ -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);
}


+ 8
- 8
code/espurna/light.ino View File

@ -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<lightChannels(); id++) {
char url[15];
sprintf(url, "%s/%d", MQTT_TOPIC_CHANNEL, id);
sprintf_P(url, PSTR("%s/%d"), MQTT_TOPIC_CHANNEL, id);
char key[10];
sprintf(key, "%s%d", MQTT_TOPIC_CHANNEL, id);
sprintf_P(key, PSTR("%s%d"), MQTT_TOPIC_CHANNEL, id);
apiRegister(url, key,
[id](char * buffer, size_t len) {
snprintf(buffer, len, "%d", lightChannel(id));
snprintf_P(buffer, len, PSTR("%d"), lightChannel(id));
},
[id](const char * payload) {
lightChannel(id, atoi(payload));


+ 1
- 1
code/espurna/mqtt.ino View File

@ -129,7 +129,7 @@ void mqttSend(const char * topic, const char * message) {
void mqttSend(const char * topic, unsigned int index, const char * message, bool force) {
char buffer[strlen(topic)+5];
sprintf(buffer, "%s/%d", topic, index);
sprintf_P(buffer, PSTR("%s/%d"), topic, index);
mqttSend(buffer, message, force);
}


+ 1
- 1
code/espurna/ntp.ino View File

@ -39,7 +39,7 @@ String ntpDateTime() {
int month = value.substring(12, 14).toInt();
int year = value.substring(15, 19).toInt();
char buffer[20];
sprintf(buffer, "%04d/%02d/%02dT%02d:%02d:%02d", year, month, day, hour, minute, second);
sprintf_P(buffer, PSTR("%04d/%02d/%02dT%02d:%02d:%02d"), year, month, day, hour, minute, second);
return String(buffer);
}


+ 8
- 8
code/espurna/relay.ino View File

@ -123,15 +123,15 @@ void relayPulseMode(unsigned int value, bool report) {
/*
if (report) {
char topic[strlen(MQTT_TOPIC_RELAY) + 10];
sprintf(topic, "%s/pulse", MQTT_TOPIC_RELAY);
sprintf_P(topic, PSTR("%s/pulse"), MQTT_TOPIC_RELAY);
char value[2];
sprintf(value, "%d", value);
sprintf_P(value, PSTR("%d"), value);
mqttSend(topic, value);
}
*/
char message[20];
sprintf(message, "{\"relayPulseMode\": %d}", value);
sprintf_P(message, PSTR("{\"relayPulseMode\": %d}"), value);
wsSend(message);
}
@ -299,14 +299,14 @@ void relaySetupAPI() {
for (unsigned int relayID=0; relayID<relayCount(); relayID++) {
char url[15];
sprintf(url, "%s/%d", MQTT_TOPIC_RELAY, relayID);
sprintf_P(url, PSTR("%s/%d"), MQTT_TOPIC_RELAY, relayID);
char key[10];
sprintf(key, "%s%d", MQTT_TOPIC_RELAY, relayID);
sprintf_P(key, PSTR("%s%d"), MQTT_TOPIC_RELAY, relayID);
apiRegister(url, key,
[relayID](char * buffer, size_t len) {
snprintf(buffer, len, "%d", relayStatus(relayID) ? 1 : 0);
snprintf_P(buffer, len, PSTR("%d"), relayStatus(relayID) ? 1 : 0);
},
[relayID](const char * payload) {
unsigned int value = payload[0] - '0';
@ -362,7 +362,7 @@ void relayMQTTCallback(unsigned int type, const char * topic, const char * paylo
#endif
char buffer[strlen(MQTT_TOPIC_RELAY) + 3];
sprintf(buffer, "%s/+", MQTT_TOPIC_RELAY);
sprintf_P(buffer, PSTR("%s/+"), MQTT_TOPIC_RELAY);
mqttSubscribe(buffer);
}
@ -412,7 +412,7 @@ void relaySetupMQTT() {
void relayInfluxDB(unsigned char id) {
if (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


+ 5
- 5
code/espurna/rfbridge.ino View File

@ -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<RF_MESSAGE_SIZE; p++) {
sprintf(&out[p*2], "%02X", in[p]);
sprintf_P(&out[p*2], PSTR("%02X"), in[p]);
}
return true;
}
@ -210,7 +210,7 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa
if (type == MQTT_CONNECT_EVENT) {
char buffer[strlen(MQTT_TOPIC_RFLEARN) + 3];
sprintf(buffer, "%s/+", MQTT_TOPIC_RFLEARN);
sprintf_P(buffer, PSTR("%s/+"), MQTT_TOPIC_RFLEARN);
mqttSubscribe(buffer);
mqttSubscribe(MQTT_TOPIC_RFOUT);
}
@ -251,13 +251,13 @@ void _rfbMqttCallback(unsigned int type, const char * topic, const char * payloa
void rfbStore(unsigned char id, bool status, const char * code) {
DEBUG_MSG_P(PSTR("[RFBRIDGE] Storing %d-%s => '%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


+ 5
- 5
code/espurna/web.ino View File

@ -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);


Loading…
Cancel
Save