Advertisement
Guest User

Untitled

a guest
Aug 9th, 2012
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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[] = { 0x90, 0xA2, 0xDA, 0x00, 0xFB, 0x94 }; //this is the mac address on the sticker on your Ethernet shield
  17. byte ip[] = { 192, 168, 1, 177 }; //here you are setting the ip address of the Ethernet shield yourself. The last three digits can be whatever you like. But they must be inside the pool of allowed ip's which is usually 200. So you can use any number from 192.168.1.1 to 192. 168.1.200 as long as they are not being used my any other device on you network. Use this ip when setting up port forwarding in the router for NAT-> port forwarding.
  18. byte gateway[] = { 192, 168, 1, 1 }; //this is the ip you get when you run the example sketch DhcpAddressPrinter to find this ip remember to look at the serial monitor.
  19. byte subnet[] = { 255, 255, 255, 0 }; //this can be found in cmd->ipconfig, but usually it is as it is here and so shouldn't need to be changed.
  20. EthernetServer server(80); //you will have to set this to the same as the start and end ports of the router configuration. Both the start and end ports are the same.
  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://chriscosma.co.cc/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.             client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/on.png' />");
  92.           }
  93.           else{
  94.           if(readString.indexOf("?lightoff") >0)//checks for off
  95.           {
  96.             digitalWrite(6, LOW);    // set pin 4 low
  97.             Serial.println("Led Off");
  98.             client.println("<link rel='apple-touch-icon' href='http://chriscosma.co.cc/off.png' />");
  99.           }
  100.           }
  101.           //clearing string for next read
  102.           readString="";
  103.  
  104.         }
  105.       }
  106.     }
  107.   }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement