Pages: [1]   Go Down
Author Topic: Beginner Ethernet Shield V1.1  (Read 875 times)
0 Members and 1 Guest are viewing this topic.
Pakistan
Offline Offline
Full Member
***
Karma: 0
Posts: 101
Advances are made by answering questions. Discoveries are made by questioning answers.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello
 I have given a project of communicating with PC from Arduino and with Arduino from PC over LAN using Ethernet Shield V1.1 and i have no knowledge regarding communication systems and IP address or some other address etc, i'm just stuck, please help me in its programming, i have seen the examples in IDE but didn't got any idea that whats going on in it. please help me out.

Regards
Saad Khalil.
Logged

Saad Khalil

Netherlands
Offline Offline
Sr. Member
****
Karma: 1
Posts: 290
(:-)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First of what operating system are you running? windows, mac, linux?

If windows open a command prompt (start --> run --> type: cmd <enter>)

In the command prompt window type ipconfig /all <enter>

This will give output similar to this:
Code:
Ethernet adapter Local Area Connection 3:

        Connection-specific DNS Suffix  . :
        Description . . . . . . . . . . . : D-Link DUB-E100 USB 2.0 Fast Etherne
t Adapter
        Physical Address. . . . . . . . . : 00-80-C8-38-F6-7C
        Dhcp Enabled. . . . . . . . . . . : Yes
        IP Address. . . . . . . . . . . . : 192.168.10.200
        Subnet Mask . . . . . . . . . . . : 255.255.255.0
        Default Gateway . . . . . . . . . : 192.168.10.1

Most important is ip Adress in this case 192.168.10.200. We need an unique address for the arduino, so we add 1 to the last number: 192.168.10.201

We still need to know if this address is free so the next command we type is ping 192.168.10.201<enter>
Code:
C:\>ping 192.168.10.201

Pinging 192.168.10.201 with 32 bytes of data:

Request timed out.
Request timed out.
Request timed out.
Request timed out.

Ping statistics for 192.168.10.201:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

We get a timeout so the address is free.
Now open webserver example and search for the part shown below

Code:
// 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 };
byte ip[] = { 192,168,1, 177 };

Change the ip adress to your newly created address.
In this example that would be:
Code:
byte ip[] = { 192,168,10, 201 };
Download the code to your arduino.
Now open your web browser and type in the following adress http://192.168.10.201 and see the magic.

Hope this helps to get started.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 10
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

