Hello comunity. I have been working with Arduino controllers for some time. But this is the first time I need to use ethernet shields in my progect. I have two
Arduino UNO boards
and two
WIZnet ethernet shields W5100
The task is simple. If i recieve some data on com port at one end i should transfer this data to another board, or if an io is trigered also to send data about this event to other board.,
I have found a code snapshot on arduino forum here
http://forum.arduino.cc/index.php?topic=57396.0
But due to the fact that it is for old library types I have changed it to corespond to new library types
Server board
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = {0x90, 0xa2, 0xda, 0x00, 0x23, 0x16};
byte ip[] = {10, 10, 5, 124};
const unsigned int localPort = 1369;
char recvdBuffer[UDP_TX_PACKET_MAX_SIZE+1]; //buffer to hold incoming packet
char replyBuffer[UDP_TX_PACKET_MAX_SIZE+1]; //a string to send back
byte remoteIp[4]; // Will save the originator's IP address from the received packet
unsigned int remotePort; // Will save the originator's port number from the received packet
const int ledPin = 9;
EthernetUDP udp;
void setup()
{
Ethernet.begin(mac,ip);
udp.begin(localPort);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
int recvdSize = udp.available(); // note that this includes the UDP header
if(recvdSize) {
udp.beginPacket(remoteIp, remotePort);
udp.read(recvdBuffer,UDP_TX_PACKET_MAX_SIZE);
recvdBuffer[recvdSize] = '\0';
recvdSize -= 8;
Serial.print("Received packet data size = ");
Serial.print(recvdSize);
Serial.print(" from ");
for (int i = 0; i < 4; i++) { // Print IP address xxx.xxx.xxx.xxx
Serial.print(remoteIp[i], DEC);
if (i < 3) {
Serial.print(".");
}
}
Serial.print(", port number ");
Serial.println(remotePort);
Serial.print("Contents: ");
Serial.println(recvdBuffer);
switch (recvdBuffer[0]) {
case 'H':
Serial.println("Turning LED on");
digitalWrite(ledPin, HIGH);
strncpy(replyBuffer, "H: LED is On",UDP_TX_PACKET_MAX_SIZE);
break;
case 'L':
Serial.println("Turning LED off");
strncpy(replyBuffer, "L: LED is Off", UDP_TX_PACKET_MAX_SIZE);
digitalWrite(ledPin, LOW);
break;
default:
Serial.println("No action");
strncpy(replyBuffer, "Unknown command", UDP_TX_PACKET_MAX_SIZE);
break;
}
udp.beginPacket(remoteIp, remotePort);
udp.write(replyBuffer);
}
}
Client board
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = {0x90, 0xa2, 0xda, 0x00, 0x11, 0x07};
byte ip[] = {10, 10, 5, 123};
const unsigned int localPort = 9631;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE+1];
byte remoteIp[4] = {10, 10, 5, 124};
unsigned int remotePort = 1369;
byte recvdIp[4];
unsigned int recvdPort;
const int ledPin = 9;
EthernetUDP udp;
void setup()
{
Ethernet.begin(mac, ip);
udp.begin(localPort);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
char UDPMessageBuffer[80];
void loop()
{
int packetSize = udp.available();
if(packetSize)
{
udp.beginPacket(recvdIp, recvdPort);
udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
packetBuffer[packetSize] = '\0';
packetSize -= 8;
Serial.print("Received packet size = ");
Serial.print(packetSize);
Serial.print(" from ");
for (int i = 0; i < 4; i++) { // Print IP address xxx.xxx.xxx.xxx
Serial.print(remoteIp[i], DEC);
if (i < 3) {
Serial.print(".");
}
}
Serial.print(", port number ");
Serial.println(remotePort);
Serial.print("Contents: ");
Serial.println(packetBuffer);
}
char inchar;
if (Serial.available() > 0) {
inchar = Serial.read();
switch(inchar) {
case 'H':
case 'h':
Serial.println("Sending 'H'");
strcpy(UDPMessageBuffer, "H");
udp.beginPacket(remoteIp, remotePort);
udp.write(UDPMessageBuffer);
break;
case 'L':
case 'l':
Serial.println("Sending 'L'");
strcpy(UDPMessageBuffer, "L");
udp.beginPacket(remoteIp, remotePort);
udp.write(UDPMessageBuffer);
break;
}
}
}
But it doesn't work =/
I have downloaded all the updates for the shield 5100 libraries but still no result.
My question is - If there ixists a simple (e.d. very simple) code example to send data from one board via ethernet shield - for example from com console and to triger some i/o on the other board?