I'm trying to modify the pings example from the EtherCard library so I can ping multiple hosts. The end result should be a small device which monitors other devices on my network.
Here is the modified code I have in my loop
#include <EtherCard.h>
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
byte Ethernet::buffer[700];
static uint32_t timer;
void setup () {
Serial.begin(57600);
Serial.println("\n[pings]");
if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0)
Serial.println(F("Failed to access Ethernet controller"));
if (!ether.dhcpSetup())
Serial.println(F("DHCP failed"));
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.parseIp(ether.hisip, "192.168.123.150");
ether.printIp("SRV: ", ether.hisip);
timer = -9999999;
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)
{
if (ether.parseIp(ether.hisip, "192.168.123.150") == 0)
{
Serial.println("Address parsed");
}
ether.printIp("Pinging: ", ether.hisip);
timer = micros();
ether.clientIcmpRequest(ether.hisip);
}
}
When I look at the serial monitor, I see
Address parsed
Pinging: 192.168.123.150
but this happens only on the first iteration.
Every following iteration, I see Pinging: 0.0.0.0.
I've added a variable to check the value of parseIP, which is 0 the first time, but always 1 after that.
I've tried to swap out ether.hisip completely, but that did not work, which is why I'm trying to parse the IP inside the loop (so I can swap addresses).
Why does parseIP only parse the IP correctly the first time?
Strange, it compiled when I posted it... you didn't post all of your code at first ... thesetup ()function was missing