call_delay stops working

I am new to luup development, and am trying to build a plugin to collect data from an EW4008 power meter I have. This only returns instantaneous data when you make a request of the port - it doesn’t publish data, you have to request it.
Before I worry about the serial port work, I am doing some testing of a plugin that will run every 5 seconds and do the serial port call.

I have the following function:

[code] function getData(data)
luup.log(“Get Data called: '” … data … “'”);
local randomValue = math.random(300, 6000);
luup.log("Current value: " … randomValue);
luup.variable_set(“urn:micasaverde-com:serviceId:EnergyMetering1”, “Watts”, randomValue, lul_device)

  luup.call_delay("getData", 5, "data", 0);


In the startup function for the device, I make the call:

luup.call_delay("getData", 5, "data", 0); return true;

This update the Watts value for the device, and does so every 5 seconds for a while. However, after a fairly long period (a few hours), it stops. No more calls to getData are made, and the Watts value does not get updated.

Any idea why this is happening?

Any idea why this is happening?

My guess would be that there is an error somewhere in your code that is killing it. Search through LuaUPnP.log around the time the cycle stops.

One possible cause is the use of lul_device in your callback function. This variable is magically provided in most parts of a plugin code but I have personally never relied on it being valid in a callback function. I encode it into the optional string parameter for the call and then extract it using tonumber(…) in the callback routine. If you don’t specifically provide the value, you are dependent on it being on the stack. This may work until a garbage collection cycle removes it.

Thanks for that - that was the problem. I didn’t realise it could go out of scope.

Thanks again.