As the title says... Chat on...

User avatar
By Haschu87
#48444 Hi Everybody,

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 :

Code: Select allwifi.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?
User avatar
By Haschu87
#48501 OK,

I worked a Little bit on this.
Now I am able to do it like this. i am trying just one slider at the Moment.
It works 90%. I can save the values and read it back.
But if you press the button then the sliders jumps back to the last value and doesnt stay on the actual value.
How can I solve this?

Code: Select allwifi.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
    outpin=4 -- Select right IO index !! Here is settings for GPIO2 (Lua build 20141219)

uart.setup(0, 9600, 8, 0, 1, 1 ) -- Change the baudrate to 115200 neu 9600

--neu zum speichern--
file.open("valueR.txt","r")
--file.writeline(200)
xxxR = tonumber(file.readline())
file.close()

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive",function(conn,payload)
    --next row is for debugging output only
    print("-----")
    --print(payload)
    print("-----")


    function ctrlpower()
    pwm.close(outpin)
    gpio.mode(outpin,gpio.OUTPUT)
    dotaz=string.sub(payload,kdesi[2]+1,#payload)
    if dotaz=="ON"  then gpio.write(outpin,gpio.HIGH)return end
    if dotaz=="OFF" then gpio.write(outpin,gpio.LOW)return end
    if dotaz=="FLC" then pwm.setup(outpin,2,512)pwm.start(outpin)return end
   
    --pwm.setup(outpin,1000,dotaz*10)
    --pwm.start(outpin)
    end
    --parse position POST value from header
    kdesi={string.find(payload,"pwm_a=")}
    --If POST value exist, set LED power
    if kdesi[2]~=nil then ctrlpower()
    --uart.write(0, payload) --R

   
    --file.open("valueR.txt","r")
    --file.writeline(payload)
   
    end


    --neu
    file.open("valueR.txt","r")
    xxxR = tonumber(file.readline())
    file.close()
    --neu


    testwert = 200

    conn:send('HTTP/1.1 200 OK\n\n')
    conn:send('<!DOCTYPE HTML>\n')
    conn:send('<html>\n')
    conn:send('<head><meta  content="text/html; charset=utf-8">\n')
    conn:send('<title>ESP8266</title></head>\n')
    conn:send('<body><h1>Sample GPIO output control</h1>\n')
    conn:send('<h3>For Lua NodeMcu 0.9.2 build 20141219 only !!!</h3>\n')
   conn:send('<h3>Tested with IE 11. and Chrome 39.</h3>\n')
    conn:send('<IMG SRC="http://esp8266.fancon.cz/common/led.gif" WIDTH="150" HEIGHT="101" BORDER="1"><br><br>\n')
    conn:send('<form action="" method="POST">\n')
    conn:send('<input type="range" name="pwm_a" min="0" value="'.. xxxR ..'" max="255">\n')
    --conn:send('<input type="range" name="pwm_b" min="0" max="255">\n')
    --conn:send('<input type="range" name="pwm_c" min="0" max="255">\n')
    --conn:send('<input type="range" name="pwm_d" min="0" max="255">\n')
    conn:send('<input type="submit" name=""\n')
    xxxR = tonumber(dotaz) --NEU

    --neu2
    file.open("valueR.txt","w")
    file.writeline(dotaz)
    file.close()
    --neu2
    --neu
    --conn:send('</form>\n')
    --neu
    conn:send('</body></html>\n')
    conn:on("sent",function(conn) conn:close() end)
    conn = nil

    --print("AAA")
    print(dotaz) --DAS HIER SIEHT GUT AUS
    print(kdesi[1])
    print("AAA")

   
   
    end)
end)