Code:#include
#include
/*Put your SSID & Password*/
const char* ssid = “Abdulgani”; // Enter SSID here
const char* password = “19961996”; //Enter Password here
ESP8266WebServer server(80);
uint8_t LED1pin = D7;
bool LED1status = LOW;
uint8_t LED2pin = D6;
bool LED2status = LOW;
void setup() {
Serial.begin(115200);
delay(100);
pinMode(LED1pin, OUTPUT);
pinMode(LED2pin, OUTPUT);
Serial.println(“Connecting to “);
Serial.println(ssid);
//connect to your local wi-fi network
WiFi.begin(ssid, password);
//check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected..!”);
Serial.print(“Got IP: “); Serial.println(WiFi.localIP());
server.on(“/”, handle_OnConnect);
server.on(“/led1on”, handle_led1on);
server.on(“/led1off”, handle_led1off);
server.on(“/led2on”, handle_led2on);
server.on(“/led2off”, handle_led2off);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println(“HTTP server started”);
}
void loop() {
server.handleClient();
if(LED1status)
{digitalWrite(LED1pin, HIGH);}
else
{digitalWrite(LED1pin, LOW);}
if(LED2status)
{digitalWrite(LED2pin, HIGH);}
else
{digitalWrite(LED2pin, LOW);}
}
void handle_OnConnect() {
LED1status = LOW;
LED2status = LOW;
Serial.println(“GPIO7 Status: OFF | GPIO6 Status: OFF”);
server.send(200, “text/html”, SendHTML(LED1status,LED2status));
}
void handle_led1on() {
LED1status = HIGH;
Serial.println(“GPIO7 Status: ON”);
server.send(200, “text/html”, SendHTML(true,LED2status));
}
void handle_led1off() {
LED1status = LOW;
Serial.println(“GPIO7 Status: OFF”);
server.send(200, “text/html”, SendHTML(false,LED2status));
}
void handle_led2on() {
LED2status = HIGH;
Serial.println(“GPIO6 Status: ON”);
server.send(200, “text/html”, SendHTML(LED1status,true));
}
void handle_led2off() {
LED2status = LOW;
Serial.println(“GPIO6 Status: OFF”);
server.send(200, “text/html”, SendHTML(LED1status,false));
}
void handle_NotFound(){
server.send(404, “text/plain”, “Not found”);
}
String SendHTML(uint8_t led1stat,uint8_t led2stat){
String ptr = ” \n”;
ptr +=”\n”;
ptr +=”LED Control\n”;
ptr +=”html { font-family: Helvetica; display: inline-block; margin: 0px auto;
text-align: center;}\n”;
ptr +=”body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3
{color: #444444;margin-bottom: 50px;}\n”;
ptr +=”.button {display: block;width: 80px;background-color: #1abc9c;border:
none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin:
0px auto 35px;cursor: pointer;border-radius: 4px;}\n”;
ptr +=”.button-on {background-color: #1abc9c;}\n”;
ptr +=”.button-on:active {background-color: #16a085;}\n”;
ptr +=”.button-off {background-color: #34495e;}\n”;
ptr +=”.button-off:active {background-color: #2c3e50;}\n”;
ptr +=”p {font-size: 14px;color: #888;margin-bottom: 10px;}\n”;
ptr +=”\n”;
ptr +=”\n”;
ptr +=”\n”;
ptr +=”ESP8266 Web Server\n”;
ptr +=”Using Station(STA) Mode\n”;
if(led1stat)
{ptr +=”LED1 Status: ONOFF\n”;}
else
{ptr +=”LED1 Status: OFFON\n”;}
if(led2stat)
{ptr +=”LED2 Status: ONOFF\n”;}
else
{ptr +=”LED2 Status: OFFON\n”;}
ptr +=”\n”;
ptr +=”\n”;
return ptr;
}
Programming the ESP8266(WiFi Module) To
turn on a LED
Name:
ESP8266
▪ First we need to set up a server and connect
to the WiFi server we just created.
▪ After connection is completed, an IP address
will pop up
▪ Visit the IP address, so control the led
▪ How does the led know to turn on?
▪ When visiting the IP address, the browser
will send a HTTP request to ESP8266.
When ESP8266 read the request, it knows
that user wants to turn on the LED or off.
Pictures
When connected
Successfully, this window
Will show the IP address
You should visit to
Declare PIN D7 as the output
which will be connected to the led
lights.
This is an example of what push button will be like
Final Project
Problem Description:
Wiring Diagram:
Pseudo Code:
Code:
#include
#include
/*Put your SSID & Password*/
const char* ssid = “Abdulgani”; // Enter SSID here
const char* password = “19961996”; //Enter Password here
ESP8266WebServer server(80);
uint8_t LED1pin = D7;
bool LED1status = LOW;
uint8_t LED2pin = D6;
bool LED2status = LOW;
void setup() {
Serial.begin(115200);
delay(100);
pinMode(LED1pin, OUTPUT);
pinMode(LED2pin, OUTPUT);
Serial.println(“Connecting to “);
Serial.println(ssid);
//connect to your local wi-fi network
WiFi.begin(ssid, password);
//check wi-fi is connected to wi-fi network
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected..!”);
Serial.print(“Got IP: “); Serial.println(WiFi.localIP());
server.on(“/”, handle_OnConnect);
server.on(“/led1on”, handle_led1on);
server.on(“/led1off”, handle_led1off);
server.on(“/led2on”, handle_led2on);
server.on(“/led2off”, handle_led2off);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println(“HTTP server started”);
}
void loop() {
server.handleClient();
if(LED1status)
{digitalWrite(LED1pin, HIGH);}
else
{digitalWrite(LED1pin, LOW);}
if(LED2status)
{digitalWrite(LED2pin, HIGH);}
else
{digitalWrite(LED2pin, LOW);}
}
void handle_OnConnect() {
LED1status = LOW;
LED2status = LOW;
Serial.println(“GPIO7 Status: OFF | GPIO6 Status: OFF”);
server.send(200, “text/html”, SendHTML(LED1status,LED2status));
}
void handle_led1on() {
LED1status = HIGH;
Serial.println(“GPIO7 Status: ON”);
server.send(200, “text/html”, SendHTML(true,LED2status));
}
void handle_led1off() {
LED1status = LOW;
Serial.println(“GPIO7 Status: OFF”);
server.send(200, “text/html”, SendHTML(false,LED2status));
}
void handle_led2on() {
LED2status = HIGH;
Serial.println(“GPIO6 Status: ON”);
server.send(200, “text/html”, SendHTML(LED1status,true));
}
void handle_led2off() {
LED2status = LOW;
Serial.println(“GPIO6 Status: OFF”);
server.send(200, “text/html”, SendHTML(LED1status,false));
}
void handle_NotFound(){
server.send(404, “text/plain”, “Not found”);
}
String SendHTML(uint8_t led1stat,uint8_t led2stat){
String ptr = ” \n”;
ptr +=”\n”;
ptr +=”LED Control\n”;
ptr +=”html { font-family: Helvetica; display: inline-block; margin: 0px auto; textalign: center;}\n”;
ptr +=”body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color:
#444444;margin-bottom: 50px;}\n”;
ptr +=”.button {display: block;width: 80px;background-color: #1abc9c;border:
none;color: white;padding: 13px 30px;text-decoration: none;font-size: 25px;margin: 0px
auto 35px;cursor: pointer;border-radius: 4px;}\n”;
ptr +=”.button-on {background-color: #1abc9c;}\n”;
ptr +=”.button-on:active {background-color: #16a085;}\n”;
ptr +=”.button-off {background-color: #34495e;}\n”;
ptr +=”.button-off:active {background-color: #2c3e50;}\n”;
ptr +=”p {font-size: 14px;color: #888;margin-bottom: 10px;}\n”;
ptr +=”\n”;
ptr +=”\n”;
ptr +=”\n”;
ptr +=”ESP8266 Web Server\n”;
ptr +=”Using Station(STA) Mode\n”;
if(led1stat)
{ptr +=”LED1 Status: ONOFF\n”;}
else
{ptr +=”LED1 Status: OFFON\n”;}
if(led2stat)
{ptr +=”LED2 Status: ONOFF\n”;}
else
{ptr +=”LED2 Status: OFFON\n”;}
ptr +=”\n”;
ptr +=”\n”;
return ptr;
}
Video:
1