Memory Optimizations

This commit is contained in:
fvanroie 2020-02-22 20:11:33 +01:00
parent f165908b03
commit b15043e61c
14 changed files with 173 additions and 116 deletions

View File

@ -64,7 +64,8 @@ upload_speed = 921600
; -- Shared library dependencies in all environments ; -- Shared library dependencies in all environments
lib_deps = lib_deps =
;lvgl@^7.0.0 ; Not in library yet ;lvgl@^7.0.0 ; Not in library yet
TFT_eSPI@^2.1.3 ; Tft SPI drivers ;TFT_eSPI@^2.1.3 ; Tft SPI drivers
TFT_eSPI@^1.4.20 ; Tft SPI drivers
PubSubClient@^2.7.0 ; MQTT client PubSubClient@^2.7.0 ; MQTT client
ArduinoJson@^6.14.1,>6.14.0 ; needs at least 6.14.1 ArduinoJson@^6.14.1,>6.14.0 ; needs at least 6.14.1
Syslog@^2.0.0 Syslog@^2.0.0
@ -159,8 +160,8 @@ build_flags =
[env:lolind32pro-lolintft24] [env:lolind32pro-lolintft24]
platform = espressif32 platform = espressif32
board = lolin_d32_pro board = lolin_d32_pro
upload_port = COM8 ; Change to the correct port upload_port = COM7 ; Change to the correct port
monitor_port = COM8 ; Change to the correct port monitor_port = COM7 ; Change to the correct port
monitor_speed = 115200 monitor_speed = 115200
board_build.partitions = default.csv board_build.partitions = default.csv
build_flags = build_flags =

View File

@ -1473,7 +1473,7 @@ void haspNewObject(const JsonObject & config)
} }
/** testing end **/ /** testing end **/
char msg[64]; char msg[127];
sprintf_P(msg, PSTR("HASP: Created object p[%u].b[%u]"), pageid, temp); sprintf_P(msg, PSTR("HASP: Created object p[%u].b[%u]"), pageid, temp);
debugPrintln(msg); debugPrintln(msg);
@ -1488,7 +1488,7 @@ void haspNewObject(const JsonObject & config)
void haspLoadPage(String pages) void haspLoadPage(String pages)
{ {
char msg[92]; char msg[127];
if(!SPIFFS.begin()) { if(!SPIFFS.begin()) {
errorPrintln(String(F("HASP: %sFS not mounted. Failed to load ")) + pages.c_str()); errorPrintln(String(F("HASP: %sFS not mounted. Failed to load ")) + pages.c_str());

View File

@ -126,4 +126,14 @@ void configSetup(JsonDocument & settings)
} else { } else {
configGetConfig(settings, true); configGetConfig(settings, true);
} }
}
void configOutput(const JsonObject & settings)
{
String output((char *)0);
output.reserve(127);
serializeJson(settings, output);
String passmask = F("********");
output.replace(settings[F("pass")].as<String>(), passmask);
debugPrintln(String(F("CONF: ")) + output);
} }

View File

@ -29,5 +29,6 @@ void configSetConfig(JsonObject & settings);
void configGetConfig(JsonDocument & settings); void configGetConfig(JsonDocument & settings);
void configWriteConfig(); void configWriteConfig();
bool configChanged(void); bool configChanged(void);
void configOutput(const JsonObject & settings);
#endif #endif

View File

@ -74,20 +74,28 @@ void debugSetup()
void debugLoop() void debugLoop()
{} {}
void serialPrintln(String debugText) void serialPrintln(const char * debugText)
{ {
String debugTimeText((char *)0); String debugTimeText((char *)0);
debugTimeText.reserve(128); debugTimeText.reserve(127);
debugTimeText = F("["); debugTimeText = F("[");
debugTimeText += String(float(millis()) / 1000, 3); debugTimeText += String(float(millis()) / 1000, 3);
debugTimeText += F("s] "); debugTimeText += F("s] ");
debugTimeText += ESP.getMaxFreeBlockSize();
debugTimeText += F("/");
debugTimeText += ESP.getFreeHeap(); debugTimeText += ESP.getFreeHeap();
debugTimeText += F(" "); debugTimeText += F(" ");
debugTimeText += halGetHeapFragmentation(); debugTimeText += halGetHeapFragmentation();
debugTimeText += F(" "); debugTimeText += F(" ");
debugTimeText += debugText;
Serial.println(debugTimeText); Serial.print(debugTimeText);
Serial.println(debugText);
}
void serialPrintln(String & debugText)
{
serialPrintln(debugText.c_str());
} }
#if HASP_USE_SYSLOG != 0 #if HASP_USE_SYSLOG != 0

