Control ESP8266 Over the Internet (from Anywhere)

236,613

677

116

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.

If you enjoyed this Instructable, consider visiting my Fundrazr page here. And of course, share.

5 People Made This Project!

Recommendations

  • "Can't Touch This" Family Contest

    "Can't Touch This" Family Contest
  • CNC Contest 2020

    CNC Contest 2020
  • Robots Contest

    Robots Contest

116 Discussions

1
letuantam96
letuantam96

Question 2 years ago

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
0
PraveenS90
PraveenS90

Answer 1 year ago

when u say whats mi IP, this value will changed everyday, since it is dynamic IP . if you want to have same IP every time then go for static IP by simple paying little to ISP provider/internet provider.

0
MANMOHANS9
MANMOHANS9

Answer 2 years ago

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

0
letuantam96
letuantam96

Answer 2 years ago

my Virtual Server setting:

Untitled.png
0
Jugaad it yourself
Jugaad it yourself

3 months ago

I have a questions,will this work on dynamic IP?

0
phanhuyvinh858
phanhuyvinh858

12 months ago

Hi, I tried to use the IP address from whatsmyip to fill in the IP address blank but I received an error. how can i fix itError code: 26106The IP address is not in the same subnet with LAN IP address.

0
BackwardsDave
BackwardsDave

Reply 3 months ago

He made a mistake. You are meant to put in the IP address of your ESP8266. For me it was 10.1.1.172. After you've uploaded the code to your device, you should be able to find your device's IP address in the drop-down when you start typing the IP address (start with the first few digits of your router's IP address). Otherwise look for a section called "connected devices" to find your device's IP address. Then when you try to access http://<your public IP address>:301 it should forward the HTTP requests to your device.

0
ofekpl
ofekpl

Tip 1 year ago

Instead of forwarding ports to a static chosen IP, you can use the package TinyUPnP from the Arduino Library Manager (from within the Arduino IDE).
Code base: https://github.com/ofekp/TinyUPnP.
Hope it helps somebody.

0
sachitha_bandara
sachitha_bandara

Reply 9 months ago

Hi, its seems the link is not working now. could you please update it?

0
mostafa00hammad
mostafa00hammad

Question 10 months ago on Step 4

i didn`t understanding this point ... please help me!!

0
MahdiSK
MahdiSK

11 months ago

Thanks a lot for this lesson. I have a problem. I need to receive and send data in a same time. can anyone you help me?
Thanks a lot dear Friends.

0
Stutzer
Stutzer

Question 1 year ago

Hi,
I made everything as you said (or wrote), and it does not work.
Can it be because I use internet via satelite?
I have already tried to sign up at "www.no-ip.com" and used my name which I registered there, but still not working.
If I use my IP number, 131.0.216.47:301 (from www.whatsmyip.org ) I receive following answer:
"The connection was re-defined"
If I use my no-ip name, "strudda.zapto.org", I receive the answer:
"No data was sent by strudda.zapto.org
I wonder what I am doing wrong. Can you help me?
Kindest Regards,
Ruberto

0
peachville
peachville

1 year ago on Step 5

CrtSuznik, your the best! Very easy to follow so up and running 1st go. This opens the door to a lot of what I want to do...integration of PC (x visual basic) to Alexa so I can play MY MUSIC. Be sure to obtain your WiFi ssid and password (not your PC unless it's only using WiFi).

A few asked what about additional buttons?
ESP8266WiFi.h uses a bunch of I/O pins so from what I found we're left with pins 3, 6, 7 & 8. I used pins 3 & 6 to create 6 buttons on my webpage (ON 1, ON 2, OFF 1, OFF 2, Momentary 1 and Momentary 2). Here is my code:

#include
const char* ssid = "SHAW-0B34A0"; //*** change to your ssid ***
const char* password = "X6402319"; //*** change to your password ***
const char* host = "192.168.0.22"; //*** run once, copy and paste "host" from serial monitor ***

int ledPin1 = D3;
int ledPin2 = D6;
WiFiServer server(301); //just pick any port number you like

void setup() {
Serial.begin(115200);
delay(10);
Serial.println(WiFi.localIP());
// prepare GPIO2
pinMode(ledPin1, OUTPUT);
digitalWrite(D3, LOW);
pinMode(ledPin2, OUTPUT);
digitalWrite(D6, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}//end of setup

void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String req = client.readStringUntil('\r');
client.flush();

// Match the request
if (req.indexOf("") != -10) { //checks if you're on the main page
if (req.indexOf("/OFF1") != -1) { //checks if you clicked OFF1
digitalWrite(ledPin1, LOW);
Serial.println("#1 OFF");
}
if (req.indexOf("/ON1") != -1) { //checks if you clicked ON1
digitalWrite(ledPin1, HIGH);
Serial.println("#1 ON");
}
if (req.indexOf("/MOM1") != -1) { //checks if you clicked MOM1
digitalWrite(ledPin1, HIGH);
Serial.println("MOMENTARY 1");
delay(1000);
digitalWrite(ledPin1, LOW);
}
if (req.indexOf("/OFF2") != -1) { //checks if you clicked OFF2
digitalWrite(ledPin2, LOW);
Serial.println("OFF 2");
}
if (req.indexOf("/ON2") != -1) { //checks if you clicked ON2
digitalWrite(ledPin2, HIGH);
Serial.println("#2 ON");
}
if (req.indexOf("/MOM2") != -1) { //checks if you clicked MOM2
digitalWrite(ledPin2, HIGH);
Serial.println("MOMENTARY2");
delay(1000);
digitalWrite(ledPin2, LOW);
}
}
else {
Serial.println("invalid request");
client.stop();
return;
}
// Prepare the response
String s = "HTTP/1.1 200 OK\r\n";
s += "Content-Type: text/html\r\n\r\n";
s += "\r\n\r\n";
s += " LED 1 LED 2";
s += "
";
s += " ";
s += "";
s += "
";
s += "
";
s += " ";
s += "";
s += "
";
s += "
";
s += " ";
s += "";
s += "\n";
client.flush();
// Send the response to the client
client.print(s);
delay(1);
}//end of main loop

If you want to also control the two LEDs by PC program, here it is:
www dot 49h dot com/NodeMcu.zip
The program will turn on either LED momentarily for 3 seconds.
Make sure you select the correct COM port.

Enjoy!

web_buttons.jpg
0
MANMOHANS9
MANMOHANS9

Reply 2 years ago

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

0
ConstantineC2
ConstantineC2

Reply 2 years ago

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

0
FagnerM3
FagnerM3

Reply 2 years ago

I have the same question =/