ESP8266 Serial Communication with ATMega328P I am isolating the ATMega328P from the Arduino Uno. I have used it for many other projects, so it does work as intended. Also, I have used my ESP8266-01 for other projects and works great (including serial communication).

I am trying to get the ESP8266-01 to make the ATMega328P pin 13 blink an LED. That is all.

When the ESP8266 is hooked to the TX/RX of the arduino board, the program works exactly as intended. I hook it up the exact same way to the ATMega328P and it doesn't work.

I am using baud rate of 9600 for the communication and using a 16MHz crystal for the ATMega328P.







simulate this circuit &ndash; Schematic created using CircuitLab

The controller is getting 3.3v from the supply as well. And pin 13 is supposed to turn on/off depending on the serial value. Below is the code for the ESP8266:

#include &lt;ESP8266WiFi.h&gt;

void setup() {
  Serial.begin(9600);
}

void loop() {
  delay(10000);
  Serial.write("LED ON");
  delay(10000);
  Serial.write("LED OFF");
}


And below is the code for the ATMega328P:

#include &lt;SoftwareSerial.h&gt;

SoftwareSerial esp8266(2,3);

void setup()
{
  Serial.begin(9600);
  esp8266.begin(9600);
  pinMode(13, OUTPUT);
}

void loop()
{
  if(esp8266.available()) 
  {
    String command = "";
    while(esp8266.available())
    {
      command += (char)esp8266.read();
    }

    if(command == "LED ON") {
      digitalWrite(13, HIGH);  
    }

    if(command == "LED OFF") {
      digitalWrite(13, LOW);
    }
  }

  delay(500);
}


As stated, they work great separately, but when I try serial communication with them together, it doesn't work.

P.S. I usually set my ESP8266 baud rate to 115200, but the ATMega328P, I believe, needs to run at 9600 because of the 16MHz clock frequency. Not sure about this part, though.

EDIT:







simulate this circuit
Generally, try to debug the problem.  Evidently either you're not getting data from your esp8266.available() and/or esp8266.read(), or it's not the right data.  Put traces (Serial.println(...)) in, print things out, and find out what's going wrong.



Specifically, I suspect the issue here is that you are relying on all the data being available immediately: you check esp8266.available() which will tell you whether one or more bytes is available.  Then you esp8266.read() until there are no more bytes available.  What if there's a 1ms delay between bytes arriving?  You'll get,perhaps LED O, esp8266.available() will tell you there's no more data (there's not, yet!) and you stop reading.

You could probably get it working by putting a small delay() in:

while(esp8266.available())
{
  command += (char)esp8266.read();
  delay(100);
}


That's a solution (probably) but not a good one.  A better solution would be to make your commands LED_ON\n and LED_OFF\n - and read until you get a \n.