【问题标题】:PANIC: unprotected error in call to Lua API (script1.lua:34: attempt to index global 'http' (a nil value))PANIC:调用 Lua API 时出现未受保护的错误(script1.lua:34:尝试索引全局“http”(一个 nil 值))
【发布时间】:2017-05-29 07:52:10
【问题描述】:

尝试在我的 NodeMCU(使用 Lua 编程语言)上执行 HTTP GET 请求时出现此错误。它通过 WiFi 连接。

当 PIR 传感器(被动式 IR 传感器)检测到移动时,会调用函数 roepIFTTT()。到 IFTTT 的 HTTP POST 运行良好。

PANIC: unprotected error in call to Lua API (script1.lua:35: attempt to index global 'http' (a nil value))

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x40100000, len 26096, room 16 
tail 0
chksum 0x0c
load 0x3ffe8000, len 2232, room 8 
tail 0
chksum 0x7a
load 0x3ffe88b8, len 8, room 8 
tail 0
chksum 0x5f
csum 0x5f
ŒÂœäƒoä’sƒûo|ìld$l`„âr›lŒdŒŸ

这是固件:

NodeMCU custom build by frightanic.com
    branch: master
    commit: 7b83bbb2ea134cd85ac9d63108603cc02c4e20f7
    SSL: false
    modules: adc,bit,cjson,coap,dht,file,gpio,i2c,mqtt,net,node,ow,pwm,rtctime,sntp,spi,tmr,uart,wifi,ws2812
 build  built on: 2016-11-25 08:30
 powered by Lua 5.1.4 on SDK 1.5.4.1(39cb9a32)

这是我的代码:

wifi.setmode(wifi.STATION)
wifi.sta.config("ssid", "password")
wifi.sta.connect()

ledpen = 0
gpio.mode(ledpen, gpio.OUTPUT)
gpio.write(ledpen, gpio.HIGH)

pirsensorpen = 2

gpio.mode(pirsensorpen, gpio.INT, gpio.PULLUP)

function beweging() -- This function gets called when movement is detected
    print("PIR sensor detecteerde beweging")
    gpio.write(ledpen, gpio.LOW)
    roepIFTTT(1)
    tmr.alarm(0, 2000, tmr.ALARM_SINGLE, function()
        gpio.write(ledpen, gpio.HIGH)
    end)
end

function roepIFTTT(value1)
  -- Trigger flashbutton op IFTTT
  conn = nil
  conn = net.createConnection(net.TCP, 0)
  conn:on("receive", function(conn, payload) end)
  conn:connect(80,"maker.ifttt.com")
  conn:on("connection", function(conn, payload)
  conn:send("POST /trigger/pirsensor/with/key/API_KEY?value1="..value1.." HTTP/1.1\r\nHost: maker.ifttt.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end)
  conn:close()
  print("IFTTT is aangeroepen")



-- It is about the code below that generates the error. If there is another way if creating a simple HTTP GET request, it is welcome.
    http.get("http://httpbin.org/ip", nil, function(code, data)
    if (code < 0) then
      print("HTTP request failed")
    else
      print(code, data)
    end
  end)
  -- coe above


end

function metingNaarThingSpeak()  
  local a = adc.read(0)
  print("Value of moisture sensor: "..a)
  local conn = nil
  conn = net.createConnection(net.TCP, 0)
  conn:on("receive", function(conn, payload) end)
  conn:connect(80, "api.thingspeak.com")
  conn:on("connection", function(conn, payload)
  conn:send("GET /update?api_key=API_KEY&field1="..a.." HTTP/1.1\r\nHost: api.thingspeak.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") end)
  conn:close()
  print("Meting naar ThingSpeak verzonden")
end

gpio.trig(pirsensorpen, "up", beweging)
tmr.alarm(4, 60000, tmr.ALARM_AUTO, function() metingNaarThingSpeak() end)

我做错了什么?

注意:我在这方面完全是新手,我从来没有在 Lua 中编程过,也没有使用过 NodeMCU。我还有其他的编程技能,所以编程对我来说并不是什么新鲜事。

【问题讨论】:

  • 提供有关您的代码和脚本文件的更多信息。该错误表明http库没有初始化。
  • FWIW。该语言称为Lua,而不是LUA。这不是首字母缩写词。
  • @MarcBalmer 我编辑了他的帖子并修复了标签、格式和“Lua”
  • @Marcel Sör:很酷,谢谢 ;)

标签: lua nodemcu


【解决方案1】:

错误就在这里

模块:adc,bit,cjson,coap,dht,file,gpio,i2c,mqtt,net,node,ow,pwm,rtctime,sntp,spi,tmr,uart,wifi,ws2812

您的 NodeMCU 固件缺少 HTTP 模块。

【讨论】:

    猜你喜欢
    • 2012-08-14
    • 2019-08-21
    • 2021-08-04
    • 2019-03-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    • 2018-07-11
    • 2014-05-06
    相关资源
    最近更新 更多