arduino with esp8266 problem with a button

please helpme!
My project is an Arduino with an esp8266 esp-01 attached, and a button on the Arduino. I want to turn on/off the #13 led , by pressing the button (at pin 2 arduino). I want too, turn on/off by receiveing an order by wifi. This is my code:

#define SSID        "mySSID"
#define PASS        "myPASS"
int led = 13;
const int buttonPin = 2;
int buttonState = 0;
int lastButtonState = 0;
int ledState = 0;
void setup()  
{
  pinMode(led, OUTPUT);
  pinMode(buttonPin, INPUT);
  digitalWrite(led, LOW);
  Serial.begin(9600);
  Serial.println("AT");
  delay(2000);
  Serial.println("AT+RST");
  delay(2000);
  Serial.println("AT+CWMODE=1");
  delay(8000);
  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  Serial.println(cmd);
  delay(13000);
  Serial.println("AT+CIPMUX=1");
  delay(12000);
  Serial.println("AT+CIPSERVER=1,3333");
  delay(3000);
  digitalWrite(led, HIGH);
  delay(300);
  digitalWrite(led, LOW);
  delay(300);
  digitalWrite(led, HIGH);
  delay(300);
  digitalWrite(led, LOW);
  delay(300);
  digitalWrite(led, HIGH);
  delay(300);
  digitalWrite(led, LOW);
  delay(300);
}
void loop() 
{
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH && lastButtonState == LOW) {
    delay(50);
    if (ledState == 0) {    
      digitalWrite(led, HIGH);
      ledState = 1;
    }
    else {
      digitalWrite(led, LOW);
      ledState = 0;
    }
  }
  
  if(Serial.find("led1off")) {
    digitalWrite(led, LOW);
    ledState = 0;
  }
  if(Serial.find("led1on")) {
    digitalWrite(led, HIGH);
    ledState = 1;
  }
  lastButtonState = buttonState;
}

First of all, if i upload only the button code:

void loop() 
{
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH && lastButtonState == LOW) {
    delay(50);
    if (ledState == 0) {    
      digitalWrite(led, HIGH);
      ledState = 1;
    }
    else {
      digitalWrite(led, LOW);
      ledState = 0;
    }
  }
  lastButtonState = buttonState;
}

works perfect. If i upload only the wifi code:

void loop() 
{
  if(Serial.find("led1off")) {
    digitalWrite(led, LOW);
    ledState = 0;
  }
  if(Serial.find("led1on")) {
    digitalWrite(led, HIGH);
    ledState = 1;
  }
}

Works perfect too. But if i upload it all together (first code) it doesn’t works. I need to press a lot to turn on, the order by wifi works aleatory … What happens? How i can fix it?

What did you see the LED do when you tried all the code put together?

the led turns on/off aleatory. I need to press the button ~10 times or send the same order other ~10 times to turn it on/off.

I think there is a problem with your logic. What happens if the button set the ledpin high and the wifi set it low ?

Erni: I think there is a problem with your logic. What happens if the button set the ledpin high and the wifi set it low ?

¿?¿?¿?¿?¿¿? i don't understand.

if the button set the ledpin high, the led turns on and the variable ledstatus is set to 1. And then if the wifi receive the led1off order, the ledpin is set to low and the variable ledstatus is set to 0. Is not correct?

Erni: I think there is a problem with your logic. What happens if the button set the ledpin high and the wifi set it low ?

please, where is the error? it's driving me crazy.

I just saw that if i press the button like a crazy, it reset the arduino :astonished: