Advertisement
sxiii

Arduino OpenDoor Project

Apr 6th, 2015
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.14 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h> // Initialize the libraries.
  3. byte mac[] = { 0x00, 0x05, 0xB5, 0x00, 0x05, 0xB5 }; //I left the MAC address and IP address blank.
  4. byte ip[] = { 192, 168, 1, 200 }; // You will want to fill these in with your MAC and IP address.
  5. EthernetServer server(80); // Assigning the port forwarded number. It's almost always 80.
  6. String readString; // We will be using strings to keep track of things.
  7. int val; // Using val as a variable for the PIR status.
  8. int pir = 3;
  9. void setup() {
  10.   pinMode(2, INPUT);
  11.   pinMode(pir, OUTPUT);
  12.   Ethernet.begin(mac, ip);
  13. }
  14. void loop() {
  15.   EthernetClient client = server.available();
  16.   if (client) {
  17.     while (client.connected()) {
  18.       if (client.available()) {
  19.         char c = client.read();
  20.         if (readString.length() < 100) {
  21.           readString += c;
  22.         }
  23.         if (c == '\n') {
  24.           // init
  25.             Serial.println(readString); // And here we begin including the HTML
  26.           client.println("HTTP/1.1 200 OK");
  27.           client.println("Content-Type: text/html");
  28.           client.println();
  29.           client.println("<!DOCTYPE html>");
  30.           client.println("<html lang='en'>");
  31.           client.println("<head><meta charset='UTF-8'><title>OpenDoor</title></head>");
  32.           client.println("<style>body{text-align:center;background-color:#2980b9;}h1{font-size:2.2em;color:#fff;}input{ background:#2c3e50;height:75px;width:75px; border-radius:100%; font-size:2em;border:0px solid #000;margin:5px;color:#fff;}</style>");
  33.           client.println("<body>");
  34.           client.println("<h1>Aloha</h1>");
  35.           client.println("<FORM method='GET'>");
  36.           client.println("<INPUT type=button value=1 onClick=window.location+=1>");
  37.           client.println("<INPUT type=button value=2 onClick=window.location+=2>");
  38.           client.println("<INPUT type=button value=3 onClick=window.location+=3>");
  39.           client.println("<br>");
  40.           client.println("<INPUT type=button value=4 onClick=window.location+=4>");
  41.           client.println("<INPUT type=button value=5 onClick=window.location+=5>");
  42.           client.println("<INPUT type=button value=6 onClick=window.location+=6>");
  43.           client.println("<br>");
  44.           client.println("<INPUT type=button value=7 onClick=window.location+=7>");
  45.           client.println("<INPUT type=button value=8 onClick=window.location+=8>");
  46.           client.println("<INPUT type=button value=9 onClick=window.location+=9>");
  47.           client.println("</FORM>"); // Above and below you'll see that when we click on a button, it adds a little snippet
  48.           client.println("</body>");
  49.           client.println("</html>");
  50.           if (readString.indexOf("1234 ") > 0  )
  51.           {
  52.             client.println("<script>console.log('on');</script>");
  53.             pinMode(2, OUTPUT);
  54.             delay(2000);
  55.             client.println("<script>console.log('off');</script>");
  56.             pinMode(2, INPUT);
  57.           }
  58.           else {
  59.             client.println("<script>console.log('false');</script>");
  60.           }
  61.           readString = "";
  62.           client.stop();
  63.         }
  64.       }
  65.     }
  66.   }
  67.  
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement