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?