You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.1 KiB

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