Ethernet Shield 2 (W5500) on Mega fails on serial communication and interrupt

I have an ethernet shield 2 connected to the orange pip mega 2560 board for a part of a small project.
I have to take readings from five ultrasonic sensors (connected in I2C), one IMU (UM7, connected in serial2) and two encoders (connected by interrupt pins) and need to send these sensor data to the computer using LAN cable through HTTP port.

I connect the ethernet shield to the LAN port using a cross over cable and able to read the ultrasonic sensor values in the client. Unfortunately, I am not able to take the IMU readings and encoder readings. The code used for the task is shown below.

#include <UM7.h>
#include <Wire.h>
#include <SRF10.h>
#include <Ethernet2.h>
#include <SPI.h>

UM7 imu;
volatile int countFirst = 0;
volatile int countSecond = 0;
int lastcountfirst = 0;
int lastcountsecond = 0;
unsigned long nextPrint = 0;
int ultra_1 = 0;
int ultra_2 = 0;
int ultra_3 = 0;
int ultra_4 = 0;
int ultra_5 = 0;
float imu_x = 0.0;
float imu_y = 0.0;
float imu_z = 0.0;

byte mac[] = {
  0xA8, 0x61, 0x0A, 0xAE, 0x3D, 0x96
};
IPAddress ip(169, 254, 233, 30);
EthernetServer server(80);

#define FirstCLK 2  // CLK Pin of the First Encoder, on the interrupt pin 
#define FirstDT 4  // DT pin of the First Encoder
#define SecondCLK 3 // CLK pin of the Second Encoder, on the interrupt pin 
#define SecondDT 5 // DT pin of the Second Encoder

SRF10 srf10[5] = {
  SRF10(0x70, SRF10_CENTIMETERS),
  SRF10(0x71, SRF10_CENTIMETERS),
  SRF10(0x72, SRF10_CENTIMETERS),
  SRF10(0x73, SRF10_CENTIMETERS), 
  SRF10(0x74, SRF10_CENTIMETERS),
};

// Interrupt Service Routine for the First Encoder
void ReadFirst ()
{
  static unsigned long lastInterruptTime = 0;
  unsigned long interruptTime = millis();
  // If interrupts come faster than 20ms, assume it's a bounce and ignore
  if (interruptTime - lastInterruptTime > 20)
  {
    byte First = digitalRead (FirstDT);
    if (First == 0)
      countFirst++;
    else
      countFirst--;
  } 
  // Keep track of when we were here last (no more than every 5ms)
  lastInterruptTime = interruptTime;
}


// ISR for the Second Encoder
void ReadSecond ()
{
  static unsigned long lastInterruptTime = 0;
  unsigned long interruptTime = millis();

  // If interrupts come faster than 5ms, assume it's a bounce and ignore
  if (interruptTime - lastInterruptTime > 20)
  {
    byte Second = digitalRead (SecondDT);
    if (Second == 0)
      countSecond++;
    else
      countSecond--;   
  }
  // Keep track of when we were here last (no more than every 5ms)
  lastInterruptTime = interruptTime;
}


void sendToServer(){
  EthernetClient client = server.available();
  while (client.connected()) {
    if (client.available()) {
        client.print ("Count on First encoder  = ");  
        client.print (countFirst);
        client.print (" ; Count on Second encoder = ");
        client.print (countSecond);
        client.println();
        client.print("Range : ");
        client.print(ultra_1);
        client.print(",");
        client.print(ultra_2);
        client.print(",");
        client.print(ultra_3);
        client.print(",");
        client.print(ultra_4);
        client.print(",");
        client.print(ultra_5);
        client.println();
        client.print("X = ");
        client.print(imu.quat_B/29789.09091);
        client.print("; Y = ");
        client.print(imu.quat_C/29789.09091);
        client.print("; Z = ");
        client.print(imu.quat_D/29789.09091);
        client.println();
        delay(500);
}
  }
  client.stop();
  }


void setup() {
 
 pinMode (FirstCLK, INPUT_PULLUP);
 pinMode (FirstDT, INPUT_PULLUP);
 pinMode (SecondCLK, INPUT_PULLUP);
 pinMode (SecondDT, INPUT_PULLUP);
 attachInterrupt (0, ReadFirst, LOW);   // interrupt 0 is pin 2, interrupt 1 is pin 3
 attachInterrupt (1, ReadSecond, LOW);
 SPI.begin();
 Ethernet.begin(mac, ip);

 Wire.begin();
 SRF10::setInterval(500);
 
 Serial.begin(9600);
 Serial1.begin(57600);
    
 randomSeed(analogRead(0));
 server.begin();
 delay(600);
}

void loop() {

  // Encoder

while (true){
 if ( countFirst != lastcountfirst || countSecond != lastcountsecond)
  {
    Serial.print ("Count on First encoder  = ");  
    Serial.print (countFirst);
    Serial.print (" ; Count on Second encoder = ");
    Serial.print (countSecond);
    Serial.println();
  }
 lastcountfirst = countFirst;
 lastcountsecond = countSecond;
 
// SRF10

    SRF10::update();
    if (millis()> nextPrint){
      Serial.print("Range : ");
      ultra_1 = srf10[0].read();
      ultra_2 = srf10[1].read();
      ultra_3 = srf10[2].read();
      ultra_4 = srf10[3].read();
      ultra_5 = srf10[4].read();
      Serial.print(ultra_1);
      Serial.print(",");
      Serial.print(ultra_2);
      Serial.print(",");
      Serial.print(ultra_3);
      Serial.print(",");
      Serial.print(ultra_4);
      Serial.print(",");
      Serial.print(ultra_5);
      Serial.println();
      nextPrint = millis() + 1000;
    }
 

 //IMU 
if (Serial2.available() > 0) {
    if (imu.encode(Serial2.read())) {  
      Serial.print("X = ");
      imu_x = imu.quat_B/29789.09091;
      imu_y = imu.quat_C/29789.09091;
      imu_z = imu.quat_D/29789.09091;
      Serial.print(imu_x);
      Serial.print("; Y = ");
      Serial.print(imu_y);
      Serial.print("; Z = ");
      Serial.print(imu_z);
      Serial.println();
      delay(500);
      
    }
  }

sendToServer();

 }
}

When I connect the shield and read the sensor data through serial, everything works fine. But…

  • The serial2 stops working when I open the http port on a laptop. It will continue working when I close the http session
  • The interrupt routines are not working, when it works it will immediately clash the http client

Can you suggest what I am doing wrong in this session?

(Note: I am not doing any soldering or anything to enable the interrupt, is it necessary?)

Thank you in advance.

The serial2 stops working when I open the http port on a laptop. It will continue working when I close the http session

Serial2 probably keeps working but it won't be read in your code because the networking code is written rather blocking.
Your code does no HTTP! Just using port 80 doesn't make a HTTP server!

The interrupt routines are not working, when it works it will immediately clash the http client

How do you know that they don't work? A HTTP won't work because you don't do any HTTP in the code.

You have several delay() calls in your code, none of them make sense.

  while (client.connected()) {

That code is incompatible with HTTP (even if the rest would follow the protocol). With that code you block the rest of the sketch while a connection to the PC exists.

"I connect the ethernet shield to the LAN port using a cross over cable and able to read the ultrasonic sensor values in the client."

That kind of indicates that the client is receiving data from the server. With no html code included, the client is probably treating the data that displays as simple text and displaying that.