#ifdef WIZ1
byte mac1[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip1(192, 168, 1, 179);
unsigned int localPort1 = 8899; // local port to listen on
#endif
#ifdef WIZ2
byte mac2[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEC };
IPAddress ip2(192, 168, 1, 177);
unsigned int localPort2 = 8877; // local port to listen on
#endif
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
// An EthernetUDP instance to let us send and receive packets over UDP
#ifdef WIZ1
EthernetUDP Udp1;
#endif
#ifdef WIZ2
EthernetUDP Udp2;
#endif
void EthSelect(int id)
{
if (id == WIZNET1_CS)
{
digitalWrite(WIZNET2_CS, HIGH); // Turn it off
Ethernet.init(WIZNET1_CS);
}
else
{
digitalWrite(WIZNET1_CS, HIGH); // Turn it off
Ethernet.init(WIZNET2_CS);
}
}
void setup()
{
pinMode(GREEN_LED, OUTPUT);
// This is to disable the Wiz until ready to go
pinMode(WIZNET_RESET, OUTPUT);
delay(100);
digitalWrite(WIZNET_RESET, LOW);
delay(10);
digitalWrite(WIZNET_RESET, HIGH);
delay(100);
//Ethernet.init(WIZNET1_CS); // Most Arduino shields
EthSelect(WIZNET1_CS);
// start the Ethernet
Ethernet.begin(mac1, ip1);
#endif
#ifdef WIZ2
EthSelect(WIZNET1_CS);
// start the second Ethernet
Ethernet.begin(mac2, ip2);
#endif
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
#ifdef WIZ1
// Check for Ethernet hardware present
Ethernet.init(WIZNET1_CS); // Most Arduino shields
if (Ethernet.hardwareStatus() == EthernetNoHardware)
Serial.println("Ethernet shield 1 was not found. Sorry, can't run without hardware. ");
if (Ethernet.linkStatus() == LinkOFF)
{
Serial.println("Ethernet cable 1 is not connected.");
}
#endif
#ifdef WIZ2
EthSelect(WIZNET2_CS);
//Ethernet.init(WIZNET2_CS); // Most Arduino shields
if (Ethernet.hardwareStatus() == EthernetNoHardware)
Serial.println("Ethernet shield 2 was not found. Sorry, can't run without hardware. ");
if (Ethernet.linkStatus() == LinkOFF)
{
Serial.println("Ethernet cable 2 is not connected.");
}
#endif
if (packetSize)
{
Serial.print("Received packet from 1 of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp1.remoteIP();
for (int i=0; i < 4; i++) {
Serial.print(remote[i], DEC);
if (i < 3) {
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp1.remotePort());
// read the packet into packetBufffer
Udp1.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
// send a reply to the IP address and port that sent us the packet we received
Udp1.beginPacket(Udp1.remoteIP(), Udp1.remotePort());
Udp1.write(ReplyBuffer);
Udp1.endPacket();
}
#endif
packetSize= Udp2.parsePacket();
if (packetSize)
{
Serial.print("Received packet from 2 of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp2.remoteIP();
for (int i=0; i < 4; i++) {
Serial.print(remote[i], DEC);
if (i < 3) {
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp2.remotePort());
// read the packet into packetBufffer
Udp2.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
// send a reply to the IP address and port that sent us the packet we received
Udp2.beginPacket(Udp2.remoteIP(), Udp2.remotePort());
Udp2.write(ReplyBuffer);
Udp2.endPacket();
}
#endif
delay(10);
count++;
// Always nice to see a flickering light to know it's running
if ((count % 10) < 5)
digitalWrite(GREEN_LED, HIGH);
else
digitalWrite(GREEN_LED, LOW);
}