Go Down

Topic: Serial communication between arduino and ESP8266 (Read 34709 times) previous topic - next topic

AshwiniGunjal

Code: [Select]

/ 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

Tapas08

#1
Mar 15, 2017, 04:38 pm Last Edit: Mar 15, 2017, 04:44 pm by Tapas08
try setting EspSerial to 115200.
Code: [Select]
ESPserial.begin(115200)
I faced the same problem recently and changing the baud rate worked. Although baud rate passed to Serial.begin() is ok.

AshwiniGunjal

I tried for 115200 baud rate.still it not working

Tapas08


AshwiniGunjal


Tapas08

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 -> https://create.arduino.cc/projecthub/ROBINTHOMAS/programming-esp8266-esp-01-with-arduino-011389

bilekj

#6
Mar 21, 2017, 09:39 pm Last Edit: Mar 21, 2017, 09:51 pm by bilekj
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.

BitSmith

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

haskor

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

ieee488

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.

https://roboindia.com/tutorials/nodemcu-arduino

bilekj

@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.

engrmuhammadshoaib14

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);
}

aditya_11

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

talamascool

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?

Go Up