EtherCard Library - Test ping ....

579 views
Skip to first unread message

phillips321

unread,
Oct 2, 2011, 2:46:55 PM10/2/11
to nanode-users
Hi guys,

I would like to use my nanode as a Net Monitor. I'll simply like to
use it to ping a server every 60 seconds or so and then depoending on
the outcome turn an LED on/buzzer etc...

I have hacked the example from the EtherCard lib to use the Nanode MAC
chip and all that side of things is working well so far. Now i just
need to know the status of a ping.

void loop {
if ping completes then
led off
buzzer off
else led on
led on
buzzer on
end if
delay 60
}

Many thanks

P.s. My code so far is below:

Here's my code so far:
#include <EtherCard.h>
#include <NanodeUNIO.h>

byte mymac[] = { 0x88,0x88,0x88,0x88,0x88,0x88 };
byte Ethernet::buffer[700];
static uint32_t timer;
char buf[20];
NanodeUNIO unio(NANODE_MAC_DEVICE);
boolean r;


void setup () {
Serial.begin(57600);
Serial.println("Nanode Network Monitor");
getmac();

if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
if (!ether.dhcpSetup())
Serial.println("DHCP failed");

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

#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, "209.85.147.105");
#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);
}
}

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

void getmac(){
boolean mactest; char buf[20];
NanodeUNIO unio(NANODE_MAC_DEVICE);
Serial.print("Reading MAC address... ");
mactest=unio.read(mymac,NANODE_MAC_ADDRESS,6);
if (mactest){
Serial.println("success");
}
else{
Serial.println("failure");
}
sprintf(buf,"%02X:%02X:%02X:%02X:%02X:
%02X",mymac[0],mymac[1],mymac[2],mymac[3],mymac[4],mymac[5]);
Serial.print("MAC address is ");Serial.println(buf);
}

phillips321

unread,
Oct 2, 2011, 2:52:31 PM10/2/11
to nanode-users
And to those who would like syntax highlighting: http://pastebin.com/AXH4qxE4
Reply all
Reply to author
Forward
0 new messages