Advertisement
tuxmartin

Ovladani rele Arduino ENC28J60 Ethernet

Sep 3rd, 2013
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <EtherCard.h>
  2. /* https://github.com/jcw/ethercard */
  3.  
  4. /*
  5. http://www.lucadentella.it/2012/08/10/enc28j60-e-arduino-7/
  6. https://github.com/lucadentella/enc28j60_tutorial/tree/master/_7_WebLed
  7. */
  8.  
  9. #define STATIC 0  // set to 1 to disable DHCP (adjust myip/gwip values below)
  10.  
  11. #if STATIC
  12. // ethernet interface ip address
  13. static byte myip[] = { 192,168,1,44 };
  14. // gateway ip address
  15. static byte gwip[] = { 192,168,1,1 };
  16. #endif
  17.  
  18. //static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01};
  19. static byte mymac[] = { 0x00, 0x00, 0x6C, 0x01, 0x02, 0x03 };
  20. byte Ethernet::buffer[700];
  21.  
  22. const int vstup = 7;
  23. const int ledPin = 2;
  24. boolean ledStatus;
  25.  
  26. char* on = "ON";
  27. char* off = "OFF";
  28. char* statusLabel;
  29. char* buttonLabel;
  30.  
  31. void setup () {
  32.  
  33.   Serial.begin(9600);
  34.   Serial.println("WebLed Demo");
  35.  
  36. if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
  37.     Serial.println( "Failed to access Ethernet controller");
  38. #if STATIC
  39.   ether.staticSetup(myip, gwip);
  40. #else
  41.   if (!ether.dhcpSetup())
  42.     Serial.println("DHCP failed");
  43. #endif
  44.  
  45.   ether.printIp("IP:  ", ether.myip);
  46.   ether.printIp("GW:  ", ether.gwip);  
  47.   ether.printIp("DNS: ", ether.dnsip);
  48.  
  49.   Serial.println();
  50.  
  51.   pinMode(vstup, 7);
  52.  
  53.   pinMode(ledPin, OUTPUT);
  54.   digitalWrite(ledPin, LOW);
  55.   ledStatus = false;
  56. }
  57.  
  58. void loop() {
  59.  
  60.   word len = ether.packetReceive();
  61.   word pos = ether.packetLoop(len);
  62.  
  63.   int buttonState = digitalRead(vstup);
  64.   //Serial.println("Input state: " + buttonState);
  65.  
  66.   if(pos) {
  67.    
  68.     if(strstr((char *)Ethernet::buffer + pos, "GET /?status=ON") != 0) {
  69.       Serial.println("Received ON command");
  70.       ledStatus = true;
  71.     }
  72.  
  73.     if(strstr((char *)Ethernet::buffer + pos, "GET /?status=OFF") != 0) {
  74.       Serial.println("Received OFF command");
  75.       ledStatus = false;
  76.     }
  77.    
  78.     if(ledStatus) {
  79.       digitalWrite(ledPin, HIGH);
  80.       statusLabel = on;
  81.       buttonLabel = off;
  82.     } else {
  83.       digitalWrite(ledPin, LOW);
  84.       statusLabel = off;
  85.       buttonLabel = on;
  86.     }
  87.    
  88. Serial.println("Input state: " + buttonState);
  89.     BufferFiller bfill = ether.tcpOffset();
  90.     bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
  91.       "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
  92.       "<html><head><title>WebLed</title></head>"
  93.       "<body style=\"font-size: 200%\">LED Status: $S "
  94.       "<br><a href=\"/?status=$S\"><strong><input style=\"font-size: 200%\" type=\"button\" value=\"$S\"><strong></a>"
  95.       "</body></html>"      
  96.       ), statusLabel, buttonLabel, buttonLabel, buttonState);
  97.     ether.httpServerReply(bfill.position());
  98.   }
  99. }
  100.  
  101. void showString (PGM_P s) {
  102.         char c;
  103.         while ((c = pgm_read_byte(s++)) != 0)
  104.             Serial.print(c);
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement