Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- // Replace with your network credentials
- const char* ssid = "Honor8";
- const char* password = "1344134770";
- ESP8266WebServer server(80); //instantiate server at port 80 (http port)
- String page = "";
- int LEDPin = 2;
- void setup(void){
- //the HTML of the web page
- page = "<h1>Simple NodeMCU Web Server</h1><p><a href=\"LEDOn\"><button>ON</button></a> <a href=\"LEDOff\"><button>OFF</button></a></p>";
- //make the LED pin output and initially turned off
- pinMode(LEDPin, OUTPUT);
- digitalWrite(LEDPin, LOW);
- delay(100);
- Serial.begin(115200);
- WiFi.begin(ssid, password); //begin WiFi connection
- Serial.println("");
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- server.on("/", [](){
- server.send(200, "text/html", page);
- });
- server.on("/LEDOn", [](){
- server.send(200, "text/html", page);
- digitalWrite(LEDPin, HIGH);
- delay(100);
- });
- server.on("/LEDOff", [](){
- server.send(200, "text/html", page);
- digitalWrite(LEDPin, LOW);
- delay(100);
- });
- server.begin();
- Serial.println("Web server started!");
- }
- void loop(void){
- server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement