Advertisement
diegodcpbr

Arduino Shield 1

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