Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4.  
  5. // Replace with your network credentials
  6. const char* ssid = "Honor8";
  7. const char* password = "1344134770";
  8.  
  9. ESP8266WebServer server(80); //instantiate server at port 80 (http port)
  10.  
  11. String page = "";
  12. int LEDPin = 2;
  13. void setup(void){
  14. //the HTML of the web page
  15. page = "<h1>Simple NodeMCU Web Server</h1><p><a href=\"LEDOn\"><button>ON</button></a>&nbsp;<a href=\"LEDOff\"><button>OFF</button></a></p>";
  16. //make the LED pin output and initially turned off
  17. pinMode(LEDPin, OUTPUT);
  18. digitalWrite(LEDPin, LOW);
  19.  
  20. delay(100);
  21. Serial.begin(115200);
  22. WiFi.begin(ssid, password); //begin WiFi connection
  23. Serial.println("");
  24.  
  25. // Wait for connection
  26. while (WiFi.status() != WL_CONNECTED) {
  27. delay(500);
  28. Serial.print(".");
  29. }
  30. Serial.println("");
  31. Serial.print("Connected to ");
  32. Serial.println(ssid);
  33. Serial.print("IP address: ");
  34. Serial.println(WiFi.localIP());
  35.  
  36. server.on("/", [](){
  37. server.send(200, "text/html", page);
  38. });
  39. server.on("/LEDOn", [](){
  40. server.send(200, "text/html", page);
  41. digitalWrite(LEDPin, HIGH);
  42. delay(100);
  43. });
  44. server.on("/LEDOff", [](){
  45. server.send(200, "text/html", page);
  46. digitalWrite(LEDPin, LOW);
  47. delay(100);
  48. });
  49. server.begin();
  50. Serial.println("Web server started!");
  51. }
  52.  
  53. void loop(void){
  54. server.handleClient();
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement