CRTC Electronics

Click here to edit subtitle

AP and Webserver

turn on LED

#include <ESP8266WiFi.h>

#include <WiFiClient.h> 

#include <ESP8266WebServer.h>


ESP8266WebServer server(80);


const char *ssid = "Brown's ESP";

const char *password = "";

int espLedState = LOW;

// ****Add int stage =0;  ***** //



/*******************************************************************/


void setup() {

  ServerSetup();

//  ***** Set LEDpins to output,...    pinMode(5, OUTPUT); *****  //

}



void loop() {

    server.handleClient();

//  ***** Call lights(); ***** //

}


/*******************************************************************/


void ServerSetup() {

    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("/EspLedOn", handleLedOn);

    server.on("/EspLedOff", handleLedOff);

// **** #2 Add: allOn, allOff, flash1  ***** //

    server.begin();

    Serial.println("HTTP server beginned");

    pinMode(LED_BUILTIN, OUTPUT);

    digitalWrite(LED_BUILTIN, espLedState);

}


/*******************************************************************/


void handleRoot() {

    webPage();

}


void handleLedOn() {

  espLedState = LOW;

  digitalWrite(LED_BUILTIN, espLedState);

  webPage();

}


void handleLedOff() {

  espLedState = HIGH;

  digitalWrite(LED_BUILTIN, espLedState);

  webPage();

}

// **** #3 Add: allOn, allOff, flash1  ***** //


/*******************************************************************/


void webPage(){

  String htmlCode; 

  htmlCode = "<html><head>";

  htmlCode += "<h1>Brown's ESP8266 AP WebServer</h1><br/>\n";

  

  if(espLedState == LOW){

    htmlCode += "<big>The ESP LED is now <b>ON</b></big><br/>\n";

  }else{

    htmlCode += "<big>The ESP LED is now <b>OFF</b></big><br/>\n";

  }

  htmlCode +=

    "<a href=\"EspLedOn\"><button style=\"display: block; width: 100%;\">ON</button></a><br/>"

    "<a href=\"EspLedOff\"><button style=\"display: block; width: 100%;\">OFF</button></a><br/>"

// **** #1 Add: allOn, allOff, flash1  ***** //

;

   htmlCode += "</html>";


  server.send(200, "text/html", htmlCode);

}



//  ***** Add lights() {     }   *****  //