Guest User

Untitled

a guest
Mar 29th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.91 KB | None | 0 0
  1. #include <EtherCard.h>
  2.  
  3. static byte mymac[] = { 0xD0,0x37,0x72,0x9B,0x3F,0x9E  };
  4. static byte myip[] = { 192,168,0,190 };
  5.  
  6. #define BUFFER_SIZE   500
  7. byte Ethernet::buffer[BUFFER_SIZE];
  8. BufferFiller bfill;
  9.  
  10. #define CS_PIN       10
  11.  
  12. #define RELAIS_1     2
  13. #define RELAIS_2     3
  14. #define RELAIS_3     4
  15. bool relais1Status = false;
  16. bool relais2Status = false;
  17. bool relais3Status = false;
  18.  
  19. const char http_OK[] PROGMEM =
  20. "HTTP/1.0 200 OK\r\n"
  21. "Content-Type: text/html\r\n"
  22. "Pragma: no-cache\r\n\r\n";
  23.  
  24. const char http_Found[] PROGMEM =
  25. "HTTP/1.0 302 Found\r\n"
  26. "Location: /\r\n\r\n";
  27.  
  28. const char http_Unauthorized[] PROGMEM =
  29. "HTTP/1.0 401 Unauthorized\r\n"
  30. "Content-Type: text/html\r\n\r\n"
  31. "<h1>401 Unauthorized</h1>";
  32.  
  33. void homePage()
  34. {
  35.   bfill.emit_p(PSTR("$F"
  36.   "<META HTTP-EQUIV=\"refresh\" CONTENT=\"25\">"
  37.     "<title>Arduino Relais Webserver 2</title>"
  38.     "<h1><center><br /><br /><a href=\"?relais1=$F\">$F</a>"
  39.     "<br /><br /><a href=\"?relais2=$F\">$F</a>"
  40.     "<br /><br /><a href=\"?relais3=$F\">$F</a>"
  41.  
  42.     "</center></h1>"
  43.     ),
  44.   http_OK,
  45.   relais1Status?PSTR("off"):PSTR("on"),
  46.   relais1Status?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"),
  47.   relais2Status?PSTR("off"):PSTR("on"),
  48.   relais2Status?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"),
  49.   relais3Status?PSTR("off"):PSTR("on"),
  50.   relais3Status?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>")
  51.  
  52.   );
  53. }
  54.  
  55. void setup()
  56. {
  57.   Serial.begin(115200);  
  58.  
  59.   pinMode(RELAIS_1, OUTPUT);
  60.   pinMode(RELAIS_2, OUTPUT);
  61.   pinMode(RELAIS_3, OUTPUT);
  62.  
  63.   if (ether.begin(BUFFER_SIZE, mymac, CS_PIN) == 0)
  64.     Serial.println("Cannot initialise ethernet.");
  65.   else
  66.     Serial.println("Ethernet initialised.");
  67.  
  68.   ether.staticSetup(myip);
  69.    
  70.   Serial.println("Setting up DHCP");
  71.   if (!ether.dhcpSetup())
  72.     Serial.println( "DHCP failed");
  73.  
  74.   ether.printIp("My IP: ", ether.myip);
  75.   ether.printIp("Netmask: ", ether.netmask);
  76.   ether.printIp("GW IP: ", ether.gwip);
  77.   ether.printIp("DNS IP: ", ether.dnsip);
  78.  
  79. }
  80.  
  81. void loop()
  82. {
  83.   digitalWrite(RELAIS_1, relais1Status);
  84.   digitalWrite(RELAIS_2, relais2Status);
  85.   digitalWrite(RELAIS_3, relais2Status);
  86.  
  87.   delay(1);   // necessary for my system
  88.   word len = ether.packetReceive();   // check for ethernet packet
  89.   word pos = ether.packetLoop(len);   // check for tcp packet
  90.  
  91.   if (pos) {
  92.     bfill = ether.tcpOffset();
  93.     char *data = (char *) Ethernet::buffer + pos;
  94.     if (strncmp("GET /", data, 5) != 0) {
  95.       // Unsupported HTTP request
  96.       // 304 or 501 response would be more appropriate
  97.       bfill.emit_p(http_Unauthorized);
  98.     }
  99.     else {
  100.       Serial.print("----");
  101.       Serial.print(data);
  102.       Serial.println("----");
  103.       data += 5;
  104.  
  105.       if (data[0] == ' ') {
  106.         // Return home page
  107.         homePage();
  108.       }
  109.       else if (strncmp("?relais1=on ", data, 12) == 0) {
  110.         relais1Status = true;        
  111.         bfill.emit_p(http_Found);
  112.       }
  113.       else if (strncmp("?relais2=on ", data, 12) == 0) {
  114.         relais2Status = true;        
  115.         bfill.emit_p(http_Found);
  116.       }
  117.       else if (strncmp("?relais3=on ", data, 12) == 0) {
  118.         relais3Status = true;        
  119.         bfill.emit_p(http_Found);
  120.       }
  121.       else if (strncmp("?relais1=off ", data, 13) == 0) {
  122.         relais1Status = false;        
  123.         bfill.emit_p(http_Found);
  124.       }
  125.       else if (strncmp("?relais2=off ", data, 13) == 0) {
  126.         relais2Status = false;        
  127.         bfill.emit_p(http_Found);
  128.       }
  129.       else if (strncmp("?relais3=off ", data, 13) == 0) {
  130.         relais3Status = false;        
  131.         bfill.emit_p(http_Found);
  132.       }
  133.       else {
  134.         // Page not found
  135.         bfill.emit_p(http_Unauthorized);
  136.       }
  137.     }
  138.  
  139.     ether.httpServerReply(bfill.position());    // send http response
  140.   }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment