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);
}
}
}
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 theSerialEvent
example, that comes with the Arduino IDE