function wait_for_wifi_conn ( )
   tmr.alarm (1, 1000, 1, function ( )
      if wifi.sta.getip ( ) == nil then
         print ("Waiting for Wifi connection")
         --led rot
         ws2812.writergb(4, string.char(50, 0, 0))
      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 ( ))
--         print ("join Multicast 192.168.1.90")
--         net.multicastJoin(wifi.sta.getip(), "192.168.1.90")
--       led gruen
         ws2812.writergb(4, string.char(0, 50, 0, 0,0,0))
         tmr.delay(1000000)
      end
   end)
end

--empfangene daten zerlegen,led ansteuern
function parse (payload)
    r = string.sub(payload,1,3)
    g = string.sub(payload,4,6)
    b = string.sub(payload,7,9)
    r1 = string.sub(payload,10,12)
    g1 = string.sub(payload,13,15)
    b1 = string.sub(payload,16,18)
    rgb = string.char(r,g,b,r1,g1,b1)
    print(rgb)
    ws2812.writergb(4,rgb)
end

--ausschalten
function switchoff ()
    --node off
    print("off")
    ws2812.writergb(4, string.char(50, 0, 0, 0,0,0))
    tmr.delay(200000)
    ws2812.writergb(4, string.char(0, 0, 0, 0,0,0))
    tmr.delay(200000)
    ws2812.writergb(4, string.char(50, 0, 0, 0,0,0))
    tmr.delay(200000)
    ws2812.writergb(4, string.char(0, 0, 0, 0,0,0))
    --Led-Controller OFF
    gpio.write(7, gpio.LOW)
    tmr.delay(10000)
    --Power off
    wifi.sta.disconnect()
    gpio.write(5, gpio.LOW)
end

akku = 0
wifi.sta.disconnect()
--blau fuer on
ws2812.writergb(4, string.char(0, 0, 50, 0,0,0))
tmr.delay(1000000)
if (adc.readvdd33() > 3650) then
	--LED-Controller Ein
	gpio.mode(7, gpio.OUTPUT)
	gpio.write(7, gpio.HIGH)
	--blau fuer on
	ws2812.writergb(4, string.char(0, 0, 50, 0,0,0))
	tmr.delay(1000000)
	--rot fuer batt low, gruen fuer batt ok    
    akku = 1
    ws2812.writergb(4, string.char(0, 50, 0, 0,0,0))
    print("voltage "..adc.readvdd33())
    tmr.delay(1000000)
    --led off
    ws2812.writergb(4, string.char(0, 0, 0, 0,0,0))
    wifi.setmode(wifi.STATION)
    wifi.sta.config("cocktail","cocktailglas")
    wifi.sta.connect()
    wait_for_wifi_conn()
    
        --get new ip after lost
tmr.alarm(0, 30000, 1, function() 
	print("reconnect")
	if (akku == 1) then
		if wifi.sta.getip ( ) == nil then
		wifi.sta.connect()
		wait_for_wifi_conn()
		end
	end
end )
 
else 
    akku = 0
    print("battery low. sleep")
    print("voltage "..adc.readvdd33())
    --node off
	switchoff()
end

tmr.delay(1000000)




--send udp from linux: 
--echo -n "170000070150000050" >/dev/udp/172.23.92.16/31302
srv = net.createServer(net.UDP) 
srv:on("receive",function(srv,payload)  
--     print("payload=",payload)
--     print("len",string.len(payload))
     if (string.len(payload)==18) then parse(payload)
     else 
        if (string.len(payload)==6) then 
            if (string.sub(payload,1,6) == "offoff") then 
                switchoff()
            end  
        end
     end
end)
srv:listen(31302)


--akkustand: abschalten nach 15 min 
tmr.alarm(2, 900000, 1, function() 
    print("akku")
    wifi.sta.disconnect()
   if (adc.readvdd33() > 3700) then
    print("ok")
    akku = 1
    wifi.sta.connect()
    else
        akku = 0
        print("battery low. sleep")
        print("voltage "..adc.readvdd33())
		switchoff()
    end
end )






