Opening gates using a phone Part 2 — Port Forwarding

Brian Dooley
3 min readJan 11, 2021

--

Opening the gate using my phone on my home network was exciting but quite limited unless I could do it from other networks as well. I needed to find a way to reach my home network from any internet connection. This is where port forwarding comes in.

Port forwarding redirects requests from the internet outside your local network to specific IP addresses within your network. I allowed external requests to access port 5000 of my home router’s external IP address to reach my gate application running on port 5000 internally.

Static IP address

The first action I took was to make sure that the Pi’s internal IP address remains static. It is not good to have internet requests directed to the Pi’s IP address if that address changes. Fortunately, this was simply a case of updating a setting in the Pi.

To do this, perform the following:

  • Note the Pi’s current internal IP address. I used the hostname -I command.
  • Edit the dhcpcd.conf file

sudo nano /etc/dhcpcd.conf

  • Scroll to the bottom of the file and add the following snippet of code

interface wlan0

static IP_address=<current IP address>/24

static routers=192.168.0.1

static domain_name_servers=192.168.0.1

interface = The network interface you are using. wlan0 for wifi networks or eth0 for ethernet networks.

static IP_address = The IP address to set your device to. It should be the address you noted in step 1 since your Pi is already using it.

static routers = The IP address of your gateway (likely the IP address or your router)

static domain_name_servers = The IP address of your DNS (likely the IP address of your router).

  • Exit the editor by pressing ctrl+x and then Y to confirm your changes.
  • Reboot the Pi.
  • After rebooting, enter hostname -I in a terminal, and it should output the same IP address you noted earlier.

Port forwarding

Once I made my internal IP address static, the next step was to forward external requests. This is where port forwarding comes in.

All routers should have some facility to allow port forwarding. In my case, it was a lot less complicated than I thought it would be. The method I used was:

  1. Access the router’s user interface by browsing to the router’s internal address.
  2. Log in to the router as admin. The username and password should be available from your internet provider.
  3. Navigate to the port forwarding (or mapping) section. This section can be in different locations depending on the router, but you should find it if you look around.
  4. Add a new port mapping and enter the details for the local static IP address I set up earlier. In my case, the details looked like this.

If you have an option for Port Type, select Port. The Public Port and Local Port should be the port your application is running on (5000) and, if you have an option for Service, make sure you select TCP.

Save the details entered. There should be no need to restart the router.

Wrap up

And that was it. I was able to test this out by finding my IPv4 IP address from https://whatismyipaddress.com/ .

I opened the gate by calling http://109.77.119.150:5000/zapper.

--

--