View File

@ -5,7 +5,8 @@ void debugSetup(void);
void debugLoop(void); void debugLoop(void);
void debugStop(void); void debugStop(void);
void serialPrintln(String debugText); void serialPrintln(String & debugText);
void serialPrintln(const char * debugText);
void syslogSend(uint8_t log, const char * debugText); void syslogSend(uint8_t log, const char * debugText);

View File

@ -72,7 +72,7 @@ bool IRAM_ATTR guiCheckSleep()
/* Serial debugging */ /* Serial debugging */
void debugLvgl(lv_log_level_t level, const char * file, uint32_t line, const char * dsc) void debugLvgl(lv_log_level_t level, const char * file, uint32_t line, const char * dsc)
{ {
char msg[128]; char msg[127];
sprintf(msg, PSTR("LVGL: %s@%d->%s"), file, line, dsc); sprintf(msg, PSTR("LVGL: %s@%d->%s"), file, line, dsc);
debugPrintln(msg); debugPrintln(msg);
} }
@ -341,7 +341,7 @@ void guiSetup(TFT_eSPI & screen, JsonObject settings)
/* Setup Backlight Control Pin */ /* Setup Backlight Control Pin */
if(guiBacklightPin >= 0) { if(guiBacklightPin >= 0) {
char msg[128]; char msg[127];
sprintf(msg, PSTR("LVGL: Backlight Pin = %i"), guiBacklightPin); sprintf(msg, PSTR("LVGL: Backlight Pin = %i"), guiBacklightPin);
debugPrintln(msg); debugPrintln(msg);

View File

@ -13,7 +13,7 @@ String esp32ResetReason(uint8_t cpuid)
RESET_REASON reason = rtc_get_reset_reason(cpuid); RESET_REASON reason = rtc_get_reset_reason(cpuid);
String resetReason((char *)0); String resetReason((char *)0);
resetReason.reserve(32); resetReason.reserve(127);
resetReason += F("CPU"); resetReason += F("CPU");
resetReason += cpuid; resetReason += cpuid;
@ -78,7 +78,7 @@ String halGetResetInfo()
{ {
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
String resetReason((char *)0); String resetReason((char *)0);
resetReason.reserve(64); resetReason.reserve(127);
resetReason += String(esp32ResetReason(0)); resetReason += String(esp32ResetReason(0));
resetReason += F(" / "); resetReason += F(" / ");

View File

@ -28,7 +28,7 @@ uint16_t httpPort = 80;
FS * filesystem = &SPIFFS; FS * filesystem = &SPIFFS;
File fsUploadFile; File fsUploadFile;
char httpUser[32] = ""; char httpUser[32] = "";
char httpPassword[64] = ""; char httpPassword[32] = "";
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
@ -99,6 +99,7 @@ bool httpIsAuthenticated(const String & page)
return false; return false;
} }
} }
char buffer[127]; char buffer[127];
snprintf(buffer, sizeof(buffer), PSTR("HTTP: Sending %s page to client connected from: %s"), page.c_str(), snprintf(buffer, sizeof(buffer), PSTR("HTTP: Sending %s page to client connected from: %s"), page.c_str(),
webServer.client().remoteIP().toString().c_str()); webServer.client().remoteIP().toString().c_str());
@ -557,7 +558,7 @@ void handleFileList()
} }
output += F("\"}"); output += F("\"}");
char msg[64]; char msg[127];
sprintf(msg, PSTR("HTTP: * %s (%u bytes)"), file.name(), (uint32_t)file.size()); sprintf(msg, PSTR("HTTP: * %s (%u bytes)"), file.name(), (uint32_t)file.size());
debugPrintln(msg); debugPrintln(msg);
@ -1107,9 +1108,7 @@ bool httpGetConfig(const JsonObject & settings)
settings[FPSTR(F_CONFIG_USER)] = httpUser; settings[FPSTR(F_CONFIG_USER)] = httpUser;
settings[FPSTR(F_CONFIG_PASS)] = httpPassword; settings[FPSTR(F_CONFIG_PASS)] = httpPassword;
serializeJson(settings, Serial); configOutput(settings);
Serial.println();
return true; return true;
} }
@ -1138,8 +1137,6 @@ bool httpSetConfig(const JsonObject & settings)
httpPort = settings[FPSTR(F_CONFIG_PORT)].as<uint8_t>(); httpPort = settings[FPSTR(F_CONFIG_PORT)].as<uint8_t>();
} }
serializeJson(settings, Serial); configOutput(settings);
Serial.println();
return changed; return changed;
} }

