I want to controll an LED Bulb with an Atmega 8 and an ESP8266.
I have an older Version but this is not running really safe so I tried to work on the lua thing today with the ESPlorer.
I want to have 4 sliders to control each Color. Then I want ot be able to send this Color with UART to the Atmega.
A simple UART Communication I have tested and it is working but I am not sure how I am able to send the variables from the sliders.
Here is the code :
wifi.setmode(wifi.SOFTAP) -- set mode as access point
wifi.ap.config({ssid= "ESP_XXX", pwd="123456789"}) -- configure own SSID as ‘ESP_TEST’ and password as
uart.setup(0, 9600, 8, 0, 1, 1 ) -- Change the baudrate to 115200 neu 9600
uart.write(0, "Test")
tmr.delay(1000000)
gpio.mode(3, gpio.OUTPUT)
-- Let's start off with the light off
gpio.write(3, gpio.LOW)
-- Simple web server for the ESP8266
-- Author: NodeMCU.com
--srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,request)
print(request)
uart.write(0, j)
_, j = string.find(request, 'color=')
if j ~= nil then
command = string.sub(request, j + 1)
if command == 'on' then
--just for testing
gpio.write(3, gpio.HIGH)
tmr.delay(1000)
uart.write(0, "color|")
uart.write(0, "0") --ID
uart.write(0, "|")
uart.write(0, "100") --R
uart.write(0, "|")
uart.write(0, "150") --G
uart.write(0, "|")
uart.write(0, "200") --B
uart.write(0, "|")
uart.write(0, "255") --W
else
gpio.write(3, gpio.LOW)
uart.write(0, "TestOff")
uart.write(0, "TestOff")
end
end
conn:send('<!DOCTYPE html>')
conn:send('<html>')
conn:send('<body style="background:#000000">')
conn:send('<title></title>')
conn:send('<meta charset="utf-8" />')
conn:send('<meta name="viewport" content="width=device-width, initial-scale=1" />')
conn:send('<script>')
conn:send('function myFunction(id) ')
conn:send('{')
conn:send('var r = document.getElementsByName("dr")[0].value;')
conn:send('var g = document.getElementsByName("dg")[0].value;')
conn:send('var b = document.getElementsByName("db")[0].value;')
conn:send('var w = document.getElementsByName("dw")[0].value;')
--conn:send('var url = "/set="+id+","+r+","+g+","+b+","+w;')
conn:send('var url = "/color|"+id+"|"+r+"|"+g+"|"+b+"|"+w;')
conn:send('var loader = document.getElementById("loader");')
conn:send('loader.src = url;')
conn:send('}')
conn:send('</script>')
conn:send('<table style=width:100%>')
conn:send('<tr>')
conn:send('<th colspan=2>')
conn:send('RGBW lamp')
conn:send('</th>')
conn:send('<tr>')
conn:send('<th style="border:2px solid black;" width=5% bgcolor="red">')
conn:send('</th>')
conn:send('<th>')
conn:send('<input type="range" name="dr" min="0" max="255" value="128" style="width:95%" />')
conn:send('</th>')
conn:send('</tr>')
conn:send('<tr>')
conn:send('<th style="border:2px solid black;" width=5% bgcolor="green">')
conn:send('</th>')
conn:send('<th>')
conn:send('<input type="range" name="dg" min="0" max="255" value="128" style="width:95%"/><br>')
conn:send('</th>')
conn:send('</tr>')
conn:send('<tr>')
conn:send('<th style="border:2px solid black;" width=5% bgcolor="blue">')
conn:send('</th>')
conn:send('<th>')
conn:send('<input type="range" name="db" min="0" max="255" value="128" style="width:95%"/><br>')
conn:send('</th>')
conn:send('</tr>')
conn:send('<tr>')
conn:send('<th style="border:2px solid black;" width=5% bgcolor="white">')
conn:send('</th>')
conn:send('<th>')
conn:send('<input type="range" name="dw" min="0" max="255" value="128" style="width:95%"/><br>')
conn:send('</th>')
conn:send('</tr>')
conn:send('<tr>')
conn:send('<th colspan=2>')
conn:send('<p><button onclick="myFunction(0)" style="width:100%">SET Color 1</button></p>')
conn:send('<p><button onclick="myFunction(1)" style="width:100%">SET Color 2</button></p>')
conn:send('</th>')
conn:send('</tr>')
conn:send('</table>')
conn:send('<iframe width="1" height="1" src="" id="loader" frameborder="0"></iframe>')
conn:send('</body>')
conn:send('</html>')
end)
end)
I want to be able to set 2 Colors thats why I have 2 Buttons.
If I set all sliders to 50 and press the "Set Color 1" button I want the ESP to send this Message with UART "Color,0,50,50,50,50"
If I set all sliders to 70 and press the "Set Color 2" button I want the ESP to send this Message with UART "Color,1,70,70,70,70"
Whats is the best way for me to do this?