Skip to content

Commit

Permalink
Version 2.2-beta
Browse files Browse the repository at this point in the history
HW Rev 2:
- Spilt program in multiple tasks, this makes it easier to spot issues
- Stale tasks at least should result in a log entry
- Include reset reason in human readable format

All HW revs:
- Prevent memory corruption in MQTT PubSubClient lib (issue knolleary/pubsubclient#832)
- Switch to MQTT PubSubClient lib without dynamic memory allocation
- MQTT subscribed topic buffer max size
- MQTT Dynamic JSON to Static JSON buffer
- Logging buffer as local short lived vars
- Config save and reset moved to looptask
- More robust wifi scan code
- Monitor heap largest block free
- Added build instructions

This release contains much changes, especially for HW revision 2. Please test and report any issues. If this release is stable I will push it as stable release.
  • Loading branch information
arjenhiemstra committed Feb 19, 2021
1 parent 66a3ed8 commit e7558ba
Show file tree
Hide file tree
Showing 21 changed files with 1,071 additions and 504 deletions.
8 changes: 4 additions & 4 deletions compiled_firmware_files/firmware.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"hw_rev": {
"1": {
"latest_fw":"2.1",
"link":"https://github.com/arjenhiemstra/ithowifi/raw/master/compiled_firmware_files/hardware_rev_1/NRG_itho_wifi_HW1x_FW2.1.bin"
"latest_fw":"2.2-beta",
"link":"https://github.com/arjenhiemstra/ithowifi/raw/master/compiled_firmware_files/hardware_rev_1/NRG_itho_wifi_HW1x_FW2.2-beta.bin"
},
"2": {
"latest_fw":"2.1",
"link":"https://github.com/arjenhiemstra/ithowifi/raw/master/compiled_firmware_files/hardware_rev_2/NRG_itho_wifi_HW2x_FW2.1.bin"
"latest_fw":"2.2-beta",
"link":"https://github.com/arjenhiemstra/ithowifi/raw/master/compiled_firmware_files/hardware_rev_2/NRG_itho_wifi_HW2x_FW2.2-beta.bin"
}
}
}
Binary file not shown.
Binary file not shown.
23 changes: 20 additions & 3 deletions software/NRG_itho_wifi/02_HTML.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void handleAPI(AsyncWebServerRequest *request) {

int params = request->params();
for(int i=0;i<params;i++){
char logBuff[LOG_BUF_SIZE] = "";
AsyncWebParameter* p = request->getParam(i);

if(strcmp(p->name().c_str(), "get") == 0) {
Expand Down Expand Up @@ -218,7 +219,7 @@ void handleDebug(AsyncWebServerRequest *request) {
response->print(F("<div>Config version: "));
response->print(CONFIG_VERSION);
response->print(F("<br><br><span>Itho I2C connection status: </span><span id=\'i2cstat\'>unknown</span></div>"));
response->print(F("<span>File system: </span><span>"));
response->print(F("<br><span>File system: </span><span>"));
#if defined (__HW_VERSION_ONE__)
SPIFFS.info(fs_info);
response->print(fs_info.usedBytes);
Expand All @@ -230,8 +231,24 @@ void handleDebug(AsyncWebServerRequest *request) {
response->print(fs_info.totalBytes);
#elif defined (__HW_VERSION_TWO__)
response->print(SPIFFS.totalBytes());
#endif
response->print(F(" bytes total</span></div>"));
response->print(F(" bytes total</span>"));

response->print(F("<br><br><span>CC1101 task memory: </span><span>"));
response->print(TaskCC1101HWmark);
response->print(F(" bytes free</span>"));
response->print(F("<br><span>MQTT task memory: </span><span>"));
response->print(TaskMQTTHWmark);
response->print(F(" bytes free</span>"));
response->print(F("<br><span>Web task memory: </span><span>"));
response->print(TaskWebHWmark);
response->print(F(" bytes free</span>"));
response->print(F("<br><span>Config and Log task memory: </span><span>"));
response->print(TaskConfigAndLogHWmark);
response->print(F(" bytes free</span>"));
response->print(F("<br><span>SysControl task memory: </span><span>"));
response->print(TaskSysControlHWmark);
response->print(F(" bytes free</span></div>"));
#endif
response->print(F("<br><br><div id='syslog_outer'><div style='display:inline-block;vertical-align:top;overflow:hidden;padding-bottom:5px;'>System Log:</div>"));


Expand Down
149 changes: 79 additions & 70 deletions software/NRG_itho_wifi/06_Websock_func.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@



void notifyClients(AsyncWebSocketMessageBuffer* message) {
#if defined (__HW_VERSION_TWO__)
yield();
if (xSemaphoreTake(mutexWSsend, (TickType_t) 100 / portTICK_PERIOD_MS) == pdTRUE) {
#endif

ws.textAll(message);

#if defined (__HW_VERSION_TWO__)
xSemaphoreGive(mutexWSsend);
}
#endif

}

void notifyClients(const char * message, size_t len) {

AsyncWebSocketMessageBuffer * WSBuffer = ws.makeBuffer((uint8_t *)message, len);
notifyClients(WSBuffer);

}

void jsonWsSend(const char* rootName) {
DynamicJsonDocument root(4000);

Expand Down Expand Up @@ -41,34 +65,18 @@ void jsonWsSend(const char* rootName) {
AsyncWebSocketMessageBuffer * buffer = ws.makeBuffer(len); // creates a buffer (len + 1) for you.
if (buffer) {
serializeJson(root, (char *)buffer->get(), len + 1);
ws.textAll(buffer);
notifyClients(buffer);
}
}


void jsonWifiscanresult(int id, const char* ssid, int sigval, int sec) {
StaticJsonDocument<512> root;
//JsonObject root = jsonBuffer.createObject();
JsonObject wifiscanresult = root.createNestedObject("wifiscanresult");
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);

}

void jsonLogMessage(const __FlashStringHelper * str, logtype type) {
if (!str) return;
int length = strlen_P((PGM_P)str);
if (length == 0) return;
#if defined (__HW_VERSION_ONE__)
#if defined (__HW_VERSION_ONE__)
if (length < 400) length = 400;
char message[400+1] = "";
char message[400 + 1] = "";
strncat_P(message, (PGM_P)str, length);
jsonLogMessage(message, type);
#else
Expand All @@ -77,26 +85,38 @@ void jsonLogMessage(const __FlashStringHelper * str, logtype type) {
}

void jsonLogMessage(const char* message, logtype type) {
StaticJsonDocument<512> root;
JsonObject messagebox;

switch (type) {
case RFLOG:
messagebox = root.createNestedObject("rflog");
break;
default:
messagebox = root.createNestedObject("messagebox");
#if defined (__HW_VERSION_TWO__)
yield();
if (xSemaphoreTake(mutexJSONLog, (TickType_t) 500 / portTICK_PERIOD_MS) == pdTRUE) {
#endif

StaticJsonDocument<512> root;
JsonObject messagebox;

switch (type) {
case RFLOG:
messagebox = root.createNestedObject("rflog");
break;
default:
messagebox = root.createNestedObject("messagebox");
}

messagebox["message"] = message;

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


notifyClients(buffer, len);

#if defined (__HW_VERSION_TWO__)
xSemaphoreGive(mutexJSONLog);
}
#endif

messagebox["message"] = message;

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


ws.textAll(buffer, len);


}

void jsonSystemstat() {
Expand All @@ -118,7 +138,7 @@ void jsonSystemstat() {
char buffer[512];
size_t len = serializeJson(root, buffer);

ws.textAll(buffer, len);
notifyClients(buffer, len);
}


Expand Down Expand Up @@ -170,12 +190,7 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT

JsonObject obj = p.value();
if (systemConfig.set(obj)) {
if (saveSystemConfig()) {
jsonLogMessage(F("System settings saved successful"), WEBINTERFACE);
}
else {
jsonLogMessage(F("System settings save failed: Unable to write config file"), WEBINTERFACE);
}
saveSystemConfigflag = true;
}
}
}
Expand All @@ -184,12 +199,7 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT

JsonObject obj = p.value();
if (wifiConfig.set(obj)) {
if (saveWifiConfig()) {
jsonLogMessage(F("Wifi settings saved successful, reboot the device"), WEBINTERFACE);
}
else {
jsonLogMessage(F("Wifi settings save failed: Unable to write config file"), WEBINTERFACE);
}
saveWifiConfigflag = true;
}
}
}
Expand Down Expand Up @@ -230,7 +240,7 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
}
if (parseOK) {
remotes.removeRemote(remotes.getRemoteIDbyIndex(number));
saveRemotes = true;
saveRemotesflag = true;
}
}
else if (msg.startsWith("{\"itho_update_remote")) {
Expand All @@ -241,28 +251,18 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT
char remoteName[32];
strlcpy(remoteName, root["value"] | "", sizeof(remoteName));
remotes.updateRemoteName(index, remoteName);
saveRemotes = true;
saveRemotesflag = true;
}
}
#endif
else if (msg.startsWith("{\"reboot")) {
shouldReboot = true;
}
else if (msg.startsWith("{\"resetwificonf")) {
if (resetWifiConfig()) {
jsonLogMessage(F("Wifi settings restored, reboot the device"), WEBINTERFACE);
}
else {
jsonLogMessage(F("Wifi settings restore failed, please try again"), WEBINTERFACE);
}
resetWifiConfigflag = true;
}
else if (msg.startsWith("{\"resetsysconf")) {
if (resetSystemConfig()) {
jsonLogMessage(F("System settings restored, reboot the device"), WEBINTERFACE);
}
else {
jsonLogMessage(F("System settings restore failed, please try again"), WEBINTERFACE);
}
resetSystemConfigflag = true;
}
else if (msg.startsWith("{\"itho")) {
StaticJsonDocument<128> root;
Expand All @@ -279,17 +279,17 @@ void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventT



void sendScanDataWs()
{
void wifiScan() {
int n = WiFi.scanComplete();
if (n == -2) {
WiFi.scanNetworks(true);
}
else if (n) {
char logBuff[LOG_BUF_SIZE] = "";
sprintf(logBuff, "Wifi scan found %d networks", n);
logInput(logBuff);
jsonLogMessage(logBuff, WEBINTERFACE);
strcpy(logBuff, "");

//sort networks
int indices[n];
for (int i = 0; i < n; i++) {
Expand Down Expand Up @@ -351,11 +351,19 @@ void sendScanDataWs()
String ssidStr = WiFi.SSID(indices[i]);
ssidStr.toCharArray(ssid, sizeof(ssid));

sprintf(logBuff, "%d - SSID:%s, Signal:%d, Security: %d", i, ssid, signalStrengthResult, sec);
logInput(logBuff);
strcpy(logBuff, "");
StaticJsonDocument<512> root;
JsonObject wifiscanresult = root.createNestedObject("wifiscanresult");
wifiscanresult["id"] = i;
wifiscanresult["ssid"] = ssid;
wifiscanresult["sigval"] = signalStrengthResult;
wifiscanresult["sec"] = sec;

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

jsonWifiscanresult(i, WiFi.SSID(indices[i]).c_str(), signalStrengthResult, sec);
notifyClients(buffer, len);

delay(50);
}
WiFi.scanDelete();
if (WiFi.scanComplete() == -2) {
Expand All @@ -382,7 +390,8 @@ void otaWSupdate(size_t prg, size_t sz) {
char buffer[256];
size_t len = serializeJson(root, buffer);

ws.textAll(buffer, len);
notifyClients(buffer, len);
delay(50);
}
}

Expand Down
Loading

0 comments on commit e7558ba

Please sign in to comment.