Arduino ethernet BOARD example

Hello,

I try to find some information or help with my arduino ethernet board. I can find a lot of shield info but not for the board.

I am searching for a basic GET example to return some values to my arduino ethernet from a php coded page

In the examples i only can find example code for the shield But that doesnt work and then still i am looking for an example of the php code to send the right values

Thanks!

I am searching for a basic GET example to return some values to my arduino ethernet from a php coded page

The examples provided show how to get a page from a script on a server. You have a server. It is running a script, written with PHP. It seems like it should be quite easy to get that script to run.

Sending the GET request is simple:

    // Make a HTTP request:
    client.println("GET /pub/getValue.php?ID=2 HTTP/1.0");
    client.println();

In the examples i only can find example code for the shield

I think you are not understanding something, then. The code does not run on the shield. It runs on the Arduino.

But that doesnt work

What code doesn't work? What doesn't it do?

and then still i am looking for an example of the php code to send the right values

We have no ideas what values the PHP script should send, so it is impossible to help you there.

I wish it was so easy ;-)

I will test the example, thanks for tat!

I no it runs on the board and not on the shield... but i think the code for a board+shield differs from a arduino board with build in ethernet because that is the board i am using

The example code for WebClient says connetion failed, and the code speakes about the shield, not about build in ethernet

I understand you cannot help when you dont know what code is needed to send. But just displaying the text "test" from a static value on a page should help me to understand how it is working.

Thanks!

edit o and sending is working good, the thing is that i want to return something

How to enter MAC address in the code, I've the ethernet sheild with sticker 90-A2-DA-0D-5B-E9. and examples in the code writen some thing like this, mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; Is there any conversion to do or what?

waheedsat: How to enter MAC address in the code, I've the ethernet sheild with sticker 90-A2-DA-0D-5B-E9. and examples in the code writen some thing like this, mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; Is there any conversion to do or what?

Obviously your examples aren't using the same address, but if your sticker read DE-AD-BE-EF-FE-ED then your mac declaration would be similar to this:

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

Some simple client test code you can try (not PHP related).

//zoomkat 4-04-12
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605 
//the arduino lan IP address { 192, 168, 1, 102 } may need 
//to be modified modified to work with your router.

#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 assigned to arduino
//byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page server IP address
EthernetClient client;
//////////////////////

void setup(){

  Ethernet.begin(mac, ip);
  //Ethernet.begin(mac, ip, gateway, gateway, subnet);
  Serial.begin(9600); 
  Serial.println("Better client test 4/04/12"); // so I can keep track of what is loaded
  Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
  // check for serial input
  if (Serial.available() > 0) //if something in serial buffer
  {
    byte inChar; // sets inChar as a byte
    inChar = Serial.read(); //gets byte from buffer
    if(inChar == 'e') // checks to see byte is an e
    {
      sendGET(); // call sendGET function below when byte is an e
    }
  }  
} 

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

void sendGET() //client function to send/receive GET request data.
{
  if (client.connect(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop(); //stop client

}

thanks zoomkat, that is running! now i need to copy paste this in the right place :) :cold_sweat:

okidoki, thats is running now, and returns a lot of text on the serial monitor

now i have the name that i want to return on the display between tags <name>John Doe</name>
but the server sends me

HTTP/1.1 200 OK
Date: Fri, 31 Aug 2012 09:48:45 GMT
Server: Apache
X-Powered-By: PleskLin
Content-Length: 22
Connection: close
Content-Type: text/html

<name>John Doe</name>

the rest of the returned text from the server i want to remove so that i only have left the name
is this called a string then?

because i do not know how this is called i cannot find info on how to go on in google…

thats is running now, and returns a lot of text on the serial monitor

Nothing happens automatically. You must have some code that makes this happen. Where is that code?

now i have the name that i want to return on the display between tags

OK.

but the server sends me

A bunch of stuff that includes the stuff of interest.

Perhaps you noticed that blank line, there. The data of interest follows that blank line. So, read and discard (or display on the serial monitor) the characters up to the empty line. You’ll need to determine whether the blank line is caused by ‘\n’, ‘\r’, ‘\n’, ‘\r’ or by ‘\r’, ‘\n’, ‘\r’, ‘\n’ or by ‘\n’, ‘\n’, or by ‘\r’, ‘\r’. It will be one of those 4 sets of characters that precedes the data of interest.

Of course, since you are sending a character to mark the start of your data (’<’), you could just read and discard data until that arrives. Then, read and discard data until the ‘>’ arrives. Then, read and store data until the next ‘<’ arrives. Then, read and discard data until the client runs out of data.

Then, show the stored data on the LCD.

Nothing happens automatically. You must have some code that makes this happen. Where is that code?

while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
}

Perhaps you noticed that blank line

yes i did

but how do i delete the first lines? or how is it called doing this? then i can read/search about this...

but how do i delete the first lines?

From where? You are not saving the response anywhere, so there is nowhere to delete them from.

All that you are doing is sending each character received to the serial port/Serial Monitor.

Thanks Paul,

Mwaaaaa is so complicated this... so i know now that - first save it - remove before tag - remove after tag - print it...

is that right?

This is the code I use:

  while (client.connected()) { 
    while (client.available()) {
      char c = client.read(); //gets byte from ethernet buffer
      Serial.print(c); //prints byte to serial monitor 
     }
  }
  client.stop();

i have got this from the twitter example and it looks like it does the job

 ///////
    // add incoming byte to end of line:
    currentLine += c; 

    // if you get a newline, clear the line:
    if (c == '\n') {
      currentLine = "";
    } 
    // if the current line ends with <text>, it will
    // be followed by the tweet:
    if ( currentLine.endsWith("<name>")) {
      // tweet is beginning. Clear the tweet string:
      readingName = true; 
      name = "";
    }
    // if you're currently reading the bytes of a tweet,
    // add them to the tweet String:
    if (readingName) {
      if (c != '<') {
        name += c;
      } 
      else {
        // if you got a "<" character,
        // you've reached the end of the tweet:
        readingName = false;
        Serial.println(name);  
   //     lcd.setCursor(0,1);
     //   lcd.println(name);
      //  delay(1000); 
        // close the connection to the server:

      }
    }

Only thing i do not understand is that it returns from

John Doe

it gives

John Doe

Only thing i do not understand is that it returns from

From that snippet, I have no idea why, either, since there isn’t sufficient code shown.

    if ( currentLine.endsWith("<name>")) {

None of your example lines ENDED with . One of them STARTS with and one ends with </name>.

o sorry i missed a piece of the code here is it complete

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer

    ///////
    // add incoming byte to end of line:
    currentLine += c; 

    // if you get a newline, clear the line:
    if (c == '\n') {
      currentLine = "";
    } 
    // if the current line ends with <text>, it will
    // be followed by the tweet:
    if ( currentLine.endsWith("<name>")) {
      // tweet is beginning. Clear the tweet string:
      readingName = true; 
      name = "";
    }
    // if you're currently reading the bytes of a tweet,
    // add them to the tweet String:
    if (readingName) {
      if (c != '<') {
        name += c;
      } 
      else {
        // if you got a "<" character,
        // you've reached the end of the tweet:
        readingName = false;
        Serial.println(name);

      }
    }

  }

but the problem is that it returns in the beginning of the string the last character of the separator

and with

Serial.println(name);

i got it on the screen
but if i do

lcd.println(name);

it is not working…
why? is it because its a string?

  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer

So, if there is a client connected (client.connected() is true) but there is not data to read (client.available() is o (or false)), read some data. That should be && (and), not || (or).

    if ( currentLine.endsWith("<name>")) {

No line ends with , so this will always return false.

o sorry i missed a piece of the code here is it complete

Funny definition of complete.

Hi Paul,

Thanks for helping me out!
I am getting more and more close to a working thing :slight_smile:

The lcd now displays the name that is between with this code

while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read(); //gets byte from ethernet buffer
     // add incoming byte to end of line:
    //  currentLine += c; 

    // if you get a newline, clear the line:
    if (c == '\n') {
      currentLine = "";
    } 
  
    if ( c =='<') {
       
      readingName = true; 
      name = "";
    }
     
    if (readingName) {
      if (c != '>') {
        name += c;
      } 
      else {
        
        readingName = false;

        Serial.println(name);
        lcd.setCursor(0,1);
        lcd.print(name);
        delay(1000);

      }
    }

  }

The only thing that is driving me really crazy is that the ‘<’ is still shown at the beginning of the name on the display

The php code gives a html with

Can you please help me out to remove that firs character?

Thanks!

If your client returns “Joe”, I don’t see how that code extracts just Joe, or >Joe.

    if ( c =='<') {
       
      readingName = true; 
      name = "";
    }

Presumably, readingName is false until this code is executed. Hard to tell, since you post ALL of your code.

So, the arrival of the < sets a flag that says to store the characters that follow.

    if (readingName) {
      if (c != '>') {
        name += c;
      }

c contains ‘<’, still, so it should be stored in name.

The characters “ame” are not ‘<’ and not ‘>’ (the only special characters), so they should be added to name, too.

Then, you read a ‘>’ (at the end of ). It is a ‘>’, so that if block is skipped, and the else portion is executed.

      else {
        
        readingName = false;

        Serial.println(name);
        lcd.setCursor(0,1);
        lcd.print(name);
        delay(1000);

      }

I’d expect to see “ame” show up on the lcd for one second.

I think what you really need to do is store data starting when the ‘>’ (at the end of ) arrives, and stop when the ‘/’ (in ) arrives. Then, lop off the last character and display the rest.

Are you sure this is what you want?

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    // if client.connected (not available), it reads, and you are not ignoring -1 (255)
    char c = client.read(); //gets byte from ethernet buffer or -1 (nothing available)

This replaces all that. It reads only if characters are available, then closes the connection after the server does. Maybe it is just me...

  while (client.connected()) { 
    while (client.available()) {
      char c = client.read(); //gets byte from ethernet buffer
      Serial.print(c); //prints byte to serial monitor 
     }
  }
  client.stop();

edit: Since I haven't mentioned this in a while, I will here. While there are characters in the w5100 rx buffer (client.available() > 0), the connection will not close. There is no sense checking client.connected() while client.available().