Hi everyone,
I’ve had an electricimp controlling a relay for about 3 years, running the same code without any issue. Recently I had to update the code to remove a couple of deprecated/not-supported-anymore calls and ever since a made the changes, I’m seeing random restarts, pointing the reason is memory error.
I’ve gone through the code many times, but I don’t seem to find anything that could be causing this (maybe an infinite recursion, or some excess memory allocation?).
This is an excerpt of the log; there is a suspicious amount of reconnects, likely those are also resets
2015-11-20 23:30:17 UTC+1 [Status] Device connected 2015-11-20 23:30:17 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-20 23:30:48 UTC+1 [Exit Code] imp restarted, reason: memory error 2015-11-20 23:30:48 UTC+1 [Status] Device connected 2015-11-20 23:30:48 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-20 23:31:52 UTC+1 [Status] Device connected 2015-11-20 23:31:52 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-20 23:35:08 UTC+1 [Status] Device connected 2015-11-20 23:35:09 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-20 23:37:04 UTC+1 [Status] Device connected 2015-11-20 23:37:04 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-21 00:00:45 UTC+1 [Status] Device connected 2015-11-21 00:00:45 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-21 00:03:31 UTC+1 [Status] Device connected 2015-11-21 00:03:31 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-21 00:04:49 UTC+1 [Status] Device connected 2015-11-21 00:04:49 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-21 00:05:04 UTC+1 [Status] Device connected 2015-11-21 00:05:04 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-21 00:05:55 UTC+1 [Status] Device connected 2015-11-21 00:05:56 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-21 00:06:31 UTC+1 [Status] Device connected 2015-11-21 00:06:31 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-21 00:07:32 UTC+1 [Status] Device connected 2015-11-21 00:07:32 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-21 00:07:45 UTC+1 [Exit Code] imp restarted, reason: memory error 2015-11-21 00:07:45 UTC+1 [Status] Device connected 2015-11-21 00:07:45 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-21 00:07:57 UTC+1 [Status] Device connected 2015-11-21 00:07:57 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-21 00:08:09 UTC+1 [Status] Device connected 2015-11-21 00:08:09 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-21 00:08:22 UTC+1 [Status] Device connected 2015-11-21 00:08:22 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-21 00:08:33 UTC+1 [Exit Code] imp restarted, reason: memory error 2015-11-21 00:08:33 UTC+1 [Status] Device connected 2015-11-21 00:08:34 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-21 00:09:42 UTC+1 [Status] Device connected 2015-11-21 00:09:42 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-22 07:32:23 UTC+1 [Status] Device connected 2015-11-22 07:32:24 UTC+1 [Device] 78f540f - release-32.12 - Thu Jul 16 09:55:33 2015 2015-11-22 07:33:19 UTC+1 [Agent] Received state: 0 2015-11-22 07:33:19 UTC+1 [Agent] My state: 1
This is the code on the device:
local currentState = 0;/* __ State management ______________________________________________________ */
function ConfigureThermostatState()
{
/* Relay pin */
hardware.pin8.configure(DIGITAL_OUT);/* Button pin */ hardware.pin9.configure(DIGITAL_IN_PULLUP, ButtonChanged); /* Retrieve any previous state in server storage */ if ("lastState" in server.permanent) { currentState = server.permanent.lastState; //server.log("Loaded: " + currentState); SetState(currentState); }
}
// value = 0: relay ON
// value = 1: relay OFF
function SetState(value)
{
//server.log("D: SetState f: " + value);
local bit = value.tointeger();
//server.log("D: SetState b: " + bit);
hardware.pin8.write(bit);
imp.sleep(0.5);
//server.show(format(“State: %s”, bit ? “On” : “Off”));
currentState = bit;
}function GetState()
{
return hardware.pin8.read();
}function GetButtonState()
{
return hardware.pin9.read();
}function ButtonChanged()
{
// Process button_down only
if (GetButtonState() == 1) return;//server.log("Button pressed"); SetState(GetReverseState()); SaveCurrentState();
}
function GetReverseState()
{
return currentState == 0 ? 1 : 0;
}function SaveCurrentState()
{
//server.log("save: " + currentState);
server.setpermanentvalues( { lastState = currentState } );// Send to the server for consumption logging // server.log("send state to server: " + currentState); agent.send("setstateresponse", currentState);
}
/* __ Watchdog ______________________________________________________________ */
/* This code prevents NAT routers from dropping the connection after
inactivity by pinging the server every 5 minutes (the ping is actually done
by the wakeup call)
*/function WatchdogHandler() {
imp.wakeup(5 * 60, WatchdogHandler);
//server.log("ss " + GetSignalStrength());
}function GetSignalStrength()
{
local dbs = imp.rssi();
if (dbs < -87) return “-----”;
if (dbs < -82) return “O----”;
if (dbs < -77) return “OO—”;
if (dbs < -72) return “OOO–”;
if (dbs < -67) return “OOOO-”;
if (dbs == 0) return “Wifi off”;return "OOOOO";
}
/* __ Agent commands ________________________________________________________ */
agent.on(“querystate”, function(msg)
{
agent.send(“querystateresponse”, GetState());
});agent.on(“setstate”, function(msg)
{
//server.log("D: SetState: " + msg);// Reverse local bit = msg.tointeger(); bit = bit == 0 ? 1 : 0; SetState(bit); SaveCurrentState();
});
/* __ Initialization ________________________________________________________ */
server.log(imp.getsoftwareversion());
WatchdogHandler();
ConfigureThermostatState();
Any help would be much appreciated!
Cheers,
David