Light Cube/Projektcode
Aus C3MAWiki
Weitere Optionen
Dies ist ein Projekt, an dem momentan aktiv gearbeitet wird. Wenn du Fragen hast oder mitarbeiten möchtest, wende dich an Berkay.
Dies ist der vollständige Projektcode. Die Projektseite: Light Cubes
ESP-Platine/Bauteil ESP NodeMCU Devboard
wifi.setmode(wifi.STATION)
wifi.sta.config("SSID", "PW")
ws2812.init();
local ledCount = 34;
local leds = string.char(10,00,0):rep(ledCount)
local hadIP = false;
local tick = false;
function readjson(data)
ok, json = pcall(cjson.decode, data)
if ok then
leds = ""
for ledid = 1, ledCount, 1 do
local ledData = json[ledid];
leds = leds .. string.char(ledData.g,ledData.r,ledData.b)
end
else
print("failed to decode!")
end
end
local pseudoRemote = {}
for ledid = 1, ledCount, 1 do
pseudoRemote[ledid]={}
pseudoRemote[ledid].r=100
pseudoRemote[ledid].g=0
pseudoRemote[ledid].b=ledid
end
local remoteData = cjson.encode(pseudoRemote);
print(remoteData)
local srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print("Got something...")
print(payload);
readjson(payload)
conn:send("HTTP/1.1 200 OK\n")
conn:send("Content-Type: text/html\n\n")
end)
end)
local led1 = 0;
local led2 = 0;
local led3 = 0;
function countBlinks(eingabe)
while(eingabe > 0) do
if(eingabe >= 100) then
led1 = led1 +1
eingabe = eingabe -100
else
if(eingabe >= 10) then
led2 = led2 +1
eingabe = eingabe -10
else
led3 = led3 +1
eingabe = eingabe -1
end
end
end
end
function update()
ws2812.write(leds)
end
local anSekunde = false;
function blinkIP()
anSekunde = not anSekunde
if(anSekunde) then
if led1 > 0 then
ws2812.write(string.char(0,20,0))
led1 = led1 -1
return
end
if (led2 > 0) then
ws2812.write(string.char(0,0,0) .. string.char(0,20,0))
led2 = led2 -1
return
end
if (led3 > 0) then
ws2812.write(string.char(0,0,0):rep(2) .. string.char(0,20,0))
led3 = led3 -1
return
end
tmr.alarm(0, 100, tmr.ALARM_AUTO, update)
else
ws2812.write(string.char(00,0,0):rep(ledCount))
end
end
function wlaninit ()
if(not wifi.sta.getip())then
tick = not tick;
if(tick)then
ws2812.write(string.char(0,0,20):rep(ledCount))
else
ws2812.write(string.char(0,0,0):rep(ledCount))
end
return;
else
if not hadIP then
hadIP = true
local IP = wifi.sta.getip()
local o1,o2,o3,o4 = IP:match("(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)%.(%d%d?%d?)" )
local eingabe = tonumber(o4)
countBlinks(eingabe);
tmr.alarm(0, 1000, tmr.ALARM_AUTO, blinkIP)
end
end
end
tmr.alarm(0, 100, tmr.ALARM_AUTO, wlaninit)