Advertisement
frankiepankie

Arduino stoplicht

Jun 21st, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.38 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,4,8 };  
  7. // gateway ip address  
  8. static byte gwip[] = { 192,168,4,1 };  
  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.4.8"  
  32.   "</title></head>"  
  33.   "<body>"  
  34.    "<html>"
  35. " <head>"
  36. "<h3>Arduino stoplicht</h3>"
  37. "<a href=\"http://192.168.4.8/?state=blinkred\">ROOD (knipperen)</a></td>"
  38. "<br>"
  39. "<a href=\"http://192.168.4.8/?state=critical\">ROOD</a></td>"
  40. "<br>"
  41. "<a href=\"http://192.168.4.8/?state=warning\">ORANJE</a></td>"
  42. "<br>"
  43. "<a href=\"http://192.168.4.8/?state=ok\">GROEN</a></td>"
  44. "</body>"
  45. "</html>"
  46.  
  47.   "</body>"
  48.   "</html>";
  49.  
  50. void setup(){  
  51.   Serial.begin(9600);      // open the serial port at 9600 bps:  
  52.  
  53.    
  54. // Set Pin2 to be an Output  
  55.   pinMode(red_pin, OUTPUT);  
  56. // Set Pin4 to be an Output  
  57.   pinMode(orange_pin, OUTPUT);  
  58.   // Set Pin6 to be an Output  
  59.   pinMode(green_pin, OUTPUT);  
  60.  
  61. //test om bij opstarten alle lampen even te laten branden
  62. digitalWrite(red_pin, LOW);  
  63. digitalWrite(orange_pin, LOW);  
  64. digitalWrite(green_pin, LOW);  
  65.  
  66. delay(500);
  67.  
  68. digitalWrite(red_pin, LOW);  
  69. digitalWrite(orange_pin, LOW);  
  70. digitalWrite(green_pin, HIGH);  
  71.  
  72. delay(500);
  73.  
  74. digitalWrite(red_pin, LOW);  
  75. digitalWrite(orange_pin, HIGH);  
  76. digitalWrite(green_pin, LOW);  
  77.  
  78. delay(500);
  79.  
  80. digitalWrite(red_pin, HIGH);  
  81. digitalWrite(orange_pin, LOW);  
  82. digitalWrite(green_pin, LOW);  
  83.    
  84. delay(500);
  85.  
  86. digitalWrite(red_pin, LOW);  
  87. digitalWrite(orange_pin, LOW);  
  88. digitalWrite(green_pin, LOW);
  89.  
  90.  
  91.   // Zorgen dat bij aanzetten Arduino de groene lamp standaard brand
  92.   digitalWrite(green_pin, HIGH);
  93.  
  94. // Scary complex intializing of the EtherCard - I don't understand this stuff (yet0    
  95.   ether.begin(sizeof Ethernet::buffer, mymac);  
  96. // Set IP using Static  
  97.   ether.staticSetup(myip, gwip);  
  98. ether.printIp("IP-adres:  ", ether.myip);
  99.   ether.printIp("Gateway:  ", ether.gwip);  
  100. }  
  101.  
  102.  
  103. int ledState = 0;
  104. int blink = 0;
  105.  
  106. //TOGEVOEGD 28-04
  107. long previousMillis = 0; //houdt de vorige millis bij
  108. long interval = 1000;     //elke 1000 milliseconden willen we ledState veranderen
  109.  
  110. void loop()
  111. {    
  112.     word len = ether.packetReceive();  
  113.     word pos = ether.packetLoop(len);  
  114.     unsigned long currentMillis = millis();
  115.    
  116.     // state=OK ---> Green LED on  
  117.     if(strstr((char *)Ethernet::buffer + pos, "GET /?state=ok") != 0)  
  118.     {  
  119.         Serial.println("Received state=ok command");
  120.        Serial.println("Groene lamp staat nu: AAN");
  121.       Serial.println("");
  122.         digitalWrite(red_pin, LOW);  
  123.         digitalWrite(orange_pin, LOW);  
  124.         digitalWrite(green_pin, HIGH);  
  125.         blink = false;
  126.     }    
  127.      
  128.     // state=warning ---> Orange LED on  
  129.     if(strstr((char *)Ethernet::buffer + pos, "GET /?state=warning") != 0)  
  130.     {  
  131.         Serial.println("Received state=warning command");
  132.        Serial.println("Oranje lamp staat nu: AAN");
  133.       Serial.println("");
  134.         digitalWrite(red_pin, LOW);  
  135.         digitalWrite(orange_pin, HIGH);  
  136.         digitalWrite(green_pin, LOW);  
  137.         blink = false;
  138.     }      
  139.      
  140.      
  141.      // state=critical ---> RED LED on  
  142.     if(strstr((char *)Ethernet::buffer + pos, "GET /?state=critical") != 0)  
  143.     {  
  144.         Serial.println("Received state=critical command");
  145.         Serial.println("Rode lamp staat nu: AAN");
  146.        Serial.println("");
  147.         digitalWrite(red_pin, HIGH);  
  148.         digitalWrite(orange_pin, LOW);  
  149.         digitalWrite(green_pin, LOW);  
  150.         blink = false;
  151.     }
  152.    
  153.     // IF state=blinkred  ----> RED LED blinking
  154.     if(strstr((char *)Ethernet::buffer + pos, "GET /?state=blinkred") != 0)  
  155.     {  
  156.         Serial.println("Received state=blinkred command");
  157.        Serial.println("Rode lamp KNIPPERT nu");
  158.        Serial.println("");
  159.         digitalWrite(red_pin, LOW);  
  160.         digitalWrite(orange_pin, LOW);  
  161.         digitalWrite(green_pin, LOW);  
  162.         blink = true;
  163.     }
  164.      
  165.     ////TOGEVOEGD 28-04
  166.     if(blink)
  167.     {  
  168.         //1000 milliseconden voorbij? Ja -> verander ledState en schrijf dit naar de LED
  169.         if(currentMillis - previousMillis > interval)
  170.         {
  171.             previousMillis = currentMillis;  
  172.            
  173.             //Vorige keer LOW geschreven? Dan schrijven we nu HIGH en andersom.
  174.             if (ledState == LOW)
  175.             {
  176.                 ledState = HIGH;
  177.             }
  178.             else
  179.             {
  180.                 ledState = LOW;
  181.             }
  182.             digitalWrite(red_pin, ledState);
  183.         }                
  184.     }    
  185.  
  186.     //Return a page so the request is completed.  
  187.     memcpy_P(ether.tcpOffset(), page, sizeof page);  
  188.     ether.httpServerReply(sizeof page - 1);  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement