尝试索引全局 'websocket'
Attempt to index global 'websocket'
我对 ESP8266 还很陌生。我正在尝试将 WebSocket 添加到 Lua 代码中,但每次我尝试使用 WebSocket 查看 documentation 时,设备都会在尝试索引全局 websocket
时抛出错误(a nil
值)。我不太确定是否有要导入的东西,任何人都可以帮我解决这个问题。
function connectToSocket()
print ("Connect to socket called, OK.")
local ws_client = websocket.createClient()
end
wifi.setphymode(wifi.PHYMODE_N)
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","PWD")
wifi.sta.eventMonReg(wifi.STA_IDLE, function() print("IDLE") end)
wifi.sta.eventMonReg(wifi.STA_CONNECTING, function() print("CONNECTING...") end)
wifi.sta.eventMonReg(wifi.STA_WRONGPWD, function() print("WRONG PASSWORD!!!") end)
wifi.sta.eventMonReg(wifi.STA_APNOTFOUND, function() print("NO SUCH SSID FOUND") end)
wifi.sta.eventMonReg(wifi.STA_FAIL, function() print("FAILED TO CONNECT") end)
wifi.sta.eventMonReg(wifi.STA_GOTIP, function()
print("GOT IP "..wifi.sta.getip())
connectToSocket()
end)
wifi.sta.eventMonStart()
wifi.sta.connect()
我发现上面的代码存在三个问题。
主要问题似乎是您的固件缺少 websocket
模块。如果您碰巧手动构建它,请取消注释 https://github.com/nodemcu/nodemcu-firmware/blob/master/app/include/user_modules.h#L75。
此外,所有事件处理程序都需要在相应事件有机会被触发之前注册。我看到你打算 这样做。但是,默认情况下 wifi.sta.config
使用 auto connect=true
,在这种情况下,WiFi 注册过程会在事件监视器启动之前启动。
最后,wifi.sta.config
的签名在几个月前更改了(有关详细信息,请参阅文档)。现在你必须说 wifi.sta.config{"SSID","PWD"}
从而传递 Lua table.
我对 ESP8266 还很陌生。我正在尝试将 WebSocket 添加到 Lua 代码中,但每次我尝试使用 WebSocket 查看 documentation 时,设备都会在尝试索引全局 websocket
时抛出错误(a nil
值)。我不太确定是否有要导入的东西,任何人都可以帮我解决这个问题。
function connectToSocket()
print ("Connect to socket called, OK.")
local ws_client = websocket.createClient()
end
wifi.setphymode(wifi.PHYMODE_N)
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","PWD")
wifi.sta.eventMonReg(wifi.STA_IDLE, function() print("IDLE") end)
wifi.sta.eventMonReg(wifi.STA_CONNECTING, function() print("CONNECTING...") end)
wifi.sta.eventMonReg(wifi.STA_WRONGPWD, function() print("WRONG PASSWORD!!!") end)
wifi.sta.eventMonReg(wifi.STA_APNOTFOUND, function() print("NO SUCH SSID FOUND") end)
wifi.sta.eventMonReg(wifi.STA_FAIL, function() print("FAILED TO CONNECT") end)
wifi.sta.eventMonReg(wifi.STA_GOTIP, function()
print("GOT IP "..wifi.sta.getip())
connectToSocket()
end)
wifi.sta.eventMonStart()
wifi.sta.connect()
我发现上面的代码存在三个问题。
主要问题似乎是您的固件缺少 websocket
模块。如果您碰巧手动构建它,请取消注释 https://github.com/nodemcu/nodemcu-firmware/blob/master/app/include/user_modules.h#L75。
此外,所有事件处理程序都需要在相应事件有机会被触发之前注册。我看到你打算 这样做。但是,默认情况下 wifi.sta.config
使用 auto connect=true
,在这种情况下,WiFi 注册过程会在事件监视器启动之前启动。
最后,wifi.sta.config
的签名在几个月前更改了(有关详细信息,请参阅文档)。现在你必须说 wifi.sta.config{"SSID","PWD"}
从而传递 Lua table.