0% found this document useful (0 votes)
85 views2 pages

ESP8266 LED Control Code Example

This document contains code for an ESP8266 WiFi module that controls 3 LEDs based on commands received from an Android device over WiFi. It connects to WiFi, sets up a web server, defines functions to check for commands and send responses, and includes a loop to listen for commands and control the LEDs accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views2 pages

ESP8266 LED Control Code Example

This document contains code for an ESP8266 WiFi module that controls 3 LEDs based on commands received from an Android device over WiFi. It connects to WiFi, sets up a web server, defines functions to check for commands and send responses, and includes a loop to listen for commands and control the LEDs accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <ESP8266WiFi.

h>
WiFiClient client;
WiFiServer server(80);
const char* ssid = "espcontroller"; //router ssid
const char* password = "engrpandaece"; //router pw
String command = ""; // Command received from Android device

// Set led Pins


int led1 = 16;
int led2 = 5;
int led3 = 4;

void setup()
{
[Link](115200);

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);

digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);

connectWiFi();
[Link]();
}

void loop()
{
client = [Link]();
if (!client) return;
command = checkClient ();
[Link](command);

if (command == "B" || command == "Red on")


{
digitalWrite(led1, LOW);
}
else if (command == "b" || command == "Red on")
{
digitalWrite(led1, HIGH);
}
else if (command == "C" || command == "Green on")
{
digitalWrite(led2, HIGH);
}
else if (command == "c" || command == "Green off")
{
digitalWrite(led2, LOW);
}
else if (command == "D" || command == "Blue on")
{
digitalWrite(led3, LOW);
}
else if (command == "d" || command == "blue")
{
digitalWrite(led3, HIGH);
}
else if (command == "all on")
{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
}
else if (command == "alloff")
{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
}
sendBackEcho(command); // send command echo back to android device
command = "";
}
/* connecting WiFi */
void connectWiFi()
{
[Link]("Connecting to WIFI");
[Link](ssid, password);
while ((!([Link]() == WL_CONNECTED)))
{
delay(300);
[Link]("..");
}
[Link]("");
[Link]("WiFi connected");
[Link]("NodeMCU Local IP is : ");
[Link](([Link]()));
}

/* check command received from Android Device */


String checkClient (void)
{
while (![Link]()) delay(1);
String request = [Link]('\r');
[Link](0, 5);
[Link]([Link]() - 9, 9);
return request;
}

/* send command echo back to android device */


void sendBackEcho(String echo)
{
[Link]("HTTP/1.1 200 OK");
[Link]("Content-Type: text/html");
[Link]("");
[Link]("<!DOCTYPE HTML>");
[Link]("<html>");
[Link](echo);
[Link]("</html>");
[Link]();
delay(1);
}

You might also like