Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
philm001 opened this issue Oct 19, 2023 · 1 comment
Open

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

philm001 opened this issue Oct 19, 2023 · 1 comment
Labels
type: imperfection Perceived defect in any part of project

Comments

@philm001
Copy link

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));
      }
    }
}
@per1234 per1234 added the type: imperfection Perceived defect in any part of project label Oct 19, 2023
@philm001
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: imperfection Perceived defect in any part of project
Projects
None yet
Development

No branches or pull requests

2 participants