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

时间:2018-04-14 09:42:35

标签: lua esp8266

我写了一个LUA脚本,当我调用网址时,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 ("Waiting for Wifi connection")
      else
         tmr.stop (1)
         print ("ESP8266 mode is: " .. wifi.getmode ( ))
         print ("The module MAC address is: " .. wifi.ap.getmac ( ))
         print ("Config done, IP is " .. 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> Hello, NodeMcu.</h1>") 

        -- Close the connection for the request
        --cn:on("sent",function(cnn) cnn:close() end) 
    end)
    --conn:send("<h1> Hello, 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

感谢帮助=)

0 个答案:

没有答案