Tags: , , , , , ,

When you want to connect to a device outside your Wifi network, you need to set up port forwarding. Once your Raspberry Pi or ESP is connected to your Internet router, you can access it from anywhere by configuring port forwarding.

N.B: For cybersecurity reasons, it can be very dangerous to set up a port forwarding without taking care. For this reason, you should only set up a redirection if you’re sure of what you’re doing, and you should put in place the necessary safeguards (password, etc.).

Principle

On the same network, devices will communicate with their local addresses (e.g. 192.168.x.xxx). Only the router has a public address. When we configure port forwarding, we ask the router to open a public port and link it to a local address.

Ex:

  • public router IP: 82.95.256.144
  • router local IP: 192.168.1.254
  • local device IP: 192.168.1.17 (default port 80)

So if we redirect port 600 to the device’s IP address, we’ll be able to access the device by typing 82.95.256.144:600.

This procedure can be very useful for developing advanced home automation or IoT functions requiring remote access on Raspberry Pi, ESP8266, EP32 or any other device with an Internet connection.

Find your router’s IP address

  • Under Raspberry Pi or Linux
ip route
raspberry-pi-get-ipaddress Configuring port forwarding
  • Under Windows
ipconfig
windows-ipconfig Configuring port forwarding

These commands give, among other things, the local IP address of your router (here 192.168.1.254).

You can use the netstat -a command to get a list of connections to the device and the ports used.

Configure port forwarding

In your browser, you can enter the IP address to access the router’s home page. You will be asked for your login details.

N.B: The port forwarding procedure differs from one box to another. If you search for “box-name Port Forwarding” on the Internet, you should come across the procedure to follow.

router-welcome-page Configuring port forwarding

Luego busca la opción “Redirección de Puertos” o NAT/PAT. En un bbox, Servicios de la Box > Redirección de Puerto> Configuración.

Click on “Add a rule” and enter the redirection information

From the previous example:

  • Rule name: MyPortForwarding
  • Protocol: TCP
  • Device local IP address: 192.168.1.17
  • Internal port: 80 (can be a set value)
  • External port: 600

N.B: The external port can take a value between 0 and 65535. Port numbers below 1024 or above 49150 are reserved for the system.

router-port-forwarding Configuring port forwarding

Applications

Sources