Greetings, greatest forum of the world.
I've learned so much here, thanks for all the interesting posts and answers!
Enough pleasantries.
I have a problem of my own. I have a couple of arduino's that rely on a sort of telnet protocol to communicate between.
I have searched everywhere, but can't seem to prevent two arduino's connecting to one server at the same time. This is a problem because even though the reference says the ethernet library can have four simultaneous connections I can't seem to find how to address them separately.
in all the examples written on the website (and even on this forum)
client.println
writes to ALL clients connected.
I have read that some have used other libraries like ethernet2 to overcome this, but I wanted to try a different approach.
I wanted to sent a UDP packages to see if an arduino is currently communicating with someone else...
I found this example somewhere on this forum but can't get it to work.
I have two arduino's (with an ethernet shield) connected to a router.
I can ping both of them. They can connect to each other via a little telnet program I wrote... But when I upload the following code, the lights flash, but nothing happens... Any ideas?
The code compiles without errors but I can't figure out what is wrong. I have tried to change the ip addresses, ports... If anyone could tell me where to look next I'd be grateful...
Or post some simple code to sent one byte to another Duino over UDP...
Here's the code for the sender :
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x90, 0xA2, 0xDA, 0x00, 0x85, 0x46 };
IPAddress ip(192, 168, 2, 178);
IPAddress rem_ip(192, 168, 2, 177);
unsigned int localPort = 8887; // local port to listen on
unsigned int remPort = 8888; // remote port to send to
char rcv; // Receive character (make this an Array for String Rx)
char snd; // Transmit character (make this an Array for String Rx)
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup()
{
// start the Ethernet and UDP:
Serial.begin(9600);
Ethernet.begin(mac,ip);
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
Udp.begin(localPort);
}
void loop()
{
Udp.beginPacket(rem_ip, remPort);
Udp.write('1');
Udp.endPacket();
delay(1000);
Udp.beginPacket(rem_ip, remPort);
Udp.write('0');
Udp.endPacket();
delay(1000);
}
Here's the code for the receiver
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x90, 0xA2, 0xDA, 0x00, 0x85, 0x46 };
IPAddress ip(192, 168, 2, 177);
IPAddress rem_ip(192, 168, 2, 178);
unsigned int localPort = 8888; // local port to listen on
unsigned int remPort = 8887; // remote port to send to
char rcv; // Receive character (make this an Array for String Rx)
char snd; // Transmit character (make this an Array for String Rx)
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup()
{
// start the Ethernet and UDP:
Serial.begin(9600);
Ethernet.begin(mac,ip);
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
Udp.begin(localPort);
}
void loop()
{
if (RcvPkt())
{
// We just received a UDP packet so Act on that.
if (rcv=='1')
{
//digitalWrite(LEDPin,HIGH);
Serial.println("on");
}
else if (rcv=='0')
{
//digitalWrite(LEDPin,LOW);
Serial.println("off");
}
}
}
int RcvPkt()
{
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(&rcv,1);
Serial.println("Contents:");
Serial.println(rcv);
}
return packetSize;
}
Thank you very much! Hope it is something simple I missed.