So I am working on a project that requires me to use UDP communication, so I am trying to familiarize myself with the code for it. I tried running the UDPSendReceiveString example on Arduino and processing, but even without editing the code it is not working for me. When I compile and run the program on the Arduino IDE I get no errors, and when I run and press a button for the processing program I get confirmation that I am sending the packets of information. However, there is never any packet received on my Arduino's end. I have not even changed up the code for either besides commenting out some lines. If anyone has an experience or tips on this it would be much appreciated.
Read and follow this link: How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum
Know that words, poetry never beats facts, technical information.
do you have some networking hardware?
You MUST edit that example to get it work (and I suspect you should understand how UDP works before trying). You need to customise the IP address you want to assign to Arduino, together with the UDP receiving port: they both must be the same in your sending program.
Give us more information about what you're trying to do, together with processing side if you want more specific replies.
What Arduino model are you using and what network hardware?
I am working with a an arduino uno and ethernet shield 2. I am trying to send the message through and ethernet cable on my computer. This is my code for the arduino server:
#include <Ethernet.h>
#include <EthernetUdp.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0xA8, 0x61, 0x0A, 0xAE, 0xA9, 0x91};
IPAddress ip(192, 168, 1, 177);
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() {
// You can use Ethernet.init(pin) to configure the CS pin
Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH Shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit FeatherWing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit FeatherWing Ethernet
// start the Ethernet
Ethernet.begin(mac, ip);
// 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
}
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// start UDP
Udp.begin(localPort);
}
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 packetBuffer
Udp.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
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
delay(10);
}
This is my code for the processing program:
import hypermedia.net.*;
UDP udp; // define the UDP object
void setup() {
udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000
//udp.log( true ); // <-- printout the connection activity
udp.listen( true ); // and wait for incoming message
}
void draw()
{
}
void keyPressed() {
String ip = "192.168.1.177"; // the remote IP address
int port = 8888; // the destination port
udp.send("Hello World", ip, port ); // the message to send
}
void receive( byte[] data ) { // <-- default handler
//void receive( byte[] data, String ip, int port ) { // <-- extended handler
for(int i=0; i < data.length; i++)
print(char(data[i]));
println();
}
I have edited the mac address on the code for my arduino and uncommented the init(10) function for my specific shield. I was doing some research and it said that as long as I'm on a LAN that I don't need to change the IP address but that I just need to make sure that it is unique to the network. I'm really just trying to copy the arduino example and I am not sure why it isn't working.
Network above really means subnet so you must edit the IP address x.y.z.t so that the hardcoded network matches your LAN. You must ensure to hardcode "t" so it is outside the DHCP scope. Or you can use the DHCP client to assign an address to the UDP devices.
Ex: if your PC on the IPV4 LAN has an address of 192.168.1.x then your network is 192.168.1. and your UDP broadcasts and receives must be on this network. With a few edits to your home router, you can restrict your DHCP server assignments to allow your UDP devices a "clean" address space for hardcoding the IP addresses; thus avoiding any possible DHCP conflicts with hardcoded addresses.
and the WebClient example works?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.