PANIC when using tmr.alarm in conn:on("receive") function
Posted: Sat Jan 09, 2016 4:40 pm
When I add code to turn an LED on and turn it off with a timer, I get the following output with an error (intermittently, which is annoying):
I seem to have narrowed it down to the tmr.alarm function. Is there a workaround, or perhaps a better way to leave the LED on for a second?
The following code runs just fine, no issues.
Here is the full code causing the error (changes emphasized):
When a connection happens:
Then the module resets.
So the reading of the DHT22 is successful, so is the sending to the client, but then I get this panic. I expect that is because of the timer function.
Any thoughts on this?
EDIT: Using the following build:
NodeMCU 0.9.6 build 20150627 powered by Lua 5.1.4
Code: Select all
PANIC: unprotected error in call to Lua API (attempt to call a number value)
I seem to have narrowed it down to the tmr.alarm function. Is there a workaround, or perhaps a better way to leave the LED on for a second?
The following code runs just fine, no issues.
Code: Select all
-- dht22_tcp_panic
print(wifi.sta.getip())
dhtpin = 2
reading = nil
ledpin = 7
gpio.mode(ledpin,gpio.OUTPUT)
srv=net.createServer(net.TCP,5)
srv:listen(8080,function(conn)
conn:on("receive", function(client,payload)
print("Connection received")
-- Turn on LED
gpio.write(ledpin,gpio.HIGH)
-- If the payload is correct, read the DHT sensor
if (payload == "gettemp") then
local status,temp,humi,temp_decimal,humi_decimal = dht.read(dhtpin)
if( status == dht.OK ) then
-- Integer firmware using this example
reading = string.format(
"%d.%03d, %d.%03d",
math.floor(temp),
temp_decimal,
math.floor(humi),
humi_decimal
)
readingT = string.format("%d.%03d",math.floor(temp),temp_decimal)
readingH = string.format("%d.%03d",math.floor(humi),humi_decimal)
elseif( status == dht.ERROR_CHECKSUM ) then
reading = "DHT Checksum error.";
readingT = nil;
readingH = nil;
elseif( status == dht.ERROR_TIMEOUT ) then
reading = "DHT Time out.";
readingT = nil;
readingH = nil;
end
else
reading = "Wrong payload";
end
print(reading)
function sendreading ()
print(client);
client:send(reading);
end
-- Try to send the reading
if pcall(sendreading) then
print("reading sent")
else
print("error sending reading")
end
-- Try to close the connection
if pcall(function() client:close() end) then
print("client closed")
else
print("error closing client")
end
gpio.write(ledpin,gpio.LOW)
end)
-- commented out, not sure if I need this
-- conn:on("sent", function(conn) conn:close() end)
end)
Here is the full code causing the error (changes emphasized):
Code: Select all
-- dht22_tcp
print(wifi.sta.getip())
dhtpin = 2
reading = nil
ledpin = 7;
gpio.mode(ledpin,gpio.OUTPUT);
srv=net.createServer(net.TCP,5)
srv:listen(8080,function(conn)
conn:on("receive", function(client,payload)
print("Connection received")
-- Turn on LED and set a timer to turn it off after 1 second
-- ------------ BEGIN ADDED CODE -------------------
gpio.write(ledpin,gpio.HIGH)
tmr.alarm(1,1000,0,function()
gpio.write(ledpin,gpio.LOW)
end)
-- -------- END ADDED CODE --------------------
-- If the payload is correct, read the DHT sensor
if (payload == "gettemp") then
local status,temp,humi,temp_decimal,humi_decimal = dht.read(dhtpin)
if( status == dht.OK ) then
-- Integer firmware using this example
reading = string.format(
"%d.%03d, %d.%03d",
math.floor(temp),
temp_decimal,
math.floor(humi),
humi_decimal
)
readingT = string.format("%d.%03d",math.floor(temp),temp_decimal)
readingH = string.format("%d.%03d",math.floor(humi),humi_decimal)
elseif( status == dht.ERROR_CHECKSUM ) then
reading = "DHT Checksum error.";
readingT = nil;
readingH = nil;
elseif( status == dht.ERROR_TIMEOUT ) then
reading = "DHT Time out.";
readingT = nil;
readingH = nil;
end
else
reading = "Wrong payload";
end
print(reading)
function sendreading ()
print(client);
client:send(reading);
end
-- Try to send the reading
if pcall(sendreading) then
print("reading sent")
else
print("error sending reading")
end
-- Try to close the connection
if pcall(function() client:close() end) then
print("client closed")
else
print("error closing client")
end
end)
-- conn:on("sent", function(conn) conn:close() end)
end)
When a connection happens:
Code: Select all
Connection received
21.700, 42.900
userdata: 3fff3bb8
reading sent
client closed
PANIC: unprotected error in call to Lua API (attempt to call a number value)
Then the module resets.
So the reading of the DHT22 is successful, so is the sending to the client, but then I get this panic. I expect that is because of the timer function.
Any thoughts on this?
EDIT: Using the following build:
NodeMCU 0.9.6 build 20150627 powered by Lua 5.1.4