learnelectronics

ESP32 House Automation

Sep 15th, 2017
13,368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. /*
  2.  ESP32 House Automation
  3.  
  4.  learnelectronics
  5.  14 SEPT 2017
  6.  
  7.  www.youtube.com/c/learnelectronics
  8.  
  9.  */
  10.  
  11. #include <WiFi.h>
  12.  
  13. const char* ssid     = "SETCONTROLSFOR";
  14. const char* password = "THEHEARTOFTHESUN";
  15.  
  16. WiFiServer server(80);
  17.  
  18. void setup()
  19. {
  20.     Serial.begin(115200);
  21.     pinMode(5, OUTPUT);      // set the LED pin mode
  22.     pinMode(12,OUTPUT);
  23.     pinMode(13,OUTPUT);
  24.    
  25.  
  26.     delay(10);
  27.  
  28.     // We start by connecting to a WiFi network
  29.  
  30.     Serial.println();
  31.     Serial.println();
  32.     Serial.print("Connecting to ");
  33.     Serial.println(ssid);
  34.  
  35.     WiFi.begin(ssid, password);
  36.  
  37.     while (WiFi.status() != WL_CONNECTED) {
  38.         delay(500);
  39.         Serial.print(".");
  40.     }
  41.  
  42.     Serial.println("");
  43.     Serial.println("WiFi connected.");
  44.     Serial.println("IP address: ");
  45.     Serial.println(WiFi.localIP());
  46.    
  47.     server.begin();
  48.  
  49. }
  50.  
  51. int value = 0;
  52.  
  53. void loop(){
  54.  WiFiClient client = server.available();   // listen for incoming clients
  55.  
  56.   if (client) {                             // if you get a client,
  57.     Serial.println("New Client.");           // print a message out the serial port
  58.     String currentLine = "";                // make a String to hold incoming data from the client
  59.     while (client.connected()) {            // loop while the client's connected
  60.       if (client.available()) {             // if there's bytes to read from the client,
  61.         char c = client.read();             // read a byte, then
  62.         Serial.write(c);                    // print it out the serial monitor
  63.         if (c == '\n') {                    // if the byte is a newline character
  64.  
  65.           // if the current line is blank, you got two newline characters in a row.
  66.           // that's the end of the client HTTP request, so send a response:
  67.           if (currentLine.length() == 0) {
  68.             // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  69.             // and a content-type so the client knows what's coming, then a blank line:
  70.             client.println("HTTP/1.1 200 OK");
  71.             client.println("Content-type:text/html");
  72.             client.println();
  73.  
  74.             // the content of the HTTP response follows the header:
  75.             client.print("Click <a href=\"/L\">here</a> to turn KITCHEN on.<br>");
  76.             client.print("Click <a href=\"/H\">here</a> to turn KITCHEN off.<br>");
  77.            
  78.  
  79.             client.print("Click <a href=\"/I\">here</a> to turn WALKWAY on.<br>");
  80.             client.print("Click <a href=\"/J\">here</a> to turn WALKWAY off.<br>");
  81.  
  82.             client.print("Click <a href=\"/O\">here</a> to turn BACK DOOR on.<br>");
  83.             client.print("Click <a href=\"/P\">here</a> to turn BACK DOOR off.<br>");
  84.            
  85.  
  86.             // The HTTP response ends with another blank line:
  87.             client.println();
  88.             // break out of the while loop:
  89.             break;
  90.           } else {    // if you got a newline, then clear currentLine:
  91.             currentLine = "";
  92.           }
  93.         } else if (c != '\r') {  // if you got anything else but a carriage return character,
  94.           currentLine += c;      // add it to the end of the currentLine
  95.         }
  96.  
  97.         // Check to see if the client request was "GET /H" or "GET /L":
  98.         if (currentLine.endsWith("GET /H")) {
  99.           digitalWrite(5, HIGH);               // GET /H turns the LED on
  100.         }
  101.         if (currentLine.endsWith("GET /L")) {
  102.           digitalWrite(5, LOW);                // GET /L turns the LED off
  103.         }
  104.  
  105.         if (currentLine.endsWith("GET /I")) {
  106.           digitalWrite(12, HIGH);               // GET /H turns the LED on
  107.         }
  108.         if (currentLine.endsWith("GET /J")) {
  109.           digitalWrite(12, LOW);                // GET /L turns the LED off
  110.         }
  111.  
  112.         if (currentLine.endsWith("GET /O")) {
  113.           digitalWrite(13, HIGH);               // GET /H turns the LED on
  114.         }
  115.         if (currentLine.endsWith("GET /P")) {
  116.           digitalWrite(13, LOW);                // GET /L turns the LED off
  117.         }
  118.       }
  119.     }
  120.     // close the connection:
  121.     client.stop();
  122.     Serial.println("Client Disconnected.");
  123.   }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment