# Communication between NodeMCU esp8266 and Arduino Mega 2560

**URL:** <https://forum.arduino.cc/t/communication-between-nodemcu-esp8266-and-arduino-mega-2560/640426>\
**Category:** Programming\
**Created:** [March 4, 2020, 5:59pm UTC](https://forum.arduino.cc/t/communication-between-nodemcu-esp8266-and-arduino-mega-2560/640426 "2020-03-04T17:59:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MiroslavKandra](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@MiroslavKandra](https://forum.arduino.cc/u/MiroslavKandra)\
**Post date:** [March 4, 2020, 5:59pm UTC](https://forum.arduino.cc/t/communication-between-nodemcu-esp8266-and-arduino-mega-2560/640426/1 "2020-03-04T17:59:20Z")

</div>

Hi guys  
I have been trying to set serial communication between NodeMCU ESP8266 and Arduino Mega 2560(clone).  
I found out how to do that on this website: [https://create.arduino.cc/projecthub/pawan-kumar3/serial-communication-between-nodemcu-and-arduino-640819](https://create.arduino.cc/projecthub/pawan-kumar3/serial-communication-between-nodemcu-and-arduino-640819).

But i wanted it in reverse so ESP will send something to arduino and when arduino will receive something, it puts that into some variable and then for example turn on a led.

But I have one trouble. I cannot convert received text from serial into any variable cause it only displays 1 or some wierd characters.  
Can somebody help me with this?

Here is code for ESP8266:

```cpp
void setup() {
  
  Serial.begin(115200);
  
}

void loop() { 

  Serial.println('1');
  delay(1000);
  Serial.println('2');
  delay(1000);

}

```

And here is Arduino code:

```cpp
void setup(){
  pinMode(13, OUTPUT);
 Serial.begin(115200);
 Serial1.begin(115200);
/*while (!Serial1) {
    ; // wait for serial port to connect. Needed for native USB port only
  }*/
}
void loop()
{
  
  if (Serial1.available()) {
    Serial.write(Serial1.read());

  int str = Serial1.read();
  if(str==1){
    digitalWrite(13, HIGH);
    }
    if(str==2){
    digitalWrite(13, LOW);
    }}
    
}

```

On arduino i have wires on RX1, TX1, PMW port 13 with ground and one ground is directly connected to ESP  
On ESP i have only RX connected to TX1 and TX to RX1 and also that ground.

Thanks for any help 🙂

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [March 4, 2020, 6:38pm UTC](https://forum.arduino.cc/t/communication-between-nodemcu-esp8266-and-arduino-mega-2560/640426/2 "2020-03-04T18:38:02Z")

</div>

you send '1' and test 1

---

<div class="post-metadata">

**Author:** ![MiroslavKandra](https://avatars.discourse-cdn.com/v4/letter/m/f04885/32.png) [@MiroslavKandra](https://forum.arduino.cc/u/MiroslavKandra)\
**Post date:** [March 4, 2020, 6:48pm UTC](https://forum.arduino.cc/t/communication-between-nodemcu-esp8266-and-arduino-mega-2560/640426/3 "2020-03-04T18:48:01Z")

</div>

Yeah that is mistake but it is not reason why it is not working....i have tried it different times with "", '', or just in () but still doesnt work..

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:21am UTC](https://forum.arduino.cc/t/communication-between-nodemcu-esp8266-and-arduino-mega-2560/640426/4 "2021-05-06T00:21:37Z")

</div>

Closed on May 6, 2021, 12:21am UTC


