Components and supplies
LED (generic)
NodeMCU ESP8266 Breakout Board
Project description
Code
Code
c_cpp
1//Nodemcu tutorial 2//controlling led with web server. 3//made by shauryacool in arduino ide 4//tutorial link: https://create.arduino.cc/projecthub/shauryacool/web-server-led-control-with-nodemcu-and-nodemcu-setup-c2a84f 5//copyrights reserved 6#include <ESP8266WiFi.h>//We need it or compilers oxygen tank will explode. 7 8const char* ssid = "ssid";//Write your network ssid 9const char* password = "password";//Network password 10 11int ledPin = D7; 12WiFiServer server(80); 13 14void setup() { 15 16 pinMode(ledPin,OUTPUT); 17 digitalWrite(ledPin,LOW);//We start with the led turned off. 18 19 Serial.begin(115200);//remember to set the serial monitor to 115200 or you will see a lot of junk text. 20 Serial.println(); 21 Serial.print("Wifi connecting to "); 22 Serial.println( ssid );//your ssid 23 24 WiFi.begin(ssid,password);//connecting to wifi 25 26 Serial.println(); 27 Serial.print("Connecting");//connecting 28 29 while( WiFi.status() != WL_CONNECTED ){ 30 delay(500); 31 Serial.print("."); 32 } 33 34 35 Serial.println(); 36 37 Serial.println("Wifi Connected Success!");//If network connection is success we will see these messages 38 Serial.print("NodeMCU IP Address : "); 39 Serial.println(WiFi.localIP() ); 40 41 server.begin(); 42 Serial.println("NodeMCU Server started");//server has started 43 44 // Print the IP address 45 Serial.print("Use this URL to connect: "); 46 Serial.print("http://"); 47 Serial.print(WiFi.localIP()); 48 Serial.println("/"); 49 50} 51 52void loop() { 53 54 // Check if a client has connected 55 WiFiClient client = server.available(); 56 if (!client) { 57 return; 58 } 59 60 // Wait until the client sends some data 61 Serial.println("Hello New client");//Come on, we also have to welcome the client, but the client will not able to see it. 62 while(!client.available()){ 63 delay(1); 64 } 65 66 // Read the first line of the request 67 String request = client.readStringUntil('\r'); 68 Serial.println(request); 69 client.flush(); 70 71 // Match the request 72 73 int value = LOW; 74 if (request.indexOf("/LED=ON") != -1) { 75 digitalWrite(ledPin, HIGH); 76 value = HIGH; 77 } 78 if (request.indexOf("/LED=OFF") != -1) { 79 digitalWrite(ledPin, LOW); 80 value = LOW; 81 } 82 83// Set ledPin according to the request 84//digitalWrite(ledPin, value); 85 86 // Return the response 87 client.println("HTTP/1.1 200 OK"); 88 client.println("Content-Type: text/html"); 89 client.println(""); // do not forget this one 90 client.println("<!DOCTYPE HTML>"); 91 client.println("<html>"); 92 client.println("<body>"); 93 client.println("<h1>LED Control</h1>"); 94 client.println("<p>Made by Shaurya Pratap Singh</p>"); 95 96 client.print("Led pin is now: "); 97 98 if(value == HIGH) { 99 client.print("On"); 100 } else { 101 client.print("Off"); 102 } 103 client.println("<br><br>"); 104 client.println("<a href=\\"/LED=ON\\"\\"><button>Turn On </button></a>"); 105 client.println("<a href=\\"/LED=OFF\\"\\"><button>Turn Off </button></a><br />"); 106 client.println("</html>"); 107 108 delay(1); 109 Serial.println("Client disonnected"); 110 Serial.println(""); 111 112}
Code
c_cpp
1//Nodemcu tutorial 2//controlling led with web server. 3//made by shauryacool in arduino ide 4//tutorial link: https://create.arduino.cc/projecthub/shauryacool/web-server-led-control-with-nodemcu-and-nodemcu-setup-c2a84f 5//copyrights reserved 6#include <ESP8266WiFi.h>//We need it or compilers oxygen tank will explode. 7 8const char* ssid = "ssid";//Write your network ssid 9const char* password = "password";//Network password 10 11int ledPin = D7; 12WiFiServer server(80); 13 14void setup() { 15 16 pinMode(ledPin,OUTPUT); 17 digitalWrite(ledPin,LOW);//We start with the led turned off. 18 19 Serial.begin(115200);//remember to set the serial monitor to 115200 or you will see a lot of junk text. 20 Serial.println(); 21 Serial.print("Wifi connecting to "); 22 Serial.println( ssid );//your ssid 23 24 WiFi.begin(ssid,password);//connecting to wifi 25 26 Serial.println(); 27 Serial.print("Connecting");//connecting 28 29 while( WiFi.status() != WL_CONNECTED ){ 30 delay(500); 31 Serial.print("."); 32 } 33 34 35 Serial.println(); 36 37 Serial.println("Wifi Connected Success!");//If network connection is success we will see these messages 38 Serial.print("NodeMCU IP Address : "); 39 Serial.println(WiFi.localIP() ); 40 41 server.begin(); 42 Serial.println("NodeMCU Server started");//server has started 43 44 // Print the IP address 45 Serial.print("Use this URL to connect: "); 46 Serial.print("http://"); 47 Serial.print(WiFi.localIP()); 48 Serial.println("/"); 49 50} 51 52void loop() { 53 54 // Check if a client has connected 55 WiFiClient client = server.available(); 56 if (!client) { 57 return; 58 } 59 60 // Wait until the client sends some data 61 Serial.println("Hello New client");//Come on, we also have to welcome the client, but the client will not able to see it. 62 while(!client.available()){ 63 delay(1); 64 } 65 66 // Read the first line of the request 67 String request = client.readStringUntil('\ '); 68 Serial.println(request); 69 client.flush(); 70 71 // Match the request 72 73 int value = LOW; 74 if (request.indexOf("/LED=ON") != -1) { 75 digitalWrite(ledPin, HIGH); 76 value = HIGH; 77 } 78 if (request.indexOf("/LED=OFF") != -1) { 79 digitalWrite(ledPin, LOW); 80 value = LOW; 81 } 82 83// Set ledPin according to the request 84//digitalWrite(ledPin, value); 85 86 // Return the response 87 client.println("HTTP/1.1 200 OK"); 88 client.println("Content-Type: text/html"); 89 client.println(""); // do not forget this one 90 client.println("<!DOCTYPE HTML>"); 91 client.println("<html>"); 92 client.println("<body>"); 93 client.println("<h1>LED Control</h1>"); 94 client.println("<p>Made by Shaurya Pratap Singh</p>"); 95 96 client.print("Led pin is now: "); 97 98 if(value == HIGH) { 99 client.print("On"); 100 } else { 101 client.print("Off"); 102 } 103 client.println("<br><br>"); 104 client.println("<a href=\\"/LED=ON\\"\\"><button>Turn On </button></a>"); 105 client.println("<a href=\\"/LED=OFF\\"\\"><button>Turn Off </button></a><br />"); 106 client.println("</html>"); 107 108 delay(1); 109 Serial.println("Client disonnected"); 110 Serial.println(""); 111 112}
Downloadable files
Schematics
Schematics
Comments
Only logged in users can leave comments
shauryacool
0 Followers
•1 Projects
2
0