LED with Node MCU

SRJT09303

Jugaadu and his mother are fed up of Chandan's habit of always leaving the room lights on while he leaves the room. His mother asks Jugaadu to make something that can help to operate room lights even remotely. Therefore, it can help them save power?br

  1. Node MCU
  2. Jumper Wires
  3. USB Cable
  4. Foamsheet
  5. Pipe
  6. Gluegun
  7. LEDs
  8. Lamp Holder
  9. Cutter
  10. Breadboard
  11. Scale

    #include  

const char* ssid = "SID"; 
const char* password = "12345678"; 

int LED = D0; // led connected to D0 
WiFiServer server(80); 

void setup() 
{ 
Serial.begin(115200); 
pinMode(LED, OUTPUT); 
digitalWrite(LED, LOW); 

Serial.print("Connecting to Internet "); 
WiFi.begin(ssid, password); 
while (WiFi.status() != WL_CONNECTED) 
{ 
delay(500); 
Serial.print("."); 
} 
Serial.println("WiFi connected"); 

/*-------- server started---------*/ 
server.begin(); 
Serial.println("Server started"); 

/*------printing ip address--------*/ 
Serial.print("IP Address of network: "); 
Serial.println(WiFi.localIP()); 
Serial.print("Copy and paste the following URL: https://"); 
Serial.print(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); 
value = HIGH; 
} 
if (request.indexOf("/LED=OFF") != -1) 
{ 
digitalWrite(LED, LOW); 
value = LOW; 
} 

/*------------------Creating html page---------------------*/ 

client.println("HTTP/1.1 200 OK"); 
client.println("Content-Type: text/html"); 
client.println(""); 
client.println(""); 
client.println(""); 

client.print("LED is: "); 

if(value == HIGH) 
{ 
client.print("ON"); 
} 
else 
{ 
client.print("OFF"); 
} 
client.println("

"); client.println(""); client.println("
"); client.println(""); delay(1); Serial.println("Client disonnected"); Serial.println(""); }