user82915: 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:

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); 
    }
  }
}


user42015: 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.



82915: <p>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:</p>
<p><a href="https://i.stack.imgur.com/srZXP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/srZXP.png" alt="enter image description here" /></a></p>
<p>can anyone help me please to fix ?</p>
<p>sender sketch:</p>
<pre><code>#include &lt;ESP8266WiFi.h&gt;
#include &lt;WiFiUdp.h&gt;

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

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

void loop() {
  Udp.beginPacket(&quot;10.5.50.23&quot;, localPort);
  int val = Serial.read();
  Udp.print(val);
  Udp.endPacket();
  delay(10);
}
</code></pre>
<p>receiver sketch:</p>
<pre><code>#include &lt;ESP8266WiFi.h&gt;
#include &lt;WiFiUdp.h&gt;

const char* ssid = &quot;MikroTik&quot;;
const char* password = &quot;12345678&quot;;
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(&quot;.&quot;);
  }
  Udp.begin(localPort);
}

void loop() {
  delay(10);
  if (Udp.parsePacket()) {
    int len = Udp.read(packetBuffer, 255);
    if (len &gt; 0) {
      packetBuffer[len] = 0;
      Serial.println(packetBuffer); 
    }
  }
}
</code></pre>
 42015: <p>The problem is with the different interpretations of the binary data and lies in these two lines:</p>
<pre><code>int val = Serial.read();
Udp.print(val);
</code></pre>
<p>What you write into the Serial Monitor is ASCII encoded text. Each byte of data corresponds to one character. If you write an <code>a</code> this is taken as the ASCII representation. In hexadecimal this character is <code>0x61</code> and in decimal <code>97</code>. Thats the actual value that gets transmitted to the ESP over Serial. Then you read that into an integer. SO <code>val</code> would have the value 97. Then you use <code>Udp.print(val)</code>. All <code>print()</code> 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: &quot;Here you have a number (integer). Print it into the UDP packet in human readable (ASCII) form.&quot; So the UDP packet now contains the ASCII string &quot;97&quot;. You probably typed the digit <code>5</code> and then <code>4</code> a few times. Their decimal values are 53 and 52.</p>
<p>There probably also is a function called <code>Udp.write()</code>. The <code>write()</code> functions by convention don't change the data. They just write it without interpretation. So by using <code>Udp.write()</code> the actual binary value of the received data would be send and the letter <code>a</code> would appear as letter <code>a</code> in the Serial output of the receiver.</p>
<p>This works good as long as you only want to send single bytes in your packets. Also note, that <code>Serial.read()</code> will return <code>-1</code> if there is no data to read in the buffer. So to handle that you need to step up the sender code:</p>
<ul>
<li><p>Only read from Serial, if there is actually data in the buffer. You can use an if statement for this like:</p>
<pre><code>  if(Serial.available()){ // read in this if statement
</code></pre>
</li>
<li><p>Read all data from Serial up to a specific delimiter character, which marks the end of a complete message. Typically the newline character <code>\n</code> 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.</p>
</li>
</ul>
