Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iotguider/3d1616e1eb6a8906e6dd66941a92b8c3 to your computer and use it in GitHub Desktop.
Save iotguider/3d1616e1eb6a8906e6dd66941a92b8c3 to your computer and use it in GitHub Desktop.
Wireless controlled LED using ESP8266 WiFi Module
#include <ESP8266WiFi.h>
const char* ssid = "WiFi Name (SSID)";
const char* password = "WiFi Password";
int LED = 0; // led connected to D0
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
Serial.print("Connecting to the Newtork");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
server.begin(); // Starts the Server
Serial.println("Server started");
Serial.print("IP Address of network: ");
Serial.println(WiFi.localIP());
Serial.println("/");
}
void loop()
{
WiFiClient client = server.available();
if (!client)
{
return;
}
Serial.println("Waiting for new client");
while(!client.available())
{
delay(1);
}
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
int value = LOW;
if(request.indexOf("/LED=ON") != -1)
{
digitalWrite(LED, HIGH); // Turn ON LED
value = HIGH;
}
if(request.indexOf("/LED=OFF") != -1)
{
digitalWrite(LED, LOW); // Turn OFF LED
value = LOW;
}
client.println("HTTP/1.1 200 OK"); // standalone web server with an ESP8266
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("LED: ");
if(value == HIGH)
{
client.print("ON");
}
else
{
client.print("OFF");
}
client.println("<br><br>");
client.println("<a href=\"/LED=ON\"\"><button>ON</button></a>");
client.println("<a href=\"/LED=OFF\"\"><button>OFF</button></a><br />");
client.println("</html>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment