Control ESP8266 Over the Internet (from Anywhere)

296K705174

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

125 Comments

have nodemcu v3 with esp8266. I have uploaded the below code for turn off and on home appliances and make the react expo app and use axios and making http request to nodemcu ip for on off the appliances but when I build and install the apk so after installed the http api which I was using in axios is not accesible due to http security reasons. Can tell any what can I do in this situation or any other way that widly used for communication like these situations ?

NodeMCU code :
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ArduinoJson.h> // Include the ArduinoJson library
#define FAN D3 // GPIO pin connected to the relay module
#define MAIN_LIGHT D4
#define SECONDARY_LIGHT D5
#define OUTDOOR_LIGHT D6
#define SOCKET_SWITCH D7

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins on NodeMCU)
#define OLED_RESET LED_BUILTIN // Change this if your reset pin is not D4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

WiFiServer server(9090);
void setup() {

pinMode(FAN, OUTPUT); // Set relay pin as output
digitalWrite(FAN, HIGH); // Turn off relay initially

pinMode(MAIN_LIGHT, OUTPUT);
digitalWrite(MAIN_LIGHT, HIGH);


pinMode(SECONDARY_LIGHT, OUTPUT);
digitalWrite(SECONDARY_LIGHT, HIGH);


pinMode(OUTDOOR_LIGHT, OUTPUT);
digitalWrite(OUTDOOR_LIGHT, HIGH);


pinMode(SOCKET_SWITCH, OUTPUT);
digitalWrite(SOCKET_SWITCH, HIGH);

Serial.begin(9600);
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}

// Clear the buffer
display.clearDisplay();

Serial.begin(9600);
WiFi.begin("Koshin 4G", "*2014May05*");

while (WiFi.status() != WL_CONNECTED) {
Serial.print("..");
// Set text size, color, and print a message
// Clear the buffer
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.println(F("Connecting to WiFi..."));
display.display();
}

Serial.println("NodeMCU is Connected!");
Serial.println(WiFi.localIP());

// Set text size, color, and print a message
// Clear the buffer
display.clearDisplay();
display.setTextSize(1.5);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.println(F("NodeMCU is Connected!"));
display.println(WiFi.localIP());
display.display();
server.begin(); // Start the server

}

void sendResponse(WiFiClient &client, const String &response) {
String jsonResponse = "{\"data\": \"" + response + "\"}";
client.println("HTTP/1.1 200 OK");
client.println("Access-Control-Allow-Origin: *");
client.println("Access-Control-Allow-Methods: GET,POST,PUT,DELETE");
client.println("Access-Control-Allow-Headers: Content-Type, Authorization");
client.println("Content-Type: application/json");
client.print("Content-Length: ");
client.println(jsonResponse.length());
client.println();
client.println(jsonResponse);
}
void loop() {
WiFiClient client = server.available(); // Check for a client connection
if (client) { // Check if a client is available

String request = client.readStringUntil('\n');
request.trim();
Serial.println(request);

// conditions for fan
if (request == "GET /?applianceName=fan&status=true HTTP/1.1"){
digitalWrite(FAN, LOW);
}else if(request == "GET /?applianceName=fan&status=false HTTP/1.1"){
digitalWrite(FAN, HIGH);
}
// conditions for main light
else if(request == "GET /?applianceName=main_light&status=true HTTP/1.1"){
digitalWrite(MAIN_LIGHT, LOW);
}else if(request == "GET /?applianceName=main_light&status=false HTTP/1.1"){
digitalWrite(MAIN_LIGHT, HIGH);
}

// conditions for secondary light
else if(request == "GET /?applianceName=secondary_light&status=true HTTP/1.1"){
digitalWrite(SECONDARY_LIGHT, LOW);
}else if(request == "GET /?applianceName=secondary_light&status=false HTTP/1.1"){
digitalWrite(SECONDARY_LIGHT, HIGH);
}

// conditions for outdoor light
else if(request == "GET /?applianceName=outdoor_light&status=true HTTP/1.1"){
digitalWrite(OUTDOOR_LIGHT, LOW);
}else if(request == "GET /?applianceName=outdoor_light&status=false HTTP/1.1"){
digitalWrite(OUTDOOR_LIGHT, HIGH);
}

// conditions for socket switch
else if(request == "GET /?applianceName=socket_switch&status=true HTTP/1.1"){
digitalWrite(SOCKET_SWITCH, LOW);
}else if(request == "GET /?applianceName=socket_switch&status=false HTTP/1.1"){
digitalWrite(SOCKET_SWITCH, HIGH);
}


sendResponse(client, request);
client.stop(); // Close the connection

}
}

Is anyone have any other solution for this kind off issue please let me know.
the ip of nodemcu is http that is accesbile through development but when installed apk due to security reasons this is not acessible how can I access it from installed app or something else I can do please tell me

Thanks for your advice - I enclose pictures from my router config - hope they help people - ( I am using Port 80 ( the WebPage port ) on the LAN side ). And it shows that you need to set up both the Forwarding and allow access through the Firewall.
So in my case - I type in XXX.XXX.XXX.XXX:31 and get to the Webpage ( great stuff )

BUT - My question to you guys though is :-
All I am getting is the static webpage - which is running it's JavaScript - so is opening menus and everything ( front end ) - but it isn't actually communicating with the backend - the statii aren't correct and the Commands aren't being to the ESP8266.
Everything works fine when I am connecting to the ESP8266 from the same WiFi network. Do you have any ideas ?

So from the external webversion - I am not getting the Event Log etc ... ( as per the enclosed picture )

FrontEnd htm code is available from gregmccormack.uk/microPLC_HMI
Is installing a server program on the esp8266 the only way to access the GPIO ports ? Maybe i an access io ports using a router set up access address and AT commands? Any ideas ?

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!

bonjour
utilise le port 80 sa fonctionne trés bien

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.

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

bonjour
comment changer la taille du text sur le site web
dans le programme arduino
merci d'avance
This tutorial only enables you to access the ESP8266 over a closed network. It does NOT let you control it from the internet. Notice the IP address in 'Step 4...', that is a network IP, not an internet IP. Not sure why he blacked out the last number since you can't access it anyway.
Nope. That's the whole point. From anywhere=any network, anywhere
Do I want to enter my laptop IP or ESP IP..?
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.
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.
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.
More Comments