UDP Back & Forth... What am I doing wrong? SOLVED

Hey Everyone! I am trying to use 2 separate Unos w/ ethernet shields to send UDP back and forth. Node 1 needs to recieve command "atRotPos1" from the Node 2 sensor module when the proximity sensor is detected. When using Wireshark on PC I know that each arduino is communicating to the PC (I have it send the same command to multiple devices) but they don't appear to be talking to eachother.

Any ideas? I have tried changing Ips, mac addresses, and everything else I could think of and I could really appreciate the help.

NODE 1 (Receiver Code);

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <DigiPotX9Cxxx.h>
DigiPot pot(2,3,4);

        //NERDY ETHERNET STUFF

char node_id[] = "NODE1 - ROTATION"; //CHANGE THIS FOR DIFFERENT NODE NAMES
EthernetUDP Udp;
byte mac[] = {0xDE, 0xDA, 0xEB, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,4,15); //SET THIS AS THE NODE IP ADDRESS
unsigned int localport = 5001;
IPAddress remoteIP(192,168,4,71); //IP ADDRESS OF LAPTOP VENUE MAGIC
IPAddress serverIP(192,168,4,134); //IP ADDRESS OF SERVER 1 VENUE MAGIC
IPAddress desktopIP(192,168,4,114); //IP ADDRESS OF SERVER 2 VENUE MAGIC
IPAddress node2(192,168,4,16); //IP ADDRESS OF NODE 2 VENUE MAGIC
unsigned int remotePort = 5001;

       //********************

unsigned long currentLedMillis; //a variable to store the time the command was sent so the LED can blink :)
char packet[255];
char reply[] = "Command Recieved [NODE1]"; //CHANGE NODE NAME HERE
int len;
int potLevel;
int motorSpeed;
int startingMotorSpeed;
int rotPos = 0;
int goalRot = 5;
int safety = 1;

    //COMMANDS THAT CAN BE RECIEVED FROM VM

char ESTOP[] = "ESTOP"; //FIRST COMMAND IS ARDUINO LANG, SECOND COMMAND IS VM LANG
char RESETESTOP[] = "RESETESTOP";
char rotClockwise[] = "rotClockwise";
char rotCounterClockwise[] = "rotCounterClockwise";
char rotStop[] = "rotStop";
char rotPos1[] = "rotPos1";
char rotPos2[] = "rotPos2";
char rotPos3[] = "rotPos3";
char rotPos4[] = "rotPos4";

char rotPos1cc[] = "rotPos1cc";
char rotPos2cc[] = "rotPos2cc";
char rotPos3cc[] = "rotPos3cc";
char rotPos4cc[] = "rotPos4cc";
char rotDirClockwise[] = "rotDirClockwise";
char rotDirCounterClockwise[] = "rotDirCounterClockwise";

    //COMMANDS THAT CAN BE RECIEVED FROM OTHER NODES
char atRotPos1[] = "atRotPos1"; //(can be recieved from NODE# - RV)
char atRotPos2[] = "atRotPos2";
char atRotPos3[] = "atRotPos3";
char atRotPos4[] = "atRotPos4";





int greenLed = 14; //Is Motor Spinning light
int redLed = 9; //Error light
int testButton = 6;
int clockwise = 8; //Relay Connection
int counterClockwise = 7;
int estop = 5;



void setup(){
  pinMode(clockwise, OUTPUT);
  pinMode(counterClockwise, OUTPUT);
  digitalWrite(clockwise, LOW);
  digitalWrite(counterClockwise, LOW);
  pot.set(0); //sets speed of motor to 0
  pinMode(testButton, INPUT_PULLUP);
  pinMode(estop, INPUT_PULLUP);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  
  Serial.begin(9600);
  Serial.println("****************");
  Serial.print("You Are Monitoring: ");
  Serial.println(node_id);
  Serial.print("This Node's IP Address is:");
  Ethernet.begin(mac,ip);
  Serial.print(" ");
  Serial.println(Ethernet.localIP());
  Udp.begin(localport);

          //CHECK TO SEE IF ETHERNET IS CONNECTED AND DISPLAY ERROR IF IT ISNT SMH

  if (Ethernet.hardwareStatus() == EthernetNoHardware) { //if no hardware
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
    Serial.println("Ethernet shield was not found. Can't run without hardware.");
      while (true) {
      }
    digitalWrite(redLed, LOW);
  }

}

      //**************************

     
