ESP_AP_WebServer.ino
/*
* NodeMCU/ESP8266 act as AP (Access Point) and simplest Web Server
* to control GPIO (on-board LED)
* Connect to AP "arduino-er", password = "password"
* Open browser, visit 192.168.4.1
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
const char *ssid = "arduino-er";
const char *password = "password";
int stateLED = LOW;
ESP8266WebServer server(80);
void handleRoot() {
response();
}
void handleLedOn() {
stateLED = LOW;
digitalWrite(LED_BUILTIN, stateLED);
response();
}
void handleLedOff() {
stateLED = HIGH;
digitalWrite(LED_BUILTIN, stateLED);
response();
}
const String HtmlHtml = "<html><head>"
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" /></head>";
const String HtmlHtmlClose = "</html>";
const String HtmlTitle = "<h1>Arduino-er: ESP8266 AP WebServer exercise</h1><br/>\n";
const String HtmlLedStateLow = "<big>LED is now <b>ON</b></big><br/>\n";
const String HtmlLedStateHigh = "<big>LED is now <b>OFF</b></big><br/>\n";
const String HtmlButtons =
"<a href=\"LEDOn\"><button style=\"display: block; width: 100%;\">ON</button></a><br/>"
"<a href=\"LEDOff\"><button style=\"display: block; width: 100%;\">OFF</button></a><br/>";
void response(){
String htmlRes = HtmlHtml + HtmlTitle;
if(stateLED == LOW){
htmlRes += HtmlLedStateLow;
}else{
htmlRes += HtmlLedStateHigh;
}
htmlRes += HtmlButtons;
htmlRes += HtmlHtmlClose;
server.send(200, "text/html", htmlRes);
}
void setup() {
delay(1000);
Serial.begin(9600);
Serial.println();
WiFi.softAP(ssid, password);
IPAddress apip = WiFi.softAPIP();
Serial.print("visit: \n");
Serial.println(apip);
server.on("/", handleRoot);
server.on("/LEDOn", handleLedOn);
server.on("/LEDOff", handleLedOff);
server.begin();
Serial.println("HTTP server beginned");
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, stateLED);
}
void loop() {
server.handleClient();
}
Next:
- NodeMCU/ESP8266 implement WebSocketsServer to control RGB LED
- more example of "ESP8266 core for Arduino".
Thank you for posting this bro...
ReplyDeleteI copied the same code but it says
ReplyDelete'response' was not declared in this scope.
What might be the problem?
I re-tried it without problem. May be your browser insert some invisible code in the text.
Deleteyou can try:
- Use another browser.
- Use simplest text editor, such as notepad. Paste it, and copy again.
hope can help.
Same error with me.
DeleteCopy response function and paste it above the other functions. And also copy string constants above the response function. The error will be removed.
DeleteSame error.
Delete"Copy response function and paste it above the other functions. And also copy string constants above the response function. The error will be removed."
I still dont get it. i'm completelly noob. Can u just write the code to fix it. Thankyou.
Great post and great blog you have created... thanx buddy
ReplyDeletethat's what I was looking for, thank you
ReplyDeleteRecive digital input (switch , pir ) show on browser (display circle red or blue)
ReplyDeleteWorks great.
ReplyDeleteSelected all the code.
Dropped it in the Arduino IDE.
Upload.
Done compiling.
Great.
Response was not declared in this scope, same problem cant solve it ; (
ReplyDeletehope somebody can help, the user that says about the invisible text, i dont think so, maybe is an indented issue
read comments for resolved issues, it has been resolved
DeleteHow do I add another on and off button? I tried but I could not :-(
ReplyDeleteHow can i control GPIOs (or, i want to ON/OFF LEDs from gpio0 or gpio2 instead of builtin ).i tried to modifi the code but it doesn't work properly!!
ReplyDeleteThanks..
dear,
ReplyDeletepls help, what/how to modifiy the code to make LED_builtin off in first run.
thank you!
Modify the code (in the beginning of the program):
Deleteint stateLED = LOW;
to:
int stateLED = HIGH;
Hi, how do i resolve the issue when it said 'LED_BUILTIN' was not declared? It highlighted the pinMode(LED_BUILTIN, OUTPUT) part for this error. Can anybody please help.. thank you
ReplyDeleteHave you install and select esp8266 board in Arduino IDE? Please check http://arduino-er.blogspot.com/2016/03/blink-nodemcu-on-board-led-using.html
DeleteYes sir, i did installed it. I think i have solved the problem. I declare int LED_BUILTIN = D0; and it solved the problem. Anyway, Thank you for responding sir. Your blog help me with my project a lot. :)
ReplyDeleteGood sketch!
ReplyDeleteVery functional for NodeMCU.
It was Very impressive mate, would you please advise me how to change the code make a push button switch? I mean, by pressing the ON button the LED is ON for a certain amount of time and turns off afterwards.
ReplyDeletegreat work bro!!!the code worked perfectly fine....but can u send the code for glowing multiple LEDs
ReplyDeleteI want to control D0~D8 LED on and off , please send the code for multiple LEDs ... Thx
ReplyDeletei want to connect my NODEMCU to any of the router while it act as a AP. i mean when i use it as a AP it show SSID and PASSWORD on web , when i write my home network id and password it connect to the router..please tell how to use
ReplyDeleteHello. How can I have custom IP address of the esp8266 module? for example I want to set 192.168.100.1 instead of 192.168.4.1
ReplyDeleteGood nigth, please i have a problem with the wifi conection betwen nodemcu and a notebook, they conect has weel in the first time, and after i disconect and reconect again the wifi do not respond, and i need turn off the nodemcu, wait about 2 minutes, delete the conection on the wifi manager in the notebook and after all this steps, i reconect the wifi on the nodemcu and the wifi starts talk again!
ReplyDeleteAnyone now why this hapining?
Hello!
ReplyDeleteI have serial connect an arduino with nodemcu and i send data.
Is it possible to attach values inside the the html?
Can I implement the code as wanted and respond correctly, the problem is when I shot the computer and I connect to an external source, it disappears and does not appear even me putting it back on the computer, then I have to record again to appear, someone can tell me which the problem?
ReplyDelete