Commit 2e965c6a26e402030783f95cbf9f95e264b725e1

Authored by Wisnu
0 parents

Add new file

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