void loop(){

        //CHECK FOR ETHERNET CABLE CONNECTION

  if (Ethernet.linkStatus() == LinkOFF) {
   Serial.println("Ethernet cable is not connected.");
   digitalWrite(redLed, HIGH);
   digitalWrite(greenLed, LOW);
   pot.set(0);
   delay(2000);
  }
  else {
  }

          //IF TEST BUTTON PRESSED
    
  if (digitalRead(testButton) == LOW) { //
    Udp.beginPacket(remoteIP, remotePort);
    Udp.print("Test - ");
    Udp.println(node_id);
    Serial.print("Test - ");
    Serial.println(node_id);
    Udp.endPacket();
    Udp.beginPacket(serverIP, remotePort);
    Udp.print("Test - ");
    Udp.println(node_id);
    Udp.endPacket();
    Udp.beginPacket(desktopIP, remotePort);
    Udp.print("Test - ");
    Udp.println(node_id);
    Udp.endPacket();
        safety = 1;
    digitalWrite(redLed, LOW);
    delay(100);
}

    
  if (digitalRead(estop) == LOW) { //
    pot.set(0);
    digitalWrite(clockwise, LOW);
    digitalWrite(counterClockwise, LOW);
    Udp.beginPacket(remoteIP, remotePort);
    Udp.println("ESTOP");
    Serial.println("Test - ");
    Udp.endPacket();
    Udp.beginPacket(serverIP, remotePort);
    Udp.println("ESTOP");
    Udp.endPacket();
    Udp.beginPacket(desktopIP, remotePort);
    Udp.println("ESTOP");
    Udp.endPacket();
        safety = 0;
    digitalWrite(redLed, HIGH);
    while(digitalRead(estop) == LOW);
}
          //*****************************
    
int packetSize = Udp.parsePacket();

if (packetSize) { // a packet is recieved
 int len = Udp.read(packet, 255);
 if (len > 0) {
  packet[len] = '\0';
 }
 digitalWrite(greenLed, HIGH);
 delay(10);
 digitalWrite(greenLed, LOW);
 Serial.print("Recieved: ");
 Serial.println(packet);
 currentLedMillis = millis();



                     //MOTOR SPEED SETTING COMMANDS

  int potLevel = atoi(packet); //turns the first line of the command into a digit
      if (potLevel > 0) { //if digit is motor command
        pot.set(potLevel);
        Serial.print("Motor Speed = ");
        Serial.print(potLevel);
        Serial.println("%");
        Serial.println("****************");
      }

                     //COMMANDS 

 if (strstr(packet, ESTOP)) {                                   //ESTOP
      pot.set(0);
      digitalWrite(clockwise, LOW);
      digitalWrite(counterClockwise, LOW);
      digitalWrite(redLed, HIGH);
      Serial.println("EMERGENCY STOP RECIEVED");
      safety = 0;  
    } 

 if (strstr(packet, RESETESTOP)) {                                   //ESTOP
      pot.set(0);
      digitalWrite(redLed, LOW);
      Serial.println("E-ESTOP OVERRIDEN, RESUME OPERATIONS");
      safety = 1; 
      digitalWrite(redLed, LOW); 
    } 

 if (strstr(packet, rotClockwise) && safety == 1) {                            //rotate clockwise
      digitalWrite(greenLed, HIGH);
      digitalWrite(counterClockwise, LOW); //High is clockwise.
      digitalWrite(clockwise, HIGH); //High is clockwise.
      delay(5);
      Serial.println("Beginning Clockwise Rotation.");
      

    Udp.beginPacket(remoteIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    Udp.beginPacket(serverIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    Udp.beginPacket(desktopIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    } 

 if (strstr(packet, rotCounterClockwise) && safety ==1) {                    //rotate counterclockwise
      digitalWrite(greenLed, HIGH);
      digitalWrite(clockwise, LOW); //High is clockwise.
      digitalWrite(counterClockwise, HIGH); //High is clockwise.
      delay(5);
      Serial.println("Beginning CounterClockwise Rotation.");

    Udp.beginPacket(remoteIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    Udp.beginPacket(serverIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    Udp.beginPacket(desktopIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    } 

 if (strstr(packet, rotStop) && safety ==1) {                                //stop Rotation
     digitalWrite(clockwise, LOW);
     digitalWrite(counterClockwise, LOW);
      digitalWrite(greenLed, LOW);
      delay(5);
      Serial.println("Stopping rotation.");    
    
    Udp.beginPacket(remoteIP, remotePort);
    Udp.println("notRotating");
    Udp.endPacket();
    Udp.beginPacket(serverIP, remotePort);
    Udp.println("notRotating");
    Udp.endPacket();
    Udp.beginPacket(desktopIP, remotePort);
    Udp.println("notRotating");
    Udp.endPacket();
    } 


            //SENSOR COMMANDS


 if (strstr(packet, atRotPos1) && safety ==1) {                                //at rotation position 1
      delay(5);
      Serial.println("From Sensor Node, at Position 1.");    
      rotPos = 1;
    } 

if (strstr(packet, atRotPos2) && safety ==1) {                                //at rotation position 2
      delay(5);
      Serial.println("From Sensor Node, at Position 2.");    
      rotPos = 2;
    } 

if (strstr(packet, atRotPos3) && safety ==1) {                                //at rotation position 3
      delay(5);
      Serial.println("From Sensor Node, at Position 3.");    
      rotPos = 3;
    } 


if (strstr(packet, atRotPos4) && safety ==1) {                                //at rotation position 4
      delay(5);
      Serial.println("From Sensor Node, at Position 4.");    
      rotPos = 4;
    } 


            //POSITIONAL COMMANDS


 if (strstr(packet, rotPos1) && safety == 1) {                    //rotate to pos1
      digitalWrite(greenLed, HIGH);
      Serial.println("Rotating to position 1...");
      digitalWrite(counterClockwise, LOW);
      digitalWrite(clockwise, HIGH);
      goalRot = 1;
    Udp.beginPacket(remoteIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    Udp.beginPacket(serverIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    Udp.beginPacket(desktopIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    } 

 
 if (strstr(packet, rotPos2) && safety == 1) {                    //rotate to pos2
      digitalWrite(greenLed, HIGH);
      Serial.println("Rotating to position 2...");
      digitalWrite(counterClockwise, LOW);
      digitalWrite(clockwise, HIGH);
      goalRot = 2;
      Udp.beginPacket(remoteIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    Udp.beginPacket(serverIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    Udp.beginPacket(desktopIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    } 
    
  if (strstr(packet, rotPos3) && safety == 1) {                    //rotate to pos3
      digitalWrite(greenLed, HIGH);
      Serial.println("Rotating to position 3...");
      digitalWrite(counterClockwise, LOW);
      digitalWrite(clockwise, HIGH);
      goalRot = 3;
      Udp.beginPacket(remoteIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    Udp.beginPacket(serverIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    Udp.beginPacket(desktopIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    } 

 
 if (strstr(packet, rotPos4) && safety == 1) {                    //rotate to pos4
      digitalWrite(greenLed, HIGH);
      Serial.println("Rotating to position 4...");
      digitalWrite(counterClockwise, LOW);
      digitalWrite(clockwise, HIGH);
      goalRot = 4;
      Udp.beginPacket(remoteIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    Udp.beginPacket(serverIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    Udp.beginPacket(desktopIP, remotePort);
    Udp.println("Rotating");
    Udp.endPacket();
    } 
  


  if (rotPos == goalRot) {
    digitalWrite(counterClockwise, LOW);
    digitalWrite(clockwise, LOW);
    digitalWrite(greenLed, LOW);
      Serial.println("Rotated to position ");
      Serial.print(rotPos);
      goalRot = 5;
    Udp.beginPacket(remoteIP, remotePort);
    Udp.println("notRotating");
    Udp.endPacket();
    Udp.beginPacket(serverIP, remotePort);
    Udp.println("notRotating");
    Udp.endPacket();
    Udp.beginPacket(desktopIP, remotePort);
    Udp.println("notRotating");
    Udp.endPacket();
  }

}

}

NODE 2 (Sender code);

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>


        //NERDY ETHERNET STUFF

char node_id[] = "NODE2 - RV Sense"; //CHANGE THIS FOR DIFFERENT NODE NAMES
EthernetUDP Udp;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //7F-F9-C4-A1-FD-9B //29-8A-85-05-52-4C
IPAddress ip(192,168,4,16); //SET THIS AS THE NODE IP ADDRESS
unsigned int localport = 5001;
IPAddress remoteIP(192,168,4,71); //IP ADDRESS OF LAPTOP VENUE MAGIC
IPAddress serverIP(192,168,4,134); //IP ADDRESS OF SERVER 1 VENUE MAGIC
IPAddress desktopIP(192,168,4,114); //IP ADDRESS OF SERVER 2 VENUE MAGIC
IPAddress node2(192,168,4,15); //IP ADDRESS OF NODE 2 VENUE MAGIC
unsigned int remotePort = 5001;

       //********************

unsigned long currentLedMillis; //a variable to store the time the command was sent so the LED can blink :)
char packet[255];
char reply[] = "Command Recieved [NODE2]"; //CHANGE NODE NAME HERE
int len;
int potLevel;
int motorSpeed;
int startingMotorSpeed;
int rotPos = 0;
int goalRot = 5;
int safety = 1;


char ESTOP[] = "ESTOP"; //FIRST COMMAND IS ARDUINO LANG, SECOND COMMAND IS VM LANG
char RESETESTOP[] = "RESETESTOP";

    //COMMANDS THAT CAN BE RECIEVED FROM VM

int prox1 = 2;
int prox2 = 3;
int prox3 = 5;
int prox4 = 6;
int redLed = 9;
int testButton = 7;


void setup(){
  pinMode(prox1, INPUT_PULLUP);
  pinMode(prox2, INPUT_PULLUP);
  pinMode(prox3, INPUT_PULLUP);
  pinMode(prox4, INPUT_PULLUP);
  pinMode(testButton, INPUT_PULLUP);
  pinMode(redLed, OUTPUT);
  
  Serial.begin(9600);
  Serial.println("****************");
  Serial.print("You Are Monitoring: ");
  Serial.println(node_id);
  Serial.print("This Node's IP Address is:");
  Ethernet.begin(mac,ip);
  Serial.print(" ");
  Serial.println(Ethernet.localIP());
  Udp.begin(localport);

          //CHECK TO SEE IF ETHERNET IS CONNECTED AND DISPLAY ERROR IF IT ISNT SMH

  if (Ethernet.hardwareStatus() == EthernetNoHardware) { //if no hardware
    digitalWrite(redLed, HIGH);
    Serial.println("Ethernet shield was not found. Can't run without hardware.");
      while (true) {
      }
    digitalWrite(redLed, LOW);
  }

}

      //**************************

     
void loop(){

        //CHECK FOR ETHERNET CABLE CONNECTION

  if (Ethernet.linkStatus() == LinkOFF) {
   Serial.println("Ethernet cable is not connected.");
   digitalWrite(redLed, HIGH);
   delay(2000);
  }
  else {
  }

          //IF TEST BUTTON PRESSED
    
  if (digitalRead(testButton) == LOW) { //
    Udp.beginPacket(remoteIP, remotePort);
    Udp.print("Test - ");
    Udp.println(node_id);
    Serial.print("Test - ");
    Serial.println(node_id);
    Udp.endPacket();
    Udp.beginPacket(serverIP, remotePort);
    Udp.print("Test - ");
    Udp.println(node_id);
    Udp.endPacket();
    Udp.beginPacket(desktopIP, remotePort);
    Udp.print("Test - ");
    Udp.println(node_id);
    Udp.endPacket();

  }
     if (digitalRead(prox1) == LOW) { //
    Udp.beginPacket(remoteIP, remotePort);
    Udp.println("atRotPos1");
    Serial.println("atRotPos1");
    Udp.endPacket();
    Udp.beginPacket(serverIP, remotePort);
    Udp.println("atRotPos1");
    Udp.endPacket();
    Udp.beginPacket(desktopIP, remotePort);
    Udp.println("atRotPos1");
    Udp.endPacket();
}
          //*****************************
    
int packetSize = Udp.parsePacket();

if (packetSize) { // a packet is recieved
 int len = Udp.read(packet, 255);
 if (len > 0) {
  packet[len] = '\0';
 }
 Serial.print("Recieved: ");
 Serial.println(packet);
 currentLedMillis = millis();

                     //COMMANDS 

 if (strstr(packet, ESTOP)) {                                   //ESTOP
      digitalWrite(redLed, HIGH);
      Serial.println("EMERGENCY STOP RECIEVED");
    } 

 if (strstr(packet, RESETESTOP)) {                                   //ESTOP
      digitalWrite(redLed, LOW);
      Serial.println("E-ESTOP OVERRIDEN, RESUME OPERATIONS");
    } 









}

}

Thanks everyone :slight_smile:

May we ask for the complete serial output of both nodes for a given time frame?

That's a very bad code for a sketch that should answer UDP messages.

Sorry I don’t fully understand your question, if you are asking about the frequency of serial output for the sender node in normal operation its a message every few seconds or so.

Do you have suggestions to improve this bad code?

Both codes print to the serial interface. Post the output you get for both nodes for lets say 10 seconds.

I don't know what it's used for (there is no comment) so I would say: remove it. It's in the first code only so it seems to be less important. In a code that handles network packets you shouldn't have any such while loop or any delay() call anyway. If you think you need it, use flags for the status information and millis() for the timing. Make your loop as fast as possible.
For the same reason, increase your serial speed dramatically (p.e. 115200).

It doesn't help you if the debugging code (serial output) sends something different than the UDP code ("Test" vs. "ESTOP").

Just remembered what the while loop is for, it makes sure the controller doesnt react to any new UDP messages if the ESTOP (emergency stop) command is recieved.

I fixed that error where the serial was printing something different than the UDP message. Here ;s a video of the problem :slight_smile: IMG_9001.MOV - Google Drive

Might want to try sending to the local broadcast address, for your setup 192.168.4.255..
Sending a packet to this address should be received by all listening to 5001, in your local LAN..
Send once, everyone gets it same time..
keep in mind UDP is not guaranteed and some consideration may need to be taken..

good luck.. ~q

You need some "divide and conquer" methodology. You should roll things back to a simple communications link that works. Then bring in all the other stuff, digipot, applications functions, etc. Otherwise you will keep guessing and poking, is the problem in the comms, or is it in the layers of other stuff you added?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.