Advertisement
diegodcpbr

Arduino Shield

Feb 13th, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.37 KB | None | 0 0
  1. /*
  2.  -----------------------
  3.  Подключаємо піни "ENC28J60 Module" до Arduino Uno.
  4.  VCC -   3.3V
  5.  GND -    GND
  6.  SCK - Pin 13
  7.  SO  - Pin 12
  8.  SI  - Pin 11
  9.  CS  - Pin 10 Можна вибрати будь-який.
  10.   ------------------
  11.  */
  12.  
  13. #include <EtherCard.h>  // Подключаєм скачану бібліотеку. https://yadi.sk/d/R57sVoglbhTRN
  14. //#define STATIC 1
  15. // MAC Address має бути унікальним у вашій мережі. Можна змінити.
  16. // Ethernet interface ip address
  17. static byte mymac[] = { 0x5A,0x5A,0x5A,0x5A,0x5A,0x5A };
  18. static byte myip[] = { 192,168,0,50 };
  19. // Gateway ip address
  20. static byte gwip[] = { 192,168,0,1 };
  21. // DNS ip address
  22. static byte dnsip[] = { 8,8,8,8 };
  23. // Subnet mask
  24. static byte mask[] = { 255,255,255,0 };
  25.  
  26. // Буфер, чим більше даних на Web сторінці, тим більше потрібне значення буфера.
  27. byte Ethernet::buffer[900];
  28. BufferFiller bfill;
  29.  
  30. // Масив задіяних номерів пінів Arduino, для керування 4 реле.
  31. int LedPins[] = {
  32.   2,3,4,5};
  33.  
  34. // Масив для фіксації змін.
  35. boolean PinStatus[] = {
  36.   1,2,3,4};
  37.  
  38. //-------------
  39.  
  40. const char http_OK[] PROGMEM =
  41. "HTTP/1.0 200 OK\r\n"
  42. "Content-Type: text/html\r\n"
  43. "Pragma: no-cache\r\n\r\n";
  44. //"<meta http-equiv=\"refresh\" content=\"5;">;
  45.  
  46. const char http_Found[] PROGMEM =
  47. "HTTP/1.0 302 Found\r\n"
  48. "Location: /\r\n\r\n";
  49.  
  50. const char http_Unauthorized[] PROGMEM =
  51. "HTTP/1.0 401 Unauthorized\r\n"
  52. "Content-Type: text/html\r\n\r\n"
  53. "<h1>401 Unauthorized</h1>";
  54.  
  55. //------------
  56.  
  57. // Робимо функцію для оформлення нашої Web сторінки.
  58. void homePage()
  59. {
  60.   bfill.emit_p(PSTR("$F"
  61.     "<title>ArduinoPIN Webserver</title>"
  62.     "Relay 1: <a href=\"?ArduinoPIN1=$F\">$F</a><br />"
  63.     "Relay 2: <a href=\"?ArduinoPIN2=$F\">$F</a><br />"  
  64.     "Relay 3: <a href=\"?ArduinoPIN3=$F\">$F</a><br />"
  65.     "Relay 4: <a href=\"?ArduinoPIN4=$F\">$F</a>"),  
  66.  
  67.   http_OK,
  68.   PinStatus[1]?PSTR("off"):PSTR("on"),
  69.   PinStatus[1]?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"),
  70.   PinStatus[2]?PSTR("off"):PSTR("on"),
  71.   PinStatus[2]?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"),
  72.   PinStatus[3]?PSTR("off"):PSTR("on"),
  73.   PinStatus[3]?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"),
  74.   PinStatus[4]?PSTR("off"):PSTR("on"),
  75.   PinStatus[4]?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"));
  76. }
  77.  
  78. //------------------------
  79.  
  80.  
  81.  
  82. void setup()
  83. {
  84.   Serial.begin(9600);
  85.  
  86.   // По замовчуванню в бібліотеці "ethercard" (CS-pin) = № 8.
  87.   // if (ether.begin(sizeof Ethernet::buffer, mymac) == 0).
  88.   // and change it to: Змінюємо (CS-pin) на 10.
  89.   // if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0).
  90.  
  91.   if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0);
  92.  
  93.   if (!ether.dhcpSetup());
  94.  
  95.   // Виводимо в Serial монітор IP адресу, яку нам автоматично присвоїв наш Router.
  96.   // Динамічна IP адреса незручна, бо періодично змінюється.
  97.   // Нам доведеться часто дізнаватися, яка адреса нашої Web сторінки.
  98.   ether.printIp("My Router IP: ", ether.myip); // Виводимо в Serial монітор IP адресу, яку нам присвоїв Router.
  99.  
  100.   //Також ми можемо встановити статичну IP адресу нашої сторінки і звертатися до Arduino по ній
  101.  
  102.     //ether.staticSetup(myip);
  103.  
  104.   ether.printIp("My SET IP: ", ether.myip); // Виводимо в Serial монітор статичну IP адресу.
  105.   //-----
  106.  
  107.   for(int i = 0; i <= 4; i++)
  108.   {
  109.     pinMode(LedPins[i],OUTPUT);
  110.     digitalWrite (LedPins[i],HIGH);
  111.     PinStatus[i]=false;
  112.   }  
  113. }
  114.  
  115. // --------------------------------------
  116.  
  117. void loop()
  118. {
  119.  
  120.  
  121.   delay(1); // Смикаєм мікроконтролер.
  122.  
  123.   word len = ether.packetReceive();   // check for ethernet packet / перевірити ethernet пакети.
  124.   word pos = ether.packetLoop(len);   // check for tcp packet / перевірити TCP пакети.
  125.  
  126.   if (pos) {
  127.     bfill = ether.tcpOffset();
  128.     char *data = (char *) Ethernet::buffer + pos;
  129.     if (strncmp("GET /", data, 5) != 0) {
  130.       bfill.emit_p(http_Unauthorized);
  131.     }
  132.    
  133.    
  134.     else {
  135.  
  136.       data += 5;
  137.       if (data[0] == ' ') {      
  138.         homePage(); // Return home page Якщо виявлено зміни на сторінці, запускаємо функцію.
  139.         for (int i = 0; i <= 3; i++)digitalWrite(LedPins[i],!PinStatus[i+1]);
  140.       }
  141.  
  142.       // "16" = кількість символів "?ArduinoPIN1=on ".
  143.       else if (strncmp("?ArduinoPIN1=on ", data, 16) == 0) {
  144.         PinStatus[1] = true;        
  145.         bfill.emit_p(http_Found);
  146.       }
  147.       else if (strncmp("?ArduinoPIN2=on ", data, 16) == 0) {
  148.         PinStatus[2] = true;        
  149.         bfill.emit_p(http_Found);
  150.       }
  151.       else if (strncmp("?ArduinoPIN3=on ", data, 16) == 0) {
  152.         PinStatus[3] = true;        
  153.         bfill.emit_p(http_Found);
  154.       }
  155.       else if (strncmp("?ArduinoPIN4=on ", data, 16) == 0) {
  156.         PinStatus[4] = true;
  157.         bfill.emit_p(http_Found);
  158.              
  159.       }
  160.  
  161.  
  162.       //------------------------------------------------------  
  163.  
  164.  
  165.       else if (strncmp("?ArduinoPIN1=off ", data, 17) == 0) {
  166.         PinStatus[1] = false;        
  167.         bfill.emit_p(http_Found);
  168.       }
  169.       else if (strncmp("?ArduinoPIN2=off ", data, 17) == 0) {
  170.         PinStatus[2] = false;        
  171.         bfill.emit_p(http_Found);
  172.       }
  173.       else if (strncmp("?ArduinoPIN3=off ", data, 17) == 0) {
  174.         PinStatus[3] = false;        
  175.         bfill.emit_p(http_Found);
  176.       }
  177.       else if (strncmp("?ArduinoPIN4=off ", data, 17) == 0) {
  178.         PinStatus[4] = false;
  179.         bfill.emit_p(http_Found);
  180.  
  181.          
  182.       }
  183.      
  184.  
  185.  
  186.       //---------------------------
  187.  
  188.  
  189.       else {
  190.         // Page not found
  191.         bfill.emit_p(http_Unauthorized);
  192.       }
  193.     }
  194.     ether.httpServerReply(bfill.position());    // send http response
  195.   }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement