Day 13- Wifi LED usng NodeMCU
1) Arduino Code to Wifi LED
2) Mit App for Wifi LED
3) Update Mit app aia file for 4 Ch
4) Make 4ch Home automaton for using Push button and Mobile app
--------------------------------------------------------------------------------------------------------------------
Code
#include <ESP8266WiFi.h>
//Hotspot created // 192.168.4.1
const char AP_NameChar[] = "My Wifi LED";
const char WiFiPassword[] = "";
WiFiServer server(80);
String request = "";
int LED_Pin = 16;
void setup()
{
Serial.begin(115200);
pinMode(LED_Pin, OUTPUT);
digitalWrite(LED_Pin, LOW);//Turn on LED
delay(400);
digitalWrite(LED_Pin, HIGH);//Turn off LED
WiFi.disconnect();
boolean conn = WiFi.softAP(AP_NameChar, WiFiPassword);
server.begin();
}
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Read the first line of the request
request = client.readStringUntil('\r');
if ( request.indexOf("LEDON") > 0 ) {
digitalWrite(LED_Pin, LOW);
//client.flush();
client.println("HTTP/1.1 200 OK\r\n");
client.println( "LEDON");
client.flush();
Serial.println("inside ON");
}
else if ( request.indexOf("LEDOFF") > 0 ) {
digitalWrite(LED_Pin, HIGH);
//client.flush();
client.println("HTTP/1.1 200 OK\r\n");
client.println( "LEDOFF");
client.flush();
Serial.println("inside OFF");
}
delay(5);
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------
4ch wifi led
Code
#include <ESP8266WiFi.h>
//Hotspot created // 192.168.4.1
const char AP_NameChar[] = "MySmartBulb";
const char WiFiPassword[] = "12345678";
WiFiServer server(80);
String request = "";
int LED_Pin1 = 16; //D0
int LED_Pin2 = 5; //D1
int LED_Pin3 = 4; //D2
int LED_Pin4 = 0; //D3
void setup()
{
Serial.begin(115200);
pinMode(LED_Pin1, OUTPUT);
pinMode(LED_Pin2, OUTPUT);
pinMode(LED_Pin3, OUTPUT);
pinMode(LED_Pin4, OUTPUT);
digitalWrite(LED_Pin2, LOW);
digitalWrite(LED_Pin1, LOW);//Turn on LED
digitalWrite(LED_Pin3, LOW);
digitalWrite(LED_Pin4, LOW);
Serial.println("Server is being prepared");
WiFi.disconnect();
boolean conn = WiFi.softAP(AP_NameChar, WiFiPassword);
server.begin();
delay(1000);
}
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Read the first line of the request
request = client.readStringUntil('\r');
if ( request.indexOf("LED1ON") > 0 ) {
digitalWrite(LED_Pin1, LOW);
//client.flush();
client.println("HTTP/1.1 200 OK\r\n");
client.println( "LED1ON");
client.flush();
Serial.println("inside ON");
}
else if ( request.indexOf("LED1OFF") > 0 ) {
digitalWrite(LED_Pin1, HIGH);
//client.flush();
client.println("HTTP/1.1 200 OK\r\n");
client.println( "LEDOFF");
client.flush();
Serial.println("inside OFF");
}
else if ( request.indexOf("LED2ON") > 0 ) {
digitalWrite(LED_Pin2, LOW);
//client.flush();
client.println("HTTP/1.1 200 OK\r\n");
client.println( "LED 2 ON");
client.flush();
Serial.println("inside OFF");
}
else if ( request.indexOf("LED2OFF") > 0 ) {
digitalWrite(LED_Pin2, HIGH);
//client.flush();
client.println("HTTP/1.1 200 OK\r\n");
client.println( "LED2OFF");
client.flush();
Serial.println("inside 2 OFF");
}
else if ( request.indexOf("LED3ON") > 0 ) {
digitalWrite(LED_Pin3, LOW);
//client.flush();
client.println("HTTP/1.1 200 OK\r\n");
client.println( "LED3OFF");
client.flush();
Serial.println("inside 3 OFF");
}
else if ( request.indexOf("LED3OFF") > 0 ) {
digitalWrite(LED_Pin3, HIGH);
//client.flush();
client.println("HTTP/1.1 200 OK\r\n");
client.println( "LED3OFF");
client.flush();
Serial.println("inside 3 OFF");
}
else if ( request.indexOf("LED4ON") > 0 ) {
digitalWrite(LED_Pin4, LOW);
//client.flush();
client.println("HTTP/1.1 200 OK\r\n");
client.println( "LED4OFF");
client.flush();
Serial.println("inside 4 OFF");
}
else if ( request.indexOf("LED4OFF") > 0 ) {
digitalWrite(LED_Pin4, HIGH);
//client.flush();
client.println("HTTP/1.1 200 OK\r\n");
client.println( "LED4OFF");
client.flush();
Serial.println("inside 4 OFF");
}
delay(5);
}
-------------------------------------------------------------------
No comments:
Post a Comment