Tuesday, May 3, 2016

NodeMCU/ESP8266 act as AP (Access Point) and web server to control GPIO

Further works on previous exercise "NodeMCU/ESP8266 act as AP (Access Point) and simplest Web Server"; this example of NodeMCU/ESP8266 act as AP and implement a web server to control GPIO (on-board LED) using HTML interface.


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

28 comments:

  1. Thank you for posting this bro...

    ReplyDelete
  2. I copied the same code but it says
    'response' was not declared in this scope.
    What might be the problem?

    ReplyDelete
    Replies
    1. I re-tried it without problem. May be your browser insert some invisible code in the text.
      you can try:
      - Use another browser.
      - Use simplest text editor, such as notepad. Paste it, and copy again.

      hope can help.

      Delete
    2. Copy response function and paste it above the other functions. And also copy string constants above the response function. The error will be removed.

      Delete
    3. Same error.
      "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.

      Delete
  3. Great post and great blog you have created... thanx buddy

    ReplyDelete
  4. that's what I was looking for, thank you

    ReplyDelete
  5. Recive digital input (switch , pir ) show on browser (display circle red or blue)

    ReplyDelete
  6. Works great.
    Selected all the code.
    Dropped it in the Arduino IDE.
    Upload.
    Done compiling.
    Great.

    ReplyDelete
  7. Response was not declared in this scope, same problem cant solve it ; (
    hope somebody can help, the user that says about the invisible text, i dont think so, maybe is an indented issue

    ReplyDelete
    Replies
    1. read comments for resolved issues, it has been resolved

      Delete
  8. How do I add another on and off button? I tried but I could not :-(

    ReplyDelete
  9. How 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!!
    Thanks..

    ReplyDelete
  10. dear,
    pls help, what/how to modifiy the code to make LED_builtin off in first run.
    thank you!

    ReplyDelete
    Replies
    1. Modify the code (in the beginning of the program):
      int stateLED = LOW;

      to:
      int stateLED = HIGH;

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

    ReplyDelete
    Replies
    1. Have 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

      Delete
  12. Yes 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. :)

    ReplyDelete
  13. Good sketch!
    Very functional for NodeMCU.

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

    ReplyDelete
  15. great work bro!!!the code worked perfectly fine....but can u send the code for glowing multiple LEDs

    ReplyDelete
  16. I want to control D0~D8 LED on and off , please send the code for multiple LEDs ... Thx

    ReplyDelete
  17. i 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

    ReplyDelete
  18. Hello. 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

    ReplyDelete
  19. Good 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!
    Anyone now why this hapining?

    ReplyDelete
  20. Hello!
    I have serial connect an arduino with nodemcu and i send data.
    Is it possible to attach values inside the the html?

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