Skip to content

Commit

Permalink
Fixing a few regressions where setting up a brand new device with
Browse files Browse the repository at this point in the history
the firmware would cause it to not be actually configurable! (opps)
Also, implemented web-based viewing of device logs and upping
version to 1.2.0!
  • Loading branch information
coogle committed Sep 18, 2017
1 parent fe676cd commit f413eef
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 65 deletions.
2 changes: 1 addition & 1 deletion library.properties
@@ -1,5 +1,5 @@
name=CoogleIOT
version=1.1.1
version=1.2.0
author=John Coggeshall <john@coggeshall.org>
maintainer=John Coggeshall <john@coggeshall.org>
sentence=An IOT library for ESP8266-12 to provide WiFi Configuration, MQTT Client, OTA updates and more.
Expand Down
151 changes: 90 additions & 61 deletions src/CoogleIOT.cpp
Expand Up @@ -196,10 +196,38 @@ CoogleIOT& CoogleIOT::critical(String msg)
return log(msg, CRITICAL);
}

File& CoogleIOT::getLogFile()
{
return logFile;
}

String CoogleIOT::getLogs()
{
return CoogleIOT::getLogs(false);
}

String CoogleIOT::getLogs(bool asHTML)
{
String retval;

if(!logFile || !logFile.size()) {
return retval;
}

logFile.seek(0, SeekSet);

while(logFile.available()) {
retval += (char)logFile.read();
}

logFile.seek(0, SeekEnd);

return retval;
}

