Friday, November 23, 2012

Simple Ethernet

This is the first in a series of Ethernet projects. This first project will display the values coming from the analog ports in a web browser on a remote machine. Additional posts will cover controlling the Arduino from a web browser, and reading and writing to the SD card for logging purposes. Stay tuned!

I popped a Arduino EtherShield SD on to an older Duemilanove (328), and loaded the following sketch. I changed the MAC address to the one printed on the bottom of my EtherShield, and put in a applicable IP address from my network. When I pointed my browser to the new IP address, I got the following output:

analog input 0 is 1023
analog input 1 is 0
analog input 2 is 1023
analog input 3 is 771
analog input 4 is 551
analog input 5 is 437

This makes sense, because I have A0 and A2 connected to 5v, A1 connected to Gnd, and A3-A5 are left floating, and the values change every second or so.

Sketch from examples folder (Ethernet, Web Server)

  Web Server

 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield.

 * Ethernet shield attached to pins 10, 11, 12, 13
 * Analog inputs attached to pins A0 through A5 (optional)

 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe


#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0x90, 0xA2, 0xDA, 0x00, 0x23, 0x36 };
IPAddress ip(192,168,254, 177);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
 // Open serial communications and wait for port to open:
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  Serial.print("server is at ");

void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c =;
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
                    // add a meta refresh tag, so the browser pulls again every 5 seconds:
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            int sensorReading = analogRead(analogChannel);
            client.print("analog input ");
            client.print(" is ");
");          break;
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
    // give the web browser time to receive the data
    // close the connection:
    Serial.println("client disonnected");

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...