loading

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

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

www.ebay.com/itm/NodeMcu-ESP8266-V3-Lua-CH340-WIFI-Internet-Development-Board-Module-FTDI-Arduino-/162043136554?hash=item25ba860a2a:g:duUAAOSwxp9W765h

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:

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

<p>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 &quot;TCP&quot;, not &quot;ALL&quot;. In my case, i configured it as &quot;ALL&quot; and it wouldn't work.</p><p>the tutorials are great and excellent explained, though, just i thought i could help a little too!!</p>
<p>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. </p><p>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 192.168.1.1 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.</p>
<p>works at the first try! it's a nice start :)</p>
<p>Using an ESP8266 12E I had to change ledpin to be LED_BUILTIN. Also i swapped the low and high in the digitalWrite statement. </p><p>This guide really helped, thanks!</p>
<p>Do you know if its possible to have the led on only when a web button is pressed and off when released?</p>
<p>Connecting to myNetworkName</p><p>........................................................................(always)</p><p>and newer connected.</p><p>What s my mistake?</p>
<p>I had the same thing, fixed it by typing in the network name again, including all characters (i.e. &quot;TELUS3302-2.4G&quot;)</p>
<p>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.</p>
<p>If you are behind firewall and your IP is not public, than I suggest to use a ThingSpeak.readFloatField command to control the Wemos . </p><p>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..</p>
<p>Hi and sorry if my question is stupid. I can access my web server from within my LAN (192.168.1.100:666) and Port 666 is opened. I get x.my.public.IP from whatismyip.com but when I try to access x.my.public.IP:666 chrome returns &quot;ERR_CONNECTION_REFUSED&quot;. Thanks in advance</p>
<p>i also try this.but it work without internet connections.led is not blink when i connect to internet.so i can not blink it from anywhere.please help me</p>
<p>I also have your probem. If you can fix it, please help me at lexuanbac291193@gmail.com. thank you so much.</p>
<p>I found the problem. x.my.public.IP:666 can be accessed only from outside the LAN. Hope this can help someone else.</p>
<p>i also try this.but it work without internet connections.led is not blink when i connect to internet.so i can not blink it from anywhere.please help me</p>
<p>Fabulous!</p>
<p>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 </p>
<p>Thanks for this fantastic Instructable. I found one mistake in the code. You defined int ledPin = D3. An &quot;int&quot; requires a number, so &quot;D3&quot; returns an error. Then I tried to use &quot;3&quot;, but it didn't do anything. For some strange reason, &quot;int ledPin = 4;&quot; works, and &quot;int ledPin = 5;&quot; 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. </p><p>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.</p>
<p>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. </p>
This is interesting while trying to acces the public ip:port (xxx.xxx.xxx.xxx:7821) 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 whatismy.org is giving us service provider router address instead of public ip?
<p>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 .</p><p>&quot;error: Failed to open COM6</p><p>error: espcomm_open failed</p><p>error: espcomm_upload_mem failed&quot;</p><p>Any ideas on how I can communicate with the board ? (Arduino IDE is running on windows 10 and the &quot;ports&quot; option under &quot;tools&quot; is greyed out so I am unable to change it)</p><p>p.s I purchased three boards so it is hardly unlikely that all three are faulty.</p>
<p>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)</p>
<p>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 &quot;The connection has timed out.&quot; 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?</p>
<p>Replaced the power supply as someone suggested and strange enough that resolved the mobile browser problems. Thanks to all that PMed with ideas.</p>
<p>Thanks for the recipe book. You got me onto the ESP8266! </p><p>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. </p>
<p>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?</p>
<p>the very first sketch that worked &quot;out of the box&quot; !!! Really not kidding, just copied and pasted the sketch into the Arduino (v 1.6.7) and uploaded it to the LUA board (http://tinyurl.com/zjc9lmo) and off I went! No errors, no problems, it just works! 10 points!! </p>
<p>Why port 8888? How could you do that?</p>
<p>it's just a choice I made. The port is configurable in the sketch.</p>
<p>xxx.xxx.xx.xx is totally unclear but let's say 192.168.nn.nn:301 instead. I made it :D Thank you!</p>
<p>Wow, there is some inaccurate info floating around in this comment list.</p><p>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.</p><p>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, </p><p>Both no-ip and Dynamic DNS offer detailed 'how tos' in configuring your 'static ip address' and maintaining it.</p><p>Good luck.</p>
<p>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.</p><p>no-ip is way to go on that kind of situations as you mentioned. </p>
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.
can you control this when you are not connected to the same WiFi?
indeed you can...thats the point of the &quot;anywhere&quot; part
no you cannot. If you are connected to internal IP 192.168.0.xxx you are not accessible from outside. You have to have static ip address or dyn DNS to do that.
<p>He clearly states in his instructions to use whatsmyip.com to determine your IP address. This will only show your routers &quot;public&quot; address. This is the address to use with &quot;port forwarding&quot;. If needed you can google your routers model to find out more...</p>
<p>No - if you dont have public IP - it will show your providers IP. And this will not help you, because you dont have access to the providers router.</p><p>But nevermind. I didnt want to troll this recipe - it is still helpful. </p>
<p>Sorry vladimir but you know nothing about networking. please stop giving false information to other people. </p>
I think you are correct, for the most part? The ip on the router, for folk using a regular home Internet provider the routers ip will be assigned via the providers dhcp server and it is subject to change. So you would need an outside facing static ip so it would not change or use a service to point to whatever the dhcp assigned ip changes to. I was setting up ip cameras at my house and I think the issue would be similar.
<p>I understand what Vladimir means as here in Australia dynamic IPs are the norm to discourage people setting up servers without paying extra for a business service. Even leaving the router on 24/7 the IP will change at least once a week.</p>
<p>If you have a private IP for the device and you have a login to your local Cable modem, DSL modem, Fiber modem, etc., you can do port forwarding on the modem/router, so that if you connect to your modem's IP address on a certain port (say 99) that will forward all connections to internal address 192.168.0.100 on port 80. You could do this for whatever devices you have internally. Port 100 forwards to port 80 on IP 192.168.0.101, port 101 forwards to port 80 on IP 192.168.0.102, port 102 forwards to port 22 on 192.168.0.1, etc.. It's certainly doable. I have yet to have a cable/DSL modem/router from the cable/DSL company which did not allow me do do this.</p>
thats why we used port forwarding
<p>But still you have to have public IP on your router. Otherwise you cannot address your router from outside.</p>
<p>Vlad - I'm not sure where you're going with this, but your'e absolutely wrong. Just about every router in existence allows you to setup up a DMZ pass through. IP addresses for your ISP's routers are perfectly transparent to the outside world, and incoming traffic is automatically copied to the DMZ host. In addition, you can specify ports for incoming traffic and route them to internal IPs. <br><br>I have had as many as 4 ESP8266's connected to the outside world using only the IP of my home router to connect remotely. Trust me... it's totally doable without having to have a DNS provider. </p>
<p>Not &quot;absolutely wrong.&quot;</p><p>The I.P returned from whatsmyip.com shows your router's I.P</p><p> not your provider's servers' IP.</p><p>However, this value can change daily or hourly for many users.</p><p>Under those circumstances, connecting from the outside is not possible without extra software to somehow publish the current I.P</p>
<p>wow... Everyone take a step back and think for a minute. OK, I have been accessing my home computer over the Internet for three years. My IP has never changed. With the &quot;internet of things&quot; popularity more and more people will be using the Internet for exactly what this article covers. &quot;Littlemonkeyjoe&quot; covered it well with his explanation of using different ports to use the same IP address to access different devices. Instead of arguing about this on here, check with your own ISP. </p><p>And remember, This is the Internet. Anything is possible and nothing is impossible. </p>
All routers has a public ip. As long as you have admin access to the router connected to the cloud you should be ok.
<p>Hi, I noticed in your code &quot;int ledPin = D3&quot; and &quot;// prepare GPIO2</p><p> pinMode(ledPin, OUTPUT);</p><p> digitalWrite(D3, LOW); My question: is GPIO2 the same as D3 and why is that?</p><p>Thanks, Lee</p>
<p>Nice post, jJust wandering to use the WeMos board to do the same ....</p>
<p>Have just tested with the WeMos D1 board and I confirm, the code works fine without any modification -- great post !</p>

About This Instructable

100,140views

622favorites

License:

More by CrtSuznik:EASY Arduino OLED sensor data display Control ESP8266 over the internet (from anywhere) High voltage power supply 
Add instructable to: