-->
Page 1 of 1

inconsistent interrupt behaviour

PostPosted: Mon Feb 08, 2016 5:16 pm
by rmeldo
Hi

I have a problem which I cannot understand.

I am not very experienced in NodeMCU so I might have missed something obvious, but I have ran out of ideas hence me asking.

I have a piece of working code which uploads temperature from a DS18B20 onto a website.
There is a tmr alarm for uploads at regular intervals and two further uploads which can be triggered by two pushbuttons through interrupts.

The code works perfectly if I launch the script via ESPlorer by clicking on the file.

However, when I call the script from init.lua the regular uploads work but when the two buttons are being pressed nothing is detected.

I am using the following firmware:
NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4


the code is below. There is still is some repeated code but it serves the purpose of demonstrating the problem:

init.lua:
Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("aSSID","aPWD")
tmr.alarm(1, 1000, 1, function()
    if wifi.sta.getip()== nil then
        print("IP unavaiable, Waiting...")
    else
        tmr.stop(1)
        print("Config done, IP is "..wifi.sta.getip())

        dofile("mainUpload.lua")
    end
 end)


mainUpload.lua

Code: Select alllocal str = 0

local tmrSecond = 1000          -- microseconds
local minute    = 60000         -- milliseconds
local debounce  = 0.2 * tmrSecond
local sensorNo  = 1
local tempUploadInterval = 30 * tmrSecond
local theKey    = "key=abba"
local webSite   = "a-web-server.com"     -- #####   Change This

---------------------------------------------------------------------
-- MAIN
---------------------------------------------------------------------
print("")
print("Started the main program:")
print("   ###  Temperature Sensor # 1, SSID = " .. wifi.sta.getconfig())
print("")

gpio.mode(7,gpio.INT,gpio.PULLUP)         -- Pushbutton 1
gpio.trig(7, "low",onSwitch_1_Detection)  -- interrupt

gpio.mode(2,gpio.INT,gpio.PULLUP)         -- Pushbutton 2
gpio.trig(2, "low",onSwitch_2_Detection)  -- interrupt


-- Temperature Detection loop
tmr.alarm(0, tempUploadInterval, 1, function()
   print(" ")
   t=require("ds18b20")
   local dataStr = (theKey ..
                    "&sensorNo="    .. tostring(sensorNo) ..
                    "&tooHot=0"     ..
                    "&tooCold=0"    ..
                    "&temperature=" .. t.readNumber(6))
                   
   local sendStr = ("POST /log-room-temperature.php HTTP/1.1\r\n"..
                    "Host: " .. webSite .. "\r\n"..
                    "Accept: */*\r\n"..
                    "Content-Type: application/x-www-form-urlencoded\r\n"..
                    "Content-Length: " .. tostring(string.len(dataStr)) .."\r\n\r\n"..
                    dataStr ..
                    "\r\n\r\n")
   print(sendStr)
             
   conn=net.createConnection(net.TCP, 0)
   conn:on("connection",function(conn, payload)
      conn:send(sendStr)
   end)
   conn:on("receive", function(conn, payload)
      print(payload)
   conn:close()
   end)
   conn:connect(80,webSite)
   -- Release after use
   t = nil
   ds18b20 = nil
   package.loaded["ds18b20"]=nil
   --print(dataStr)
end)


function onSwitch_1_Detection(level)
   if level < 1 then
       tmr.delay(debounce)
       print("---> Switch 1 PRESSED <--")
       print(" ")
       t=require("ds18b20")
       local dataStr = (theKey ..
                    "&sensorNo="    .. tostring(sensorNo) ..
                    "&tooHot=1"     ..
                    "&tooCold=0"    ..
                    "&temperature=" .. t.readNumber(6))                   
       local sendStr = ("POST /log-room-temperature.php HTTP/1.1\r\n"..
                    "Host: " .. webSite .. "\r\n"..
                    "Accept: */*\r\n"..
                    "Content-Type: application/x-www-form-urlencoded\r\n"..
                    "Content-Length: " .. tostring(string.len(dataStr)) .."\r\n\r\n"..
                    dataStr ..
                    "\r\n\r\n")
   print(sendStr)
             
   conn=net.createConnection(net.TCP, 0)
   conn:on("connection",function(conn, payload)
      conn:send(sendStr)
   end)
   conn:on("receive", function(conn, payload)
      print(payload)
   conn:close()
   end)
   conn:connect(80,webSite)
   -- Release after use
   t = nil
   ds18b20 = nil
   package.loaded["ds18b20"]=nil
   --print(dataStr)
   
   else
       tmr.delay(debounce)
       print("---> Switch 1 RELEASED <--")
       print(" ")
   end
end

function onSwitch_2_Detection(level)
   if level <1 then
       tmr.delay(debounce)
       print("---> Switch 2 PRESSED <--")
       print(" ")
       t=require("ds18b20")
       local dataStr = (theKey ..
                    "&sensorNo="    .. tostring(sensorNo) ..
                    "&tooHot=0"     ..
                    "&tooCold=1"    ..
                    "&temperature=" .. t.readNumber(6))                   
       local sendStr = ("POST /log-room-temperature.php HTTP/1.1\r\n"..
                    "Host: " .. webSite .. "\r\n"..
                    "Accept: */*\r\n"..
                    "Content-Type: application/x-www-form-urlencoded\r\n"..
                    "Content-Length: " .. tostring(string.len(dataStr)) .."\r\n\r\n"..
                    dataStr ..
                    "\r\n\r\n")
   print(sendStr)
             
   conn=net.createConnection(net.TCP, 0)
   conn:on("connection",function(conn, payload)
      conn:send(sendStr)
   end)
   conn:on("receive", function(conn, payload)
      print(payload)
   conn:close()
   end)
   conn:connect(80,webSite)
   -- Release after use
   t = nil
   ds18b20 = nil
   package.loaded["ds18b20"]=nil
   --print(dataStr)
   
   else
       tmr.delay(debounce)
       print("---> Switch 2 RELEASED <--")
       print(" ")
   end
end


Could anyone please help?

Many Thanks
Riccardo

Re: inconsistent interrupt behaviour

PostPosted: Tue Feb 09, 2016 4:17 am
by rmeldo
I managed to solve this myself.

The problem was that I was calling the two interrupt functions before having defined. They were defined further down in the file.

Thanks anyway
Riccardo