Advertisement
Guest User

Code

a guest
Nov 30th, 2012
15,332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. //simple button GET server code to control servo and arduino pin 5
  2. //for use with IDE 1.0
  3. //open serial monitor to see what the arduino receives
  4. //use the \ slash to escape the " in the html
  5. //for use with W5100 based ethernet shields
  6. //Powering a servo from the arduino usually DOES NOT WORK.
  7. //note that the below bug fix may be required
  8. // http://code.google.com/p/arduino/issues/detail?id=605
  9.  
  10. #include <SPI.h>
  11. #include <Ethernet.h>
  12.  
  13. #include <Servo.h>
  14. Servo myservo;  // create servo object to control a servo
  15.  
  16. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
  17. byte ip[] = { 192, 168, 1, 177 }; // ip in lan
  18. byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
  19. byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
  20. EthernetServer server(80); //server port
  21.  
  22. String readString;
  23.  
  24. //////////////////////
  25.  
  26. void setup(){
  27.  
  28.   pinMode(6, OUTPUT); //pin selected to control
  29.   //start Ethernet
  30.   Ethernet.begin(mac, ip, gateway, subnet);
  31.   server.begin();
  32.   //the pin for the servo co
  33.   //enable serial data print
  34.   Serial.begin(9600);
  35.   Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
  36. }
  37.  
  38. void loop(){
  39.   // Create a client connection
  40.   EthernetClient client = server.available();
  41.   if (client) {
  42.     while (client.connected()) {
  43.       if (client.available()) {
  44.         char c = client.read();
  45.  
  46.         //read char by char HTTP request
  47.         if (readString.length() < 100) {
  48.  
  49.           //store characters to string
  50.           readString += c;
  51.           //Serial.print(c);
  52.         }
  53.  
  54.         //if HTTP request has ended
  55.         if (c == '\n') {
  56.  
  57.           ///////////////
  58.           Serial.println(readString); //print to serial monitor for debuging
  59.  
  60.           client.println("HTTP/1.1 200 OK"); //send new page
  61.           client.println("Content-Type: text/html");
  62.           client.println();
  63.  
  64.           client.println("<HTML>");
  65.           client.println("<HEAD>");
  66.           client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
  67.           client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
  68.           client.println("<link rel='stylesheet' type='text/css' href='http://homeautocss.net84.net/a.css' />");
  69.           client.println("<TITLE>Home Automation</TITLE>");
  70.           client.println("</HEAD>");
  71.           client.println("<BODY>");
  72.           client.println("<H1>Home Automation</H1>");
  73.           client.println("<hr />");
  74.           client.println("<br />");
  75.          
  76.           client.println("<a href=\"/?lighton\"\">Turn On Light</a>");
  77.           client.println("<a href=\"/?lightoff\"\">Turn Off Light</a><br />");        
  78.  
  79.           client.println("</BODY>");
  80.           client.println("</HTML>");
  81.  
  82.           delay(1);
  83.           //stopping client
  84.           client.stop();
  85.  
  86.           ///////////////////// control arduino pin
  87.           if(readString.indexOf("?lighton") >0)//checks for on
  88.           {
  89.             digitalWrite(6, HIGH);    // set pin 4 high
  90.             Serial.println("Led On");
  91.           }
  92.           else{
  93.           if(readString.indexOf("?lightoff") >0)//checks for off
  94.           {
  95.             digitalWrite(6, LOW);    // set pin 4 low
  96.             Serial.println("Led Off");
  97.           }
  98.           }
  99.           //clearing string for next read
  100.           readString="";
  101.  
  102.         }
  103.       }
  104.     }
  105.   }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement