Ethernet shield Coding

Dear all,
I am starting new project using Ethernet. I need step for executing web server application. I have Certain data to be monitored on web server.
I have these many data to be monitored on webserver

  1. date and time
    2)latitude
    3)longitude
    4)Desired angle
    5)actual angle
    etc
    Action to be control:
    Motor forward and reverse
    value to be change:
    1)date and time
    2)angles
  2. limit of wind speed.

Question :

  1. is it possible to do using web server application??
  2. Basic steps for configure web server
    3)How to create our own control tags. see youtube 2

Sheild i am using.
http://arduino.cc/en/Main/ArduinoEthernetShield

Here i have added one video where we can fine any change in analog value also change in web server. How to make it. Is there any software available to create tags.

Testing code

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

// MAC address can be anything that is unique within your network.
// IP is the address the Arduino Ethernet Card would respond to.  It needs to be an unused address within your network.
byte mac[] = {0x00, 0x1E, 0x2A, 0x77, 0x24, 0x02 };
byte ip[] = {192,168,7,12 }; // This is typically 10.0.0.x

Server server(80); // Port 80 is http.

//-- Commands and parameters (sent by browser) --
char cmd[15];    // Nothing magical about 15, increase these if you need longer commands/parameters
char param1[15];
char param2[15];

//-- Sample Ports ---
void SetupSamplePorts()
{
  // To illustrate how to use this, I have an LED and a Potentiometer.
  // The 10K potentiometer left lead is connected to GND, right lead to +5V, and middle lead to Analog 0.
  // The LED cathode is on digital pin 7 and anode is on pin 8.
  pinMode(7,OUTPUT); digitalWrite(7,LOW);  // I use this pin as GND for the LED.
  pinMode(8,OUTPUT); // Sample output, unable to use built-in LED at pin 13 because Ethernet Shield uses pins 10,11,12,13.
}

void setup()
{
  Ethernet.begin(mac, ip);
  server.begin();

  Serial.begin(57600);
  SetupSamplePorts();
}

#define bufferMax 128
int bufferSize;
char buffer[bufferMax];

void loop()
{
  Client client = server.available();
  if (client)
  {
    WaitForRequest(client);
    ParseReceivedRequest();
    PerformRequestedCommands();
    
    client.stop();
  }
}

void WaitForRequest(Client client) // Sets buffer[] and bufferSize
{
  bufferSize = 0;
 
  while (client.connected()) {
    if (client.available()) {
      char c = client.read();
      if (c == '\n')
        break;
      else
        if (bufferSize < bufferMax)
          buffer[bufferSize++] = c;
        else
          break;
    }
  }
  
  PrintNumber("bufferSize", bufferSize);
}

void ParseReceivedRequest()
{
  Serial.println("in ParseReceivedRequest");
  Serial.println(buffer);
  
  //Received buffer contains "GET /cmd/param1/param2 HTTP/1.1".  Break it up.
  char* slash1;
  char* slash2;
  char* slash3;
  char* space2;
  
  slash1 = strstr(buffer, "/") + 1; // Look for first slash
  slash2 = strstr(slash1, "/") + 1; // second slash
  slash3 = strstr(slash2, "/") + 1; // third slash
  space2 = strstr(slash2, " ") + 1; // space after second slash (in case there is no third slash)
  if (slash3 > space2) slash3=slash2;

  PrintString("slash1",slash1);
  PrintString("slash2",slash2);
  PrintString("slash3",slash3);
  PrintString("space2",space2);
  
  // strncpy does not automatically add terminating zero, but strncat does! So start with blank string and concatenate.
  cmd[0] = 0;
  param1[0] = 0;
  param2[0] = 0;
  strncat(cmd, slash1, slash2-slash1-1);
  strncat(param1, slash2, slash3-slash2-1);
  strncat(param2, slash3, space2-slash3-1);
  
  PrintString("cmd",cmd);
  PrintString("param1",param1);
  PrintString("param2",param2);
}

void PerformRequestedCommands()
{
  if ( strcmp(cmd,"digitalWrite") == 0 ) RemoteDigitalWrite();
  if ( strcmp(cmd,"analogRead") == 0 ) RemoteAnalogRead();
}

