NodeMCU 外部回调函数产生错误
NodeMCU external callback function generates error
我正在使用 NodeMCU 0.9.6 和 Adafruit HUZZAH ESP8266。
以下代码在 http.get
语句中定义回调函数,有效:
http.get("http://192.168.2.38/ICmd.py?i=esp8266001", nil, function(code, data)
if (code < 0) then
print("***ICmd HTTP request failed")
else
print("***ICmd callback: ", code, data)
end
end)
以下代码将 http.get
语句中的回调函数定义移动为独立函数:
function chkICmdResp(code, data)
if (code < 0) then
print("***ICmd HTTP request failed")
else
print("***ICmd callback: ", code, data)
end
end
http.get("http://192.168.2.38/ICmd.py?i=esp8266001", nil, chkICmdResp(code, data) )
第二种格式不起作用,它会生成错误:
stdin:2: attempt to compare nil with number
stack traceback:
stdin:2: in function 'chkICmdResponse'
stdin:1: in main chunk
我是不是做错了什么(NodeMCU noobie)?或者这是设计特点还是错误?
如果能够有一个可以从多个 http.get 调用中引用的回调函数就好了。
注意:我也尝试使用 net.socket:on("receive",...)
模块进行此操作,并在尝试使用在调用语句之外定义的函数时得到相同的错误结果。
您尝试执行的操作与您的固件提供的功能之间根本不匹配。不要使用那些旧的 0.9.x NodeMCU 二进制文件。它们基于旧的 Espressif SDK,不再维护并且充满错误。
HTTP 客户端模块才推出几个月。它目前仅在 NodeMCU dev
分支中可用,即甚至在当前 master
中也不可用。成功秘诀:
- 从包含 HTTP 模块(默认关闭)的
dev
分支构建固件:http://nodemcu.readthedocs.org/en/dev/en/build/
- 将二进制文件闪存到您的芯片:http://nodemcu.readthedocs.org/en/dev/en/flash/
- 按照记录使用 HTTP 模块:http://nodemcu.readthedocs.org/en/dev/en/modules/http/#httpget
如果您决定坚持使用 net 模块而不是使用 HTTP,文档中有一个小示例:http://nodemcu.readthedocs.org/en/dev/en/modules/net/#netsocketsend
我正在使用 NodeMCU 0.9.6 和 Adafruit HUZZAH ESP8266。
以下代码在 http.get
语句中定义回调函数,有效:
http.get("http://192.168.2.38/ICmd.py?i=esp8266001", nil, function(code, data)
if (code < 0) then
print("***ICmd HTTP request failed")
else
print("***ICmd callback: ", code, data)
end
end)
以下代码将 http.get
语句中的回调函数定义移动为独立函数:
function chkICmdResp(code, data)
if (code < 0) then
print("***ICmd HTTP request failed")
else
print("***ICmd callback: ", code, data)
end
end
http.get("http://192.168.2.38/ICmd.py?i=esp8266001", nil, chkICmdResp(code, data) )
第二种格式不起作用,它会生成错误:
stdin:2: attempt to compare nil with number
stack traceback:
stdin:2: in function 'chkICmdResponse'
stdin:1: in main chunk
我是不是做错了什么(NodeMCU noobie)?或者这是设计特点还是错误?
如果能够有一个可以从多个 http.get 调用中引用的回调函数就好了。
注意:我也尝试使用 net.socket:on("receive",...)
模块进行此操作,并在尝试使用在调用语句之外定义的函数时得到相同的错误结果。
您尝试执行的操作与您的固件提供的功能之间根本不匹配。不要使用那些旧的 0.9.x NodeMCU 二进制文件。它们基于旧的 Espressif SDK,不再维护并且充满错误。
HTTP 客户端模块才推出几个月。它目前仅在 NodeMCU dev
分支中可用,即甚至在当前 master
中也不可用。成功秘诀:
- 从包含 HTTP 模块(默认关闭)的
dev
分支构建固件:http://nodemcu.readthedocs.org/en/dev/en/build/ - 将二进制文件闪存到您的芯片:http://nodemcu.readthedocs.org/en/dev/en/flash/
- 按照记录使用 HTTP 模块:http://nodemcu.readthedocs.org/en/dev/en/modules/http/#httpget
如果您决定坚持使用 net 模块而不是使用 HTTP,文档中有一个小示例:http://nodemcu.readthedocs.org/en/dev/en/modules/net/#netsocketsend