Nice Imp power-up message someone wrote

At one time, I saw a nice function someone wrote that displays information about the Imp when it powers on. I had put it at the bottom of a device code. It showed imp firmware version, wifi strength, etc. The status (information) was displayed in the server.log.

Does anyone know where that is? I can’t find it. I don’t remember who authored it.

Is it this one (remember seeing this a couple weeks ago) in a forum post.

That works for me. I added the WiFi signal strength too.
Thanks.

Note that in some scenarios e.g. wake from timer and squirrel has already called setsendtimeoutpolicy(RETURN_ON_ERROR, …) the imp may be disconnected when bootMessage() is called.

In these cases the first server.log() will return an error and you will get an ‘index not found’ exception for i.active (because there is no active connection).

If you’re coding those scenarios you probably already knew that though :slight_smile:

I’m not using it as a function. I copied just the inside part of that function and I just stuck it at the bottom of my device code and it displays whenever the imp is powered on, or “build/run”. Since it shows on the log, I can view it later if I need to know.

FWIW, I updated this a while back. Here’s the latest version, which I usually #include quite late in the run:

// Boot device information functions
function bootMessage() {
    // Present OS version and network connection information
    local a = split(imp.getsoftwareversion(), "-");
    server.log("impOS version " + a[2]);
    local i = imp.net.info();
    local w = i.interface[i.active != null ? i.active : 0];
    local s = w.type == "wifi" ? ("connectedssid" in w ? w.connectedssid : w.ssid) : "";
    local t = "Connected by " + (w.type == "wifi" ? "WiFi on SSID \"" + s + "\"" : "Ethernet");
    server.log(t + " with IP address " + i.ipv4.address);

    // Present the reason for the start-up
    s = logWokenReason();
    if (s.len() > 0) server.log(s);
}

function logWokenReason() {
    // Return the recorded reason for the device’s start-up
    local reason = "";
    local causes = [ "Cold boot", "Woken after sleep", "Software reset", "Wakeup pin triggered",
                     "Application code updated", "Squirrel error during the last run"
                     "This device has a new impOS", "Woken by a snooze-and-retry event",
                     "imp003 Reset pin triggered", "This device has just been re-configured",
                     "Restarted by server.restart()" ];
    try {
        reason = "Device restarted: " + causes[hardware.wakereason()];
    } catch (err) {
        reason = "Device restarted: Reason unknown";
    }

    return reason;
}

// Present device information on boot
bootMessage();
1 Like

Really nice!
This should be in the sample code section.

For those interested in this … below is @Smittytone code with WiFi signal strength added.

// Boot device information functions
function bootMessage() {
    // Present OS version and network connection information
    local a = split(imp.getsoftwareversion(), "-");
    server.log("impOS version " + a[2]);
    local i = imp.net.info();
    local w = i.interface[i.active != null ? i.active : 0];
    local s = w.type == "wifi" ? ("connectedssid" in w ? w.connectedssid : w.ssid) : "";
    local t = "Connected by " + (w.type == "wifi" ? "WiFi on SSID \"" + s + "\"" : "Ethernet");
    server.log(t + " with IP address " + i.ipv4.address);

    // WiFi Signal Strength
    local rssi = imp.rssi();
    local bars = 5;

    if (rssi < -87) {
        bars = 0;
    } else if (rssi < -82) {
        bars = 1;
    } else if (rssi < -77) {
        bars = 2;
    } else if (rssi < -72) {
        bars = 3;
    } else if (rssi < -67) {
        bars = 4;
    }

    if (bars == 1) {
        server.log("Signal Strength: " + rssi + "dBm (1 bar)");
    } else {
        server.log("Signal Strength: " + rssi + "dBm (" + bars + " bars)");
    }
    
    // Present the reason for the start-up
    s = logWokenReason();
    if (s.len() > 0) server.log(s);
}

function logWokenReason() {
    // Return the recorded reason for the device’s start-up
    local reason = "";
    local causes = [ "Cold boot", "Woken after sleep", "Software reset", "Wakeup pin triggered",
                     "Application code updated", "Squirrel error during the last run"
                     "This device has a new impOS", "Woken by a snooze-and-retry event",
                     "imp003 Reset pin triggered", "This device has just been re-configured",
                     "Restarted by server.restart()" ];
    try {
        reason = "Device restarted: " + causes[hardware.wakereason()];
    } catch (err) {
        reason = "Device restarted: Reason unknown";
    }

    return reason;
}

// Present device information on boot
bootMessage();