0

I am using an Arduino nano and an ESP8266. I have burnt an Arduino, but when I send a request to the Arduino LED, it takes some time to turn ON.

Here is my code:

#include <SoftwareSerial.h>
SoftwareSerial esp8266(2,3); //Pin 2 & 3 of Arduino as RX and TX. Connect TX and RX of ESP8266 respectively.
#define DEBUG true
#define led_pin 11 //LED is connected to Pin 11 of Arduino
#define LED1 6
#define LED2 7
#define LED3 4
#define LED4 5

void setup()
  {
    pinMode(led_pin, OUTPUT);
    digitalWrite(led_pin, LOW);
     pinMode(LED1,OUTPUT);
 pinMode(LED2,OUTPUT);
 pinMode(LED3,OUTPUT);
 pinMode(LED4,OUTPUT);
    Serial.begin(115200);
    esp8266.begin(115200); //Baud rate for communicating with ESP8266. Your's might be different.
    esp8266Serial("AT+RST\r\n", 5000, DEBUG); // Reset the ESP8266
    esp8266Serial("AT+CWMODE=1\r\n", 5000, DEBUG); //Set station mode Operation
    esp8266Serial("AT+CWJAP=\"shubham\",\"shubh@1\"\r\n", 5000, DEBUG);//Enter your WiFi network's SSID and Password.

    //while(!esp8266.find("OK")) 
    //{
     //}
    esp8266Serial("AT+CIFSR\r\n", 5000, DEBUG);
     esp8266Serial("AT+CIFSR\r\n", 5000, DEBUG);
      esp8266Serial("AT+CIFSR\r\n", 5000, DEBUG);//You will get the IP Address of the ESP8266 from this command. 
    esp8266Serial("AT+CIPMUX=1\r\n", 5000, DEBUG);
    esp8266Serial("AT+CIPSERVER=1,80\r\n", 5000, DEBUG);
  }

void loop()
  {
   String IncomingString="";
 boolean StringReady = false;

 while (esp8266.available()){
   IncomingString=esp8266.readString();
   StringReady= true;
  }

  if (StringReady){
    Serial.println("Received String: " + IncomingString);

  if (IncomingString.indexOf("LED1=ON") != -1) {
    digitalWrite(LED1,HIGH);


   }

  if (IncomingString.indexOf("LED1=OFF") != -1) {
    digitalWrite(LED1,LOW);

   }

     if (IncomingString.indexOf("LED2=ON") != -1) {
    digitalWrite(LED2,HIGH);
   }

  if (IncomingString.indexOf("LED2=OFF") != -1) {
    digitalWrite(LED2,LOW);
   }

     if (IncomingString.indexOf("LED3=ON") != -1) {
    digitalWrite(LED3,HIGH);
   }

  if (IncomingString.indexOf("LED3=OFF") != -1) {
    digitalWrite(LED3,LOW);
   }

  if (IncomingString.indexOf("LED4=ON") != -1) {
    digitalWrite(LED4,HIGH);
   }

  if (IncomingString.indexOf("LED4=OFF") != -1) {
    digitalWrite(LED4,LOW);
   }

  }
 }


String esp8266Serial(String command, const int timeout, boolean debug)
  {
    String response = "";
    esp8266.print(command);
    long int time = millis();
    while ( (time + timeout) / millis())
      {
        while (esp8266.available())
          {
            char c = esp8266.read();
            response += c;
          }
      }
    if (debug)
      {
        Serial.print(response);
      }
    return response;
  }

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.