Ethernet module

All about Arduino, Jeenode and other Clones (hardware & Software)

Moderator: Mdamen

Digit
Global Moderator
Global Moderator
Posts: 3388
Joined: Sat Mar 25, 2006 10:23 am
Location: Netherlands
Contact:

Re: Ethernet module

Post by Digit »

Try this one:

Code: Select all

// Ping a remote server, also uses DHCP and DNS.
// 2011-06-12 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php
// $Id: pings.pde 7725 2011-06-13 14:07:56Z jcw $

#include <EtherCard.h>

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,10,75 };
static byte gwip[] = {192,168,10,60 };
static byte dnsip[] = {192,168,10,10 };

byte Ethernet::buffer[700];
static uint32_t timer;

// called when a ping comes in (replies to it are automatic)
static void gotPinged (byte* ptr) {
  ether.printIp(">>> ping from: ", ptr);
}

void setup () {
  Serial.begin(57600);
  Serial.println("\n[pings]");
  
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");
//  if (!ether.dhcpSetup())
//    Serial.println("DHCP failed");

  if (!ether.staticSetup(myip, gwip, dnsip))
    Serial.println("IP address setup failed");

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);  

#if 1
  // use DNS to locate the IP address we want to ping
  if (!ether.dnsLookup(PSTR("www.google.com")))
    Serial.println("DNS failed");
#else
  ether.parseIp(ether.hisip, "74.125.77.99");
#endif
  ether.printIp("SRV: ", ether.hisip);
    
  // call this to report others pinging us
  ether.registerPingCallback(gotPinged);
  
  timer = -9999999; // start timing out right away
  Serial.println();
}

void loop () {
  word len = ether.packetReceive(); // go receive new packets
  word pos = ether.packetLoop(len); // respond to incoming pings
  
  // report whenever a reply to our outgoing ping comes back
  if (len > 0 && ether.packetLoopIcmpCheckReply(ether.hisip)) {
    Serial.print("  ");
    Serial.print((micros() - timer) * 0.001, 3);
    Serial.println(" ms");
  }
  
  // ping a remote server once every few seconds
  if (micros() - timer >= 5000000) {
    ether.printIp("Pinging: ", ether.hisip);
    timer = micros();
    ether.clientIcmpRequest(ether.hisip);
  }
}
It's a bit modified - I added static IP setup.
My guess is that DHCP doesn't work. It didn't here :( , so that might be the issue.
So I commented out the DHCP part and added static IP. Change the IP addresses (myip, gwip and dnsip) according to your local LAN!
After this small modification, I get this (open the Serial Monitor window):

Code: Select all

[pings]
IP:  192.168.10.75
GW:  192.168.10.60
DNS: 192.168.10.10
SRV: 74.125.79.104

Pinging: 74.125.79.104
  24.728 ms
Pinging: 74.125.79.104
  24.108 ms
Pinging: 74.125.79.104
  24.232 ms
Pinging: 74.125.79.104
  23.900 ms
Pinging: 74.125.79.104
  24.732 ms
Pinging: 74.125.79.104
  24.032 ms
Pinging: 74.125.79.104
  24.492 ms
That's what you should see too.

BTW, forget about the 2nd sketch you posted. That one is for the Arduino Ethernet shield, not for the JeeLabs EtherCard.
Using a sketch for Ethernet Shield with an EtherCard is like using a ATI driver for a nVidia card :)

If you need example, see File > Examples > Ethercard in the Arduino IDE, don't use the File > Examples > Ethernet examples.

Tip: I would report the issue at JeeLabs, this info can be useful for JCW.
Digit
Global Moderator
Global Moderator
Posts: 3388
Joined: Sat Mar 25, 2006 10:23 am
Location: Netherlands
Contact:

Re: Ethernet module

Post by Digit »

PS
Dj, don't forget to update the EtherCard library!!
jcw
Starting Member
Starting Member
Posts: 15
Joined: Mon Nov 24, 2008 12:36 am
Location: Netherlands
Contact:

Re: Ethernet module

Post by jcw »

The DHCP stuff is indeed not stable yet. Part of the problem is that I can only test with stuff I have here, and there seem to be subtle differences between routers. There is no doubt a superset which works in most places, but we're not there yet. To help requires some deep TCP/IP digging, such as setting up WireShark and sending me a dump of a DHCP "discussion" - well, as far as it gets, that is.

What brand router are you using?

I'm not tracking this forum on a regular basis, so if you want me in the loop (and others working with the EtherCard library), it might be best to check on the JeeLabs forum (forum.jeelabs.net).
phoenixb
Advanced Member
Advanced Member
Posts: 512
Joined: Thu Jul 23, 2009 1:00 pm
Location: Netherlands

Re: Ethernet module

Post by phoenixb »

Hi,

@Robert, first i have download the latest ethercard library and I have test your demo and now i getting some live in from the ethercard, thanks for the help i now use for the time being a static ip adres (the second sketch i have delete so i can not use it anymore)

For the router I use at this moment a WRT54G with a DD-WRT custom rom on it, so maybe is it a combination of the router.
At my work I have to do something like that (Now I sit to think, (is that normal english?)), I had a hardware restriction on half duplex and 10mb and a normal router on full duplex 10/100mb that has some trouble to give a ip over DHCP and with a static ip the ping times ar really high.

Code: Select all

[pings]
IP:  192.168.10.125
GW:  192.168.10.1
DNS: 192.168.1.254
SRV: 74.125.79.104

Pinging: 74.125.79.104
  39.224 ms
Pinging: 74.125.79.104
  39.592 ms
Pinging: 74.125.79.104
  39.664 ms
Pinging: 74.125.79.104
  40.800 ms
Pinging: 74.125.79.104
  39.908 ms
Pinging: 74.125.79.104
  40.272 ms
__________________
Digit
Global Moderator
Global Moderator
Posts: 3388
Joined: Sat Mar 25, 2006 10:23 am
Location: Netherlands
Contact:

Re: Ethernet module

Post by Digit »

OK, now at least you know that your EtherCard is working, that's a good thing :)
Now you can move on with creating your own sketch and make it do what you want it to do 8)

Don't forget to visit the JeeLabs forum and post your DHCP findings there too, as JCW mentioned earlier.
Post Reply

Return to “Raspberry, Arduino, Cubietruck and other clones Forum”