Control ESP8266 Over the Internet (from Anywhere)
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
shubhamnagar0999 4 months ago
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
Greg_007 9 months ago
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
tinwizardpcb 1 year ago
letuantam96 6 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!
BackwardsDave 3 years ago
Check the answer here: https://arduino.stackexchange.com/a/75825/66119
j-marct 1 year ago
utilise le port 80 sa fonctionne trés bien
PraveenS90 5 years 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.
MANMOHANS9 5 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
letuantam96 6 years ago
my Virtual Server setting:
j-marct 1 year ago
comment changer la taille du text sur le site web
dans le programme arduino
merci d'avance
RossRobotics 2 years ago
Crtekkk 2 years ago
J MUKUNTH 3 years ago
ΝεκτάριοςΚ 3 years ago
BackwardsDave 3 years ago
https://arduino.stackexchange.com/a/75825/66119
Jugaad it yourself 4 years ago
BackwardsDave 3 years ago
See this tutorial to add an IP change notification service so you know when your IP changes. https://medium.com/@DavidKlempfner/dynamic-ip-address-code-ae6f400cb510
phanhuyvinh858 4 years ago
BackwardsDave 4 years ago
ofekpl 5 years ago
Code base: https://github.com/ofekp/TinyUPnP.
Hope it helps somebody.