什么导致我的Nodemcu/ESP8266重启?

我正在使用类似霍尔效应传感器的传感器来统计中断的数量。在一些随机时间之后,通常在开机1-2小时后,它会重置,然后在随机的时间间隔内随机重置

counter = 0;
sampletime = 0;
lastrisetime = tmr.now()
pin = 2

do
  gpio.mode(pin, gpio.INT)

  local function rising(level)
    -- 为了消除短时间内的多次计数(.5秒),需要取差
        if ((tmr.now() - lastrisetime) > 500000) then
        lastrisetime = tmr.now();
    end
    -- 当tmr.now()归零时,需要考虑该特定计数
    if ((tmr.now() - lastrisetime) < 0) then
        lastrisetime = tmr.now();
    end
  end

  local function falling(level)
    if ((tmr.now() - lastrisetime) > 500000) then
        -- 只有在引脚下降时才计算
        -- 它就像正弦曲线,所以峰值或谷值都会被计数
            counter = counter + 1;
        print(counter)
        lastrisetime = tmr.now();
        sampletime = lastrisetime;
    end
    -- 当tmr.now()归零时,需要考虑该特定计数
    if ((tmr.now() - lastrisetime) < 0) then
        lastrisetime = tmr.now();
            counter = counter + 1;
        print(counter)
    end
  end

  gpio.trig(pin, "up", rising)
  gpio.trig(pin, "down", falling)
end

这是我在CoolTerm上得到的错误信息,我每隔几个小时检查一次内存,你可以在那里看到结果。

NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4
> 连接...
已连接
print(node.heap())
22920
> print(node.heap())
22904
> print(node.heap())
22944
> print(node.heap())
22944
> 2. .print(node.heap())
22944
> print(node.heap())
22944
> ∆.)ç˛.䂸 æ ¸@H7.àåË‘

NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4
> 连接...
已连接
 print(node.heap())
21216
> F.)ç˛.¶Ùå¶1.@H  .ÊÍ

NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4
> 连接...
已连接
H!໩.ä‚D.æ ¸å¶H.åb‘

NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4
> 连接...
已连接
 print(node.heap())
22904
> print(node.heap())
21216
>

感谢您抽出时间阅读此文。期待您的宝贵意见。

点赞
用户3082873
用户3082873

可能是看门狗定时器问题。

在你的中断服务例程中,看起来你等待的时间太长了。

最好从中删除计时操作,只需设置一个标志,在另一个循环中检查标志状态并完成计时操作。

2016-12-02 11:31:01
用户131929
用户131929

NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4

使用最新版本的NodeMCU固件是首要的事情。0.9.x版本已经过时,存在许多错误,并且不再受支持。请参考这里 https://github.com/nodemcu/nodemcu-firmware/#releases

lastrisetime = tmr.now()

真正的问题是,我认为tmr.now()在2147秒时会翻转。我在编写适当的去抖函数时学到了这个知识点。

-- 参考 https://github.com/hackhitchin/esp8266-co-uk/blob/master/tutorials/introduction-to-gpio-api.md 
-- 和 http://www.esp8266.com/viewtopic.php?f=24&t=4833&start=5#p29127
local pin = 4    --> GPIO2

function debounce (func)
    local last = 0
    local delay = 50000 -- 50ms * 1000 as tmr.now() has μs resolution

    return function (...)
        local now = tmr.now()
        local delta = now - last
        if delta < 0 then delta = delta + 2147483647 end; -- 因为delta翻转提出,https://github.com/hackhitchin/esp8266-co-uk/issues/2
        if delta < delay then return end;

        last = now
        return func(...)
    end
end

function onChange ()
    print('引脚值已更改为 '..gpio.read(pin))
end

gpio.mode(pin, gpio.INT, gpio.PULLUP) -- 参见 https://github.com/hackhitchin/esp8266-co-uk/pull/1
gpio.trig(pin, 'both', debounce(onChange))
2016-12-03 07:54:05