Basic ESP-8266 Web Server Controlling Two LEDs
Posted: Thu Feb 18, 2016 12:07 am
I found this information at http://randomnerdtutorials.com/esp8266- ... duino-ide/ and the sketch would not compile. I contacted Rui Santos and he said he had posted an older version of the sketch and he has since updated it. This sketch below is my updated version of his original old non-compiling sketch. I'm not associated with randomnerdstutorial.com in any way.
Here's the video:
And here is the code:
Here's the video:
And here is the code:
Code: Select all
/*********
Rui Santos
Complete project details at http://randomnerdtutorials.com
Code Update Feb. 12, 2016 David Connolly
*********/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
MDNSResponder mdns;
// Replace with your network credentials
const char* ssid = "SSID";
const char* password = "Your_Password";
int gpio0Pin = 0;
int gpio2Pin = 2;
ESP8266WebServer server(80);
String webPage = "";
void setup(void) {
webPage += "<h1>ESP8266 Web Server</h1><p>Socket #1 <a href=\"socket1On\"><button>ON</button></a> <a href=\"socket1Off\"><button>OFF</button></a></p>";
webPage += "<p>Socket #2 <a href=\"socket2On\"><button>ON</button></a> <a href=\"socket2Off\"><button>OFF</button></a></p>";
// preparing GPIOs
pinMode(gpio0Pin, OUTPUT);
digitalWrite(gpio0Pin, LOW);
pinMode(gpio2Pin, OUTPUT);
digitalWrite(gpio2Pin, LOW);
delay(1000);
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
server.on("/", []() {
server.send(200, "text/html", webPage);
});
server.on("/socket1On", []() {
server.send(200, "text/html", webPage);
digitalWrite(gpio0Pin, HIGH);
delay(1000);
});
server.on("/socket1Off", []() {
server.send(200, "text/html", webPage);
digitalWrite(gpio0Pin, LOW);
delay(1000);
});
server.on("/socket2On", []() {
server.send(200, "text/html", webPage);
digitalWrite(gpio2Pin, HIGH);
delay(1000);
});
server.on("/socket2Off", []() {
server.send(200, "text/html", webPage);
digitalWrite(gpio2Pin, LOW);
delay(1000);
});
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
}