Close

Updated LUA Code

A project log for VPN Light Switch

A Wi-Fi based LED switch to check that your VPN is running on your router and control it.

bleckyBlecky 01/27/2016 at 11:390 Comments

Updated init.lua for the PCB with the Osram LED:

function vpnconnect ()
  conn=net.createConnection(net.TCP, 0)
  conn:on("receive", function(sck, c)
    if string.find(c,"CONNECTED") then
      print("Connected")
      state = 1
      gpio.write(5, gpio.HIGH) --RED
      gpio.write(6, gpio.LOW) --GREEN
      gpio.write(7, gpio.HIGH) --BLUE
    end
    if string.find(c,"RECONNECTING") then
      print("Standby")
      gpio.write(5, gpio.HIGH) --RED
      gpio.write(6, gpio.HIGH) --GREEN
      gpio.write(7, gpio.LOW) --BLUE
      state = 0
    end
    if string.find(c,"AUTH") then
      print("Auth")
      state = 1
      gpio.write(5, gpio.LOW) --RED
      gpio.write(6, gpio.HIGH) --GREEN
      gpio.write(7, gpio.LOW) --BLUE
    end
    end
  )
  conn:on("disconnection", function(sck, c)
    print("VPN Disabled")
    gpio.write(5, gpio.LOW) --RED
    gpio.write(6, gpio.HIGH) --GREEN
    gpio.write(7, gpio.HIGH) --BLUE
    connected = 0
    end
  )
end


function buttonpress (level)
  if triggered == 0 then
    if level == 0 then
      if connected == 1 then
        triggered = 1
        tmr.alarm(1, 200, 0, function() --debounce delay
          if state == 0 then
            conn:send("hold off\r\n hold release\r\n")
            state = 1
          else
            conn:send("hold on\r\n signal SIGHUP\r\n")
            state = 0
          end
          triggered = 0
          end
        )
      end
    end
  end
end


--Don't forget to associate with AP with (once off) commands:
--wifi.setmode(wifi.STATION)
--wifi.sta.config("accesspointname","yourpassword")
--wifi.sta.connect()
--print(wifi.sta.status()) <--5 means associated
--print(wifi.sta.getip())


gpio.mode(4,gpio.INT)
gpio.mode(5, gpio.OUTPUT) --RED
gpio.mode(6, gpio.OUTPUT) --GREEN
gpio.mode(7, gpio.OUTPUT) --BLUE

--Set output low to turn on LED
gpio.write(5, gpio.LOW) --RED
gpio.write(6, gpio.HIGH) --GREEN
gpio.write(7, gpio.HIGH) --BLUE


connected = 0
state = 0
triggered = 0


gpio.trig(4, "down", buttonpress)


tmr.alarm(0, 1000, 1, function()
  if connected == 0 then
    vpnconnect()
    conn:connect(7505,"192.168.1.1")
    connected = 1
  end
  if connected == 1 then
    conn:send("state\r\n")
  end
  end
) 

Discussions