Ethernet icon indicating copy to clipboard operation
Ethernet copied to clipboard

ESP32 UDP.read() eventually returns byte value of 1

Open philm001 opened this issue 1 year ago • 1 comments

I am experiencing an issue with my Ethernet shield where when I send individual characters of t, e, s, t the last t always returns a value of 1 from udp.read. Then the program hangs for a little bit and then a I can start sending data again.

Now, when I send the text string test, in my char buffer, I instead get tess on the output.

Here is my simple code that I am using to run the program:

#include <EthernetUdp.h>

#define Ethernet_CS_PIN 00
#define MAX_BUFFER_SIZE 30

// Global variables for the Ethernet portion
/// @brief A byte array to store the mac address of the controller
byte mac[] = {0x00, 0xBA, 0xDA, 0xDF, 0xAC, 0x02};

/// @brief The Port for communication
unsigned int localPort = 8888;

/// @brief The instance of the UDP object that is used for communication
EthernetUDP udp;

/// @brief the IP Address
IPAddress ip(192, 168, 1, 178);

char packetBuffer[MAX_BUFFER_SIZE];

bool ethernetHardwareConnected = false;

void setup() {
    /* Needed to turn on the level translators*/
    pinMode(2, OUTPUT);
    digitalWrite(2, HIGH);

    Serial.begin(115200);
    while(!Serial);
    Ethernet.init(Ethernet_CS_PIN);
    Ethernet.begin(mac, ip);

    if (Ethernet.hardwareStatus() == EthernetNoHardware)
        Serial.println(F("Ethernet Hardware not connected"));
    else if (Ethernet.hardwareStatus() == EthernetW5100)
    {
        Serial.println(F("Ethernet connected"));
        udp.begin(localPort);
        ethernetHardwareConnected = true;
    }
}

void loop() {
    if(ethernetHardwareConnected){
      int packetSize = udp.parsePacket();
      if (packetSize)
      {
          Serial.println("Ethernet Command in the loop:");

          while(udp.available()){
            Serial.println("UDP BYtes:");
            Serial.println(udp.read());
          }

          //String test = udp.readString();
          
          //udp.read(packetBuffer, MAX_BUFFER_SIZE);
          //int bytesRead = udp.readBytes(packetBuffer, MAX_BUFFER_SIZE - 1);
          //packetBuffer[bytesRead] = '\0';
          //Serial.println(test);

          // Reset the buffer to 0
          memset(packetBuffer, 0, MAX_BUFFER_SIZE * sizeof(char));
      }
    }
}

philm001 avatar Oct 19 '23 12:10 philm001

Issue solved

I think that there should be update or at least a comment to state that for the ESP32, a value of 8 MHz instead of 14 MHz is best

philm001 avatar Oct 19 '23 17:10 philm001