i am also new to Ethernet Shield. I am doing Home Automation Project where i will be controlling LED's using web server. Even tho i have got the code for controlling LED's i can't understand how the code works. Here is the code.
#include <SPI.h>
#include <Client.h>
#include <Ethernet.h>
#include <Server.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1,2 };           // ip in lan
byte gateway[] = { 192, 168, 0, 1 };            // internet access via router
byte subnet[] = { 255, 255, 255, 0 };                   //subnet mask
Server server(80);                                      //server port
byte sampledata=50;            //some sample data - outputs 2 (ascii = 50 DEC)
int ledPin = 13;  // LED pin
char link[]="http://www.scienceprog.com/"; //link data
String readString = String(30); //string for fetching data from address
boolean LEDON = false; //LED status flag
void setup(){
//start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
//Set pin 4 to output
  pinMode(ledPin, OUTPUT);
//enable serial datada print
  Serial.begin(9600);
}
void loop(){
// Create a client connection
Client client = server.available();
  if (client) {
    while (client.connected()) {
   if (client.available()) {
    char c = client.read();
     //read char by char HTTP request
    if (readString.length() < 100)
      {
        //store characters to string
        readString += c; //replaces readString.append(c);
      }
        //output chars to serial port
        Serial.print(c);
        //if HTTP request has ended
        if (c == '\n') {
          //dirty skip of "GET /favicon.ico HTTP/1.1"
          if (readString.indexOf("?") <0)
          {
            //skip everything
          }
          else
          //lets check if LED should be lighted
        if(readString.indexOf("L=1") >0)//replaces if(readString.contains("L=1"))
           {
             //led has to be turned ON
             digitalWrite(ledPin, HIGH);    // set the LED on
             LEDON = true;
           }else{
             //led has to be turned OFF
             digitalWrite(ledPin, LOW);    // set the LED OFF
             LEDON = false;
           }
          // now output HTML data starting with standart header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          //set background to yellow
          client.print("<body style=background-color:yellow>");
          //send first heading
          client.println("<font color='red'><h1>HTTP test routines</font></h1>");
          client.println("<hr />");
          client.println("<hr />");
          //output some sample data to browser
          client.println("<font color='blue' size='5'>Sample data: ");
          client.print(sampledata);//lets output some data
          client.println("<br />");//some space between lines
          client.println("<hr />");
          //drawing simple table
          client.println("<font color='green'>Simple table: ");
          client.println("<br />");
          client.println("<table border=1><tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr>");
          client.println("<tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr></table>");
          client.println("<br />");
          client.println("<hr />");
          //printing some link
          client.println("<font color='blue' size='5'>Link: ");
          client.print("<a href=");
          client.print(link);
          client.println(">Visit Scienceprog!</a>");
          client.println("<br />");
          client.println("<hr />");
          //controlling led via checkbox
          client.println("<h1>LED control</h1>");
          //address will look like http://192.168.1.110/?L=1 when submited
          if (LEDON)
          client.println("<form method=get name=LED><input type=checkbox name=L value=1 CHECKED>LED<br><input type=submit value=submit></form>");
          else
          client.println("<form method=get name=LED><input type=checkbox name=L value=1>LED<br><input type=submit value=submit></form>");
          client.println("<br />");
          //printing LED status
          client.print("<font size='5'>LED status: ");
          if (LEDON)
              client.println("<font color='green' size='5'>ON");
          else
              client.println("<font color='grey' size='5'>OFF");
          client.println("<hr />");
          client.println("<hr />");
          client.println("</body></html>");
          //clearing string for next read
          readString="";
          //stopping client
          client.stop();
            }
          }
        }
      }
}

Could you please explain me how the code works. Or could you teach me step by step process of how to work with Ethernet Shield? I understand how to setup the ip address. Just teach me about client and server and how to communicate.

Thanks in advance !!!!!!!!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I have given a project of communicating with PC from Arduino and with Arduino from PC
If the PC and the Arduino are the only devices involved, a cable is a lot cheaper and easier to use. Why is the Ethernet shield involved at all?

If you know nothing about network programming, this job is too big for you.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some simple server code that will will make arduino pin 4 high and low from a simple web page.

Code:
//zoomkat 12-18-10
//routerbot code
//for use with IDE 0021
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//address will look like http://192.168.1.102:84/ when submited
//for use with W5100 based ethernet shields

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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
Server server(84); //server port

String readString;
 
 //////////////////////

void setup(){

pinMode(4, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();

//enable serial data print
Serial.begin(9600);
Serial.println("servertest1"); // so I can keep track of what is loaded
}

void loop(){
// Create a client connection
Client client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();

//read char by char HTTP request
if (readString.length() < 100) {

//store characters to string
readString += c;
//Serial.print(c);
}

//if HTTP request has ended
if (c == '\n') {

///////////////
Serial.println(readString);

  //now output HTML data header

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

client.println("<HTML>");
   client.println("<HEAD>");
     client.println("<TITLE>Arduino GET test page</TITLE>");
   client.println("</HEAD>");
   client.println("<BODY>");
   
    client.println("<H1>HTML form GET example</H1>");

    client.println("<FORM ACTION=\"http://192.168.1.102:84\" method=get >");

    client.println("Pin 4 \"on\" or \"off\": <INPUT TYPE=TEXT NAME=\"LED\" VALUE=\"\" SIZE=\"25\" MAXLENGTH=\"50\"><BR>");

        client.println("<INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Change Pin 4!\">");

    client.println("</FORM>");
   
client.println("<BR>");

   client.println("</BODY>");
client.println("</HTML>");

  delay(1);
  //stopping client
client.stop();

/////////////////////
if(readString.indexOf("on") >0)//checks for on
           {
             digitalWrite(4, HIGH);    // set pin 4 high
             Serial.println("Led On");
             }
if(readString.indexOf("off") >0)//checks for off
           {
             digitalWrite(4, LOW);    // set pin 4 low
             Serial.println("Led Off");
             }
//clearing string for next read
readString="";
 
}}}}}
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pakistan
Offline Offline
Full Member
***
Karma: 0
Posts: 101
Advances are made by answering questions. Discoveries are made by questioning answers.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First of what operating system are you running? windows, mac, linux?

