Ethernet Communications Between Arduinos on a Switch Only

Hi,

We had a project a while back where we needed two arduinos to be able to communicate over ethernet via a network switch (not a router). It is just Arduino - switch - fibre link - switch - Arduino.

We decided to hire someone to add the thernet side of things to our working code. We had a code that did what we needed to on one Arduino. Read in pots/switches/joysticks, and then output PWM. We just needed the joysticks on one end of the link and pwm output at the other Arduino.

The person we hired has only made it work via a router, and not as requested via a switch. And don't seem to know how to fix it.

Im hoping somebody could have a look at the two sketches attached. Which work if there is a router in the arrangement, but don't if only via a switch.

Ground end (switchs/pots etc)

#include <Adafruit_GFX.h>    
#include <UIPEthernet.h>

uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x06,0x05};
EthernetClient client;

signed long next;
int flag=0 , flag1=0;
#include "Adafruit_ST7735.h" 
#include <SPI.h>


int Arry_to_send[11]={0};


    #define TFT_CS   10
    #define TFT_RST  9  
    #define TFT_DC   8    
    #define TFT_SCLK 13   
    #define TFT_MOSI 11


      int focusPin = A4;    
      int zoomPin = A3;    
      int tiltPin = A2;    
      int panPin = A1;    
      int focusspeedPin = A6;    
      int zoomspeedPin = A7;    
      int tiltspeedPin = A8;    
      int panspeedPin = A5;    
 

 // Digital Pins, more will be added

  int modeLockPin = 21;
  int modeTrackPin = 22; 
  int modeCutPin = 23;   
  int linkledPin = 24;   
  int contledPin = 25;
  int gimledPin = 26;



  int focusVal = 0;    
  int zoomVal = 0;    
  int tiltVal = 0;    
  int panVal = 0;    
  int focusspeedVal = 0;    
  int zoomspeedVal = 0;    
  int tiltspeedVal = 0;    
  int panspeedVal = 0;    
  int modeLockVal = 0;
  int modeTrackVal = 0; 
  int modeCutVal = 0;  

  String  focusVal_s ="";
void setup() {
  Serial.begin(9600);

  pinMode(modeLockPin, INPUT);  
  pinMode(modeTrackPin, INPUT);  
  pinMode(modeCutPin, INPUT);  
  Ethernet.begin(mac);
  Serial.println(Ethernet.dnsServerIP());
}

void loop() 
{
  if (((signed long)(millis() - next)) > 0)
    {
      next = millis() + 5000;
      Serial.println("Client Try connect");
     if (client.connect(IPAddress(192,168,1,6),1000))
        {
          Serial.println("Client connected ");
          while(1)
          {
            if (client.connect(IPAddress(192,168,1,6),1000))
        {
            sensors.requestTemperatures();

  
         focusVal = analogRead(focusPin);    
          zoomVal = analogRead(zoomPin);    
          tiltVal = analogRead(tiltPin);    
           panVal = analogRead(panPin);    
    focusspeedVal = analogRead(focusspeedPin);    
     zoomspeedVal = analogRead(zoomspeedPin);    
     tiltspeedVal = analogRead(tiltspeedPin);    
      panspeedVal = analogRead(panspeedPin);  
  

          modeLockVal = digitalRead(modeLockPin); //need these values in the air end
          modeTrackVal = digitalRead(modeTrackPin);
          modeCutVal = digitalRead(modeCutPin);

Arry_to_send[0]=focusVal;
Arry_to_send[1]=zoomVal;
Arry_to_send[2]=tiltVal;
Arry_to_send[3]=panVal;
Arry_to_send[4]=focusspeedVal;
Arry_to_send[5]=zoomspeedVal;
Arry_to_send[6]=tiltspeedVal;
Arry_to_send[7]=panspeedVal;
//Arry_to_send[8]=modeLockVal;
//Arry_to_send[9]=modeTrackVal;
//Arry_to_send[10]=modeCutVal;

for( int x=0 ; x<8 ;x++)
 {
  client.print(Arry_to_send[x]);
  client.print("\n");
  Serial.println(Arry_to_send[x]);  
 }
 
Serial.println("------------------- new-----------------");
        }
          } 
        }
    }
 
 
}

Couldn't fit this in the first post as well:

Air End (pwm output and serial VISCA output)

#include <SPI.h>
#include <Servo.h>

#include <UIPEthernet.h>
EthernetServer server = EthernetServer(1000);
uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
int array_index=0;

//------------------- used to catch values -------------------
int flag=0;
byte intBuffer[12];
String intData = "";
int delimiter = (int) '\n';
int old=1;
int i=0;

int received_Array[10]={0};


  byte address_set[4]= {0x88, 0x30, 0x01, 0xFF};
  byte if_clear[5]= {0x88, 0x01, 0x00, 0x01, 0xFF};
  byte command_cancel[3]= {0x81,0x21,0xFF};
  byte zoom_teleVar[6]= {0x81, 0x01, 0x04, 0x07, 0x23, 0xFF}; //zoom in 81 01 04 07 2p FF - p=0 (low) to 7 (high)
  byte zoom_wideVar[6]= {0x81, 0x01, 0x04, 0x07, 0x33, 0xFF}; //zoom out 81 01 04 07 3p FF - p=0 (low) to 7 (high)
  byte zoom_stop[6]= {0x81, 0x01, 0x04, 0x07, 0x00, 0xFF}; //stop zoom
  byte auto_focus[6]= {0x81, 0x01, 0x04, 0x38, 0x02, 0xFF}; //auto focus
  const int thisdelay= 250; //time between sets


    Servo panServo;
    Servo tiltServo;
    Servo rollServo;
    Servo panspeedServo;
    Servo tiltspeedServo;
    Servo rollspeedServo;
    Servo modeServo;
    
  


  int focusVal = 0;    
  int zoomVal = 0;    
  int tiltVal = 0;    
  int panVal = 0;    
  int focusspeedVal = 0;    
  int zoomspeedVal = 0;    
  int tiltspeedVal = 0;    
  int panspeedVal = 0;    
  int modeLockVal = 0;
  int modeTrackVal = 0; 
  int modeCutVal = 0;


