Serial read and send to UDP?

Hi guys, 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:

Screenshot 2022-02-18 142440

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

What's the problem?
Do you have an ASCII chart nearby?

1 Like

the serial print side(MEGA2560), printing the analogread to serial3 port and the sender side (ESP8266) reading the serial from the MEGA2560 and send the value to udp.
The problem is that the numbers received from the UDP are incorrect.

We seem to be looking at different code.

1 Like

MEGA2560 side:

void setup()
{
  Serial3.begin(115200);
}

void loop()
{
  int sensorValue = analogRead(A0);
  Serial3.println(sensorValue);
}

Did you get any clues from the ASCII chart?

the Analog output is just numbers. there is no ascii chart

The numbers in the picture look a lot like ASCII decimal digits to me.

Print converts numbers to text and that text is printed.

1 Like

thanks for reply. how can I solve this problem ?

Please help. This is very important for me

Start there

1 Like

but I don't know how to fix this problem !!!

Take another look a reply #12, and think about what Serial.read returns.

1 Like

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 1024, it displays the number 1

But you changed "val" to be a char, right?

1 Like

char val = Serial.read();
Udp.write(val);

OK, time to slow down, and stop and think.

You have no flow control, so your results are going to be muddled.

void loop()
{
  int sensorValue = analogRead(A0);
  Serial3.println(sensorValue);
}

Is spamming Serial3, yet the receiver has got a 10ms delay in it.
You will lose characters.

1 Like

ok I do it. see the result:

18:46:36.770 -> 2
18:46:36.817 -> 0
18:46:36.817 -> 2
18:46:36.817 -> 1
18:46:36.817 -> 1
18:46:36.863 -> 

18:46:36.863 -> 0
18:46:36.863 -> 7
18:46:36.863 -> 

18:46:36.863 -> 0
18:46:36.910 -> 

18:46:36.910 -> 
18:46:36.910 -> 
18:46:36.910 -> 

18:46:36.910 -> 
18:46:36.910 -> 
18:46:36.910 -> 2

Ok the problem solved by the below sketch:

  Serial.readBytesUntil('\n', recv_buf, 9);
  recv_buf[9] = 0;  // make sure it's null terminated
  sscanf(recv_buf, "%d", &recv_int);
  Udp.print(recv_int);