Advertisement
Guest User

Untitled

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