fork download
  1. #include <ESP8266WiFi.h>
  2.  
  3. const char* ssid="Bad Boys "
  4. const char* password="876543210"
  5.  
  6. int LOCK=4;//GPI04 d2
  7. WiFiServer server(80);
  8.  
  9. void setup(){
  10. Serial.begin(115200);
  11. delay(10);
  12.  
  13. pinMOode(LOCK,OUTPUT);
  14. digitalWrite(LOCK, LOW);
  15.  
  16. //connect to WiFi network
  17. Serial.println();
  18. Serial.println();
  19. serial.print("connecting to");
  20. Serial.println(ssid);
  21.  
  22. WiFi.begin(ssid,password);
  23.  
  24. while (WiFi.status() !=WL_CONNECTED) {
  25. delay(500);
  26. Serial.print(".");
  27. }
  28. Serial.println("");
  29. Serial.println("WiFi connected");
  30.  
  31. //start the server
  32. server.begin();
  33. Serial.println("Server started");
  34. Serial.println("");
  35. Serial.println("************WiFi doorlock********************");
  36.  
  37. // print the IP address
  38. Serial.print("Use this URL to connect");
  39. Serial.print("http://");
  40. Serial.print(WiFi.localIP());
  41. Serial.println("/");
  42.  
  43. }
  44.  
  45. void loop() {
  46. // Check if a client has connected
  47. wiFiClient client = server.available();
  48. if (!client) {
  49. return;
  50. }
  51.  
  52. //wait until the client sends some data
  53. serial.println("new client");
  54. while(!client.available()){
  55. delay(1);
  56. }
  57.  
  58. //Read the first line of the request
  59. String request = client.readstringUntil('\r');
  60. Serial.println(request);
  61. client.flush();
  62.  
  63. //Match the request
  64.  
  65. int value=LOW;
  66. if (request.indexOf("/LOCK=ON") != -1) {
  67. digitalwrite(LOCK, HIGH);
  68. value = LOW;
  69. }
  70. if (request.indexOf("/LOCK=OFF") != -1) {
  71. digitalWrite(LOCK, LOW);
  72. value = LOW;
  73. }
  74.  
  75. //set LOCK according to the request
  76. //digitalWrite(LOCK, value);
  77.  
  78. // Return the response
  79. client.println("HTTP/1.1 200 OK");
  80. client.println("Content-Type: text/html");
  81. client.println("802.11b/g/n");//do not forget this one
  82. Client.println("<!DOCTYPE HTML>");
  83. client.println("<html>");
  84.  
  85. client.print("Door is now: ");
  86.  
  87. if(value == HIGH){
  88. client.print("Open");
  89. } else
  90. client.print("Closed");
  91. }
  92. client.println("<br><br>");
  93. client.println("<a href=\"/LOCK=ON\"\"><button><h1>Turn on</h1> </button></a>");
  94. client.println("<a href=\"/LOCK=OFF\"\"><button><h1>Turn off </h1></button></a><br />");
  95. client.println("</html>");
  96.  
  97. delay(1);
  98. Serial.println("client disconnected");
  99. Serial.println("");
  100.  
  101. }
Success #stdin #stdout 0.02s 25740KB
stdin
Standard input is empty
stdout
#include <ESP8266WiFi.h>

const char* ssid="Bad Boys "
const char* password="876543210"

int LOCK=4;//GPI04 d2
WiFiServer server(80);

void setup(){
  Serial.begin(115200);
  delay(10);

  pinMOode(LOCK,OUTPUT);
  digitalWrite(LOCK, LOW);

  //connect to WiFi network
  Serial.println();
  Serial.println(); 
  serial.print("connecting to");
  Serial.println(ssid);

  WiFi.begin(ssid,password);

  while (WiFi.status() !=WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  //start the server
  server.begin();
  Serial.println("Server started");
  Serial.println("");
  Serial.println("************WiFi doorlock********************");

  // print the IP address
  Serial.print("Use this URL to connect");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");

}

void loop() {
  // Check if a client has connected
  wiFiClient client = server.available();
  if (!client) {
    return;
  }

  //wait until the client sends some data
  serial.println("new client");
  while(!client.available()){
    delay(1);
  }

  //Read the first line of the request
  String request = client.readstringUntil('\r');
  Serial.println(request);
  client.flush();

  //Match the request

  int value=LOW;
  if (request.indexOf("/LOCK=ON") != -1) {
    digitalwrite(LOCK, HIGH);
    value = LOW;
  }
  if (request.indexOf("/LOCK=OFF") != -1) {
    digitalWrite(LOCK, LOW);
     value = LOW;
  }

  //set LOCK according to the request
    //digitalWrite(LOCK, value);

    // Return the response
    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println("802.11b/g/n");//do not forget this one
    Client.println("<!DOCTYPE HTML>");
    client.println("<html>");

    client.print("Door is now: ");

    if(value == HIGH){
      client.print("Open");
    } else 
      client.print("Closed");
    }
    client.println("<br><br>");
    client.println("<a href=\"/LOCK=ON\"\"><button><h1>Turn on</h1> </button></a>");
    client.println("<a href=\"/LOCK=OFF\"\"><button><h1>Turn off </h1></button></a><br />");
    client.println("</html>");

    delay(1);
    Serial.println("client disconnected");
    Serial.println("");

  }