Guest User

Untitled

a guest
Nov 15th, 2014
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <SD.h>
  4. #include <TextFinder.h>
  5.  
  6. byte MACAdress[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //MAC Adresse
  7. byte IPAdress[] = {10, 0, 12, 12}; //IP Adresse
  8. byte sdPin = 4; //Pin der SD-Karte
  9. int const HTTPPORT = 80;
  10.  
  11. EthernetServer myServer(HTTPPORT); // Webserver auf angegebenen Port starten
  12.  
  13. File webFile;
  14.  
  15. void setup() {
  16. Ethernet.begin(MACAdress, IPAdress); // Ethernet initialisieren
  17. myServer.begin(); // Server starten
  18. Serial.begin(9600);
  19. Serial.println("ARDUINO - STEUERUNG");
  20. Serial.println("Initialisiere SD-Karte...");
  21. if (!SD.begin(sdPin))
  22. {
  23. Serial.println(" - Initialisierung der SD-Karte fehlgeschlagen!");
  24. return;
  25. }
  26. Serial.println(" - SD-Karte erfolgreich initialisiert.");
  27.  
  28. if (!SD.exists("index.htm"))
  29. {
  30. Serial.println(" - Datei (index.htm) wurde nicht gefunden!");
  31. return;
  32. }
  33. Serial.println(" - Datei (index.htm) wurde gefunden.");
  34.  
  35. Serial.println();
  36. Serial.println("Verbraucher schalten");
  37. }
  38.  
  39. void SendOKpage(EthernetClient &client)
  40. {
  41. boolean currentLineIsBlank = true;
  42. char c = client.read();
  43. client.println("HTTP/1.1 200 OK");
  44. client.println("Content-Type: text/html");
  45. client.println("Connection: close");
  46. client.println();
  47.  
  48. webFile = SD.open("index.htm"); //Webseite laden
  49. if (webFile)
  50. {
  51. while(webFile.available())
  52. {
  53. client.write(webFile.read()); //Webseite an Client schicken
  54. }
  55. webFile.close();
  56. }
  57. if (c == '\n')
  58. {
  59. currentLineIsBlank = true;
  60. }
  61. else if (c != '\n')
  62. {
  63. currentLineIsBlank = false;
  64. }
  65. delay(1);
  66. client.stop();
  67. }
  68.  
  69.  
  70. void SendAuthentificationpage(EthernetClient &client)
  71. {
  72. client.println("HTTP/1.1 401 Authorization Required");
  73. client.println("WWW-Authenticate: Basic realm=\"Secure Area\"");
  74. client.println("Content-Type: text/html");
  75. client.println("Connnection: close");
  76. client.println();
  77. client.println("<!DOCTYPE HTML>");
  78. client.println("<HTML> <HEAD> <TITLE>Error</TITLE>");
  79. client.println(" </HEAD> <BODY><H1>401 Unauthorized.</H1></BODY> </HTML>");
  80. }
  81.  
  82.  
  83. char linebuf[80];
  84. int charcount=0;
  85. boolean authentificated=false;
  86.  
  87. void loop() {
  88. EthernetClient client = myServer.available();
  89. if (client) {
  90. Serial.println("new client");
  91. memset(linebuf,0,sizeof(linebuf));
  92. charcount=0;
  93. authentificated=false;
  94. boolean currentLineIsBlank = true;
  95. while (client.connected()) {
  96. if (client.available()) {
  97. char c = client.read();
  98. linebuf[charcount]=c;
  99. if (charcount<sizeof(linebuf)-1) charcount++;
  100. Serial.write(c);
  101. if (c =='\n' && currentLineIsBlank) {
  102. if (authentificated)
  103. SendOKpage(client);
  104. else
  105. SendAuthentificationpage(client);
  106. break;
  107. }
  108. if (c == '\n') {
  109. // you're starting a new line
  110. currentLineIsBlank = true;
  111. if (strstr(linebuf,"Authorization: Basic")>0 && strstr(linebuf,"cbGxhOmdlcg==")>0)
  112. authentificated=true;
  113. memset(linebuf,0,sizeof(linebuf));
  114. charcount=0;
  115. }
  116. else if (c != '\r') {
  117. // you've gotten a character on the current line
  118. currentLineIsBlank = false;
  119. }
  120. }
  121. }
  122. delay(1);
  123. client.stop();
  124. Serial.println("client disconnected");
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment