Close

VPN Light Switch LUA Script

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 08/15/2015 at 08:540 Comments

Here is some code to monitor the status of the VPN server. The RGB LEDs are common anode, with the anode connected to 3.3V (and a 100Ohm resistor on each leg connected to GPIO). The ESP module sinks the current:

LED on - gpio.write(3, gpio.LOW)
LED off - gpio.write(3, gpio.HIGH)
This is so the modules still boot without going into bootloader mode, while keeping the resistor values quite small (brighter LEDs).

Unfortunately because the ESP01 module only has two GPIOs, it is difficult to add both button and LED functionality at the same time. I will break these connections out later. But for the moment, the button is connected to GPIO 3 and the LED to GPIO 4.

The button is connected as follows:

                         VCC                  
                        +-+-+                 
                          |                   
                          |                   
                        +-+-+                 
                        |   |                 
                        |10K|                 
                        |   |                 
                        +-+-+                 
                          |                   
                          |                   
           +-------+      |                   
GPIO ------+  100  +------+                   
           +-------+      |                   
                          +                   
                           \  Switch          
                            \                 
                          +                   
                          |                   
                        +---+                 
                         +-+                  
                          +                   
                         GND

The code:

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(3, gpio.HIGH)
      gpio.write(4, gpio.LOW) 
    end 
    if string.find(c,"RECONNECTING") then 
      print("Standby") 
	  --gpio.write(3, gpio.HIGH) 
	  gpio.write(4, gpio.HIGH)
      state = 0 
    end 
    if string.find(c,"AUTH") then 
      print("Auth") 
      state = 1 
	  --gpio.write(3, gpio.LOW) 
      gpio.write(4, gpio.LOW) 
    end 
    end 
  ) 
  conn:on("disconnection", function(sck, c) 
    print("VPN Disabled") 
	--gpio.write(3, gpio.LOW) 
    gpio.write(4, gpio.HIGH) 
    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 

--Currently using GPIO3 as button, all LED references commented out
--gpio.mode(3, gpio.OUTPUT) 
gpio.mode(3,gpio.INT) 
gpio.mode(4, gpio.OUTPUT) 

connected = 0 
state = 0 
triggered = 0 

gpio.trig(3, "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 
) 

Note that when the VPN is in "standby", the service is still running, it just changes the IP tables so the VPN is no longer used. The service daemon will report that it is still running.

You can use LuaLoader to save the file to the ESP module so it runs on boot.

To stop it while testing just run:

tmr.stop(0)
conn:close() 

Discussions