Introduction: Control ESP8266 Over the Internet (from Anywhere)

Picture of 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:

Picture of 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".

I hightly recommend buying the ESP8266 NodeMcu from this link:ESP8266

If you use the code (send the seller a message when you buy it) saying "Instructables", he will make sure to quickly send you a top quality ESP8266.

Step 2: Arduino IDE+ESP8266:

Picture of Arduino IDE+ESP8266:

-Open up the Arduino IDE

-Go to File->Preferences->Additional Boards Manager URLs: ->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:

Picture of Access From Anywhere:

First you need to go to 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

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

Step 5: What Now???

Now...just type the 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.


vinay3003 (author)2017-12-26

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

Fwoosley (author)2017-12-18

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????

ThienABCD (author)2017-12-14

Hello everybody! WAN ip address of main router.

192.168.xx.xx: LAN ip address.

Good luck!

filmote (author)2016-05-13

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?

CiroA5 (author)filmote2017-12-06

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!

nahuel.sebastian.10 (author)2017-11-14

i need a public static ip?

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 =/

three_jeeps (author)2016-04-19

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.

FagnerM3 (author)three_jeeps2017-11-28


sipek (author)three_jeeps2016-05-01

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.

three_jeeps (author)sipek2016-05-01

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.

antechro (author)2017-10-20

I have a Wemos D1 mini.The port is 80 and I don't know how chage it. I mean to change inside Wemos not in IDE. If I chane in IDE for exemple from 80 to 301 don't work. I want these because I have in the same network an IP NVR which use also port 80.Have you any idea.

FagnerM3 (author)antechro2017-11-28

Did u try with AT command? Like, AT+CIPSERVER=1, NumberPort .

SushantN6 (author)2017-11-16

Hi, CrtSuzniK,

First of all thanks for this instruction,

However, i would like to know, if this can be done via mobile hotspot ?



sanchitagrawal4 (author)2017-07-03


I am using my mobile hotspot for internet connection and not wifi router. So how can I control ESP8266 from anywhere in this case?

Did u find how to do it using Hotspot ?

leonbernhard (author)2017-10-03

the Code was really precise and understandable, but now I want to control several

LED`s ( ESP 8266 12-E ), should I just copy the Gpio`s or the whole Code ?

rawest (author)2017-09-27

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?

carlrajack (author)2016-06-01

Sorry but I have not been able to get off the ground. I plugged the NodeMCU board into my PC's USB port and first of all I do not think it is being recognized. Anyway, I followed your instructions with the Arduino IDE and downloaded your code. I did change the serial speed to 9600 bps in the IDE. The code validated okay but did not upload to the ESP8266 with the following errors .

"error: Failed to open COM6

error: espcomm_open failed

error: espcomm_upload_mem failed"

Any ideas on how I can communicate with the board ? (Arduino IDE is running on windows 10 and the "ports" option under "tools" is greyed out so I am unable to change it)

p.s I purchased three boards so it is hardly unlikely that all three are faulty.

ecuadan (author)carlrajack2017-09-25

Make sure that Upload Speed = 115200

What the ESP8266 board is asking for is:


So when you try to read the Serial Monitor make sure to also select baud9600

CrtSuznik (author)carlrajack2016-06-20

