Troubleshooting Ethernet data transfer? - Still not working! :(

I am using a Nano as a telemetry transmitter and a Mega as a receiver, both with ENC28J60 Ethernet shields.

I had the system working on a “quiet network” but when I moved it to a busy link (shared with a 5MP camera), sometimes, on a hard power-up, it works but sometimes it doesn’t so I am suspecting it is not handling network errors properly.

In troubleshooting the link, I wanted to verify the transmitted bytes and I noticed that my transmit routine (which should be sending 6 bytes every 5 seconds) prints the 6 bytes on the Serial.print but numBytes only prints as 6 the first time through the loop and returns 0 every pass thereafter. I am not sure what, if anything, is being sent by the udp.write command.

This is the udp transmit routine:

void sendData (){

  // data to send 
  DOFDATA dd;
  dd.compass = compass ; // 0 to 359 degrees, 0 to 168$
  dd.pressure = pressure ; //  to 1200 "H2O, 0 to 4B0$
  dd.pitch = pitch ; // -20 to +20 Degrees, ffec to 14$
  
  udp.beginPacket(IPAddress(10,0,199,6),5000);
  int numBytes = udp.write((byte*)&dd, sizeof(dd));
  Serial.println (numBytes);
  udp.endPacket() ;
  
  Serial.print ("Bytes ");
  Serial.println (numBytes);
  
  Serial.print ("Heading ") ;
  Serial.println (compass) ;
  
  Serial.print ("Pressure ") ; 
  Serial.println (pressure) ;

  Serial.print ("Pitch ") ; 
  Serial.println (pitch) ; 
  Serial.println (" ");

  return ;
}

For those who want to see the whole program, this is it:

/*
 * Convert to UDP Ethernet 06/02/2017
  Telemetry Transmit with 10DOF Sensors
 */
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303_U.h>
#include <Adafruit_BMP085_U.h>
#include <Adafruit_L3GD20_U.h>
#include <Adafruit_10DOF.h>
#include <UIPEthernet.h>

EthernetUDP udp;

// Assign a unique ID to the sensors
Adafruit_10DOF                dof   = Adafruit_10DOF();
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
Adafruit_LSM303_Mag_Unified   mag   = Adafruit_LSM303_Mag_Unified(30302);
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

//Define soft variables

int compass = 0 ; // Compass in degrees, default 0 degrees
int pressure = 600 ; // Pressure in Pascals,
int pitch = 0 ; // pitch in degrees
int checksum = 0 ;
int len ;
int numBytes ;
// Timing

int tick = 0 ; // 1 Second clock
unsigned long currentT = 0 ;
unsigned long lastT = 0 ;


struct DOFDATA
{
  uint16_t compass;
  uint16_t pressure;
  uint16_t pitch;
};

float heading ;

//************** SETUP ***********************

void setup() {
  // 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
  }

  uint8_t mac[6] = {0x11,0x22,0x33,0x04,0x05,0x06};
  Ethernet.begin(mac,IPAddress(10,0,199,5));
  Serial.begin(9600);
  
 /* 
  // Initialise the sensors
    Serial.println("initialize sensors"); 

  if(!accel.begin())
  {
    // There was a problem detecting the LSM303 ... check your connections
    Serial.println(F("Ooops, no LSM303 detected ... Check your wiring!"));
    while(1);
  }

  if(!mag.begin())
  {
    // There was a problem detecting the LSM303 ... check your connections 
    Serial.println("Ooops, no LSM303 detected ... Check your wiring!");
    while(1);
  }
    Serial.println("initialized mag");
  
  if(!bmp.begin())
  {
    // There was a problem detecting the BMP180 ... check your connections
    Serial.println("Ooops, no BMP180 detected ... Check your wiring!");
    while(1);
  }
      Serial.println("initialize bmp");

  Serial.println("Tx Started here");
*/
}

//************** LOOP ***********************

void loop() {

// 1 Second timer loop
  (currentT = (millis()/1000)) ;
  if (currentT > lastT) {
    
    (lastT = currentT ) ;
    tick++  ;  
  }
    
  if (tick >= 6) {
   (tick = 0) ;
  }

  switch(tick){

  case 3: {
   upData () ; // update data
//    sensorRead() ; // read sensors - SKIP fror now
    tick++ ;
    break ;
  }

  case 5: {
    sendData () ; //transmit data
    tick++ ;
    break ;
  }
  }  
} 
  
// *********************** Sensor Reads *****************  
void sensorRead()
{
  sensors_event_t accel_event;
  sensors_event_t mag_event;
  sensors_event_t bmp_event;
  sensors_vec_t   orientation;

  // Calculate pitch and roll from the raw accelerometer data
  accel.getEvent(&accel_event);
  if (dof.accelGetOrientation(&accel_event, &orientation))
    {
      // 'orientation' should have valid .roll and .pitch fields

      (pitch =(orientation.pitch));
 
    
   // Get a new sensor event
    sensors_event_t event;
    bmp.getEvent(&event);
 
  // Display the results (barometric pressure is measure in hPa)
    if (event.pressure)
      {
    // Display atmospheric pressure in hPa
    (pressure = (event.pressure));
       }
  else
    {
    Serial.println("Sensor error");
    }
  }
  
  // Calculate the heading using the magnetometer
  mag.getEvent(&mag_event);
  if (dof.magGetOrientation(SENSOR_AXIS_Z, &mag_event, &orientation))
    {
    // 'orientation' should have valid .heading data now

    (compass = (orientation.heading));

    } 

  return ;

}

// ******************* DATA SEND ****************

void sendData (){

  // data to send 
  DOFDATA dd;
  dd.compass = compass ; // 0 to 359 degrees, 0 to 168$
  dd.pressure = pressure ; //  to 1200 "H2O, 0 to 4B0$
  dd.pitch = pitch ; // -20 to +20 Degrees, ffec to 14$
  
  udp.beginPacket(IPAddress(10,0,199,6),5000);
  int numBytes = udp.write((byte*)&dd, sizeof(dd));
  Serial.println (numBytes);
  udp.endPacket() ;
  
  Serial.print ("Bytes ");
  Serial.println (numBytes);
  
  Serial.print ("Heading ") ;
  Serial.println (compass) ;
  
  Serial.print ("Pressure ") ; 
  Serial.println (pressure) ;

  Serial.print ("Pitch ") ; 
  Serial.println (pitch) ; 
  Serial.println (" ");

  return ;
}

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

void upData (){
  // inc. fudged data values

  compass=(compass+10) ;  // inc. compass 0 to 359 degrees, 0 to 168$
  if (compass>360)  {
    compass=0 ;
      }

  pressure  = (pressure +10) ; // inc pressure // 0 to 1200 "H2O, 0 to 4B0$
  if (pressure>1200) {
    pressure = 0 ;
      }

  pitch++ ; // inc pitch // -20 to +20 Degrees, ffec to 14$
  if (pitch>20) {
    pitch= (-20) ;
   }
  return ;
}

Any help would be appreciated!

In most ethernet libraries, udp.beginPacket and udp.endPacket functions return success/fail values.

I changed udp.endPacket to capture the return value and it says the transmission was successful every time but I still don't understand why numBytes is correct only the first time through the tx loop.

  udpFlag = udp.endPacket() ;
  Serial.println (udpFlag) ;

Is it possible you are running out of SRAM?

The compiler says I am using 69% of my program storage space, 74% of dynamic memory, so itr doesn't sound like IK am out of memory.

AFAIK, that dynamic memory value includes only global variables. This works for Uno/Mega, so it should work for the Nano. Call it where you may suspect your code is failing.

int freeRam() {
  extern int __heap_start,*__brkval;
  int v;
  return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int) __brkval);  
}

// then when you want to check your RAM,
Serial.println(freeRam());

I put that in my code with the print freeRam at the end of the upd.write section and it returns 500 each time

Does anybody know where a ping request is handled? Is it on the ENC28J60 or in the Nano? I don't get a response when I ping the Nano address (10.0.199.5).

Last time I checked, the TCP stack is located in the Arduino (Nano/Uno/Mega/etc)

It has to be some kind of "error handling" problem.

To simplify the network, I connected the Tx and Rx directly to a switch, powered everything up again, and it was working again. Step by step, I added the network between the switch and the Rx - worked - added the WiFi radio between the switch and the network - still working - added the 5MP camera - still working. Everything back in the final configuration and it is all working again/now.

I don't know if the problem was a result of an Ethernet error that wasn't properly handled or if it could have been a glitch in the software on one of the Arduinos, maybe from a dirty power-up (when not connected to a computer by the USB).

Whatever it was, it is running now and I have no idea what the glitch was.....

Figured out the cause of the problem so I will start a new thread with a more appropriate title.