esp8266 & ws2812 lua脚本在http请求后挂起

我写了一个LUA脚本,在调用URL时ws2812的颜色会改变。

当您第一次调用URL时(http://IPADDRESS/?color=green&key=rpi),ESP会挂起。颜色将被切换,但这取决于ESP的挂起。

我尝试了很多。我不知道如何解决我的问题。PixelRing(ws2812)连接到D4(gpio2)。

你有一个解决方法吗?

local SSID = "*******"
local SSID_PASSWORD = "******"

LED_COUNT = 12
SECRET_KEY = "rpi"

local buffer = nil

--ws2812.init()

function wait_for_wifi_conn ( )
   tmr.alarm (1, 1000, 1, function ( )
      if wifi.sta.getip ( ) == nil then
         print ("正在等待WiFi连接")
      else
         tmr.stop (1)
         print ("ESP8266模式是:" .. wifi.getmode ( ))
         print ("模块MAC地址是:" .. wifi.ap.getmac ( ))
         print ("配置完成,IP是" .. wifi.sta.getip ( ))

        ws2812.init()
        buffer = ws2812.newBuffer(LED_COUNT, 3)
      end
   end)
end

function connect(conn)

print("c2", conn);
    conn:on ("receive",
    function (cn, req_data)
        if req_data:find('^GET /.* HTTP/1.1') ~= nil then
            local key = string.match(req_data, "key=(%a+)")
            local color = string.match(req_data, "color=(%a+)")

            if key == SECRET_KEY then

                local r, g, b = nil, nil, nil
                if color == "red" then
                    r = 255
                    g = 0
                    b = 0
                elseif color == "blue" then
                    r = 0
                    g = 0
                    b = 255
                elseif color == "green" then
                    r = 0
                    g = 255
                    b = 0
                end
                if r ~= nil then
                    tmr.alarm(2, 100, tmr.ALARM_SINGLE, function()
                        --buffer = ws2812.newBuffer(LED_COUNT, 3);
                        buffer:fill(g,r,b); -- grb not rgb
                        print(buffer)
                        --ws2812.init()
                        ws2812.write(nil, buffer)

                    end)
                end

            end
        end

            -- Close the connection for the request
        cn:on("sent",function(c) print("c0", c);c:close() end)
        cn:send("<h1> 你好,NodeMcu。</h1>")

        -- Close the connection for the request
        --cn:on("sent",function(cnn) cnn:close() end)
    end)
    --conn:send("<h1> 你好,NodeMcu。</h1>")
end

wifi.setmode(wifi.STATION)
wifi.sta.config {ssid=SSID, pwd=SSID_PASSWORD}
wifi.sta.autoconnect(1)

wait_for_wifi_conn()

svr = net.createServer(net.TCP, 30)

if svr then
    svr:listen (80, connect)
end

感谢帮助=)

点赞