程式範例

開啟SoftAP Mode , 並開啟DHCP.


wifi.setmode(wifi.SOFTAP)

cfg =

{

ip="192.168.10.1",

netmask="255.255.255.0",

gateway="192.168.10.1"

}

wifi.ap.setip(cfg)

cfgap={}

cfgap.ssid="ws2812"

cfgap.pwd="12345678"

wifi.ap.config(cfgap)

dhcp_config ={}

dhcp_config.start = "192.168.10.100"

wifi.ap.dhcp.config(dhcp_config)

wifi.ap.dhcp.start()

port = 9567

r=0

g=0

b=0

o=0

function receiveData(conn, data)

local a= string.find(data, "\n", 1)  

local led=string.sub(data, 1, 4)

if led == "led1" then

   r = string.sub(data, 6, a-1)

   leds_grb = string.char(g,r,b,g,r,b,g,r,b,g,r,b,g,r,b,g,r,b,g,r,b,g,r,b)

   ws2812.write(4,leds_grb)

elseif led == "led2" then

   g = string.sub(data, 6, a-1)

   leds_grb = string.char(g,r,b,g,r,b,g,r,b,g,r,b,g,r,b,g,r,b,g,r,b,g,r,b)

   ws2812.write(4,leds_grb)

else

   b = string.sub(data, 6, a-1)

   leds_grb = string.char(g,r,b,g,r,b,g,r,b,g,r,b,g,r,b,g,r,b,g,r,b,g,r,b)

   ws2812.write(4,leds_grb)

end

end

srv=net.createServer(net.TCP)

srv:listen(port,function(conn)

print("RoboRemo connected")

conn:send("dbg connected ok\n")

conn:on("receive",receiveData)  

conn:on("disconnection",function(c) 

    print("RoboRemo disconnected")

     leds_grb = string.char(o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o,o)

    ws2812.write(4,leds_grb)

end)

end)