Control ESP8266 Over the Internet (from Anywhere)

149,555

648

99

Published

Introduction: Control ESP8266 Over the Internet (from Anywhere)

There are but a few things better than (succesfully) programming and using your Arduino. Surely one of those things is using your ESP8266 as an Arduino with WiFi!

In this instructable I will show you an EASY way to get the ESP8266 working as a web server AND accessing that server from anywhere (over the internet)

Also if you find this instructable interesting, perhaps you'll like some of my others:

EASY Arduino OLED sensor data display

How to make a high voltage power supply

How to send data from Arduino to excel (and plot it)

How to display Arduino sensor readings on Nokia 5110 display

Step 1: What You'll Need:

Since the esp8266 NodeMcu is so cheap, I highly recommend buying one. You can simply plug it into your PC and use it as an Arduino. No weird commands or anything "unknown".

Step 2: Arduino IDE+ESP8266:

-Open up the Arduino IDE

-Go to File->Preferences->Additional Boards Manager URLs: http://arduino.esp8266.com/stable/package_esp8266c... ->click OK

-Close the IDE and open it up again

-Go to Tools->Board (where you'd select your version of Arduino)->Boards Manager, find the ESP8266 and click Install

You now should be able to use the ESP8266 as an Arduino. Simply select the NODEMCU 1.0 as your board and you should be ready to code. (if it doesn't work, try the 0.9 version)

Step 3: "Arduino" Code:

Since the code gets messed up when pasted, I have included it as an txt file. Download it and paste it into your Arduino IDE.

The code is commented, so you should have no trouble understanding what to change to suit your needs.

Step 4: Access From Anywhere:

First you need to go to www.whatsmyip.org and copy your IP.

You should now open up your router settings. (google how to do this for your router) Open up your browser and type in the address for your router. There you will find some settings, including something along the lines of Forwarding or port forwarding.

The important thing to note here is the "Service port" and the "IP address".

In "Service port", you should type the port that you specified in your Arduino code. (mine was 301)

In "IP Address", you should type: IP(from whatsmyip) : ServicePort

so it should look something like xxx.xxx.xx.xx:301

Just leave the other settings on Default. (or check how to port forward for your router)

Step 5: What Now???

Now...just type the xxx.xxx.xx.xx:301 into your browser and you should have a basic webpage with two buttons on it. I'm sure you can figure out how to use those.

You can type the address into your cellphone while away from home and access the ESP8266 that way. Perhaps instead of turning an LED on and off, try telling it to turn on your AC on those hot summer days.

5 People Made This Project!

Recommendations

  • Oil Contest

    Oil Contest
  • Woodworking Contest

    Woodworking Contest
  • Clocks Contest

    Clocks Contest
user

We have a be nice policy.
Please be positive and constructive.

Tips

2 Questions

please help. i follow the instruction but nothing happen

when i try connect to the xxx.xxx.xx.xx:301 it say: "This site cant be reached!"

thanks very much!

Screenshot_20180408-175829_LI.jpg

Follow the below setting

1 Make sure you have forwarded the right port, double check that you mentioned same port number in sketch and Router setting as well.

2 Check that you mentioned the correct IP address of your wemos in router setting

Lets say Wemos IP address is 192.168.0.323

and port number you mentioned in sketch is 301

so set them in router setting according to following way:

Service port 301

internal port 301

IP address 192.168.0.323

protocol all

status enable

after completion of these settings check the wan ip address of the router which also can be found by writing "what is my IP address" on Google

After finding that copy this IP address and paste to browser followed by semicolon and port number

lets say you found 34.56.23.67

Then paste it like 34.56.23.67:301

I hope it will help you

1 more answer

my Virtual Server setting:

Untitled.png

when i m checking my ip at provided site by you, its shows me "

27.49.24.51"

and my router setting says me that this ip is incorrect

is there any other way to do this?

if yes then please let me know my id: aakash.tandew1996@gmail.com

99 Comments

Go for DNS registration, it is absolutely free

It will provide you with a domain name and using that you can access to your home network even when your IP address is dynamically changing

Ask your isp. Some isp's tend to change public ip and some others don't. But to be sure that you won't have issues in the future is best to get static public ip

I have the same question =/

user

Excellent! easy to follow, straight forward, and the ino is to the point.

Thanks!

Doesn't the Port Forwarding open your firewall to be hacked?

If I want to use my hosted server instead of a server on the nodeMCU, can I simply put the web address in the "host" variable? And then set the server to port 80?

1 reply

Have you tried this? And did it work? I am also curious on how to do this

Do I need to connect my laptop to same hotspot, where ESP8266 is connected???

1 reply

No, if you did port forward properly and yes if you didn't. But then you will have to use local ip as well.

Exactly what I needed and resolved a freeze up issue I mucked around with all day yesterday.... Thanks

can you please tell how can we do this on mobile hot-spot. Will be very great of you :)

Thanks - this is great. I tried about 20 examples for the NodeMCU but could not get outside my LAN. Yours Works!
Question: The third const char defined in the sketch is "host" and i discovered I have to put the LAN IP address for the NodeMCU here or the server will NOT work outside my LAN. I'm trying to use your code in stuff I'm making but I can't figure out where in your code that "host" char is used????

Hello everybody!

xxx.xxx.xx.xx: WAN ip address of main router.

192.168.xx.xx: LAN ip address.

Good luck!

This works exactly as the instructions say. However, the board will work for a while - maybe 10 minutes - then stop responding. I am new to this and have seen all sorts of posts of similar problems but these are using an ESP8266 being controlled by an Ardunio. Has anyone here experienced it with this approach?

1 reply

I have a solution.

The cycle while (!client.available()) { //code } can be an infinite loop.

I reccomend to eliminate it if unnecessary or to include inside a command that can end the cycle if it became infinite.

I hope this will help you!

Wow, there is some inaccurate info floating around in this comment list.

1) Don't mislead ppl by saying if they have a dynamic IP address, it may change daily....yes, it may, but the *reality* is that it will remain the same for many months. It generally changes due to a) equipment failure/outtage, b) a forced reset/troubleshooting from the ISP, c) a user initiated address release. I've had 5 different ISPs over the course of 20 years, never has my address changed daily or even weekly. The norm has been on the average of about 9 months. Point is, if you want to configure the system with the current IP address, chances are it will keep that IP address for quite a while. During that time, you can try different schemes to reserve the same IP address.

2) One method of ensuring a 'static' IP address is to sign up with a service such as No-IP or DynamicDNS and chose a fully qualified domain name. IIRC, both of these orgs offer at least one free name that you can manage. Now that you have a globally recognized name, you can use either the feature on your router or, an app supplied by your ip address service to ensure that your global name is always mapped to your ISP provided IP address no matter how many times your ISP decideds to change your global IP address,

Both no-ip and Dynamic DNS offer detailed 'how tos' in configuring your 'static ip address' and maintaining it.

Good luck.

3 replies

That is what you experienced. Depending on what country you live in and what kind of structure your ISP is using you might get ip change several times even during the day. I know a certain ISP doing it to charge users to get static ip.

no-ip is way to go on that kind of situations as you mentioned.

thank you. You are correct. My comments were based on experience in three countries (US, Greece, Italy) but colleagues in other countries have indicated situations as you describe. Depending on the size of the ISP, regions in a country may be different than 'an entire country.' Thanks for pointing that out.