WebServer.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Web Server
  3. A simple web server that shows the value of the analog input pins.
  4. using an Arduino Wiznet Ethernet shield.
  5. Circuit:
  6. * Ethernet shield attached to pins 10, 11, 12, 13
  7. * Analog inputs attached to pins A0 through A5 (optional)
  8. created 18 Dec 2009
  9. by David A. Mellis
  10. modified 9 Apr 2012
  11. by Tom Igoe
  12. */
  13. #include <SPI.h>
  14. #include <Ethernet2.h>
  15. // Enter a MAC address and IP address for your controller below.
  16. // The IP address will be dependent on your local network:
  17. byte mac[] = {
  18. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  19. };
  20. IPAddress ip(192, 168, 1, 177);
  21. // Initialize the Ethernet server library
  22. // with the IP address and port you want to use
  23. // (port 80 is default for HTTP):
  24. EthernetServer server(80);
  25. void setup() {
  26. // Open serial communications and wait for port to open:
  27. Serial.begin(9600);
  28. while (!Serial) {
  29. ; // wait for serial port to connect. Needed for Leonardo only
  30. }
  31. // start the Ethernet connection and the server:
  32. Ethernet.begin(mac, ip);
  33. server.begin();
  34. Serial.print("server is at ");
  35. Serial.println(Ethernet.localIP());
  36. }
  37. void loop() {
  38. // listen for incoming clients
  39. EthernetClient client = server.available();
  40. if (client) {
  41. Serial.println("new client");
  42. // an http request ends with a blank line
  43. boolean currentLineIsBlank = true;
  44. while (client.connected()) {
  45. if (client.available()) {
  46. char c = client.read();
  47. Serial.write(c);
  48. // if you've gotten to the end of the line (received a newline
  49. // character) and the line is blank, the http request has ended,
  50. // so you can send a reply
  51. if (c == '\n' && currentLineIsBlank) {
  52. // send a standard http response header
  53. client.println("HTTP/1.1 200 OK");
  54. client.println("Content-Type: text/html");
  55. client.println("Connection: close"); // the connection will be closed after completion of the response
  56. client.println("Refresh: 5"); // refresh the page automatically every 5 sec
  57. client.println();
  58. client.println("<!DOCTYPE HTML>");
  59. client.println("<html>");
  60. // output the value of each analog input pin
  61. for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
  62. int sensorReading = analogRead(analogChannel);
  63. client.print("analog input ");
  64. client.print(analogChannel);
  65. client.print(" is ");
  66. client.print(sensorReading);
  67. client.println("<br />");
  68. }
  69. client.println("</html>");
  70. break;
  71. }
  72. if (c == '\n') {
  73. // you're starting a new line
  74. currentLineIsBlank = true;
  75. }
  76. else if (c != '\r') {
  77. // you've gotten a character on the current line
  78. currentLineIsBlank = false;
  79. }
  80. }
  81. }
  82. // give the web browser time to receive the data
  83. delay(1);
  84. // close the connection:
  85. client.stop();
  86. Serial.println("client disconnected");
  87. }
  88. }