CoogleIOT& CoogleIOT::log(String msg, CoogleIOT_LogSeverity severity)
{
String logMsg = buildLogMsg(msg, severity);
FSInfo logFileInfo;

if(_serial) {
Serial.println(logMsg);
Expand All @@ -209,12 +237,11 @@ CoogleIOT& CoogleIOT::log(String msg, CoogleIOT_LogSeverity severity)
return *this;
}

SPIFFS.info(logFileInfo);
if((logFile.size() + msg.length()) > COOGLEIOT_LOGFILE_MAXSIZE) {

if((logFileInfo.totalBytes + msg.length()) > COOGLEIOT_LOGFILE_MAXSIZE) {
logFile.close();
SPIFFS.remove(COOGLEIOT_SPIFFS_LOGFILE);
logFile = SPIFFS.open(COOGLEIOT_SPIFFS_LOGFILE, "w");
logFile = SPIFFS.open(COOGLEIOT_SPIFFS_LOGFILE, "a+");

if(!logFile) {
if(_serial) {
Expand All @@ -237,6 +264,7 @@ bool CoogleIOT::serialEnabled()
void CoogleIOT::loop()
{
struct tm* p_tm;
String remoteAPName;

if(sketchTimerTick) {
sketchTimerTick = false;
Expand All @@ -246,7 +274,6 @@ void CoogleIOT::loop()
if(heartbeatTick) {
heartbeatTick = false;
flashStatus(100, 1);
debug("Heartbeat Tick");

if(wifiFailuresCount > COOGLEIOT_MAX_WIFI_ATTEMPTS) {
info("Failed too many times to establish a WiFi connection. Restarting Device.");
Expand All @@ -256,74 +283,80 @@ void CoogleIOT::loop()
}

if(WiFi.status() != WL_CONNECTED) {
info("Not connected to WiFi. Attempting reconnection.");
if(!connectToSSID()) {
wifiFailuresCount++;
logPrintf(INFO, "Attempt %d failed. Will attempt %d times before restarting.", wifiFailuresCount, COOGLEIOT_MAX_WIFI_ATTEMPTS);
return;

remoteAPName = getRemoteAPName();

if(remoteAPName.length() > 0) {
info("Not connected to WiFi. Attempting reconnection.");
if(!connectToSSID()) {
wifiFailuresCount++;
logPrintf(INFO, "Attempt %d failed. Will attempt %d times before restarting.", wifiFailuresCount, COOGLEIOT_MAX_WIFI_ATTEMPTS);
return;
}
}

}
} else {

wifiFailuresCount = 0;
wifiFailuresCount = 0;

if(mqttClientActive) {
if(!mqttClient->connected()) {
yield();
if(!connectToMQTT()) {
flashSOS();
}
}

yield();
mqttClient->loop();
}

yield();
webServer->loop();

yield();
if(dnsServerActive) {
dnsServer.processNextRequest();
}
if(mqttClientActive) {
yield();
mqttClient->loop();
}

if(ntpClientActive) {
now = time(nullptr);
if(ntpClientActive) {
now = time(nullptr);

if(now) {
p_tm = localtime(&now);
if(now) {
p_tm = localtime(&now);

if( (p_tm->tm_hour == 12) &&
(p_tm->tm_min == 0) &&
(p_tm->tm_sec == 6)) {
yield();
syncNTPTime(COOGLEIOT_TIMEZONE_OFFSET, COOGLEIOT_DAYLIGHT_OFFSET);
if( (p_tm->tm_hour == 12) &&
(p_tm->tm_min == 0) &&
(p_tm->tm_sec == 6)) {
yield();
syncNTPTime(COOGLEIOT_TIMEZONE_OFFSET, COOGLEIOT_DAYLIGHT_OFFSET);
}
}
}
}

if(firmwareUpdateTick) {
firmwareUpdateTick = false;

checkForFirmwareUpdate();

if(_serial) {
switch(firmwareUpdateStatus) {
case HTTP_UPDATE_FAILED:
warn("Warning! Failed to update firmware with specified URL");
break;
case HTTP_UPDATE_NO_UPDATES:
info("Firmware update check completed - at current version");
break;
case HTTP_UPDATE_OK:
info("Firmware Updated!");
break;
default:
warn("Warning! No updated performed. Perhaps an invalid URL?");
break;
if(firmwareUpdateTick) {
firmwareUpdateTick = false;

checkForFirmwareUpdate();

if(_serial) {
switch(firmwareUpdateStatus) {
case HTTP_UPDATE_FAILED:
warn("Warning! Failed to update firmware with specified URL");
break;
case HTTP_UPDATE_NO_UPDATES:
info("Firmware update check completed - at current version");
break;
case HTTP_UPDATE_OK:
info("Firmware Updated!");
break;
default:
warn("Warning! No updated performed. Perhaps an invalid URL?");
break;
}
}
}
}

yield();
webServer->loop();

yield();
if(dnsServerActive) {
dnsServer.processNextRequest();
}

}

Expand Down Expand Up @@ -450,9 +483,9 @@ bool CoogleIOT::initialize()
pinMode(_statusPin, OUTPUT);
flashStatus(COOGLEIOT_STATUS_INIT);
}

info("Coogle IOT v" COOGLEIOT_VERSION " initializing..");

verifyFlashConfiguration();

randomSeed(micros());
Expand All @@ -470,7 +503,7 @@ bool CoogleIOT::initialize()
SPIFFS.format();
}

logFile = SPIFFS.open(COOGLEIOT_SPIFFS_LOGFILE, "w");
logFile = SPIFFS.open(COOGLEIOT_SPIFFS_LOGFILE, "a+");

if(!logFile) {
error("Could not open SPIFFS log file!");
Expand All @@ -491,10 +524,6 @@ bool CoogleIOT::initialize()

if(!connectToSSID()) {
error("Failed to connect to remote AP");

restartDevice();
return false;

} else {

syncNTPTime(COOGLEIOT_TIMEZONE_OFFSET, COOGLEIOT_DAYLIGHT_OFFSET);
Expand Down Expand Up @@ -1097,7 +1126,7 @@ CoogleIOT& CoogleIOT::enableSerial(int baud)
Serial.begin(baud);

while(!Serial) {
/* .... tic toc .... */
yield();
}

}
Expand Down
3 changes: 3 additions & 0 deletions src/CoogleIOT.h
Expand Up @@ -124,6 +124,9 @@ class CoogleIOT
CoogleIOT& registerTimer(int, sketchtimer_cb_t);

String buildLogMsg(String, CoogleIOT_LogSeverity);
String getLogs(bool);
String getLogs();
File& getLogFile();

bool mqttActive();
bool dnsActive();
Expand Down
4 changes: 2 additions & 2 deletions src/CoogleIOTConfig.h
Expand Up @@ -3,14 +3,14 @@

//#define COOGLEIOT_DEBUG

#define COOGLEIOT_VERSION "1.1.1"
#define COOGLEIOT_VERSION "1.2.0"

#ifndef COOGLEIOT_SPIFFS_LOGFILE
#define COOGLEIOT_SPIFFS_LOGFILE "/coogleiot-log.txt"
#endif

#ifndef COOGLEIOT_LOGFILE_MAXSIZE
#define COOGLEIOT_LOGFILE_MAXSIZE 5120
#define COOGLEIOT_LOGFILE_MAXSIZE 32768 // 32k
#endif

#ifndef COOGLEIOT_STATUS_INIT
Expand Down
15 changes: 14 additions & 1 deletion src/CoogleIOTWebserver.cpp
Expand Up @@ -64,6 +64,7 @@ CoogleIOTWebserver& CoogleIOTWebserver::initializePages()
webServer->on("/reset", std::bind(&CoogleIOTWebserver::handleReset, this));
webServer->on("/restart", std::bind(&CoogleIOTWebserver::handleRestart, this));
webServer->on("/jquery", std::bind(&CoogleIOTWebserver::handleJS, this));
webServer->on("/logs", std::bind(&CoogleIOTWebserver::handleLogs, this));

webServer->on("/api/status", std::bind(&CoogleIOTWebserver::handleApiStatus, this));
webServer->on("/api/reset", std::bind(&CoogleIOTWebserver::handleApiReset, this));
Expand Down Expand Up @@ -165,7 +166,8 @@ void CoogleIOTWebserver::handleRoot()
String page(FPSTR(WEBPAGE_Home));
String ap_name, ap_password, ap_remote_name, ap_remote_password,
mqtt_host, mqtt_username, mqtt_password, mqtt_client_id,
firmware_url, mqtt_port, local_ip, mac_address, wifi_status;
firmware_url, mqtt_port, local_ip, mac_address, wifi_status,
logs;

ap_name = iot->getAPName();
ap_password = iot->getAPPassword();
Expand Down Expand Up @@ -221,6 +223,17 @@ void CoogleIOTWebserver::handle404()
webServer->send_P(404, "text/html", WEBPAGE_NOTFOUND);
}

void CoogleIOTWebserver::handleLogs()
{
File logFile;

logFile = iot->getLogFile();

logFile.seek(0, SeekSet);
webServer->streamFile(logFile, "text/html");
logFile.seek(0, SeekEnd);
}

void CoogleIOTWebserver::handleFirmwareUploadResponse()
{
if(_manualFirmwareUpdateSuccess) {
Expand Down
1 change: 1 addition & 0 deletions src/CoogleIOTWebserver.h
Expand Up @@ -62,6 +62,7 @@ class CoogleIOTWebserver
void handleRestart();
void handleFirmwareUpload();
void handleFirmwareUploadResponse();
void handleLogs();

void handleApiStatus();
void handleApiReset();
Expand Down
18 changes: 18 additions & 0 deletions src/webpages/home.h
Expand Up @@ -148,12 +148,30 @@ const char WEBPAGE_Home[] PROGMEM = R"=====(
</tbody>
</table>
</div>
<input type="radio" name="navtabs" id="tab5" aria-hidden="true">
<label for="tab5" aria-hidden="true">Logs</label>
<div style="height: 600px;">
<div style="text-align: right;"><button class="primary" type="button" id="refreshLogBtn">Refresh Log</button></div>
<hr/>
<pre id="logContent" style="overflow-y: scroll; max-height: 470px; height: 470px;"></pre>
</div>
</div>
<button class="primary bordered" style="width: 100%" id="saveBtn">Save and Restart</button>
<script src="/jquery"></script>
<script>
$(document).ready(function() {

var loadLog = function()
{
$.get('/logs', function(result) {
$('#logContent').html(result);
$('#logContent').scrollTop($('#logContent')[0].scrollHeight);
});
};

$('#tab5').on('click', loadLog);
$('#refreshLogBtn').on('click', loadLog);

$('#resetEEPROMBtn').on('click', function(e) {
window.location.href = '/reset';
});
Expand Down
22 changes: 22 additions & 0 deletions src/webpages/home.html
Expand Up @@ -120,12 +120,34 @@ <h3>System Commands</h3>
</tbody>
</table>
</div>
<input type="radio" name="navtabs" id="tab5" aria-hidden="true">
<label for="tab5" aria-hidden="true">Logs</label>
<div style="height: 600px">
<div style="text-align: right;"><button class="primary" type="button" id="refreshLogBtn">Refresh Log</button></div>
<hr/>
<div id="logContent"></div>
</div>
</div>
<button class="primary bordered" style="width: 100%" id="saveBtn">Save and Restart</button>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$(document).ready(function() {

var loadLog = function()
{
$.get('/logs', function(result) {

result = result.trim();

result = (result.length > 0 ? '<p>' + result.replace(/[\r\n]+/,'</p><p>') + '</p>' : '');

$('#logContent').html(result);
});
};

$('#tab5').on('click', loadLog);
$('#refreshLogBtn').on('click', loadLog);

$('#reloadBtn').on('click', function(e) {
location.href = '/restart';
});
Expand Down

0 comments on commit f413eef

Please sign in to comment.