Serial communication between arduino and ESP8266

/ Basic serial communication with ESP8266
// Uses serial monitor for communication with ESP8266
//
//  Pins
//  Arduino pin 2 (RX) to ESP8266 TX
//  Arduino pin 3 to voltage divider then to ESP8266 RX
//  Connect GND from the Arduiono to GND on the ESP8266
//  Pull ESP8266 CH_PD HIGH
//
// When a command is entered in to the serial monitor on the computer 
// the Arduino will relay it to the ESP8266
//
 
#include <SoftwareSerial.h>
SoftwareSerial ESPserial(2, 3); // RX | TX
 
void setup() 
{
    Serial.begin(9600);     // communication with the host computer
    //while (!Serial)   { ; }
 
    // Start the software serial for communication with the ESP8266
    ESPserial.begin(9600);  
 
    Serial.println("");
    Serial.println("Remember to to set Both NL & CR in the serial monitor.");
    Serial.println("Ready");
    Serial.println("");    
}
 
void loop() 
{
    // listen for communication from the ESP8266 and then write it to the serial monitor
    if ( ESPserial.available() )   {  Serial.write( ESPserial.read() );  }
 
    // listen for user input and send it to the ESP8266
    if ( Serial.available() )       {  ESPserial.write( Serial.read() );  }
}

I upload this code.but ESP8266 I didn't get any "ok" response after sending "AT" command

try setting EspSerial to 115200.
ESPserial.begin(115200)
I faced the same problem recently and changing the baud rate worked. Although baud rate passed to Serial.begin() is ok.

I tried for 115200 baud rate.still it not working

can you show your circuit diagram?

ok

Thanks. It may be caused by the power supply. It requires 3.3V which you are supplying but most people (even on the arduino tutorial section) suggest that we should use linear regulator. Here see this link for reference -> Arduino Project Hub

Also consider signal level shifting from Arduino to ESP. The ESP is told not to be 5V tolerant and you could fry it when you keep it connected for a long time.
You can use very simple resistor divider.

Edit: BTW AshwiniGunjal, the schematics you posted does not fit your sketch. You should have pins 2 and 3 connected to the ESP. I hope it is a bug in the schematics and not in the real circuit.

bilekj is right about the schematics. I used pin 2 and 3, now it works! (and yes, I used level shifting)

PS defaut baudrate seems to be 115200 baud

If I am using nodemcu, what is the code should I upload to it ?

haskor:
If I am using nodemcu, what is the code should I upload to it ?

The NodeMCU is capable of doing a lot of things and may not require an Arduino.

@haskor, for connecting nodemcu to arduino (or whatever mcu) you need to download an AT firmware to nodemcu.
Search the internet for "8266 AT firmware", you will find how to do it.

Arduino UNO/ Nano Code:
/*

  • bismillah hir rahman nir raheem
  • UNO/Nano = Pin 7 & Pin 8
  • Note: Uno and ESP8266 cross connection
    */
    #include <SoftwareSerial.h>
    SoftwareSerial ArduinoUno(7,8);
    String f;
    void setup(){
    Serial.begin(9600);
    ArduinoUno.begin(115200);
    }
    void loop(){
    float i = (random(100) + 1);
    float j = (random(100) + 1);

f = String('H')+String(i)+String('T')+String(j);
ArduinoUno.println(f);
delay(2000);
}

ESP8266 Code:

/*

  • bismillah hir rahman nir raheem
  • ESP8266 = Pin D7 & Pin D8
  • Note: Uno and ESP8266 cross connection
    */
    #include <SoftwareSerial.h>
    #include <ESP8266WiFi.h>

SoftwareSerial NodeMCU(D7,D8);

void setup(){
Serial.begin(9600);
NodeMCU.begin(115200);
pinMode(D7,INPUT);
pinMode(D8,OUTPUT);
}

void loop(){
String content = "";
char character;

while(NodeMCU.available()) {
character = NodeMCU.read();
content.concat(character);
}
if (content != "") {
Serial.println(content);
}
Serial.println("Temperature");
Serial.println(content.substring(1, 6));
Serial.println("Humidity");
Serial.println(content.substring(7, 12));
delay(2000);
}

1 Like

hello,

i am trying to give IP address of current connect network to the arduino uno from ESP8266(nodemcu).
but i am able to either transmitte or receive at a time. i am getting out put if nodemcu only transmitte data to Uno. but i am not getting output on demand.

Please do the needful

hello, how about comunicating to the esp8266 and it relaying the serial port input to the arduino? or a stand alone atmega328?
i don't know if it's possible, i will try as soon as some parts that I've order arrive. but i will try to comunicate via linux cli using, maybe, the screen command with the esp8266. i will try to use it's Serial port to do whatever i want to do with my stand alone project.
is this possible?

Yes.It is possible, but it will not work relaying on the hardware serial.

Use the hardware Serial for output to serial monitor, and SoftwareSerial for both device to communicate.

You should refer to the basic serial input with start and end earmark, won't work without that.

The Tx/Rx that you see on both is parallel to the USB. You can't use that for output to monitor and at the same time communicate between the 2 devices.