Lua 代码错误。卢安诺德姆库酒店

Lua 代码错误。卢安诺德姆库酒店,lua,esp8266,nodemcu,panic,esplorer,Lua,Esp8266,Nodemcu,Panic,Esplorer,我有一个电动卷帘项目。 我遵循以下说明: 我正在使用指令中的代码,但我怀疑故障出在以下代码之一中: config.lua wifi\u设置 遗憾的是,我无法超越第四步。尝试将代码下载到ESP8266后,我只收到以下错误: 惊慌失措:调用LuaAPI时出现未受保护的错误(wifi_setup.Lua:25:错误参数#1到'config'(未找到配置表!)) 我只改变了教练让我改变的东西,我自己也试着寻找缺点,但我找不到任何。。。这是我第一次使用Lua,所以这对我来说是全新的 希望这里有人能提供一

我有一个电动卷帘项目。 我遵循以下说明:

我正在使用指令中的代码,但我怀疑故障出在以下代码之一中:

config.lua

wifi\u设置

遗憾的是,我无法超越第四步。尝试将代码下载到ESP8266后,我只收到以下错误:

惊慌失措:调用LuaAPI时出现未受保护的错误(wifi_setup.Lua:25:错误参数#1到'config'(未找到配置表!))

我只改变了教练让我改变的东西,我自己也试着寻找缺点,但我找不到任何。。。这是我第一次使用Lua,所以这对我来说是全新的

希望这里有人能提供一些帮助。这件事已经拖了好几天了

这部分问题已经解决了。检查注释以获得解决方案


可悲的是,当一个问题解决了,一个新的问题出现了

该芯片现在已成功连接到wifi和mqtt服务,但当我尝试通过输入命令“step_move(1000,FWD,2)”进行试运行时,什么也没有发生。马达应该转动

此外,当我按下按钮时,会出现一个新的紧急错误,如下所示:

恐慌:调用LuaAPI时发生未受保护的错误(button.Lua:23:尝试对upvalue“?”(一个nil值)执行算术)

巴顿·卢阿

以下是stepper.lua的代码:

    -- stepper.lua
-- code from: http://www.esp8266.com/viewtopic.php?f=19&t=2326
-- simple stepper driver for controlling a stepper motor with a
-- l293d driver
-- nodemcu pins:  0  5  6  7
stepper_pins = {1,3,2,4} -- (A-)blue, (A+)pink, (B-)yellow, (B+)orange
--stepper_pins = {1,2,3,4}
-- half or full stepping
step_states4 = {
 {1,0,0,1},
 {1,1,0,0},
 {0,1,1,0},
 {0,0,1,1}
}
step_states8 = {
 {1,0,0,0},
 {1,1,0,0},
 {0,1,0,0},
 {0,1,1,0},
 {0,0,1,0},
 {0,0,1,1},
 {0,0,0,1},
 {1,0,0,1},
}
step_states = step_states4 -- choose stepping mode
step_numstates = 4 -- change to match number of rows in step_states
step_delay = 10 -- choose speed
step_state = 0 -- updated by step_take-function
step_direction = 1 -- choose step direction -1, 1
step_stepsleft = 0 -- number of steps to move, will de decremented
step_timerid = 4 -- which timer to use for the steps
status_timerid = 2 -- timer id for posing of status messages
-- setup pins
function pins_enable()
  for i = 1, 4, 1 do
    gpio.mode(stepper_pins[i],gpio.OUTPUT)
  end
end

function pins_disable()
--  for i = 1, 4, 1 do -- no power, all pins
  for i = 2, 4, 1 do -- no power, all pins except one (to keep it in place)
    gpio.mode(stepper_pins[i],gpio.INPUT)
  end
end
-- turn off all pins to let motor rest
function step_stopstate() 
  for i = 1, 4, 1 do
    gpio.write(stepper_pins[i], 0)
  end
end

-- make stepper take one step
function step_take()
  -- jump to the next state in the direction, wrap
  step_state = step_state + step_direction
  cur_step = cur_step + step_direction * FWD
      if step_state > step_numstates then
    step_state = 1;
  elseif step_state < 1 then
    step_state = step_numstates
  end
  -- write the current state to the pins
  pins_enable()
  for i = 1, 4, 1 do
    gpio.write(stepper_pins[i], step_states[step_state][i])
  end
  -- might take another step after step_delay
  step_stepsleft = step_stepsleft-1
  if step_stepsleft > 0 then
--  if cur_step > 0 and cur_step < tot_steps and step_stepsleft > 0 then
    tmr.alarm(step_timerid, 10, 0, step_take )
    --tmr.alarm(step_timerid, 10, 0, step_take )
  else
    step_stopstate()
    step_stop()
    pins_disable()
    mq.post_status()
    if file.open("cfg_cur_step.lua", "w+") then
      file.write("cur_step=" .. cur_step .. '\n')
      file.close()
    end
  end
end

-- public method to start moving number of 'int steps' in 'int direction'    
function step_move(steps, direction, delay)
  tmr.stop(step_timerid)
  step_stepsleft = steps
  step_direction = direction
  step_delay = delay
  step_take()
end

function step_go_to(step, delay)
  if step >= cur_step then
    steps = step - cur_step
    step_move(steps, FWD, delay)
  end
  if step <= cur_step then
    steps = cur_step - step
    step_move(steps, REV, delay)
  end
end

function percent_go_to(percent, delay)
  if(percent >= 0 and percent <= 100) then
    step_stop()
    tmr.register(status_timerid, 1000, tmr.ALARM_AUTO, function () mq.post_status() end)
    tmr.start(status_timerid)
    step = percent * tot_steps / 100
    step_go_to(step, delay)
  end
end

-- public method to cancel moving
function step_stop()
  tmr.stop(step_timerid)
  tmr.stop(status_timerid)
  step_stepsleft = 0
  step_stopstate()
end
——stepper.lua
--代码来源:http://www.esp8266.com/viewtopic.php?f=19&t=2326
--简单的步进驱动器,用于控制带有
--l293d驱动程序
--nodemcu引脚:0 5 6 7
步进器_管脚={1,3,2,4}-(A-)蓝色,(A+)粉色,(B-)黄色,(B+)橙色
--步进器_引脚={1,2,3,4}
--半步或全步
步骤4={
{1,0,0,1},
{1,1,0,0},
{0,1,1,0},
{0,0,1,1}
}
步骤8={
{1,0,0,0},
{1,1,0,0},
{0,1,0,0},
{0,1,1,0},
{0,0,1,0},
{0,0,1,1},
{0,0,0,1},
{1,0,0,1},
}
步骤状态=步骤状态4——选择步骤模式
step_numstates=4——更改以匹配step_状态中的行数
步骤_延迟=10——选择速度
step_state=0——由step_take-function更新
步进方向=1——选择步进方向-1,1
step_stepsleft=0——要移动的步数将递减
step_timerid=4——步骤使用哪个计时器
status_timerid=2——设置状态消息的计时器id
--安装销
功能引脚_enable()
对于i=1,4,1 do
gpio.mode(步进器引脚[i],gpio.OUTPUT)
终止
终止
功能引脚_禁用()
--对于i=1,4,1 do--无电源,所有引脚
对于i=2,4,1 do--无电源,除一个引脚外的所有引脚(用于保持其就位)
gpio.mode(步进器引脚[i],gpio.INPUT)
终止
终止
--关闭所有销,让电机静止
函数步骤_stopstate()
对于i=1,4,1 do
gpio.write(步进器引脚[i],0)
终止
终止
--使步进电机迈出一步
函数步骤_take()
--跳转到方向的下一个状态,换行
步进状态=步进状态+步进方向
电流步进=电流步进+步进方向*前进
如果步骤状态>步骤状态,则
步骤u状态=1;
elseif step_state<1则
步骤状态=步骤状态
终止
--将当前状态写入管脚
引脚_启用()
对于i=1,4,1 do
gpio.write(步进器引脚[i],步进状态[step\u state][i])
终止
--可能一步接一步地采取另一步
步骤_stepsleft=步骤_stepsleft-1
如果step_stepsleft>0,则
--如果cur_step>0,cur_step0,则
tmr.警报(步骤\u timerid,10,0,步骤\u take)
--tmr.警报(步骤\u timerid,10,0,步骤\u take)
其他的
步骤_stopstate()
步骤(停止)
引脚_禁用()
mq.post_status()
如果file.open(“cfg\u cur\u step.lua”、“w+”),则
file.write(“cur_step=“..cur_step..'\n”)
file.close()文件
终止
终止
终止
--开始在“int方向”上移动“int步数”的公共方法
功能步骤\移动(步骤、方向、延迟)
tmr.停止(步骤\u timerid)
步骤_stepsleft=步骤
步进方向=方向
步骤延迟=延迟
步骤()
终止
功能步进至(步进,延迟)
如果步骤>=当前步骤,则
步骤=步骤-当前步骤
步进移动(步进、前进、延迟)
终止

如果步骤=0且百分比让我们逐一解析错误消息:

unprotected error in call to Lua API (wifi_setup.lua:25: bad argument #1 to 'config' (config table not found!))
未保护的错误意味着您进行了正常的函数调用,而不是受保护的调用(aka),这是一种预期会发生错误并希望提供处理方法的函数调用。由于您没有执行受保护的调用,Lua不知道如何处理错误并立即终止(这不是一件坏事)

wifi\u setup.lua:25
告诉您发生错误的文件和行

错误参数#1到'config'
表示问题是由于传递给名为
config
的函数的第一个参数引起的

未找到配置表
是该函数的实现者提供的错误消息

总之,函数调用wifi.sta.config(key,config.SSID[key])有一个问题,因为它需要一个表作为第一个参数,而您提供了一些不同的参数(例如,带有BSSID的字符串)。实际上,通过检查显示您需要将表传递给此函数:

wifi.sta.config()

设置WiFi站点配置。[……]

语法

wifi.sta.config(站点配置)

参数

站点配置
包含站点配置数据的表格 站


表格的预期布局也详细记录在该页上。

漫画提供了一个很好的答案,其实质是您需要替换

wifi.sta.config(key,config.SSID[key])

因此,一个独立的示例可以如下所示:

--wifi.setmode(wifi.NULLMODE)

config = {}
config.SSID = {}
config.SSID["ssid"] = "password"

function wifi_wait_ip()
  if wifi.sta.getip() == nil then
    print("IP unavailable, Waiting...")
  else
    tmr.stop(1)
    print("\n====================================")
    print("ESP8266 mode is: " .. wifi.getmode())
    print("MAC address is: " .. wifi.ap.getmac())
    print("IP is " .. wifi.sta.getip())
    print("====================================")
  end
end

function wifi_start(list_aps)
  if list_aps then
    for key, value in pairs(list_aps) do
      if config.SSID and config.SSID[key] then
        wifi.setmode(wifi.STATION);
        wifi.sta.config{ssid=key, pwd=config.SSID[key]}
        -- wifi.sta.connect() not needed as config() uses auto-connect=true by default
        print("Connecting to " .. key .. " ...")
        tmr.alarm(1, 2500, 1, wifi_wait_ip)
      end
    end
  else
    print("Error getting AP list")
  end
end

function start()
  print("Configuring Wifi ...")
  wifi.setmode(wifi.STATION)
  wifi.sta.getap(wifi_start)
end

start()

添加错误的代码和详细说明。否则,您的问题将被否决并关闭。尝试此操作后,“配置wifi”消息后不会发生任何事情。幸运的是,我没有得到恐慌的错误,所以我猜这是进步,呵呵。还有什么想法吗
unprotected error in call to Lua API (wifi_setup.lua:25: bad argument #1 to 'config' (config table not found!))
wifi.sta.config(key,config.SSID[key])
wifi.sta.config{ssid=key,pwd=config.SSID[key]}
--wifi.setmode(wifi.NULLMODE)

config = {}
config.SSID = {}
config.SSID["ssid"] = "password"

function wifi_wait_ip()
  if wifi.sta.getip() == nil then
    print("IP unavailable, Waiting...")
  else
    tmr.stop(1)
    print("\n====================================")
    print("ESP8266 mode is: " .. wifi.getmode())
    print("MAC address is: " .. wifi.ap.getmac())
    print("IP is " .. wifi.sta.getip())
    print("====================================")
  end
end

function wifi_start(list_aps)
  if list_aps then
    for key, value in pairs(list_aps) do
      if config.SSID and config.SSID[key] then
        wifi.setmode(wifi.STATION);
        wifi.sta.config{ssid=key, pwd=config.SSID[key]}
        -- wifi.sta.connect() not needed as config() uses auto-connect=true by default
        print("Connecting to " .. key .. " ...")
        tmr.alarm(1, 2500, 1, wifi_wait_ip)
      end
    end
  else
    print("Error getting AP list")
  end
end

function start()
  print("Configuring Wifi ...")
  wifi.setmode(wifi.STATION)
  wifi.sta.getap(wifi_start)
end

start()