void RemoteDigitalWrite()
{
  int ledPin = param1[0] - '0'; // Param1 should be one digit port
  int ledState = param2[0] - '0'; // Param2 should be either 1 or 0
  digitalWrite(ledPin, ledState);
 
  //-- Send response back to browser --
  server.print("D");
  server.print(ledPin, DEC);
  server.print(" is ");
  server.print( (ledState==1) ? "ON" : "off" );

  //-- Send debug message to serial port --
  Serial.println("RemoteDigitalWrite");
  PrintNumber("ledPin", ledPin);
  PrintNumber("ledState", ledState);
}

void RemoteAnalogRead()
{
  // If desired, use more server.print() to send http header instead of just sending the analog value.
  int analogPin = param1[0] - '0'; // Param1 should be one digit analog port
  int analogValue = analogRead(analogPin);
  
  //-- Send response back to browser --
  server.print("A");
  server.print(analogPin, DEC);
  server.print(" is ");
  server.print(analogValue,DEC);
  
  //-- Send debug message to serial port --
  Serial.println("RemoteAnalogRead");
  PrintNumber("analogPin", analogPin);
  PrintNumber("analogValue", analogValue);
}

void PrintString(char* label, char* str)
{
  Serial.print(label);
  Serial.print("=");
  Serial.println(str);
}

void PrintNumber(char* label, int number)
{
  Serial.print(label);
  Serial.print("=");
  Serial.println(number, DEC);
}[tt][tt][/tt][/tt]

  1. is it possible to do using web server application??

Most people could.

  1. Basic steps for configure web server

The Arduino doesn't need "configuration". It need programming.

3)How to create our own control tags. see youtube 2

You can't. The "control tags" are standard HTML tags.

I have attached basic Setup i made. I have connected the shield and uploaded example Web server program. I could not able to read anything on ethernet. Is possible to tell what mistake i am doing

I am using LAN connection between PC and Ethernet shield , WIndows7 OS. I am not getting any data.

Try this test code. It uses dhcp to get an ip and the other network stuff. If you don’t understand the results, post them here and I’ll see if I can help you.

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

