UDPSendReceiveString w/ Processing

Hi all,

Very new to Arduino, trying to set it up for a project I’m working on. I am currently trying to run the UDPSendReceiveString example with Processing running a script on my computer so that I can communicate to my Arduino Uno directly from my laptop. According to the documentation, I should be able to press a letter on the Processing monitor, and I should receive a message from the Arduino. However, this doesn’t seem to be working. My questions are:

  1. Is the Processing monitor that little grey square that pops up when you run the script?
  2. Should I be receiving the text from the Arduino on the Arduino’s serial monitor?
  3. Why am I not receiving anything? How do I test that I’m even sending anything?

Arduino code:

/*
 UDPSendReceiveString:
 This sketch receives UDP message strings, prints them to the serial port
 and sends an "acknowledge" string back to the sender

 A Processing sketch is included at the end of file that can be used to send
 and received messages for testing with a computer.

 created 21 Aug 2010
 by Michael Margolis

 This code is in the public domain.
 */


#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// 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, 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() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);

  Serial.begin(9600);
}

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);

    // 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);
}

Processing code:

 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();
 }

What is the IP of the PC? Windows? How are they connected? Direct or router?

I use Ubuntu and RPi to interface with Arduino UDPSendReceiveString without a hitch.

PC is Windows. Connection is from ethernet shield directly to pc ethernet port. IP is 12.189.101.224.

Then you have two choices.

  1. Change the IP of the Arduino to match the PC
    or better
  2. Change the PC IP to 192.168.1.x/24 (not 192.168.1.177)

I changed my IP to 192.168.1.1/24 on my Processing sketch and Arduino sketch. I still haven't gotten it to work. My interface on the bottom of the Processing IDE is not interactive. Could that be the problem?

When you check the network configuration on your Windows laptop, does it show 192.168.1.1?
Are you running the processing program from a terminal (command prompt)?

Do you mean my IPv4 address? Because it does not…but if I change the IP to the IPv4 address, I don’t get anything useful.

And, I’m running the Processing program from the Processing IDE. I can try running it from the terminal though.

Keeping the IP as 192.168.1.1/24 (not sure why I’m doing this or what it means), if I comment out the draw() function I receive the following console print:
– UDP session started at Tue Jun 06 16:39:10 PDT 2017 –
– bound socket to host:null, port: 6000 –
[17-06-06 16:39:10.864 -0700] close socket < port:6000, address:null >

Yes, the IPv4 address. Change the IP on the laptop to the same localnet as the Arduino.

Insure the PC doesn’t have another conflicting subnet (like wifi adapter). Insure the wifi adapter (if one installed) doesn’t have a 192.168.1.x IP.

Maybe you need to do a bit of reading about networks, specifically IP addresses, netmasks, and localnets.