try using a different version of the IDE. The newer ones might have some problems working with it. (also be sure to backup everything so it doesn't get deleted when installing the different version)

sobellinni made it! (author)2017-08-20

Great article, but some corrections.
I have the LED connected to D7 which for some reason maps to Arduino pin 13
Port forwarding works with your INTERNAL ADDRESS, so if your serial port connection with the board says it is, then this is the address you forward with port 301

then you can access the board with your external address you get from the Web service

henkpetersen (author)2017-07-10

Can't get mine to connect, just typing ........ all the time. ssid and password are correct. Also tried without password on the network and still the same

gabif6 (author)2017-06-25

hi guys, i've been trying very hard to make this work. however, i found that a configuration in the router may be different. the protocol in the router shoud be "TCP", not "ALL". In my case, i configured it as "ALL" and it wouldn't work.

the tutorials are great and excellent explained, though, just i thought i could help a little too!!

RaveeshS2 (author)2017-06-11

Thanks a lot this really helped!! I was only stuck on adding the service port on my router. Which I had to google and had to put some efforts. Figured out that from Netgear website.

To all those who dont know how to find a service port just type netstat -a in cmd you will get the list or just google . Go to your router settings mostly by typing in your browser. Go to port forwarding and select your ESP8266 device and add a port. It may take some time to display in your list.

lfabresm made it! (author)2017-05-26

works at the first try! it's a nice start :)

PapaDrag0n (author)2017-03-30

Using an ESP8266 12E I had to change ledpin to be LED_BUILTIN. Also i swapped the low and high in the digitalWrite statement.

This guide really helped, thanks!

RuneP (author)2017-03-27

Do you know if its possible to have the led on only when a web button is pressed and off when released?

OsmanV1 (author)2017-03-20

Connecting to myNetworkName


and newer connected.

What s my mistake?

g01d10x (author)OsmanV12017-03-22

I had the same thing, fixed it by typing in the network name again, including all characters (i.e. "TELUS3302-2.4G")

bill1959parker (author)2017-03-02

Hi can you please tell me can I switch this from anywhere or is it only local over my wifi ? I can use it over my wifi but not from anywhere could you please tell me what I am doing wrong. Thanks.

NovaZ (author)2017-02-12

If you are behind firewall and your IP is not public, than I suggest to use a ThingSpeak.readFloatField command to control the Wemos .

For example you will update you channel with value 10 and you will have wemos set to every minute check the value, if it is 10 then turn light on, change value back to 0, and continue with checking etc..

MarcoR5 (author)2016-06-30

Hi and sorry if my question is stupid. I can access my web server from within my LAN ( and Port 666 is opened. I get from but when I try to access chrome returns "ERR_CONNECTION_REFUSED". Thanks in advance

i also try this.but it work without internet connections.led is not blink when i connect to i can not blink it from anywhere.please help me

Listeningt (author)MarcoR52016-10-14

I also have your probem. If you can fix it, please help me at thank you so much.

MarcoR5 (author)MarcoR52016-06-30

I found the problem. can be accessed only from outside the LAN. Hope this can help someone else.

samitha rajakaruna (author)2016-12-29

i also try this.but it work without internet connections.led is not blink when i connect to i can not blink it from anywhere.please help me

DrChrisUK (author)2016-12-28


ron.wood.520 (author)2016-12-03

I did make this and I even rewrote the code so there is only 1 button to push and it will toggle between off and on. But I have another idea and I am new to the esp8266, so I figured I would ask here. what I want to do is to take 2 esp8266 and make like a led flashing (Morse Code) toy. there will be 2 units, with 2 leds. grn led will flash as you push the switch so you can see the flash code you are sent to the other unit. the 2nd unit will flash the red led as the 1st unit button is pushed. And Vise Versa. I do not want to set one up at a server, I would like both to be clients and connect to the same home network. This is the first step to another project but I do not know if this can be done. I welcome any and all help/suggestions

stannickel (author)2016-10-08

Thanks for this fantastic Instructable. I found one mistake in the code. You defined int ledPin = D3. An "int" requires a number, so "D3" returns an error. Then I tried to use "3", but it didn't do anything. For some strange reason, "int ledPin = 4;" works, and "int ledPin = 5;" works. But, #3, #6, #7 do not. I am very new to the ESP8266 and am not yet familiar with the digital ports that work so well in Arduino products but are a puzzle here. Maybe someone can straighten me out here.

In any case, I got the project to work but am using it on my LAN and not from outside and am not using Port Forwarding. This was fun enough. Lots of possibilities. Thanks again.

johnroland51 (author)2016-09-17

As said below, this is a rare example of code that works the first time! Well done. Now if I can figure out how to push data from the Arduino to the web page.... (hint, hint) Thanks very much for sharing.

NameS3 (author)2016-08-06

This is interesting while trying to acces the public ip:port ( a dialog appears asking for login username and password and offcourse its not my router panel, does my service provided set username n password for security or is giving us service provider router address instead of public ip?

lordzamiel (author)2016-05-27

Hey this worked great out of the box with one caveat. From either of the PCs I tested it with it works fine, but none of my mobile browsers can turn the LED on/off. The web page displays, but once I click the button to turn the LED on, it will display "The connection has timed out." and then the NodeMCU will stop responding to desktop requests as well until it's power-cycled. I've tested with 2 phones and a tablet (Android 4.4 and 5.1) as well as 3 different browsers, all to the same effect. Anyone else experience similar problems, or have any ideas?

lordzamiel (author)lordzamiel2016-05-27

Replaced the power supply as someone suggested and strange enough that resolved the mobile browser problems. Thanks to all that PMed with ideas.

thorathome (author)2016-05-21

Thanks for the recipe book. You got me onto the ESP8266!

I've been on Arduino for years, and love this miniature platform. I used your China source, now have two 8622s in action: one as my local NTP server, the other taking temperature.

marcelbron made it! (author)2016-04-20

the very first sketch that worked "out of the box" !!! Really not kidding, just copied and pasted the sketch into the Arduino (v 1.6.7) and uploaded it to the LUA board ( and off I went! No errors, no problems, it just works! 10 points!!

Bay Yolal (author)marcelbron2016-05-02

Why port 8888? How could you do that?

marcelbron (author)Bay Yolal2016-05-03

it's just a choice I made. The port is configurable in the sketch.

Bay Yolal made it! (author)2016-05-02 is totally unclear but let's say 192.168.nn.nn:301 instead. I made it :D Thank you!

About This Instructable




More by CrtSuznik:Attiny Programmer (using Arduino UNO)EASY Arduino OLED sensor data displayControl ESP8266 over the internet (from anywhere)
Add instructable to: