Arduino UNO not receving data after first UDP packet using ethernet shield 2

I am using an arduino UNO with Ethernet shield 2 and and LCD TFT screen. Half the time when i turn on the device the arduino is receiving data and displaying it, but rest of the time it is receiving the first UDP packet and not receving any packets after that.
I checked the sender usind wireshark and it is working properly and sending udp packets in exact intervels. And I confirmed that it is not a problem with my LCD by trying it to print data to serial ports, if received, still there was not data ión the serial port as well.
I checked if the loop is getting stuck somewhere, but no, it just is not receiving any data.
My confusion is, how can this be a software issue if this is only happening sometimes and otherwise everything is received and working properly.
I am giving below my code used for setup and receiving the data.

Setup.

 //Read Identifier of TFT screen
  uint16_t ID = tft.readID();

  //Open serial communication
  Serial.begin(9600);

  //Print TFT details over Serial port
  Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());

  //Check if TFT device is connected properly
  if(ID == 0x9486) {
    Serial.println(F("Found ILI9486 LCD driver"));
  }
  else {
    Serial.print(F("Unknown LCD driver chip: "));
    Serial.println(ID, HEX);
    Serial.println(F("Double-check wiring"));
    return;
  }

  //Configure CS pin for Ethernet shield
  Ethernet.init(10);
  //Configure MAC and IP Address for Ethernet shield 
  Ethernet.begin(mac, ip);

  //Check for Ethernet hardware and print status to Serial monitor
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware");
    //Do nothing  if no Ethernet hardware found
    while (true) {
      delay(1);
    }
  }

  //Provide a delay for link to be setup
  delay(100);

  //Check if link has been established and print link status to Serial monitor
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  //Configure local port for UDP and open communication
  Udp.begin(localPort);

  //Initializing TFT display
  tft.begin(ID);
  tft.fillScreen(BLACK);
  tft.setRotation(1);

Receive Data

  //Read received UDP Packet size
  uint8_t packetSize = Udp.parsePacket();

  if (packetSize)  {

    //Print Packetsize to Serial monitor
    Serial.print("PacketSize: ");
    Serial.println(packetSize);

    //Print IP address of device to Serial monitor
    Serial.print("remoteIP:  ");
    IPAddress remote = Udp.remoteIP();
    for (int i=0; i < 4; i++) {
      Serial.print(remote[i], DEC);
      if (i < 3) {
        Serial.print(".");
      }
    }

    //Print port number of device to Serial monitor
    Serial.print(", Port ");
    Serial.println(Udp.remotePort());

    //Read the packet into packetBuffer
    Udp.read(packetBuffer, packetSize);

    //Print dataframe to Serial monitor
    Serial.print("Data:  ");
    for(int i = 0; i < 18; i++) { 
      Serial.print(packetBuffer[i], DEC);
      Serial.print("  ");
    }
    Serial.println("\n");

  }

I have verified the hardware connections multiple times.

Please post a compilable example.

How often is a UDP packet transmitted?

/*****************************Include Libraries***************************/
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>

//Assign Colour names to values
#define BLACK   0x0000
#define RED     0xF800
#define WHITE   0xFFFF

//Declare maximum packet size to be received
#define UDP_TX_PACKET_MAX_SIZE 18

//Declare tft type for LCD module
MCUFRIEND_kbv tft;

//MAC and IP address for controller
byte mac[] = {  0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //in my code I use my ethernet shield mac
IPAddress ip(192, 168, 1, 118);

//Declare Local Ethenet port as per incoming message to listen to
unsigned int localPort = 1024;

//Declare Udp type for Ethernet module
EthernetUDP Udp;

//Declare buffers to hold parameter values over time
uint8_t packetBuffer[UDP_TX_PACKET_MAX_SIZE];
        
uint8_t Buffer[80];
uint8_t Prev_Buffer[80];


/****************************Void Setup*****************************/    
void setup() {

  //Read Identifier of TFT screen
  uint16_t ID = tft.readID();

  //Open serial communication
  Serial.begin(9600);

  //Print TFT details over Serial port
  Serial.print("TFT size is "); Serial.print(tft.width()); Serial.print("x"); Serial.println(tft.height());

  //Check if TFT device is connected properly
  if(ID == 0x9486) {
    Serial.println(F("Found ILI9486 LCD driver"));
  }
  else {
    Serial.print(F("Unknown LCD driver chip: "));
    Serial.println(ID, HEX);
    Serial.println(F("Double-check wiring"));
    return;
  }

  //Configure CS pin for Ethernet shield
  Ethernet.init(10);
  //Configure MAC and IP Address for Ethernet shield 
  Ethernet.begin(mac, ip);

  //Check for Ethernet hardware and print status to Serial monitor
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware");
    //Do nothing  if no Ethernet hardware found
    while (true) {
      delay(1);
    }
  }

  //Provide a delay for link to be setup
  delay(100);

  //Check if link has been established and print link status to Serial monitor
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  //Configure local port for UDP and open communication
  Udp.begin(localPort);

  //Initializing TFT display
  tft.begin(ID);
  tft.fillScreen(BLACK);
  tft.setRotation(1);

}

/***********************Void Loop*************************/
void loop() {

  checkEthernetData();
  updateGraph_array();
  plotGraph();

}

void checkEthernetData()  {

  //Read received UDP Packet size
  uint8_t packetSize = Udp.parsePacket();

  if (packetSize)  {

    //Print Packetsize to Serial monitor
    Serial.print("PacketSize: ");
    Serial.println(packetSize);

    //Print IP address of device to Serial monitor
    Serial.print("remoteIP:  ");
    IPAddress remote = Udp.remoteIP();
    for (int i=0; i < 4; i++) {
      Serial.print(remote[i], DEC);
      if (i < 3) {
        Serial.print(".");
      }
    }

    //Print port number of device to Serial monitor
    Serial.print(", Port ");
    Serial.println(Udp.remotePort());

    //Read the packet into packetBuffer
    Udp.read(packetBuffer, packetSize);

    //Print dataframe to Serial monitor
    Serial.print("Data:  ");
    for(int i = 0; i < 18; i++) { 
      Serial.print(packetBuffer[i], DEC);
      Serial.print("  ");
    }
    Serial.println("\n");

  }
}

/***********Update new value in array and shift old values***********/
void updateGraph_array()  {

    for(int i = 0; i < 80; i++) {
    Prev_Buffer[i] = Buffer[i];
  }

  for(int i = 79; i > 0; i--) {
    Buffer[i] = Buffer[i-1];
  }

  Buffer[0] = packetBuffer[17]/4;
}

/**********************Plot graph with array values*****************/
void plotGraph()  {
   
  for(int i = 0; i <79; i++)  {
    tft.drawLine((4*i) + 51, 101 - 1.2*Prev_Buffer[i], (4*i) + 55, 101 - 1.2*Prev_Buffer[i+1], BLACK);
    tft.drawLine((4*i) + 51, 101 - 1.2*Buffer[i], (4*i) + 55, 101 - 1.2*Buffer[i+1], RED);

  }
}

The UDP packet is recieved from the sender in 400-500ms when I checked with wireshark, also the same intervel is shown in my serial print, when my receiver is working properly.

You're printing a fair bit to serial at a very low baud rate which may cause a pause while the buffer empties - try increasing it to 115200.

I'd comment out the last two function calls in loop - see if one of them is causing a problem.

I tried increasing the serial communication baud rate to 115200, but the issue persisted.

But, when i commented the last function (plotgraph), it is working fine.

Therefore, I tried using only one buffer(rather than both prev_buffer and buffer) and with only 10-20 elements, but the only time it is working is when there are less than 10 elements and only one buffer.

Why would this be ?
Also, I have to plot graph of atleast 20 elemets for this project.

What values do you get in setup for the width and height of the LCD?

it prints 240x320.
Also, the LCD display I used is 320x480
@wildbill

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