If windows open a command prompt (start --> run --> type: cmd <enter>)

In the command prompt window type ipconfig /all <enter>

This will give output similar to this:
Code:
Ethernet adapter Local Area Connection 3:
We still need to know if this address is free so the next command we type is ping 192.168.10.201<enter>
[code]C:\>ping 192.168.10.201

Pinging 192.168.10.201 with 32 bytes of data:

Request timed out.
Request timed out.
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),

// 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 };
byte ip[] = { 192,168,1, 177 };[/code]

Change the ip adress to your newly created address.
In this example that would be:
Code:
byte ip[] = { 192,168,10, 201 };
Download the code to your arduino.
Now open your web browser and type in the following adress http://192.168.10.201 and see the magic.
Yes i'm having Windows, Thanks Wortelsoft.. i hope it will help me alot..
Logged

Saad Khalil

Pakistan
Offline Offline
Full Member
***
Karma: 0
Posts: 101
Advances are made by answering questions. Discoveries are made by questioning answers.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If the PC and the Arduino are the only devices involved, a cable is a lot cheaper and easier to use. Why is the Ethernet shield involved at all?
If you know nothing about network programming, this job is too big for you.
Thanks for Reply..
Let me clear my question again, actually i want to send some sensor data continuously to 'PC' far away with my friend via internet, with the help of my Ethernet, my PC and Arduino. Am i doing right? if yes please help me out, and yes i don't know about "Network programming"  smiley-confuse
Logged

Saad Khalil

Sussex UK / CT USA
Offline Offline
Edison Member
*
Karma: 0
Posts: 1028
Forums forever
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
...send some sensor data continuously to 'PC' far away with my friend via internet, with the help of my Ethernet, my PC and Arduino...

The good news: I tcan be done! And you've identified the way to do it.

The bad news?....

Quote
I don't know about "Network programming"...

You'll need to learn.

===
Half the problem is that you'll have to learn a whole bunch of things all at once... building and running a server (that topic alone has multiple sub-topics), HTML coding (you won't need more than a smattering)

The other half of the problem is at the receiving end. More details needed of the receiver's exact needs.

===
More good news... it CAN be done!! See http://ArduServer.com for some encouragement, some further information.

===
---

Does your friend need continuous access to what, say, the temperature is where you are just when he's curious? I.e. is it enough to set up a webpage which can be accessed anytime, and the webpage shows the temperature now? That's fairly easy.

Does your friend need a graph of what the temperatures (or whatever) have been for the past few days? Might be a job to do with a PC at your end, rather than just an Arduino. See http://www.arunet.co.uk/tkboyd/offers2fwover.htm

Another answer, which would need just Arduino and a lot of work (and an always-on internet connection) is Pachube... If your friend needs a graph of up to 30 days' data (longer if you pay... 30 day service is free). More on that at http://sheepdogsoftware.co.uk/sc2wy.htm
Logged

Pakistan
Offline Offline
Full Member
***
Karma: 0
Posts: 101
Advances are made by answering questions. Discoveries are made by questioning answers.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The good news: It can be done! And you've identified the way to do it.

WoW! Thanks for encouragement tkbyd..

Quote
More good news... it CAN be done!! See http://ArduServer.com for some encouragement, some further information.

i can see some helpful material for myself in it and hope that it 'll help me alot... and i 'll need youa help in future buddy.. So please stay tune to me.. Thanksss again
Logged

Saad Khalil

Pages: [1]   Go Up
Jump to: