Advertisement
Guest User

Untitled

a guest
Apr 29th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.30 KB | None | 0 0
  1. #include <EtherCard.h>  
  2.  
  3. // ethernet mac address - must be unique on your network  
  4. static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };  
  5. // ethernet interface ip address  
  6. static byte myip[] = { 192,168,1,199 };  
  7. // gateway ip address  
  8. static byte gwip[] = { 192,168,1,252 };  
  9.  
  10. byte Ethernet::buffer[500]; // tcp/ip send and receive buffer  
  11.  
  12. // Using a Variable for the Pin, but it is not necessary  
  13. const int red_pin = 2;  
  14. const int orange_pin = 4;  
  15. const int green_pin = 6;  
  16.  
  17. // Some stuff for responding to the request  
  18. char* on = "ON";  
  19. char* off = "OFF";  
  20. char* statusLabel;  
  21. char* buttonLabel;  
  22.  
  23. // Small web page to return so the request is completed  
  24. char page[] PROGMEM =  
  25. "HTTP/1.0 200 OK\r\n"  
  26. "Content-Type: text/html\r\n"  
  27. "Retry-After: 600\r\n"  
  28. "\r\n"  
  29. "<html>"  
  30.   "<head><title>"  
  31.     "Arduino 192.168.1.199"  
  32.   "</title></head>"  
  33.   "<body>"  
  34.    "<h3>Arduino 192.168.1.199</h3>"  
  35.   "</body>"  
  36. "</html>"  
  37. ;  
  38.  
  39. void setup(){  
  40.   Serial.begin(9600);      // open the serial port at 9600 bps:  
  41.    
  42. // Set Pin2 to be an Output  
  43.   pinMode(red_pin, OUTPUT);  
  44. // Set Pin4 to be an Output  
  45.   pinMode(orange_pin, OUTPUT);  
  46.   // Set Pin6 to be an Output  
  47.   pinMode(green_pin, OUTPUT);  
  48.  
  49. //test om bij opstarten alle lampen even te laten branden
  50. digitalWrite(red_pin, LOW);  
  51. digitalWrite(orange_pin, LOW);  
  52. digitalWrite(green_pin, LOW);  
  53.  
  54. delay(500);
  55.  
  56. digitalWrite(red_pin, LOW);  
  57. digitalWrite(orange_pin, LOW);  
  58. digitalWrite(green_pin, HIGH);  
  59.  
  60. delay(500);
  61.  
  62. digitalWrite(red_pin, LOW);  
  63. digitalWrite(orange_pin, HIGH);  
  64. digitalWrite(green_pin, LOW);  
  65.  
  66. delay(500);
  67.  
  68. digitalWrite(red_pin, HIGH);  
  69. digitalWrite(orange_pin, LOW);  
  70. digitalWrite(green_pin, LOW);  
  71.    
  72. delay(500);
  73.  
  74. digitalWrite(red_pin, LOW);  
  75. digitalWrite(orange_pin, LOW);  
  76. digitalWrite(green_pin, LOW);
  77.  
  78.  
  79.   // Zorgen dat bij aanzetten Arduino de groene lamp standaard brand
  80.   digitalWrite(green_pin, HIGH);
  81.  
  82. // Scary complex intializing of the EtherCard - I don't understand this stuff (yet0    
  83.   ether.begin(sizeof Ethernet::buffer, mymac);  
  84. // Set IP using Static  
  85.   ether.staticSetup(myip, gwip);  
  86. }  
  87.  
  88. int ledState = 0;
  89. int blink = 0;
  90.  
  91. //TOGEVOEGD 28-04
  92. long previousMillis = 0; //houdt de vorige millis bij
  93. long interval = 1000;     //elke 1000 milliseconden willen we ledState veranderen
  94.  
  95. void loop()
  96. {    
  97.     word len = ether.packetReceive();  
  98.     word pos = ether.packetLoop(len);  
  99.     unsigned long currentMillis = millis();
  100.    
  101.     // state=OK ---> Green LED on  
  102.     if(strstr((char *)Ethernet::buffer + pos, "GET /?state=ok") != 0)  
  103.     {  
  104.         Serial.println("Received state=ok command");  
  105.         digitalWrite(red_pin, LOW);  
  106.         digitalWrite(orange_pin, LOW);  
  107.         digitalWrite(green_pin, HIGH);  
  108.         blink = false;
  109.     }    
  110.      
  111.     // state=warning ---> Orange LED on  
  112.     if(strstr((char *)Ethernet::buffer + pos, "GET /?state=warning") != 0)  
  113.     {  
  114.         Serial.println("Received state=warning command");  
  115.         digitalWrite(red_pin, LOW);  
  116.         digitalWrite(orange_pin, HIGH);  
  117.         digitalWrite(green_pin, LOW);  
  118.         blink = false;
  119.     }      
  120.      
  121.     // IF state=critical  
  122.     if(strstr((char *)Ethernet::buffer + pos, "GET /?state=critical") != 0)  
  123.     {  
  124.         Serial.println("Received state=critical command");  
  125.        
  126.         digitalWrite(red_pin, LOW);  
  127.         digitalWrite(orange_pin, LOW);  
  128.         digitalWrite(green_pin, LOW);  
  129.         blink = true;
  130.     }
  131.      
  132.     ////TOGEVOEGD 28-04
  133.     if(blink)
  134.     {  
  135.         //1000 milliseconden voorbij? Ja -> verander ledState en schrijf dit naar de LED
  136.         if(currentMillis - previousMillis > interval)
  137.         {
  138.             previousMillis = currentMillis;  
  139.            
  140.             //Vorige keer LOW geschreven? Dan schrijven we nu HIGH en andersom.
  141.             if (ledState == LOW)
  142.             {
  143.                 ledState = HIGH;
  144.             }
  145.             else
  146.             {
  147.                 ledState = LOW;
  148.             }
  149.             digitalWrite(red_pin, ledState);
  150.         }                
  151.     }    
  152.  
  153.     //Return a page so the request is completed.  
  154.     memcpy_P(ether.tcpOffset(), page, sizeof page);  
  155.     ether.httpServerReply(sizeof page - 1);  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement