Advertisement
diegodcpbr

Arduino Shield 2

Feb 17th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.69 KB | None | 0 0
  1. /*
  2. ---------------------------------------------------------
  3. -             Web control relay with Arduino            -
  4. ---------------------------------------------------------
  5. Ethernet module "ENC28J60" is connected to the Arduino :
  6. VCC - 3.3V   Arduino
  7. GND - GND    Arduino
  8. SCK - Pin 13 Arduino
  9. SO  - Pin 12 Arduino
  10. SI  - Pin 11 Arduino
  11. CS  - Pin 10 Arduino
  12.  
  13. The relay is connected to the Arduino so:
  14. VCC - 5V     Arduino
  15. In1 - Pin 2  Arduino
  16. In2 - Pin 3  Arduino
  17. ...
  18. In7 - Pin 8  Arduino
  19. In8 - Pin 9  Arduino
  20. GND - GND    Arduino
  21. ---------------------------------------------------------
  22. */
  23.  
  24. #include <EtherCard.h>
  25.  
  26. // Define number of relays (MAX 8)
  27. #define NB_PINS 4
  28.  
  29. // ethernet interface mac address, must be unique on the LAN
  30. static byte mymac[] = {0x00, 0x19, 0xCB, 0xF4, 0x03, 0x01};
  31. static byte myip[] = {192, 168, 0, 26};
  32. static byte gwip[] = {192, 168, 0, 1};
  33. static byte netmask[] = {255, 255, 255, 0};
  34. static byte dnsip[] = {181, 213, 132, 2,};//181.213.132.2
  35.  
  36. // Declare PinStatus and LedPins before BufferFiller declaration
  37. boolean PinStatus[NB_PINS];
  38.  
  39. int LedPins[NB_PINS];
  40.  
  41. // buffer[500] for 4 relays
  42. // increase size buffer for more relays
  43. byte Ethernet::buffer[500];
  44. BufferFiller bfill;
  45.  
  46. const char http_OK[] PROGMEM =
  47. "HTTP/1.0 200 OK\r\n"
  48. "Content-Type: text/html\r\n"
  49. "Pragma: no-cache\r\n\r\n";
  50.  
  51. const char http_Found[] PROGMEM =
  52. "HTTP/1.0 302 Found\r\n"
  53. "Location: /\r\n\r\n";
  54.  
  55. const char http_Unauthorized[] PROGMEM =
  56. "HTTP/1.0 401 Unauthorized\r\n"
  57. "Content-Type: text/html\r\n\r\n"
  58. "<h1>401 Unauthorized</h1>";
  59.  
  60. //---------------------------------------------------------
  61. // Web page
  62. void homePage()
  63. {
  64.     bfill.emit_p(PSTR("$F"
  65.     "<title>ArduinoPIN Webserver</title>"
  66.     "<p align=center><font size=10>"
  67.     ),
  68.     http_OK);
  69.     for(int i = 0; i <= NB_PINS-1; i++)
  70.     {
  71.         bfill.emit_p(PSTR(
  72.         "R$D: <a href='?P$D=$F' style=color:$F>$F</a><br />"
  73.         ),
  74.         i+1,
  75.         i+1,
  76.         PinStatus[i]?PSTR("off"):PSTR("on"),
  77.         PinStatus[i]?PSTR("green"):PSTR("red"),
  78.         PinStatus[i]?PSTR("ON"):PSTR("OFF"));
  79.     }
  80.     bfill.emit_p(PSTR(
  81.     "</font></p>"
  82.     ));  
  83. }
  84.  
  85. //---------------------------------------------------------
  86. void setup()
  87. {
  88.     Serial.begin(9600);
  89.     Serial.print("Setup ");
  90.     for(int i = 0; i <= NB_PINS-1; i++)
  91.     {
  92.         // Pins on Arduino begin from PIN 2 to PIN 9
  93.         LedPins[i] = i+2;
  94.     }
  95.  
  96.     // if (ether.begin(sizeof Ethernet::buffer, mymac) == 0).
  97.     // and change it (CS-pin) to pin 10.
  98.     Serial.println(F("\nConnecting..."));
  99.     if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0);
  100.         Serial.println(F("Failed to access Ethernet controller"));
  101.     if (!ether.dhcpSetup());
  102.         Serial.println(F("DHCP failed"));
  103.     Serial.print("Connected\n");
  104.  
  105.     ether.printIp("My Router IP: ", ether.myip);
  106.  
  107.     //ether.staticSetup(myip);
  108.  
  109.     ether.printIp("My SET IP: ", ether.myip);
  110.  
  111.     // Debug
  112.     Serial.print("\n");
  113.     Serial.print("|-------------------------------------------|\n");
  114.     Serial.print("| Actions | LedPin       /        PinStatus |\n");
  115.     Serial.print("|-------------------------------------------|\n");
  116.     Serial.print("| Init    | ");
  117.  
  118.     for(int i = 0; i <= NB_PINS-1; i++)
  119.     {
  120.         // Initialization of pin mode and pin status
  121.         pinMode(LedPins[i],OUTPUT);
  122.         digitalWrite (LedPins[i],HIGH);
  123.         PinStatus[i]=false;
  124.         // Debug
  125.         Serial.print(LedPins[i]);
  126.         Serial.print("/");
  127.         Serial.print(PinStatus[i]);
  128.         Serial.print(" ");
  129.     }
  130.     Serial.print("\n");
  131. }
  132.  
  133. //---------------------------------------------------------
  134. void loop()
  135. {
  136.  
  137.     delay(1);
  138.  
  139.     word len = ether.packetReceive();   // check for ethernet packet
  140.     word pos = ether.packetLoop(len);   // check for tcp packet
  141.  
  142.     if (pos) {
  143.         bfill = ether.tcpOffset();
  144.         char *data = (char *) Ethernet::buffer + pos;
  145.         if (strncmp("GET /", data, 5) != 0) {
  146.             bfill.emit_p(http_Unauthorized);
  147.         }
  148.         else {
  149.             data += 5;
  150.             int relay=data[2] - 48; // ASCII code for the character '0' is decimal 48
  151.             if (data[0] == ' ') {
  152.                 // If change on home page, execute function.
  153.                 homePage();
  154.                 //Debug
  155.                 for (int i = 0; i <= NB_PINS-1; i++)digitalWrite(LedPins[i],!PinStatus[i]);
  156.                     Serial.print("| F5      | ");
  157.             } else if( relay>0 && relay<=NB_PINS ) {
  158.                 data += 4;
  159.                 if (strncmp("on ", data, 2) == 0) {
  160.                     PinStatus[relay-1] = true;        
  161.                     bfill.emit_p(http_Found);
  162.                     // Debug
  163.                     Serial.print("| R");
  164.                     Serial.print(relay-1);
  165.                     Serial.print(" ON   | ");
  166.                 } else if (strncmp("off ", data, 3) == 0) {
  167.                     PinStatus[relay-1] = false;
  168.                     bfill.emit_p(http_Found);
  169.                     // Debug
  170.                     Serial.print("| R");
  171.                     Serial.print(relay-1);
  172.                     Serial.print(" OFF  | ");
  173.                 }
  174.             }
  175.             else {
  176.                 // Page not found
  177.                 bfill.emit_p(http_Unauthorized);
  178.             }
  179.             // Debug
  180.             for(int i = 0; i <= NB_PINS-1; i++)
  181.             {
  182.                 Serial.print(LedPins[i]);
  183.                 Serial.print("/");
  184.                 Serial.print(PinStatus[i]);
  185.                 Serial.print(" ");
  186.             }
  187.             Serial.print("\n");
  188.         }
  189.         ether.httpServerReply(bfill.position());    // send http response
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement