ErfanDL
February 18, 2022, 10:56am
1
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:
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
ErfanDL
February 18, 2022, 11:03am
3
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
ErfanDL
February 18, 2022, 11:07am
5
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?
ErfanDL
February 18, 2022, 11:10am
7
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
ErfanDL
February 18, 2022, 11:15am
10
thanks for reply. how can I solve this problem ?
ErfanDL
February 18, 2022, 1:38pm
11
Please help. This is very important for me
ErfanDL
February 18, 2022, 2:25pm
13
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
ErfanDL
February 18, 2022, 2:36pm
15
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
ErfanDL
February 18, 2022, 2:40pm
17
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
ErfanDL
February 18, 2022, 3:17pm
19
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
ErfanDL
February 18, 2022, 3:33pm
20
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);