3

I have a NodeMCU (ESP8266) board that I use to control some devices over the internet. To be able to access it I have to enable port forwarding on my router and set the port manually like in the image below:

enter image description here

But with Smart Plugs (that use a cloud based app) I don't have to set up anything on my router. I am only required to type my Wi-Fi password. So my question is how Smart Plugs are accessed over the router (why they don't need manual port forwarding setting), and how to apply this to my NodeMCU so I don't have to set port forwarding manually.

1 Answer 1

7

Port forwarding is required to allow a device outside your router to access a device inside a router that performs NAT (Network Address Translation).

When a NodeMCU is acting as a simple server to respond to requests, it is inside your network, and so the port-forwarding is required so that your phone or computer on the Outside can access it.

However, for the cloud-based "smart" devices, it is different. The smart device opens a connection from itself to a server (the cloud) outside your network. In this case, the router understands that a new two-way connection is created between the cloud server and your smart device.

The only communication to your smart device is from the cloud server to the smart device. If you have an app on your mobile device or computer, that app or website only accesses the cloud server, when then passes the instruction on to your smart device. Reverse direction for status info from the smart device to your app, etc.

These smart devices rely on the infrastructure of their creators to continue to work. There are no LAN-to-LAN connections from the client to the device. Everything goes through the Internet to the remote cloud server, and back.

Many home routers support a protocol called uPNP, that allows a device inside the network to request port forwarding to itself. Some routers require this to be explicitly enabled, because it's a security risk to allow any device to be able to open holes in your firewall/router.

To have your NodeMCU device do this, look for libraries for ESP8266 that implement uPNP. I did a quick search and found https://github.com/ofekp/TinyUPnP though I cannot vouch for it's effectiveness or safety.

4
  • Great answer, thanks for the quick response. Mar 1, 2019 at 21:57
  • 1
    MQTT is another protocol worth looking at. It's commonly used in IoT devices (and supported cloud platforms such as the Amazon/Azure IoT offerings) to solve the same problem.
    – oznu
    Mar 2, 2019 at 4:35
  • Another question, To avoid port forwarding I made a website where the user sets the device status to ON and OFF. The device status is then stored in a MySQL database. The devices then makes a GET request to the database and retrieves the device status and sets the device ON or OFF. The database is queried every 10 seconds for one device. In your opinion is this approach practically and how many devices can work on one database. Sorry for bothering you. Apr 14, 2019 at 8:31
  • 1
    Whether it's practical is up to the available resources: Your database and server, the bandwidth required, etc. Apr 15, 2019 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.