Advertisement
Guest User

Moises lab 2

a guest
Dec 22nd, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.87 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3.  
  4. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
  5. byte ip[] = { 192, 168, 1, 188 }; // ip in lan
  6. byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
  7. byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
  8. EthernetServer server(8089); //server port
  9.  
  10. String readString;
  11.  
  12. #define ledPin 28
  13. #define led2Pin 29
  14. #define led3Pin 30
  15.  
  16. void setup() {
  17.  
  18.   pinMode(ledPin, OUTPUT); //pin selected to control
  19.   pinMode(led2Pin, OUTPUT); //pin selected to control
  20.   pinMode(led3Pin, OUTPUT); //pin selected to control
  21.   //start Ethernet
  22.   Ethernet.begin(mac, ip, gateway, gateway, subnet);
  23.   server.begin();
  24.  
  25.   //enable serial data print
  26.   Serial.begin(9600);
  27.   Serial.println("server multi pin button test 1.0"); // so I can keep track of what is loaded
  28. }
  29.  
  30. void loop() {
  31.   // Create a client connection
  32.   EthernetClient client = server.available();
  33.   if (client) {
  34.     while (client.connected()) {
  35.       if (client.available()) {
  36.         char c = client.read();
  37.  
  38.         //read char by char HTTP request
  39.         if (readString.length() < 30) {
  40.  
  41.           //store characters to string
  42.           readString += c;
  43.           //Serial.print(c);
  44.         }
  45.  
  46.         //if HTTP request has ended
  47.         if (c == '\n') {
  48.  
  49.           ///////////////
  50.           Serial.println(readString); //print to serial monitor for debuging
  51.  
  52.           client.println("HTTP/1.1 200 OK"); //send new page
  53.           client.println("Content-Type: text/html");
  54.           client.println();
  55.  
  56.           client.println("<HTML>");
  57.           client.println("<HEAD>");
  58.           client.println("<TITLE>Arduino</TITLE>");
  59.           client.println("</HEAD>");
  60.           client.println("<BODY>");
  61.  
  62.           client.println("<H1><center>ARDUINO WEB BUTTON</center></H1>");
  63.  
  64.           client.println("<br>");
  65.  
  66.           // custom buttons
  67.  
  68.           client.println("<table align='center' border=10 bgcolor='green' ><th><h2><div onmousedown='mDown(this,\"1ON\")' onmouseup='mUp(this,\"1OFF\")' style='background-color:'#D94A38';width:130px;height:30px;padding:10px;'>-0-</div></table>");
  69.           client.println("<br>");
  70.           client.println("<table align='center' border=10 bgcolor='green' ><th><h2><div onmousedown='mDown(this,\"2ON\")' onmouseup='mUp(this,\"2OFF\")' style='background-color:'#D94A38';width:130px;height:30px;padding:10px;'>-0-</div></table>");
  71.           client.println("<br>");
  72.           client.println("<table align='center' border=10 bgcolor='green' ><th><h2><div onmousedown='mDown(this,\"3ON\")' onmouseup='mUp(this,\"3OFF\")' style='background-color:'#D94A38';width:130px;height:30px;padding:10px;'>-0-</div></table>");
  73.  
  74.           client.println("<script>function mDown(obj,comando){obj.style.backgroundColor='#1ec5e5';obj.innerHTML='ACESO';location.href=\"?\" + comando}");
  75.           client.println("function mUp(obj,comando){obj.style.backgroundColor='#D94A38';obj.innerHTML='APAGADO';location.href=\"?\" + comando}</script>");
  76.  
  77.           client.println("</BODY>");
  78.           client.println("</HTML>");
  79.  
  80.           delay(1);
  81.          
  82.  
  83.           if(readString.indexOf("1ON") >= 0) {
  84.             digitalWrite(ledPin, HIGH);
  85.           }
  86.           if(readString.indexOf("1OFF") >= 0) {
  87.             digitalWrite(ledPin, LOW);
  88.           }
  89.           if(readString.indexOf("2ON") >= 0) {
  90.             digitalWrite(led2Pin, HIGH);
  91.           }
  92.           if(readString.indexOf("2OFF") >= 0) {
  93.             digitalWrite(led2Pin, LOW);
  94.           }
  95.           if(readString.indexOf("3ON") >= 0) {
  96.             digitalWrite(led3Pin, HIGH);
  97.           }
  98.           if(readString.indexOf("3OFF") >= 0) {
  99.             digitalWrite(led3Pin, LOW);
  100.           }
  101.          
  102.           //clearing string for next read
  103.           readString = "";
  104.           //stopping client
  105.           client.stop();
  106.         }
  107.       }
  108.     }
  109.   }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement