Advertisement
Guest User

Moises lab

a guest
Dec 22nd, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 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. const int ledPin =  28;
  13. const int led2Pin =  29;
  14. const int led3Pin =  30;
  15.  
  16. int buttonState0 = 0;
  17. int buttonState1 = 0;
  18.  
  19. int button2State0 = 0;
  20. int button2State1 = 0;
  21.  
  22. int button3State0 = 0;
  23. int button3State1 = 0;
  24.  
  25. void setup() {
  26.  
  27.  
  28.   pinMode(28, OUTPUT); //pin selected to control
  29.   pinMode(29, OUTPUT); //pin selected to control
  30.   pinMode(30, OUTPUT); //pin selected to control
  31.   //start Ethernet
  32.   Ethernet.begin(mac, ip, gateway, gateway, subnet);
  33.   server.begin();
  34.  
  35.   //enable serial data print
  36.   Serial.begin(9600);
  37.   Serial.println("server multi pin button test 1.0"); // so I can keep track of what is loaded
  38. }
  39.  
  40. void loop() {
  41.   // Create a client connection
  42.   EthernetClient client = server.available();
  43.   if (client) {
  44.     while (client.connected()) {
  45.       if (client.available()) {
  46.         char c = client.read();
  47.  
  48.         //read char by char HTTP request
  49.         if (readString.length() < 100) {
  50.  
  51.           //store characters to string
  52.           readString += c;
  53.           //Serial.print(c);
  54.         }
  55.  
  56.         //if HTTP request has ended
  57.         if (c == '\n') {
  58.  
  59.           ///////////////
  60.           Serial.println(readString); //print to serial monitor for debuging
  61.  
  62.           client.println("HTTP/1.1 200 OK"); //send new page
  63.           client.println("Content-Type: text/html");
  64.           client.println();
  65.  
  66.           client.println("<HTML>");
  67.           client.println("<HEAD>");
  68.           client.println("<TITLE>Arduino</TITLE>");
  69.           client.println("</HEAD>");
  70.           client.println("<BODY>");
  71.  
  72.           client.println("<H1><center>ARDUINO WEB BUTTON</center></H1>");
  73.  
  74.           client.println("<br>");
  75.  
  76.           // custom buttons
  77.  
  78.           client.print("<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>");
  79.           client.println("<br>");
  80.           client.print("<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>");
  81.           client.println("<br>");
  82.           client.print("<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>");
  83.  
  84.           client.println("<script>function mDown(obj,comando){obj.style.backgroundColor='#1ec5e5';obj.innerHTML='ACESO';location.href=\"?\" + comando}");
  85.           client.println("function mUp(obj,comando){obj.style.backgroundColor='#D94A38';obj.innerHTML='APAGADO';location.href=\"?\" + comando}</script>");
  86.  
  87.           client.println("</BODY>");
  88.           client.println("</HTML>");
  89.  
  90.           delay(1);
  91.           //stopping client
  92.           client.stop();
  93.  
  94.           buttonState0 = readString.indexOf('1ON') > 0;
  95.           buttonState1 = readString.indexOf('1OFF') > 0;
  96.  
  97.           button2State0 = readString.indexOf('2ON') > 0;
  98.           button2State1 = readString.indexOf('2OFF') > 0;
  99.  
  100.           button3State0 = readString.indexOf('3ON') > 0;
  101.           button3State1 = readString.indexOf('3OFF') > 0;
  102.  
  103.           if (buttonState0 == HIGH) {
  104.             digitalWrite(ledPin, HIGH);
  105.           }
  106.           if (buttonState1 == HIGH) {
  107.             digitalWrite(ledPin, LOW);
  108.           }
  109.  
  110.           if (button2State0 == HIGH) {
  111.             digitalWrite(led2Pin, HIGH);
  112.           }
  113.           if (button2State1 == HIGH) {
  114.             digitalWrite(led2Pin, LOW);
  115.           }
  116.  
  117.           if (button3State0 == HIGH) {
  118.             digitalWrite(led3Pin, HIGH);
  119.           }
  120.           if (button3State1 == HIGH) {
  121.             digitalWrite(led3Pin, LOW);
  122.           }
  123.  
  124.           //clearing string for next read
  125.           readString = "";
  126.  
  127.         }
  128.       }
  129.     }
  130.   }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement