Source Code –


// BY Electronicscoding.com
// To implement Perfect Serial Communication through commands


#define DATA_SIZE 45
char data_buffer[DATA_SIZE];
char *saveptr;

void setup()
{
  pinMode(12, OUTPUT);
  Serial.begin(9600); // Starts the serial communication
  Serial.println("HI Youtube, This is ElectronicsCoding.com");
  Serial.println(" ");
}

void loop()
{
  readdata();
}

void readdata()
{
  memset(data_buffer, 0 , sizeof(data_buffer)); // CLEAR THE BUFFER

  if (Serial.available() > 0) // CHECKING FOR SERIAL AVAILABLE
  {
    Serial.readBytes(data_buffer, DATA_SIZE); // READING THE BYTES
    String command = strtok_r(data_buffer, ",", &saveptr); 
    char* errorCheck;

    if (command == "LED")
    {
      long value = strtol(strtok_r(NULL, ",", &saveptr), &errorCheck, 10);

      if (value == 1)
      {
        digitalWrite(12, HIGH);
        Serial.println("LED ON");
        Serial.println(" ");
      }
      else if (value == 0)
      {
        digitalWrite(12, LOW);
        Serial.println("LED OFF");
        Serial.println(" ");
      }

    }

    else if (command == "HI")
    {
      Serial.println("HI Youtube, Have a nice Day!");
      Serial.println(" ");
    }
  }
}

Thanks πŸ™‚ Download the Android App to get new updates !