void setup() {



  Serial.begin(9600); 
 
  for (int i=0; i<4; i++)
  {
  Serial.write(address_set[i]);
  }

  delay(thisdelay);

  //send IF_clear set to block camera
  for (int i=0; i<5; i++){
  Serial.write(if_clear[i]);
  delay(thisdelay);
  }

  
    panServo.attach(31);
    tiltServo.attach(33);
    rollServo.attach(35);
    panspeedServo.attach(39);
    tiltspeedServo.attach(41);
    modeServo.attach(37);
    
    IPAddress myIP(192,168,1,6);
    Ethernet.begin(mac,myIP);
    server.begin();

}

void loop()

{

 if (EthernetClient client = server.available())
    {
      
      while((client.available()) > 0)
       {
          flag=1;
          char ch= client.read();
           if (ch == -1) 
           {
        
           }
          else if (ch == delimiter) 
           {
             break;
           }
           
           else 
            {
               intData += (char) ch;
            }
         }
     
    int intLength = intData.length() + 1;
    intData.toCharArray(intBuffer, intLength);
    intData = "";
    int i = atoi(intBuffer);
    received_Array[array_index]=i;
    array_index++;
    if(array_index >=8)
    {
         array_index=0;
         focusVal = received_Array[0];
          zoomVal = received_Array[1];
          tiltVal = received_Array[2];
           panVal = received_Array[3];  
    focusspeedVal = received_Array[4];   
     zoomspeedVal = received_Array[5];  
     tiltspeedVal = received_Array[6];    
      panspeedVal = received_Array[7]; 

      Serial.print("focusVal:");
      Serial.println(focusVal);
      Serial.print("zoomVal:");
      Serial.println(zoomVal);
      Serial.print("tiltVal:");
      Serial.println(tiltVal);
      Serial.print("panVal:");
      Serial.println( panVal);
      Serial.print("focusspeedVal:");
      Serial.println(focusspeedVal);
      Serial.print("zoomspeedVal:");
      Serial.println(zoomspeedVal);
      Serial.print("tiltspeedVal:");
      Serial.println(tiltspeedVal);
      Serial.print("panspeedVal:");
      Serial.println(panspeedVal);
      Serial.println("------------new--------------------");
      client.stop();
     }

     
    
    }
   

//
//     if (panVal < 700 && panVal > 300 )
//        {
//          panServo.writeMicroseconds(1480); 
//        }
//
//      else
//        {
//          panVal = map(panVal, 0, 1023, 1100, 1900);
//          panServo.writeMicroseconds(panVal); 
//        }
//
//
//      if (tiltVal < 700 && tiltVal > 300 )
//        {
//          tiltServo.writeMicroseconds(1480); 
//        }
//
//      else
//        {
//          tiltVal = map(tiltVal, 0, 1023, 1100, 1900);
//           tiltServo.writeMicroseconds(tiltVal); 
//        }
//
//
//    panspeedVal = map(panspeedVal, 0, 1023, 1100, 1900);
//    tiltspeedVal = map(tiltspeedVal, 0, 1023, 1100, 1900);
//    rollServo.writeMicroseconds(1468);  
//    panspeedServo.writeMicroseconds(panspeedVal);  
//    tiltspeedServo.writeMicroseconds(tiltspeedVal);  
//    rollspeedServo.writeMicroseconds(1500);  
//    
//if(zoomVal>700 )
//{
//delay(thisdelay);
////Send zoom tele set
//for (int i=0; i<6; i++){
//Serial.write(zoom_teleVar[i]);
//}
//}
//
////zoom OUT VISCA Camera
//
//if(zoomVal < 300 ) 
//{
//delay(thisdelay);
//for (int i=0; i<6; i++){
//Serial.write(zoom_wideVar[i]);
//}
//}
////
////zoom stop
//if(zoomVal < 600 && zoomVal > 400 ){
////send auto focus cmd
//for (int i=0; i<6; i++){
//Serial.write(auto_focus[i]);
//}
////send zoom stop
//for (int i=0; i<6; i++){
//Serial.write(zoom_stop[i]);
//}
//}
//}
////
//void sendcommand_cancel(){
//for (int i=0; i<3; i++){
//Serial.write(command_cancel[i]);
//}
//
//// sensors.getTempCByIndex(0) & sensors.getTempCByIndex(1)
//
//    delay(15); 
// 
}

Also forgot to add, it is based on the ethercard libraries with appropriate module, and both ends work running the normal dhcp script. Just currently this code wont work without a router in the equation.

Is there a way of just specifying all of the required IP information and talking between the two without a router/dhcp?

One of those devices (Air End ) is using a static IP. That device is acting as the server.
The other is contacting the first directly, but wants a IP address. Usually that is handled by a DHCP server. That would be the router.

I can't tell from your code if the arduino acting as a server is also a DHCP server. By the looks of it, not!
I'd say that just giving the arduino acting as a client a static IP address as well should do it.

place the following in setup (ground end)

    IPAddress myIP(192,168,1,12); //Use any IP address not already in use, and is on the subnet 192.168.1.0)
    Ethernet.begin(mac,myIP);

replacing the lines

  Ethernet.begin(mac);
  Serial.println(Ethernet.dnsServerIP());

Absolutely amazing, thank you, working like a charm now.

Thank you again.