byte mac[] = {  0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup() {
  Serial.begin(9600);

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else {
      Serial.print(F("IP: "));
      Serial.println(Ethernet.localIP());
      Serial.print(F("Subnet: "));
      Serial.println(Ethernet.subnetMask());
      Serial.print(F("Gateway: "));
      Serial.println(Ethernet.gatewayIP());
      Serial.print(F("DNS server: "));
      Serial.println(Ethernet.dnsServerIP());
}

void loop() {
}

edit: My bad. I forgot the includes. I added them.

Server code that supplies the arduino analog pin output to a web page.

// zoomkat's meta refresh data frame test page 5/25/13
// use http://192.168.1.102:84 in your brouser for main page
// http://192.168.1.102:84/data static data page
// http://192.168.1.102:84/datastart meta refresh data page
// for use with W5100 based ethernet shields
// set the refresh rate to 0 for fastest update
// use STOP for single data updates

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

const int analogInPin0 = A0;
const int analogInPin1 = A1;
const int analogInPin2 = A2;
const int analogInPin3 = A3;
const int analogInPin4 = A4;
const int analogInPin5 = A5;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // arduino ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
unsigned long int x=0; //set refresh counter to 0
String readString; 

//////////////////////

void setup(){
  Serial.begin(9600);
    // disable SD SPI if memory card in the uSD slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
  Serial.println("meta refresh data frame test 5/25/13"); // so I can keep track of what is loaded
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
         if (readString.length() < 100) {
          readString += c; 
         } 
        //check if HTTP request has ended
        if (c == '\n') {

          //check get atring received
          Serial.println(readString);

          //output HTML data header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          //generate data page
          if(readString.indexOf("data") >0) {  //checks for "data" page
            x=x+1; //page upload counter
            client.print("<HTML><HEAD>");
            //meta-refresh page every 1 seconds if "datastart" page
            if(readString.indexOf("datastart") >0) client.print("<meta http-equiv='refresh' content='1'>"); 
            //meta-refresh 0 for fast data
            if(readString.indexOf("datafast") >0) client.print("<meta http-equiv='refresh' content='0'>"); 
            client.print("<title>Zoomkat's meta-refresh test</title></head><BODY>
");
            client.print("page refresh number: ");
            client.print(x); //current refresh count
            client.print("

");
            
              //output the value of each analog input pin
            client.print("analog input0 is: ");
            client.print(analogRead(analogInPin0));
            
            client.print("
analog input1 is: ");
            client.print(analogRead(analogInPin1));
                        
            client.print("
analog input2 is: ");
            client.print(analogRead(analogInPin2));
            
            client.print("
analog input3 is: ");
            client.print(analogRead(analogInPin3));
                                    
            client.print("
analog input4 is: ");
            client.print(analogRead(analogInPin4));
            
            client.print("
analog input5 is: ");
            client.print(analogRead(analogInPin5));
            client.println("
</BODY></HTML>");
           }
          //generate main page with iframe
          else
          {
            client.print("<HTML><HEAD><TITLE>Zoomkat's frame refresh test</TITLE></HEAD>");
            client.print("Zoomkat's Arduino frame meta refresh test 5/25/13");
            client.print("

Arduino analog input data frame:
");
            client.print("&nbsp;&nbsp;<a href='http://192.168.1.102:84/datastart' target='DataBox' title=''yy''>META-REFRESH</a>");
            client.print("&nbsp;&nbsp;&nbsp;&nbsp;<a href='http://192.168.1.102:84/data' target='DataBox' title=''xx''>SINGLE-STOP</a>");
            client.print("&nbsp;&nbsp;&nbsp;&nbsp;<a href='http://192.168.1.102:84/datafast' target='DataBox' title=''zz''>FAST-DATA</a>
");
            client.print("<iframe src='http://192.168.1.102:84/data' width='350' height='250' name='DataBox'>");
            client.print("</iframe>
</HTML>");
          }
          delay(1);
          //stopping client
          client.stop();
          //clearing string for next read
          readString="";
        }
      }
    }
  }
}

Finally it uploaded. I am getting below error. I think there is problem for assign a mac address

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

byte mac[] = {  0x00, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup() {
  Serial.begin(9600);

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) 
  {
    Serial.println(F("failed"));
  }
  else {
      Serial.print(F("IP: "));
      Serial.println(Ethernet.localIP());
      Serial.print(F("Subnet: "));
      Serial.println(Ethernet.subnetMask());
      Serial.print(F("Gateway: "));
      Serial.println(Ethernet.gatewayIP());
      Serial.print(F("DNS server: "));
      Serial.println(Ethernet.dnsServerIP());
  } 
 /*     Serial.print("Starting ethernet...");
  if(!Ethernet.begin(mac)) Serial.println("failed");
  else {
      Serial.print("IP: ");
      Serial.println(Ethernet.localIP());
      Serial.print("Subnet: ");
      Serial.println(Ethernet.subnetMask());
      Serial.print("Gateway: ");
      Serial.println(Ethernet.gatewayIP());
      Serial.print("DNS server: ");
    Serial.println(Ethernet.dnsServerIP());
  } 
  */    
}

void loop() {
   Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) 
  {
    Serial.println(F("failed"));
  }
  else {
      Serial.print(F("IP: "));
      Serial.println(Ethernet.localIP());
      Serial.print(F("Subnet: "));
      Serial.println(Ethernet.subnetMask());
      Serial.print(F("Gateway: "));
      Serial.println(Ethernet.gatewayIP());
      Serial.print(F("DNS server: "));
      Serial.println(Ethernet.dnsServerIP());
  } 
  delay(1000);
}

Dear all,

I am new to programming with Ethernet shield. I have setup like below.
Arduino UNo board, windows 7 OS
Ethernet shieldhttp://www.rhydolabz.com/index.php?main_page=product_info&cPath=152_151&products_id=877
web server Example code from Arduino v1.0.5.
I have connected Ethernet port of shield to Ethernet port of computer.

Code compile properly.But i could not able to connect web server

/*
  Web Server
 
 A simple web server that shows the value of the analog input pins.
 using an Arduino Wiznet Ethernet shield. I have attached how i configured ip address . 
 
 Circuit:
 * 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[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,7,100);

// 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:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


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


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 = client.read();
        Serial.write(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("Connection: close");  // the connection will be closed after completion of the response
	  client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // 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(analogChannel);
            client.print(" is ");
            client.print(sensorReading);
            client.println("
");       
          }
          client.println("</html>");
          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
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

You setup your shield with IP 192.168.7.100:

IPAddress ip(192,168,7,100);

but try to access it with IP 192.168.7.12

Why are you trying to access another IP than the one you applied to the shield?

I will reply to the post you started in Networking. It belongs there.
edit: I see a moderator merged both subjects. Thanks! :slight_smile:

Try this sketch. It tests the SPI bus and the SPI end of the w5100. Does it show 192.168.2.2 on the serial monitor? If it shows anything else, like 0.0.0.0, then check the shield connection to the Arduino. Insure all pins are inserted into the sockets.

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

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,2);

void setup() {
  Serial.begin(9600);

  // disable SD card if one in the slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.println("Starting w5100");
  Ethernet.begin(mac,ip);
  Serial.println(Ethernet.localIP());
}

void loop() {
}

BTW, the dhcp example I posted in Programming wouldn’t work if you are connected directly to your PC. It doesn’t have a dhcp server set up on that interface. That would have been helpful to know.

Finally i made i work webserver.
2nd question over here is how to access over internet.?

I have connection setup like below
I have network switch 5 port.

LAN NETWORK connected to server to switch. port 0
PC Ethernet port goes to same network switch, port 1
Ethernet shield from arduino connected to network port 2

all networks are on same hub. Let me know How to configure for Below setup . I am using Windows XP operation system.

Is that a switch or a router?

Do you have a commercial account (static IP) with your ISP? If not commercial, it is probable that your ISP blocks certain ports, like port 80 requests from the internet.

@ present i have connected to switch . Where my server mentioned connected to modem.

I have similar set up as shown in image. But all device connected to switch 5 port.

From Model one LAN connection goto switch port 0
from PC one LAN cable connection goto switch port 1
from Ethernet cable goto switch port 2.
And all under same network.
Question are:
1) how to configure IP address for all, I have made test run for static ip address. connecting directly Ethernet shield and Ethernet port of PC . which is working fine
2) How to control LED in below example. Individually the code working fine.
3)How to keep option to change date and time here. Which can replicate in web server.

#include <SPI.h>
#include <Ethernet.h>
String readString;
int led = 4;
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,7, 150);
EthernetServer server(80);
int local_day=12;
int local_month=04;
int local_year=2014;
int local_h=12;
int local_m=30;
int local_s=21;
float latitude=15.08;
float longitude=58.05;
float tracker_des_angle=45.0;
float tracker_actual_pos=43.0;
int Wind_Speed=12;
int Wind_Kmph=120;

void setup()
{

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


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



}
void loop()
{
  Ethernet_webserver();
}


void Ethernet_webserver()
{
  EthernetClient client = server.available();
  if (client) 
  {
    boolean currentLineIsBlank = true;
    while (client.connected()) 
    {
      if (client.available())
      {
        char c = client.read();
        if (c == '\n' && currentLineIsBlank)
        {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          // add a meta refresh tag, so the browser pulls again every 5 seconds:
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          client.println("<TITLE>NAVYA TITLE </TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>NAVYA TITLE </H1>");
          client.println("<hr />");
          client.println("
");  
          client.println("<H2>NAVYA TRACKER PROJECT </H2>");
          client.println("
");  
          client.println("<a href=\"/?button1on\"\">Turn On LED</a>");
          client.println("<a href=\"/?button1off\"\">Turn Off LED</a>
");   
          client.println("
");     
          client.println("
"); 

          /*
          client.println("<!DOCTYPE HTML>");
           client.println("<html>");*/
          // add a meta refresh tag, so the browser pulls again every 5 seconds:
          //  client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          // add sententence here
          client.print("LOCAL DATE :"); 
          client.print(local_day);
          client.print("/");
          client.print(local_month);
          client.print("/");
          client.print(local_year); 
          client.println("
");   
          client.print("LOCAL TIME :"); 
          client.print(local_h);
          client.print("/");
          client.print(local_m);
          client.print("/");
          client.print(local_s); 
          client.println("
");   
          client.print("LATITUDE :");
          client.print(latitude); 
          client.print("
");   
          client.print("LONGITUDE :");
          client.print(longitude); 
          client.println("
");   
          client.print("DESIRED ANGLE:");
          client.print(tracker_des_angle); 
          client.println("
");   
          client.print("ACTUAL ANGLE:");
          client.print(tracker_actual_pos); 
          client.println("
");   
          client.print("WIND SPEED M/S:");
          client.print(Wind_Speed); 
          client.println("
");   
          client.print("WIND SPEED KMPH:");
          client.print(Wind_Kmph); 
          client.println("
");   

          client.println("</html>");
          //break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
          if (readString.indexOf("?button1on") >0){
            digitalWrite(led, HIGH);
          }
          if (readString.indexOf("?button1off") >0){
            digitalWrite(led, LOW);
          }
          readString="";  
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();

    Serial.println("client disonnected");


  }
}