-->
Page 1 of 2

PANIC when using tmr.alarm in conn:on("receive") function

PostPosted: Sat Jan 09, 2016 4:40 pm
by ScheissSchiesser
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):
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

Re: PANIC when using tmr.alarm in conn:on("receive") functio

PostPosted: Sat Jan 09, 2016 6:30 pm
by xtal
I would try a later version SDK 1.4.0 and 1.5.0 [Dev}
Todays dev build shows 1.5.0
master ver is nodemcu 140
ftightanic.com/ builds
It's my understanding that several problems were addressed by 1.4.0 ,,
Been using 140 for a while , much more available ram....
Started using 1.5.0 today , more available ram.....

Re: PANIC when using tmr.alarm in conn:on("receive") functio

PostPosted: Sun Jan 10, 2016 3:10 am
by ScheissSchiesser
ftightanic.com/ builds


I can't seem to find what page you're pointing me to here. Are there builds available on frightanic.com?

Re: PANIC when using tmr.alarm in conn:on("receive") functio

PostPosted: Sun Jan 10, 2016 9:11 am
by xtal
try
http://www.frightanic.com/nodemcu-custom-build/

supply your email address..
select master <> 1.4.0 ? or dev <> 1.5.0
select desired elements checkbox 7 standard + whatever else
you will receive email for download float and integer builds....