Skip to content

What does udp.write(buffer, sizeof(buffer)); actually writes ? How to send string through UDP ? #3514

@GeorgeFlorian

Description

@GeorgeFlorian

Board: ESP32-EVB
OS: Linux Mint 19.1

I would like to know if I am sending a packet through UDP correctly.

if(digitalRead(TRIGGER_PIN) == LOW) {
  logOutput("Trigger sent");
  delay(1000);
  uint8_t buffer[19] = "statechange,201,1\r";
  // send packet to server
  if (Server_IP.length()!=0) {
    udp.beginPacket(Server_IP.c_str(), Server_Port.toInt());
    udp.write(buffer, sizeof(buffer)-1);
    delay(30);
    Serial.println(udp.endPacket());
    memset(buffer, 0, 19);
  } else {
    logOutput("ERROR ! No IP for the Server was found. Please enter Server's IP !");
  }
}

This outputs:

Trigger sent
1

Trying to print buffer using

void foo(uint8_t *buff, int length) {
  for(int i=0; i<length;i++) {
    Serial.print(buff[i]);
    Serial.print(" ");
  }
  Serial.println();
}

returns each character's correct ASCII code.

Trying to print buffer using

void foo(uint8_t *buff, int length) {
  for(int i=0; i<length;i++) {
    printf("%c ", buff[i]);
  }
  Serial.println();
}

returns ␀ ␀ ␀ ␀ ␀ ␀ ␀ ␀ ␀ ␀ ␀ ␀ ␀ ␀ ␀ ␀ ␀ ␀ s t a t e c h a n g

So this begs the question: what do I send through UDP ? Do I send the string as it is ? Or what ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: StaleIssue is stale stage (outdated/stuck)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions