Skip to content

Instantly share code, notes, and snippets.

@ElectricImpSampleCode
Last active March 18, 2021 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElectricImpSampleCode/4c644050e872b23d0657 to your computer and use it in GitHub Desktop.
Save ElectricImpSampleCode/4c644050e872b23d0657 to your computer and use it in GitHub Desktop.
A simple function which uses hardware.wakereason() to reveal why an imp restarted.
function logWokenReason() {
local s = "Device restarted. Reason: ";
local c = hardware.wakereason();
switch (c) {
case WAKEREASON_POWER_ON: // 0
s += "Cold boot";
break;
case WAKEREASON_TIMER: // 1
s += "Timer woke the imp";
break;
case WAKEREASON_SW_RESET: // 2
s += "Software reset or out-of-memory error";
break;
case WAKEREASON_PIN: // 3
s += "Wakeup pin asserted";
break;
case WAKEREASON_NEW_SQUIRREL: // 4
s += "Application code updated";
break;
case WAKEREASON_SQUIRREL_ERROR: // 5
s += "Squirrel error during the last run";
break;
case WAKEREASON_NEW_FIRMWARE: // 6
s += "Device has a new impOS";
break;
case WAKEREASON_SNOOZE: // 7
s += "Woken from a snooze-and-retry event";
break;
case WAKEREASON_HW_RESET: // 8
// Requires imp003 or above
s += "RESET pin asserted";
break;
case WAKEREASON_BLINKUP: // 9
s += "Device's network settings have been re-configured";
break;
case WAKEREASON_SW_RESTART: // 10
s += "Device's application called server.restart()";
break;
default:
s += "Other (code: " + c + ")";
}
server.log(s);
}
logWokenReason();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment