Page 1 of 1

Ethernet shield

Posted: Wed Mar 17, 2010 3:35 am
by zyberb
Helo!

I have just bought ethernet shield,but im strugling to make even the simplest code work in the arduino example codes.I have tried both the webclient code and webserver,but i only get connection failed?? I just cant get it work,i have written some comments in the code.My plan is to but sensor data in pachube.com,but first i have to manage to test this basic codes.

by the way im using my schools network to test,if it make difference.
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };/* i tested it my laptops mac,and plugged the laptop out of the network.
I also tried to give it a random mac. as i have read in the forums.
*/

byte ip[] = { 10, 0, 0, 177 };/* which ip should i use?
i tried my laptops ip and plugged it out of the network.i also tried a random ip.
*/
byte server[] = { 64, 233, 187, 99 }; // Google

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  
  delay(1000);
  
  Serial.println("connecting...");
  
  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");//all the time i get this
    
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

Re: Ethernet shield

Posted: Wed Mar 17, 2010 5:27 am
by motopic
What is the exact hw?
There are 2 kinds. Wiznet(Ethernet lib) and PIC based(different lib).
Be sure to use the correct lib with the correct hw.

IP? You should use an IP appropriate for your network (duh). 10.0.0.x is a A net, biggest possible, but probably not what you are using. Plug in your computer and get the ip address. Assign this addr to the ethernet hw, unplug your pc, and plug it into the hw. Its easier to test on a private network setup.

Re: Ethernet shield

Posted: Wed Mar 17, 2010 6:38 am
by zyberb
The Hardware used is http://www.sparkfun.com/commerce/produc ... ts_id=9026
with Wiznet w5100.

Re: Ethernet shield

Posted: Wed Mar 17, 2010 7:43 pm
by stevech
zyberb wrote:The Hardware used is http://www.sparkfun.com/commerce/produc ... ts_id=9026
with Wiznet w5100.
I've used the WizNet 812MJ module which is essentially the W5100 on a plug-in board.
Glad to pass on what I've learned doing so.

Re: Ethernet shield

Posted: Sun Mar 21, 2010 2:01 pm
by zyberb
Many many thanks to!http://gkaindl.com/software/arduino-ethernet/dhcp
Now it works fine:
//  Copyright (C) 2010 Georg Kaindl
//  http://gkaindl.com
//
//  This file is part of Arduino EthernetDHCP.
//
//  EthernetDHCP is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as
//  published by the Free Software Foundation, either version 3 of
//  the License, or (at your option) any later version.
//
//  EthernetDHCP is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with EthernetDHCP. If not, see
//  <http://www.gnu.org/licenses/>.
//

//  Illustrates how to use EthernetDHCP in synchronous (blocking)
//  mode.

#include <Ethernet.h>
#include <EthernetDHCP.h>

byte mac[] = { 0x00, 0x1F, 0x16, 0x58, 0xEB, 0xA3 };

byte server[] = { 195, 88, 55, 16 }; // www.vg.no


Client client(server, 80);
const char* ip_to_str(const uint8_t*);

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

  Serial.println("Attempting to obtain a DHCP lease...");
  
  // Initiate a DHCP session. The argument is the MAC (hardware) address that
  // you want your Ethernet shield to use. This call will block until a DHCP
  // lease has been obtained. The request will be periodically resent until
  // a lease is granted, but if there is no DHCP server on the network or if
  // the server fails to respond, this call will block forever.
  // Thus, you can alternatively use polling mode to check whether a DHCP
  // lease has been obtained, so that you can react if the server does not
  // respond (see the PollingDHCP example).
  EthernetDHCP.begin(mac);

  // Since we're here, it means that we now have a DHCP lease, so we print
  // out some information.
  const byte* ipAddr = EthernetDHCP.ipAddress();
  const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
  const byte* dnsAddr = EthernetDHCP.dnsIpAddress();
  
  Serial.println("A DHCP lease has been obtained.");

  Serial.print("My IP address is ");
  Serial.println(ip_to_str(ipAddr));
  
  Serial.print("Gateway IP address is ");
  Serial.println(ip_to_str(gatewayAddr));
  
  Serial.print("DNS IP address is ");
  Serial.println(ip_to_str(dnsAddr));
  
  

  Serial.begin(9600);
  
  delay(1000);
  
  Serial.println("connecting...");
  
  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
  
  
}

void loop()
{
  
   if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
  
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
  
  // You should periodically call this method in your loop(): It will allow
  // the DHCP library to maintain your DHCP lease, which means that it will
  // periodically renew the lease and rebind if the lease cannot be renewed.
  // Thus, unless you call this somewhere in your loop, your DHCP lease might
  // expire, which you probably do not want :-)
  EthernetDHCP.maintain();
}

// Just a utility function to nicely format an IP address.
const char* ip_to_str(const uint8_t* ipAddr)
{
  static char buf[16];
  sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
  return buf;
}