bambangSud

Test ethernet

Aug 12th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1.  
  2.  
  3. #include <SPI.h>
  4. #include <Ethernet.h>
  5.  
  6. // Enter a MAC address and IP address for your controller below.
  7. // The IP address will be dependent on your local network:
  8. byte mac[] = {
  9. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  10. IPAddress ip(169,254,170,137);
  11.  
  12. // Initialize the Ethernet server library
  13. // with the IP address and port you want to use
  14. // (port 80 is default for HTTP):
  15. EthernetServer server(80);
  16.  
  17. void setup() {
  18. // Open serial communications and wait for port to open:
  19. Serial.begin(9600);
  20. while (!Serial) {
  21. ; // wait for serial port to connect. Needed for Leonardo only
  22. }
  23.  
  24.  
  25. // start the Ethernet connection and the server:
  26. Ethernet.begin(mac, ip);
  27. server.begin();
  28. Serial.print("server is at ");
  29. Serial.println(Ethernet.localIP());
  30. }
  31.  
  32.  
  33. void loop() {
  34. // listen for incoming clients
  35. EthernetClient client = server.available();
  36. if (client) {
  37. Serial.println("new client");
  38. // an http request ends with a blank line
  39. boolean currentLineIsBlank = true;
  40. while (client.connected()) {
  41. if (client.available()) {
  42. char c = client.read();
  43. Serial.write(c);
  44. // if you've gotten to the end of the line (received a newline
  45. // character) and the line is blank, the http request has ended,
  46. // so you can send a reply
  47. if (c == '\n' && currentLineIsBlank) {
  48. // send a standard http response header
  49. client.println("HTTP/1.1 200 OK");
  50. client.println("Content-Type: text/html");
  51. client.println("Connection: close"); // the connection will be closed after completion of the response
  52. client.println("Refresh: 5"); // refresh the page automatically every 5 sec
  53. client.println();
  54. client.println("<!DOCTYPE HTML>");
  55. client.println("<html>");
  56. // output the value of each analog input pin
  57. for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
  58. int sensorReading = analogRead(analogChannel);
  59. client.print("analog input ");
  60. client.print(analogChannel);
  61. client.print(" is ");
  62. client.print(sensorReading);
  63. client.println("<br />");
  64. }
  65. client.println("</html>");
  66. break;
  67. }
  68. if (c == '\n') {
  69. // you're starting a new line
  70. currentLineIsBlank = true;
  71. }
  72. else if (c != '\r') {
  73. // you've gotten a character on the current line
  74. currentLineIsBlank = false;
  75. }
  76. }
  77. }
  78. // give the web browser time to receive the data
  79. delay(1);
  80. // close the connection:
  81. client.stop();
  82. Serial.println("client disonnected");
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment