-1

I was trying to send whatever is received from serial by udp, but it doesn't work properly. the receiver side showing number 50 51 53 ..... the result like below screenshot:

enter image description here

can anyone help me please to fix ?

sender sketch:

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char* ssid = "MikroTik";
const char* password = "12345678";
unsigned int localPort = 2390;

WiFiUDP Udp;
void setup() {
  delay(1000);
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Udp.begin(localPort);
}

void loop() {
  Udp.beginPacket("10.5.50.23", localPort);
  int val = Serial.read();
  Udp.print(val);
  Udp.endPacket();
  delay(10);
}

receiver sketch:

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char* ssid = "MikroTik";
const char* password = "12345678";
unsigned int localPort = 2390; 
char packetBuffer[255];
WiFiUDP Udp;

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
  Udp.begin(localPort);
}

void loop() {
  delay(10);
  if (Udp.parsePacket()) {
    int len = Udp.read(packetBuffer, 255);
    if (len > 0) {
      packetBuffer[len] = 0;
      Serial.println(packetBuffer); 
    }
  }
}
5
  • thanks for reply. sorry I'm new to coding arduino can you help me and edit my code to working properly ?
    – ErfanDL
    Feb 18 at 13:02
  • What help do you need exactly? I explained that you should replace Udp.print() by Udp.write(). Further edits depends on what you want the code to do. You did describe that.
    – chrisl
    Feb 18 at 13:52
  • ok I changed to Udp.write(val) but on the receiver side only show first number of serial read side. for example Instead of showing the number 300, it displays the number 3
    – ErfanDL
    Feb 18 at 15:10
  • Thats the part about reading up to a delimiter character. For example you could use Serial.readStringUntil('\n') for this. It is a blocking function, but that should be ok for your case. Sorry, but I won't write the code for you. There are lots of example codes and tutorials on the web about that. You can also look at the SerialEvent example, that comes with the Arduino IDE
    – chrisl
    Feb 18 at 15:30
  • Besides the issues with the code, I hope you understand that UDP is not a "reliable" protocol. It doesn't guarantee that you'll receive everything that's sent, and it won't tell you if something gets dropped.
    – romkey
    Feb 18 at 20:35

1 Answer 1

1

The problem is with the different interpretations of the binary data and lies in these two lines:

int val = Serial.read();
Udp.print(val);

What you write into the Serial Monitor is ASCII encoded text. Each byte of data corresponds to one character. If you write an a this is taken as the ASCII representation. In hexadecimal this character is 0x61 and in decimal 97. Thats the actual value that gets transmitted to the ESP over Serial. Then you read that into an integer. SO val would have the value 97. Then you use Udp.print(val). All print() functions are there for producing a human readable representation of the data, meaning ASCII encoded (Thats a standard Arduino convention). So basically you said here: "Here you have a number (integer). Print it into the UDP packet in human readable (ASCII) form." So the UDP packet now contains the ASCII string "97". You probably typed the digit 5 and then 4 a few times. Their decimal values are 53 and 52.

There probably also is a function called Udp.write(). The write() functions by convention don't change the data. They just write it without interpretation. So by using Udp.write() the actual binary value of the received data would be send and the letter a would appear as letter a in the Serial output of the receiver.

This works good as long as you only want to send single bytes in your packets. Also note, that Serial.read() will return -1 if there is no data to read in the buffer. So to handle that you need to step up the sender code:

  • Only read from Serial, if there is actually data in the buffer. You can use an if statement for this like:

      if(Serial.available()){ // read in this if statement
    
  • Read all data from Serial up to a specific delimiter character, which marks the end of a complete message. Typically the newline character \n is used for that. You can configure the Serial Monitor to use newline as line ending (it then gets appended to the typed text when you press Enter). There are a lot of example codes for this on the web.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.