Hello,
I am trying to create a Project where an Arduino receives UDP Messages with Values for RGB LEDs and then turns the LED's on.
I have the problem that the connection seems to be not stable, or takes very long to establish. Is this normal?
I am using the following code:
#include <EthernetUdp.h>
#include <EthernetServer.h>
#include <EthernetClient.h>
#include <Ethernet.h>
#include <Dns.h>
#include <Dhcp.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 178, 3);
unsigned int localPort = 8888; // local port to listen on
// 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
EthernetUDP Udp;
void setup()
{
// start the Ethernet and UDP:
Ethernet.begin(mac, ip);
Udp.begin(localPort);
Serial.begin(9600);
Serial.println("begin");
}
void loop()
{
// 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(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
SetColor(atoi(packetBuffer));
// send a reply to the IP address and port that sent us the packet we received
//Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
//Udp.write(ReplyBuffer);
//Udp.endPacket();
}
//Serial.println(packetBuffer);
delay(8);
}
void SetColor(int value)
{
int red;
int green;
int blue;
int buffer, buffer2;
blue = value / 1000000;
green = value / 1000 - blue * 1000;
red = value - green * 1000 - blue * 1000000;
Serial.print("red: ");
Serial.println(red);
Serial.print("green: ");
Serial.println(green);
Serial.print("blue: ");
Serial.println(blue);
Serial.print("raw: ");
Serial.println(value);
}
I am very thankful for every help to improve the connection
It looks ok. What device are you using to send the Arduino packets? What is the network settings on that device?
I tried two Network devices, one was my Laptop with "Packet Sender" that was sending UDP Packets every Second. The second device was my smart home miniserver from Loxone that was sending Messages every second too. But not simultaneously.
All Devices are connected via LAN cable to a Gig Switch.
On the Arduino side i have an Arduino UNO R3 with an Ethernet board on top with a W5100 Chip.
What Network settings are of interest at this point?
Can it be that the serial Monitor might interfere with the communication to the ethernet board?
For me it seems that the Arduino does not get recognized by the switch.
The switch then does not route the message to the arduino because he doesnt know its there.
Can this be?
crouser7:
For me it seems that the Arduino does not get recognized by the switch.
The switch then does not route the message to the arduino because he doesnt know its there.
Can this be?
Could be. TP-Link devices are the ones that cause the most problems.
The laptop computer has a 192.168.178.x IP address?
edit: The serial monitor should not be a problem. I use a 115200 baud rate on all my sketches just because testing TCP stuff with a 9600 baud rate makes displaying a web page like Google's take forever.
Yes I have a TP-Link Proffesional Grade Switch.
Yes the Laptop and all other devices are in the same Subnet Mask.
I found out that i actually just have to wait long enough, sometimes like 5 minutes.
Since it will be a fixed installation it will be no problem but for development and so on it sucks.
Maybe you can have your Arduino send a dummy UDP packet in setup. Haven't tried it with the TP-Link switch.
What would happen if i send it to the arduinos ip and port. wouldn't it then go first through the switch or would the W5100 recognize that and not transmit it to the switch?
Send a packet to a dummy localnet IP. It will contact the switch with the packet send request.
I tried it, but that does not seem to have any effect, I acutally send on every second. the TX led on the board is blinking, the status leds go on for a moment and then the link led blinks and all go off again.
I can see the send UDP message not before the previous mentioned waiting time. after that i can see it nice in wireshark
Then I guess you have a choice:
- Wait the required 5 minutes
or
- Get a new switch.
Yes that's what i see as well. I will get a new Switch in future, but for now i am going to stick with this one.
But thank you very much for your support.
By the way i have my programm running as desired.
But this was a first test now it will get interesting.
I am planning on connecting an ADAU1442 via I2C to the Arduino. The Arduino then will programm the DSP Chip and my smart home then will send UDP Messages to the Arduino to set Volume and routing of the Audio Signals.
This is going to be an Multiroom Audio System DIY 