lua:init.lua:15:尝试调用方法"alarm"(nil值) [英] lua: init.lua:15: attempt to call method 'alarm' (a nil value)

查看:169
本文介绍了lua:init.lua:15:尝试调用方法"alarm"(nil值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修复我在网上找到的一段代码.(是的,我知道....)但是,如果你们能帮我解决这个错误,那就太了不起了:

i am trying to fix a piece of code i did find online. (yeah i know....) But in case you guys can help me out wihth this error it would be just amazing:

错误:lua:init.lua:15:尝试调用方法'alarm'(nil值)

Error: lua: init.lua:15: attempt to call method 'alarm' (a nil value)

代码(来自此处: https://github.com/Christoph-D/esp8266-尾灯)

dofile("globals.lc")

wifi.setmode(wifi.STATION)
wifi.sta.config(WIFI_SSID, WIFI_PASSWORD)
wifi.sta.sethostname(MY_HOSTNAME)
if WIFI_STATIC_IP then
  wifi.sta.setip({ip = WIFI_STATIC_IP, netmask = WIFI_NETMASK, gateway = WIFI_GATEWAY})
end
wifi.sta.connect()

-- Initialize the LED_PIN to the reset state.
gpio.mode(LED_PIN, gpio.OUTPUT)
gpio.write(LED_PIN, gpio.LOW)

tmr.alarm(
  MAIN_TIMER_ID, 2000, tmr.ALARM_AUTO, function ()
    if wifi.sta.getip() then
      tmr.unregister(MAIN_TIMER_ID)
      print("Config done, IP is " .. wifi.sta.getip())
      dofile("ledserver.lc")
    end
  end)

我在那里可以做什么?怎么了?

What can i do there? Whats wrong?

干杯,谢谢!!

推荐答案

全部在手册中.您只需要阅读它即可.

It is all in the manual. You just have to read it.

有一个示例,说明如何使用计时器对象的警报方法.

There is an example of how to use the alarm method of timer objects.

if not tmr.create():alarm(5000, tmr.ALARM_SINGLE, function()
  print("hey there")
end)
then
  print("whoopsie")
end

您试图调用 tmr.alarm ,但它是 tobj:alarm .该手册未提及 tmr.alarm .该功能已于2019年1月从NodeMCU中删除.

You attempted to call tmr.alarm but it is tobj:alarm. The manual does not mention tmr.alarm. This function was removed from NodeMCU in January 2019.

您使用的是在线发现的基于旧版NodeMCU的代码.它正在使用现在不推荐使用的功能.

You're using code you found online that is basedn on an older NodeMCU version. It is using functions that are deprecated by now.

请参见 https://github.com/nodemcu/nodemcu-固件/拉/2603#issuecomment-453235401

https://github.com/比较/5b22e1f9aee77095ab99dd6240ebd9dddd1cc5a0..c6444ecb6088d20e95197d808d8303c8093faab5

因此必须首先创建一个计时器对象,然后才能使用其任何方法. alarm 不再是 tmr 模块的方法.

So you have to create a timer object first befvor you can use any of its methods. alarm is not a method of the tmr module anymore.

修改

首先,您必须创建一个计时器对象 https://nodemcu.阅读thedocs.io/en/latest/modules/tmr/#tobjcreate

First you have to create a timer object https://nodemcu.readthedocs.io/en/latest/modules/tmr/#tobjcreate

local tObj = tmr.create()

然后,您必须注册回调和启动计时器. alarm 会对我们都起作用.

Then you have to register a callback and start the timer. There is a convenience function alarm that does both for us.

当我们不再需要计时器时,我们必须通过调用来释放资源

And when we do not need our timer anymore we have to free the resources by calling

tObj:unregister()

尝试类似

-- create a timer object
local tObj = tmr.create()
-- register an alarm
tObj:alarm(2000, tmr.ALARM_AUTO, function ()
    if wifi.sta.getip() then
      tObj:unregister()
      print("Config done, IP is " .. wifi.sta.getip())
      dofile("ledserver.lc")
    end
  end)

这篇关于lua:init.lua:15:尝试调用方法"alarm"(nil值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