Skip to content

Commit

Permalink
Version 1.3.1
Browse files Browse the repository at this point in the history
- Fixed memory leak in hostname construction
- Moved to stack based JSON object declarations where possible
- Limited usage op String object as much as possible
  • Loading branch information
arjenhiemstra committed Nov 5, 2020
1 parent f50ec00 commit 4efb18f
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 153 deletions.
29 changes: 15 additions & 14 deletions software/NRG_itho_wifi/02_HTML.ino
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,16 @@ void handleDebug(AsyncWebServerRequest *request) {
response->print(CONFIG_VERSION);
response->print("<br><br></div>");
response->print("<div style='padding: 10px;background-color: black;background-image: radial-gradient(rgba(0, 150, 0, 0.55), black 140%);height: 60vh;} color: white; font: 0.9rem Inconsolata, monospace;border-radius: 10px;overflow:auto'>--- System Log ---<br>");
String link = "";
String linkcur = "";
char link[24] = "";
char linkcur[24] = "";

if ( SPIFFS.exists("/logfile0.current.log") ) {
linkcur = "/logfile0.current.log";
link = "/logfile1.log";
strlcpy(linkcur, "/logfile0.current.log", sizeof(linkcur));
strlcpy(link, "/logfile1.log", sizeof(link));
}
else {
linkcur = "/logfile1.current.log";
link = "/logfile0.log";
strlcpy(linkcur, "/logfile1.current.log", sizeof(linkcur));
strlcpy(link, "/logfile0.log", sizeof(link));
}

File file = SPIFFS.open(linkcur, FILE_READ);
Expand All @@ -90,23 +91,23 @@ void handleDebug(AsyncWebServerRequest *request) {
}

void handleCurLogDownload(AsyncWebServerRequest *request) {
String link = "";
char link[24] = "";
if ( SPIFFS.exists("/logfile0.current.log") ) {
link = "/logfile0.current.log";
strlcpy(link, "/logfile0.current.log", sizeof(link));
}
else {
link = "/logfile1.current.log";
strlcpy(link, "/logfile1.current.log", sizeof(link));
}
request->send(SPIFFS, link, String(), true);
request->send(SPIFFS, link, "", true);
}

void handlePrevLogDownload(AsyncWebServerRequest *request) {
String link = "";
char link[24] = "";
if ( SPIFFS.exists("/logfile0.current.log") ) {
link = "/logfile1.log";
strlcpy(link, "/logfile1.log", sizeof(link));
}
else {
link = "/logfile0.log";
strlcpy(link, "/logfile0.log", sizeof(link));
}
request->send(SPIFFS, link, String(), true);
request->send(SPIFFS, link, "", true);
}
4 changes: 2 additions & 2 deletions software/NRG_itho_wifi/04_JS_UI.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void handleGeneralJs(AsyncWebServerRequest *request) {
response->addHeader("Server", "Itho WiFi Web Server");

response->print("var on_ap = false; var hostname = '");
response->print(EspHostname());
response->print(hostName);
response->print("'; $(document).ready(function() { $('#headingindex').text(hostname); $('#headingindex').attr('href', 'http://' + hostname + '.local'); $('#main').append(html_index); });");

request->send(response);
Expand All @@ -21,7 +21,7 @@ void handleGeneralJsOnAp(AsyncWebServerRequest *request) {
response->addHeader("Server", "Itho WiFi Web Server");

response->print("var on_ap = true; var hostname = '");
response->print(EspHostname());
response->print(hostName);
response->print("'; $(document).ready(function() { $('#headingindex').text(hostname); $('#headingindex').attr('href', 'http://' + hostname + '.local'); $('#main').append(html_wifisetup); });");

request->send(response);
Expand Down
101 changes: 44 additions & 57 deletions software/NRG_itho_wifi/06_Websock_func.ino
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@



void jsonWsSend(String rootName, boolean nested) {
void jsonWsSend(char* rootName, boolean nested) {

if (rootName == "wifisettings") {
if (strcmp(rootName, "wifisettings") == 0) {
DynamicJsonDocument root(1024);
//JsonObject root = jsonBuffer.createObject();
JsonObject nested = root.createNestedObject("wifisettings");
Expand Down Expand Up @@ -33,7 +33,7 @@ void jsonWsSend(String rootName, boolean nested) {
ws.textAll(buffer);
}
}
else if (rootName == "mqttsettings") {
else if (strcmp(rootName, "mqttsettings") == 0) {
DynamicJsonDocument root(1024);
JsonObject nested = root.createNestedObject("mqttsettings");

Expand All @@ -60,59 +60,50 @@ void jsonWsSend(String rootName, boolean nested) {

}


// Convert & Transfer Arduino elements to JSON elements
void jsonWifiscanresult(int id, String ssid, String sigval, int sec) {
DynamicJsonDocument root(128);
void jsonWifiscanresult(int id, const char* ssid, int sigval, int sec) {
StaticJsonDocument<512> root;
//JsonObject root = jsonBuffer.createObject();
JsonObject wifiscanresult = root.createNestedObject("wifiscanresult");
wifiscanresult[F("id")] = id;
wifiscanresult[F("ssid")] = ssid;
wifiscanresult[F("sigval")] = sigval;
wifiscanresult[F("sec")] = sec;
size_t len = measureJson(root);
AsyncWebSocketMessageBuffer * buffer = ws.makeBuffer(len); // creates a buffer (len + 1) for you.
if (buffer) {
serializeJson(root, (char *)buffer->get(), len + 1);
ws.textAll(buffer);
}
wifiscanresult["id"] = id;
wifiscanresult["ssid"] = ssid;
wifiscanresult["sigval"] = sigval;
wifiscanresult["sec"] = sec;

char buffer[512];
size_t len = serializeJson(root, buffer);

ws.textAll(buffer, len);

}
// Convert & Transfer Arduino elements to JSON elements
void jsonMessageBox(String message1, String message2) {
DynamicJsonDocument root(128);
void jsonMessageBox(char* message1, char* message2) {
StaticJsonDocument<128> root;
//JsonObject root = jsonBuffer.createObject();
JsonObject messagebox = root.createNestedObject("messagebox");
messagebox[F("message1")] = message1;
messagebox[F("message2")] = message2;
size_t len = measureJson(root);
AsyncWebSocketMessageBuffer * buffer = ws.makeBuffer(len); // creates a buffer (len + 1) for you.
if (buffer) {
serializeJson(root, (char *)buffer->get(), len + 1);
ws.textAll(buffer);
}
messagebox["message1"] = message1;
messagebox["message2"] = message2;

char buffer[128];
size_t len = serializeJson(root, buffer);

ws.textAll(buffer, len);
}
// Convert & Transfer Arduino elements to JSON elements
void jsonSystemstat() {
DynamicJsonDocument root(256);
StaticJsonDocument<256> root;
//JsonObject root = jsonBuffer.createObject();
JsonObject systemstat = root.createNestedObject("systemstat");
systemstat["freemem"] = sys.getMemHigh();
systemstat["memlow"] = sys.getMemLow();
systemstat["mqqtstatus"] = MQTT_conn_state;
systemstat["itho"] = itho_current_val;
systemstat["i2cstat"] = i2cstat;

systemstat[F("freemem")] = String(memHigh);

systemstat[F("memlow")] = String(memLow);

systemstat[F("mqqtstatus")] = MQTT_conn_state;
char buffer[256];
size_t len = serializeJson(root, buffer);

systemstat[F("itho")] = itho_current_val;

systemstat[F("i2cstat")] = i2cstat;

size_t len = measureJson(root);
AsyncWebSocketMessageBuffer * buffer = ws.makeBuffer(len); // creates a buffer (len + 1) for you.
if (buffer) {
serializeJson(root, (char *)buffer->get(), len + 1);
ws.textAll(buffer);
}
ws.textAll(buffer, len);
}


Expand Down Expand Up @@ -316,7 +307,6 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT




void sendScanDataWs()
{
int n = WiFi.scanComplete();
Expand All @@ -326,10 +316,8 @@ void sendScanDataWs()
else if (n) {
sprintf(logBuff, "Wifi scan found %d networks", n);
logInput(logBuff);
jsonMessageBox(logBuff, "");
strcpy(logBuff, "");

jsonMessageBox("Scan found " + String(n), "WiFi Networks");

//sort networks
int indices[n];
for (int i = 0; i < n; i++) {
Expand Down Expand Up @@ -395,7 +383,7 @@ void sendScanDataWs()
logInput(logBuff);
strcpy(logBuff, "");

jsonWifiscanresult(i, String(WiFi.SSID(indices[i])), String(signalStrengthResult), sec);
jsonWifiscanresult(i, WiFi.SSID(indices[i]).c_str(), signalStrengthResult, sec);
}
WiFi.scanDelete();
if (WiFi.scanComplete() == -2) {
Expand All @@ -413,17 +401,16 @@ void otaWSupdate(size_t prg, size_t sz) {
if (newPercent != LastPercentotaWSupdate) {
LastPercentotaWSupdate = newPercent;
if (newPercent % 2 == 0) {
DynamicJsonDocument root(256);
StaticJsonDocument<256> root;
JsonObject ota = root.createNestedObject("ota");
ota[F("progress")] = String(prg);
ota[F("tsize")] = String(content_len);
ota[F("percent")] = String(newPercent);
size_t len = measureJson(root);
AsyncWebSocketMessageBuffer * buffer = ws.makeBuffer(len); // creates a buffer (len + 1) for you.
if (buffer) {
serializeJson(root, (char *)buffer->get(), len + 1);
ws.textAll(buffer);
}
ota["progress"] = prg;
ota["tsize"] = content_len;
ota["percent"] = newPercent;

char buffer[256];
size_t len = serializeJson(root, buffer);

ws.textAll(buffer, len);
}
}

Expand Down
35 changes: 9 additions & 26 deletions software/NRG_itho_wifi/09_init_code.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ bool initFileSystem() {
//Serial.println("fs_info failed");
return false;
}
else {
FSTotal = fs_info.totalBytes;
FSUsed = fs_info.usedBytes;
//Serial.println("File System loaded");
//Serial.print("Disk size: ");Serial.println(FSTotal);
//Serial.print("Disk used: ");Serial.println(FSUsed);
}
}


Expand Down Expand Up @@ -65,25 +58,15 @@ void handleFormat()
}
}

char * EspHostname() {

void setEspHostname(char* hostName) {
// Do a little work to get a unique-ish name. Append the
// last two bytes of the MAC (HEX'd):
uint8_t mac[6];
WiFi.softAPmacAddress(mac);

sprintf(hostName, "%s%02x%02x", espName, mac[6 - 2], mac[6 - 1]);

String macID = String(mac[6 - 2], HEX) +
String(mac[6 - 1], HEX);
macID.toUpperCase();
String AP_NameString = espName + macID;

char *AP_NameChar = (char *) malloc(sizeof(char) * (AP_NameString.length() + 1));
//char AP_NameChar[AP_NameString.length() + 1];
//memset(AP_NameChar, 0, AP_NameString.length() + 1);

for (int i = 0; i < AP_NameString.length(); i++)
AP_NameChar[i] = AP_NameString.charAt(i);

return AP_NameChar;
}

void setupWiFiAP() {
Expand All @@ -99,7 +82,7 @@ void setupWiFiAP() {
WiFi.persistent(true);

WiFi.softAPConfig(apIP, apIP, netMsk);
WiFi.softAP(EspHostname(), WiFiAPPSK);
WiFi.softAP(hostName, WiFiAPPSK);

delay(500);

Expand Down Expand Up @@ -152,11 +135,11 @@ bool connectWiFiSTA()
}

#if defined(ESP8266)
WiFi.hostname(EspHostname());
WiFi.hostname(hostName);
WiFi.begin(wifiConfig.ssid, wifiConfig.passwd);
#else
WiFi.begin(wifiConfig.ssid, wifiConfig.passwd);
WiFi.setHostname(EspHostname());
WiFi.setHostname(hostName);
#endif

int i = 0;
Expand Down Expand Up @@ -194,10 +177,10 @@ bool setupMQTTClient() {
mqttClient.setBufferSize(1024);

if (systemConfig.mqtt_username == "") {
connectResult = mqttClient.connect(EspHostname());
connectResult = mqttClient.connect(hostName);
}
else {
connectResult = mqttClient.connect(EspHostname(), systemConfig.mqtt_username, systemConfig.mqtt_password);
connectResult = mqttClient.connect(hostName, systemConfig.mqtt_username, systemConfig.mqtt_password);
}

if (!connectResult) {
Expand Down
Loading

0 comments on commit 4efb18f

Please sign in to comment.