View File

@ -15,7 +15,24 @@
#include "hasp_log.h" #include "hasp_log.h"
#include "hasp_debug.h" #include "hasp_debug.h"
void debugPrintln(String debugText) void debugPrintln(String & debugText)
{
serialPrintln(debugText);
#if HASP_USE_SYSLOG != 0
syslogSend(0, debugText.c_str());
#endif
}
void debugPrintln(const __FlashStringHelper * debugText)
{
String buffer((char *)0);
buffer.reserve(127);
buffer = debugText;
debugPrintln(buffer);
}
void debugPrintln(const char * debugText)
{ {
serialPrintln(debugText); serialPrintln(debugText);

View File

@ -1,7 +1,10 @@
#ifndef HASP_LOG_H #ifndef HASP_LOG_H
#define HASP_LOG_H #define HASP_LOG_H
void debugPrintln(String debugText); void debugPrintln(String & debugText);
void debugPrintln(const __FlashStringHelper * debugText);
void debugPrintln(const char * debugText);
void errorPrintln(String debugText); void errorPrintln(String debugText);
void warningPrintln(String debugText); void warningPrintln(String debugText);

View File

@ -7,7 +7,6 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <EEPROM.h> #include <EEPROM.h>
#include <ESP.h> #include <ESP.h>
#include <DNSServer.h>
#endif #endif
#include <PubSubClient.h> #include <PubSubClient.h>
@ -24,7 +23,7 @@
#include "user_config_override.h" #include "user_config_override.h"
#endif #endif
String mqttClientId; // Auto-generated MQTT ClientID String mqttClientId((char *)0); // Auto-generated MQTT ClientID
/* /*
String mqttGetSubtopic; // MQTT subtopic for incoming commands requesting .val String mqttGetSubtopic; // MQTT subtopic for incoming commands requesting .val
String mqttGetSubtopicJSON; // MQTT object buffer for JSON status when requesting .val String mqttGetSubtopicJSON; // MQTT object buffer for JSON status when requesting .val
@ -41,8 +40,8 @@ String mqttLightBrightCommandTopic; // MQTT topic for incoming panel backlight d
String mqttLightBrightStateTopic; // MQTT topic for outgoing panel backlight dimmer state String mqttLightBrightStateTopic; // MQTT topic for outgoing panel backlight dimmer state
// String mqttMotionStateTopic; // MQTT topic for outgoing motion sensor state // String mqttMotionStateTopic; // MQTT topic for outgoing motion sensor state
String mqttNodeTopic; String mqttNodeTopic((char *)0);
String mqttGroupTopic; String mqttGroupTopic((char *)0);
bool mqttEnabled; bool mqttEnabled;
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
@ -89,29 +88,29 @@ void IRAM_ATTR mqttSendState(const char * subtopic, const char * payload)
// light = 0/1 // light = 0/1
// brightness = 100 // brightness = 100
char topic[128]; char topic[127];
sprintf_P(topic, PSTR("%sstate/%s"), mqttNodeTopic.c_str(), subtopic); snprintf_P(topic, sizeof(topic), PSTR("%sstate/%s"), mqttNodeTopic.c_str(), subtopic);
mqttClient.publish(topic, payload); mqttClient.publish(topic, payload);
debugPrintln(String(F("MQTT OUT: ")) + String(topic) + " = " + String(payload)); debugPrintln(String(F("MQTT OUT: ")) + String(topic) + " = " + String(payload));
// as json // as json
char value[256]; char value[254];
sprintf_P(topic, PSTR("%sstate/json"), mqttNodeTopic.c_str()); snprintf_P(topic, sizeof(topic), PSTR("%sstate/json"), mqttNodeTopic.c_str());
sprintf_P(value, PSTR("{\"%s\":\"%s\"}"), subtopic, payload); snprintf_P(value, sizeof(value), PSTR("{\"%s\":\"%s\"}"), subtopic, payload);
mqttClient.publish(topic, value); mqttClient.publish(topic, value);
debugPrintln(String(F("MQTT OUT: ")) + String(topic) + " = " + String(value)); debugPrintln(String(F("MQTT OUT: ")) + String(topic) + " = " + String(value));
} }
void IRAM_ATTR mqttSendNewValue(uint8_t pageid, uint8_t btnid, const char * attribute, String txt) void IRAM_ATTR mqttSendNewValue(uint8_t pageid, uint8_t btnid, const char * attribute, String txt)
{ {
char subtopic[32]; char subtopic[127];
sprintf_P(subtopic, PSTR("p[%u].b[%u].%s"), pageid, btnid, attribute); snprintf_P(subtopic, sizeof(subtopic), PSTR("p[%u].b[%u].%s"), pageid, btnid, attribute);
mqttSendState(subtopic, txt.c_str()); mqttSendState(subtopic, txt.c_str());
} }
void IRAM_ATTR mqttSendNewValue(uint8_t pageid, uint8_t btnid, int32_t val) void IRAM_ATTR mqttSendNewValue(uint8_t pageid, uint8_t btnid, int32_t val)
{ {
char value[16]; char value[127];
itoa(val, value, 10); itoa(val, value, 10);
mqttSendNewValue(pageid, btnid, "val", value); mqttSendNewValue(pageid, btnid, "val", value);
} }
@ -123,7 +122,7 @@ void IRAM_ATTR mqttSendNewValue(uint8_t pageid, uint8_t btnid, String txt)
void IRAM_ATTR mqttSendNewEvent(uint8_t pageid, uint8_t btnid, int32_t val) void IRAM_ATTR mqttSendNewEvent(uint8_t pageid, uint8_t btnid, int32_t val)
{ {
char value[16]; char value[127];
itoa(val, value, 10); itoa(val, value, 10);
mqttSendNewValue(pageid, btnid, "event", value); mqttSendNewValue(pageid, btnid, "event", value);
} }
@ -268,8 +267,8 @@ void mqttCallback(char * topic, byte * payload, unsigned int length)
if(strTopic == F("status") && if(strTopic == F("status") &&
strPayload == F("OFF")) { // catch a dangling LWT from a previous connection if it appears strPayload == F("OFF")) { // catch a dangling LWT from a previous connection if it appears
char topicBuffer[64]; char topicBuffer[127];
sprintf_P(topicBuffer, PSTR("%sstatus"), mqttNodeTopic.c_str()); snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sstatus"), mqttNodeTopic.c_str());
debugPrintln(String(F("MQTT: binary_sensor state: [")) + topicBuffer + "] : ON"); debugPrintln(String(F("MQTT: binary_sensor state: [")) + topicBuffer + "] : ON");
mqttClient.publish(topicBuffer, "ON", true); mqttClient.publish(topicBuffer, "ON", true);
return; return;
@ -281,13 +280,17 @@ void mqttReconnect()
static uint8_t mqttReconnectCount = 0; static uint8_t mqttReconnectCount = 0;
bool mqttFirstConnect = true; bool mqttFirstConnect = true;
String nodeName = haspGetNodename(); String nodeName = haspGetNodename();
// Generate an MQTT client ID as haspNode + our MAC address char topicBuffer[127];
mqttClientId = nodeName + "-" + WiFi.macAddress();
char topicBuffer[64]; // Generate an MQTT client ID as haspNode + our MAC address
sprintf_P(topicBuffer, PSTR("hasp/%s/"), nodeName.c_str()); mqttClientId = nodeName;
mqttClientId += F("-");
mqttClientId += wifiGetMacAddress(3, "");
WiFi.macAddress();
snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("hasp/%s/"), nodeName.c_str());
mqttNodeTopic = topicBuffer; mqttNodeTopic = topicBuffer;
sprintf_P(topicBuffer, PSTR("hasp/%s/"), mqttGroupName.c_str()); snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("hasp/%s/"), mqttGroupName.c_str());
mqttGroupTopic = topicBuffer; mqttGroupTopic = topicBuffer;
// haspSetPage(0); // haspSetPage(0);
@ -295,7 +298,7 @@ void mqttReconnect()
String(F(" as clientID ")) + mqttClientId); String(F(" as clientID ")) + mqttClientId);
// Attempt to connect and set LWT and Clean Session // Attempt to connect and set LWT and Clean Session
sprintf_P(topicBuffer, PSTR("%sstatus"), mqttNodeTopic.c_str()); snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sstatus"), mqttNodeTopic.c_str());
if(!mqttClient.connect(mqttClientId.c_str(), mqttUser.c_str(), mqttPassword.c_str(), topicBuffer, 0, false, "OFF", if(!mqttClient.connect(mqttClientId.c_str(), mqttUser.c_str(), mqttPassword.c_str(), topicBuffer, 0, false, "OFF",
true)) { true)) {
// Retry until we give up and restart after connectTimeout seconds // Retry until we give up and restart after connectTimeout seconds
@ -334,24 +337,24 @@ void mqttReconnect()
// Attempt to connect to broker, setting last will and testament // Attempt to connect to broker, setting last will and testament
// Subscribe to our incoming topics // Subscribe to our incoming topics
sprintf_P(topicBuffer, PSTR("%scommand/#"), mqttGroupTopic.c_str()); snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%scommand/#"), mqttGroupTopic.c_str());
if(mqttClient.subscribe(topicBuffer)) { if(mqttClient.subscribe(topicBuffer)) {
debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer); debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer);
} }
sprintf_P(topicBuffer, PSTR("%scommand/#"), mqttNodeTopic.c_str()); snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%scommand/#"), mqttNodeTopic.c_str());
if(mqttClient.subscribe(topicBuffer)) { if(mqttClient.subscribe(topicBuffer)) {
debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer); debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer);
} }
sprintf_P(topicBuffer, PSTR("%slight/#"), mqttNodeTopic.c_str()); snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%slight/#"), mqttNodeTopic.c_str());
if(mqttClient.subscribe(topicBuffer)) { if(mqttClient.subscribe(topicBuffer)) {
debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer); debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer);
} }
sprintf_P(topicBuffer, PSTR("%sbrightness/#"), mqttNodeTopic.c_str()); snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sbrightness/#"), mqttNodeTopic.c_str());
if(mqttClient.subscribe(topicBuffer)) { if(mqttClient.subscribe(topicBuffer)) {
debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer); debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer);
} }
sprintf_P(topicBuffer, PSTR("%sstatus"), mqttNodeTopic.c_str()); snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sstatus"), mqttNodeTopic.c_str());
if(mqttClient.subscribe(topicBuffer)) { if(mqttClient.subscribe(topicBuffer)) {
debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer); debugPrintln(String(F("MQTT: * Subscribed to ")) + topicBuffer);
} }
@ -367,6 +370,10 @@ void mqttReconnect()
void mqttSetup(const JsonObject & settings) void mqttSetup(const JsonObject & settings)
{ {
mqttClientId.reserve(127);
mqttNodeTopic.reserve(127);
mqttGroupTopic.reserve(127);
mqttSetConfig(settings); mqttSetConfig(settings);
mqttEnabled = mqttServer != "" && mqttPort > 0; mqttEnabled = mqttServer != "" && mqttPort > 0;
@ -394,16 +401,16 @@ bool mqttIsConnected()
void mqttStop() void mqttStop()
{ {
if(mqttClient.connected()) { if(mqttClient.connected()) {
char topicBuffer[64]; char topicBuffer[127];
sprintf_P(topicBuffer, PSTR("%sstatus"), mqttNodeTopic.c_str()); snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%sstatus"), mqttNodeTopic.c_str());
mqttClient.publish(topicBuffer, "OFF"); mqttClient.publish(topicBuffer, "OFF");
sprintf_P(topicBuffer, PSTR("%ssensor"), mqttNodeTopic.c_str()); snprintf_P(topicBuffer, sizeof(topicBuffer), PSTR("%ssensor"), mqttNodeTopic.c_str());
mqttClient.publish(topicBuffer, "{\"status\": \"unavailable\"}"); mqttClient.publish(topicBuffer, "{\"status\": \"unavailable\"}");
mqttClient.disconnect(); mqttClient.disconnect();
debugPrintln(String(F("MQTT: Disconnected from broker"))); debugPrintln(F("MQTT: Disconnected from broker"));
} }
} }
@ -415,9 +422,7 @@ bool mqttGetConfig(const JsonObject & settings)
settings[FPSTR(F_CONFIG_USER)] = String(mqttUser.c_str()); settings[FPSTR(F_CONFIG_USER)] = String(mqttUser.c_str());
settings[FPSTR(F_CONFIG_PASS)] = String(mqttPassword.c_str()); settings[FPSTR(F_CONFIG_PASS)] = String(mqttPassword.c_str());
serializeJson(settings, Serial); configOutput(settings);
Serial.println();
return true; return true;
} }
@ -472,8 +477,6 @@ bool mqttSetConfig(const JsonObject & settings)
mqttPassword = settings[FPSTR(F_CONFIG_PASS)].as<String>().c_str(); mqttPassword = settings[FPSTR(F_CONFIG_PASS)].as<String>().c_str();
} }
serializeJson(settings, Serial); configOutput(settings);
Serial.println();
return changed; return changed;
} }

View File

@ -9,29 +9,29 @@
#if defined(ARDUINO_ARCH_ESP32) #if defined(ARDUINO_ARCH_ESP32)
#include "SPIFFS.h" #include "SPIFFS.h"
#endif #endif
#include <FS.h> // Include the SPIFFS library #include <FS.h>
#endif #endif
void spiffsList() void spiffsList()
{ {
#if defined(ARDUINO_ARCH_ESP32) char buffer[127];
debugPrintln(PSTR("FILE: Listing files on the internal flash:")); debugPrintln(PSTR("FILE: Listing files on the internal flash:"));
#if defined(ARDUINO_ARCH_ESP32)
File root = SPIFFS.open("/"); File root = SPIFFS.open("/");
File file = root.openNextFile(); File file = root.openNextFile();
while(file) { while(file) {
char msg[64]; snprintf(buffer, sizeof(buffer), PSTR("FILE: * %s (%u bytes)"), file.name(), (uint32_t)file.size());
sprintf(msg, PSTR("FILE: * %s (%u bytes)"), file.name(), (uint32_t)file.size()); debugPrintln(buffer);
debugPrintln(msg);
file = root.openNextFile(); file = root.openNextFile();
} }
#endif #endif
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
debugPrintln(PSTR("FILE: Listing files on the internal flash:"));
Dir dir = SPIFFS.openDir("/"); Dir dir = SPIFFS.openDir("/");
while(dir.next()) { while(dir.next()) {
char msg[64]; snprintf(buffer, sizeof(buffer), PSTR("FILE: * %s (%u bytes)"), dir.fileName().c_str(),
sprintf(msg, PSTR("FILE: * %s (%u bytes)"), dir.fileName().c_str(), (uint32_t)dir.fileSize()); (uint32_t)dir.fileSize());
debugPrintln(msg); debugPrintln(buffer);
} }
#endif #endif
} }
@ -41,18 +41,17 @@ void spiffsSetup()
// no SPIFFS settings, as settings depend on SPIFFS // no SPIFFS settings, as settings depend on SPIFFS
#if HASP_USE_SPIFFS #if HASP_USE_SPIFFS
char msg[64]; char buffer[127];
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
if(!SPIFFS.begin()) { if(!SPIFFS.begin()) {
#else #else
if(!SPIFFS.begin(true)) { if(!SPIFFS.begin(true)) {
#endif #endif
sprintf(msg, PSTR("FILE: %%sSPI flash init failed. Unable to mount FS.")); snprintf(buffer, sizeof(buffer), PSTR("FILE: %%sSPI flash init failed. Unable to mount FS."));
errorPrintln(msg); errorPrintln(buffer);
} else { } else {
sprintf(msg, PSTR("FILE: [SUCCESS] SPI flash FS mounted")); snprintf(buffer, sizeof(buffer), PSTR("FILE: SPI Flash FS mounted"));
debugPrintln(msg); debugPrintln(buffer);
// spiffsList(); // Wait on debugSetup()
} }
#endif #endif
} }
@ -62,13 +61,21 @@ void spiffsLoop()
String spiffsFormatBytes(size_t bytes) String spiffsFormatBytes(size_t bytes)
{ {
String output((char *)0);
output.reserve(127);
if(bytes < 1024) { if(bytes < 1024) {
return String(bytes) + "B"; output += bytes;
} else if(bytes < (1024 * 1024)) { } else if(bytes < (1024 * 1024)) {
return String(bytes / 1024.0) + "KB"; output += bytes / 1024.0;
output += "K";
} else if(bytes < (1024 * 1024 * 1024)) { } else if(bytes < (1024 * 1024 * 1024)) {
return String(bytes / 1024.0 / 1024.0) + "MB"; output += bytes / 1024.0 / 1024.0;
output += "M";
} else { } else {
return String(bytes / 1024.0 / 1024.0 / 1024.0) + "GB"; output += bytes / 1024.0 / 1024.0 / 1024.0;
output += "G";
} }
output += "B";
return output;
} }

View File

@ -27,28 +27,30 @@ static WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;
#endif #endif
#ifdef WIFI_SSID #ifdef WIFI_SSID
std::string wifiSsid = WIFI_SSID; char wifiSsid[32] = WIFI_SSID;
// std::string wifiSsid = WIFI_SSID;
#else #else
std::string wifiSsid = ""; char wifiSsid[32] = "";
// std::string wifiSsid = "";
#endif #endif
#ifdef WIFI_PASSW #ifdef WIFI_PASSW
std::string wifiPassword = WIFI_PASSW; char wifiPassword[32] = WIFI_PASSW;
// std::string wifiPassword = WIFI_PASSW;
#else #else
std::string wifiPassword = ""; char wifiPassword[32] = "";
// std::string wifiPassword = "";
#endif #endif
const byte DNS_PORT = 53; // const byte DNS_PORT = 53;
// DNSServer dnsServer; // DNSServer dnsServer;
// long wifiPrevMillis = 0;
// bool wifiWasConnected = false;
// int8_t wifiReconnectAttempt = -20;
String wifiGetMacAddress(int start, const char * seperator) String wifiGetMacAddress(int start, const char * seperator)
{ {
byte mac[6]; byte mac[6];
WiFi.macAddress(mac); WiFi.macAddress(mac);
String cMac = ""; String cMac((char *)0);
cMac.reserve(127);
for(int i = start; i < 6; ++i) { for(int i = start; i < 6; ++i) {
if(mac[i] < 0x10) cMac += "0"; if(mac[i] < 0x10) cMac += "0";
cMac += String(mac[i], HEX); cMac += String(mac[i], HEX);
@ -62,9 +64,9 @@ void wifiConnected(IPAddress ipaddress)
{ {
bool isConnected = WiFi.status() == WL_CONNECTED; bool isConnected = WiFi.status() == WL_CONNECTED;
char buffer[127]; char buffer[127];
sprintf_P(buffer, PSTR("WIFI: Received IP address %s"), ipaddress.toString().c_str()); snprintf_P(buffer, sizeof(buffer), PSTR("WIFI: Received IP address %s"), ipaddress.toString().c_str());
debugPrintln(buffer); debugPrintln(buffer);
sprintf_P(buffer, PSTR("WIFI: Connected = %s"), isConnected ? PSTR("yes") : PSTR("no")); snprintf_P(buffer, sizeof(buffer), PSTR("WIFI: Connected = %s"), isConnected ? PSTR("yes") : PSTR("no"));
debugPrintln(buffer); debugPrintln(buffer);
if(isConnected) { if(isConnected) {
@ -77,7 +79,7 @@ void wifiConnected(IPAddress ipaddress)
void wifiDisconnected(const char * ssid, uint8_t reason) void wifiDisconnected(const char * ssid, uint8_t reason)
{ {
char buffer[127]; char buffer[127];
sprintf_P(buffer, PSTR("WIFI: Disconnected from %s (Reason: %d)"), ssid, reason); snprintf_P(buffer, sizeof(buffer), PSTR("WIFI: Disconnected from %s (Reason: %d)"), ssid, reason);
debugPrintln(buffer); debugPrintln(buffer);
WiFi.reconnect(); WiFi.reconnect();
} }
@ -85,7 +87,7 @@ void wifiDisconnected(const char * ssid, uint8_t reason)
void wifiSsidConnected(const char * ssid) void wifiSsidConnected(const char * ssid)
{ {
char buffer[127]; char buffer[127];
sprintf_P(buffer, PSTR("WIFI: Connected to SSID %s. Requesting IP..."), ssid); snprintf_P(buffer, sizeof(buffer), PSTR("WIFI: Connected to SSID %s. Requesting IP..."), ssid);
debugPrintln(buffer); debugPrintln(buffer);
} }
@ -156,7 +158,7 @@ void wifiSetup(JsonObject settings)
} }
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
sprintf_P(buffer, PSTR("WIFI: Connecting to : %s"), wifiSsid.c_str()); snprintf_P(buffer, sizeof(buffer), PSTR("WIFI: Connecting to : %s"), wifiSsid);
debugPrintln(buffer); debugPrintln(buffer);
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
@ -170,7 +172,7 @@ void wifiSetup(JsonObject settings)
WiFi.setSleep(false); WiFi.setSleep(false);
#endif #endif
WiFi.begin(wifiSsid.c_str(), wifiPassword.c_str()); WiFi.begin(wifiSsid, wifiPassword);
} }
bool wifiLoop() bool wifiLoop()
@ -208,12 +210,10 @@ bool wifiLoop()
bool wifiGetConfig(const JsonObject & settings) bool wifiGetConfig(const JsonObject & settings)
{ {
settings[FPSTR(F_CONFIG_SSID)] = String(wifiSsid.c_str()); settings[FPSTR(F_CONFIG_SSID)] = wifiSsid; // String(wifiSsid.c_str());
settings[FPSTR(F_CONFIG_PASS)] = String(wifiPassword.c_str()); settings[FPSTR(F_CONFIG_PASS)] = wifiPassword; // String(wifiPassword.c_str());
serializeJson(settings, Serial);
Serial.println();
configOutput(settings);
return true; return true;
} }
@ -223,27 +223,36 @@ bool wifiSetConfig(const JsonObject & settings)
{ {
bool changed = false; bool changed = false;
/* if(!settings[FPSTR(F_CONFIG_SSID)].isNull()) {
if(wifiSsid != settings[FPSTR(F_CONFIG_SSID)].as<String>().c_str()) {
debugPrintln(F("wifiSsid set"));
}
changed |= wifiSsid != settings[FPSTR(F_CONFIG_SSID)].as<String>().c_str();
wifiSsid = settings[FPSTR(F_CONFIG_SSID)].as<String>().c_str();
}
if(!settings[FPSTR(F_CONFIG_PASS)].isNull() && settings[FPSTR(F_CONFIG_PASS)].as<String>() != F("********")) {
if(wifiPassword != settings[FPSTR(F_CONFIG_PASS)].as<String>().c_str()) {
debugPrintln(F("wifiPassword set"));
}
changed |= wifiPassword != settings[FPSTR(F_CONFIG_PASS)].as<String>().c_str();
wifiPassword = settings[FPSTR(F_CONFIG_PASS)].as<String>().c_str();
}
*/
if(!settings[FPSTR(F_CONFIG_SSID)].isNull()) { if(!settings[FPSTR(F_CONFIG_SSID)].isNull()) {
if(wifiSsid != settings[FPSTR(F_CONFIG_SSID)].as<String>().c_str()) { changed |= strcmp(wifiSsid, settings[FPSTR(F_CONFIG_SSID)]) != 0;
debugPrintln(F("wifiSsid set")); strncpy(wifiSsid, settings[FPSTR(F_CONFIG_SSID)], sizeof(wifiSsid));
}
changed |= wifiSsid != settings[FPSTR(F_CONFIG_SSID)].as<String>().c_str();
wifiSsid = settings[FPSTR(F_CONFIG_SSID)].as<String>().c_str();
} }
if(!settings[FPSTR(F_CONFIG_PASS)].isNull() && settings[FPSTR(F_CONFIG_PASS)].as<String>() != F("********")) { if(!settings[FPSTR(F_CONFIG_PASS)].isNull()) {
if(wifiPassword != settings[FPSTR(F_CONFIG_PASS)].as<String>().c_str()) { changed |= strcmp(wifiPassword, settings[FPSTR(F_CONFIG_PASS)]) != 0;
debugPrintln(F("wifiPassword set")); strncpy(wifiPassword, settings[FPSTR(F_CONFIG_PASS)], sizeof(wifiPassword));
}
changed |= wifiPassword != settings[FPSTR(F_CONFIG_PASS)].as<String>().c_str();
wifiPassword = settings[FPSTR(F_CONFIG_PASS)].as<String>().c_str();
} }
serializeJson(settings, Serial); configOutput(settings);
Serial.println();
return changed; return changed;
} }