#include "SoftwareSerial.h"
SoftwareSerial esp8266(2, 3); // RX, TX
void setup()
{
Serial.begin(115200); // serial port used for debugging
esp8266.begin(115200); // your ESP's baud rate might be different
}
void loop()
{
if(esp8266.available()) // check if the ESP is sending a message
{
int i=0;
char cmd[5];
while(esp8266.available())
{
char c = esp8266.read(); // read the next character.
cmd[i]=c;
i++;
Serial.write(c); // writes data to the serial monitor
}
Serial.print("Serial1 aval");\
Serial.print(cmd);
if(cmd[0]=='o'&&cmd[1]=='n')
{
Serial.print("On is typed");
digitalWrite(9, HIGH);
}
}
if(Serial.available())
{
delay(10); // wait to let all the input command in the serial buffer
// read the input command in a string
String cmd = "";
while(Serial.available())
{
cmd += (char)Serial.read();
}
// send to the esp8266
esp8266.println(cmd);
}
}
when i type AT in console it is not detected ,also sometimes it displays random characters.
I want a code to receive serial which i will send by connecting to wifi through android app
ie. if i type "on" from mobile an led will turn on.
is the above code wrong ??