ESP8266 UPD Server, Serial communication to Arduino

Hello everyone.
I’m trying to set up an UDP server on my ESP8266, that will receive packets to turn ON or OFF an LED on PIN 12 of Arduino.

I’m having trouble at reading ESP8266’s Serial.write(); and turning on the LEDs.
Also, if using SoftwareSerial library, or normal TX RX pins of Arduino, the serial communication is laggy (about 1 second delay), even though when I read the Serial communication directly, the Serial.write is INSTANT. Just when connecting and reading into Arduino it gets laggy.

Here are my codes:

ESP8266 UDP SERVER 115200 Baud Rate (Working):

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

const char* ssid = "xx";
const char* password = "xx";
const char* data1 = "ON";
const char* data2 = "OFF";

WiFiUDP Udp;
unsigned int localUdpPort = 4210;  // local port to listen on
char incomingPacket[255];  // buffer for incoming packets
void setup()
{
  Serial.begin(115200);
  Serial.println();
  Serial.printf("Connecting to %s ", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected");

  Udp.begin(localUdpPort);
}


void loop()
{
  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    // receive incoming UDP packets
    int len = Udp.read(incomingPacket, 255);
    if (len > 0)
    {
      incomingPacket[len] = 0;
    }
    String lol = (incomingPacket);
    if (lol == data1){
      Serial.write("121");
      Serial.println();
    }
    if (lol == data2){
      Serial.write("120");
      Serial.println();
    }

    Udp.endPacket();
  }
}

The ESP8266 UDP server will read the incoming packets (sent through Packet Sender) and if it matches ON or OFF, it will print 121 for ON or 120 for OFF.

Arduino Client code (reading serial but not turning on LED):

void setup() {
Serial.begin(115200);
pinMode(12, OUTPUT);

}

void loop() {
        if (Serial.available() > 0) {
                String lol = Serial.readString();
                Serial.println(lol);
                if (lol == "121"){
                  digitalWrite(12, HIGH);
                }
                if (lol == "120"){
                  digitalWrite(12, LOW);
                }
        }
}

Arduino gets 121 in the Serial Monitor, after about 1 second, but do not turn on or off the LED.
Still, it has about 1 second lag. I’m using UDP for it’s speed, and still, I get laggy.

Any idea what I’m doing wrong?
Thanks.

    String lol = (incomingPacket);
    if (lol == data1){

Too lazy to use strcmp()?

I'm using UDP for it's speed

UDP is NOT particularly fast.

and still, I get laggy.

You are sending a string, a carriage return and a line feed to hold one bit of information.

You are using a time-wasting function to read that data.

Slow, huh? Either live with it, or learn to send and receive serial data properly.

http://forum.arduino.cc/index.php?topic=396450.0

PaulS:    String lol = (incomingPacket);    if (lol == data1){

Too lazy to use strcmp()? UDP is NOT particularly fast. You are sending a string, a carriage return and a line feed to hold one bit of information.

You are using a time-wasting function to read that data.

Slow, huh? Either live with it, or learn to send and receive serial data properly.

http://forum.arduino.cc/index.php?topic=396450.0

Thanks a lot for your reply. I had no idea about strcmp() function, as I'm new into all this, but thank to your reply, now I found some info about it here (StringComparsion), and will use it in my code(s), from now on.

I first wanted to use a web server, but still didn't get to work that well. Then, I found some information about ESP8266 UDP server and while working only with the ESP, the responses are more fast than from the web server. Can you suggest a more faster method to communicate to the ESP8266? I just wish it works almost instant. Now, via UDP I use the PacketSender for Windows to send packets to the ESP8266 and the respons are instant. Only when it has to communicate back to Arduino, there's this lag issue.

As for the Serial Input Basics tutorial, it's great, and I'm reading it now. I will try to use methods used in these two links, and will post an update. Thanks a lot!