ESP8266 UART to ATMega 2560

Good day,

I have an Arduino MEGA 2560 With WiFi and "built in"/"on board" ESP8266EX. The only link I could find that remotely describes this board is at this link.

I can upload sketches to the ESP8266EX and the Mega seperately with no issues using the DIP switches. However, I cannot seem to get the ESP8266EX to "talk" to the Mega via one of the UARTS. I can TX and RX (WiFi) between other ESP8266 modules (WeMos D1) with no problem.

So, at the moment I have a WeMos D1 with an HC-SR505 P.I.R motion sensor hooked up to it. When movement is sensed it turns on a local L.E.D (on the WeMos D1) and then sends "01" (via WiFi) to the Mega ESP8266EX module which in turn turns on a local L.E.D on pin 12 (GPI012) of the ESP8266EX board. This all works fine.

However, when the "built in"/"on-board" ESP8266EX module receives the "01" from the WeMos D1, I want it to tell the Mega that it had received a "01" by means of serial communication.

At the moment the ESP8266EX code looks like this:

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
#include <SPI.h>

int motionActivityLED = 12;

const char* ssid = "ssid";
const char* pass = "pass";
unsigned int localPort = 2390;
char packetBuffer[255];

WiFiUDP Udp;

void setup()
{
  Serial.begin(9600);
  Serial.println();
  pinMode(motionActivityLED, OUTPUT);
  //Disconnect previous WiFi configurations
  WiFi.disconnect();

  IPAddress ip(10,0,0,43);
  IPAddress gateway(10,0,0,1);
  IPAddress subnet(255,255,255,0);
  IPAddress dns(10,0,0,1);

  WiFi.config(ip, gateway, subnet,dns);
  WiFi.begin(ssid, pass);
  WiFi.mode(WIFI_STA);

  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());

  Udp.begin(localPort);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();

  if (packetSize)
    {
      // read the packet into packetBuffer
      int len = Udp.read(packetBuffer, 255);
      
      if (len > 0)
        {
          packetBuffer[len] = 0;
        }
        
    Serial.println(packetBuffer);

    if (strcmp(packetBuffer,"01")==0)              //Motion is detected
      {
        digitalWrite(motionActivityLED, HIGH);
        Serial.print("01");                                 //Send this to the UART
      }
     else if (strcmp(packetBuffer,"02")==0)      //No motion detected
      {
        digitalWrite(motionActivityLED, LOW);
        Serial.print("02");                                 //Send this to the UART
      }

//     ATMega2560.write(packetBuffer);
  }
}

And then on the Mega I have the following code to receive the incoming communication

char RXIncoming;
int RXLed = 12;

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

void loop() {
    RXIncoming = Serial.read();
    Serial.println(RXIncoming);
    if ( RXIncoming == "01" )
      {
        digitalWrite(RXLed, HIGH);
        Serial3.println(RXIncoming);
      }
     else if ( RXIncoming == "02")
      {
        digitalWrite(RXLed, LOW);
        Serial3.println(RXIncoming);
      }

}

At the moment it seems as if either the ESP8266EX is not sending or the Mega is not receiving.

Thank you

the designer and manufacturer is Robotdyn
https://robotdyn.com/mega-wifi-r3-atmega2560-esp8266-flash-32mb-usb-ttl-ch340g-micro-usb.html

Thank you ...

Juraj:
the designer and manufacturer is Robotdyn
https://robotdyn.com/mega-wifi-r3-atmega2560-esp8266-flash-32mb-usb-ttl-ch340g-micro-usb.html

That's a useful link.

It seems that communication between the Mega and the ESP8266 uses Serial3 on the Mega.

In the Instructables page there is a diagram of a separate ESP8266 using Serial1 and no mention that the Mega with combined ESP8266 uses Serial3

...R

You may follow the tutorial of this link and make your ESP8266/NodeMCU and MEGA communicating first.

Robin2:
That's a useful link.

It seems that communication between the Mega and the ESP8266 uses Serial3 on the Mega.

In the Instructables page there is a diagram of a separate ESP8266 using Serial1 and no mention that the Mega with combined ESP8266 uses Serial3

...R

I post this link here on forum at least once a month over last two years :slight_smile:
it is a popular board, sold everywhere with links and docs.

Thank you for the replies,

Just a quick question regarding the Serial Monitor. Just to orientate myself and maybe for the benefit of others. Does ALL serial activity get displayed in the Serial Monitor? or only Serial activity connected to the USB port (connected to the PC)? Reason I am asking is because in the sketch below only Serial.begin(9600); seems to be doing anything (during setup()) in the Serial Monitor and only Serial3 seems to be doing anything in loop();

void setup() {
  Serial.begin(9600);
  Serial.println("Serial_0 Begin Setup ...");
  delay(1000);
  Serial1.begin(9600);
  Serial1.println("Serial_1 Begin Setup ...");
  delay(1000);
  Serial2.begin(9600);
  Serial2.println("Serial_2 Begin Setup ...");
  delay(1000);
  Serial3.begin(9600);
  Serial3.print("Serial_3 Begin Setup ...");
  delay(1000);
}

void loop() {
   while(Serial.available())
    {
      for (int a=0;a < 5;a++)
        {
          Serial.print("Serial_0 = ");
          Serial.println(a);
        }
     }
     
   while(Serial1.available())
    {
      for (int b=0;b < 5;b++)
        {
          Serial.print("Serial_1 = ");
          Serial.println(b);
        }
     }

   while(Serial2.available())
    {
      for (int c=0;c < 5;c++)
        {
          Serial.print("Serial_2 = ");
          Serial.println(c);
        }
     }


   while(Serial3.available())
    {
      for (int d=0;d < 5;d++)
        {
          Serial.print("Serial_3 = ");
          Serial.println(d);
        }
     }
}

And the output I get is the following:

Se⸮Serial_0 Begin Setup ...
Serial_0 Begin Setup ...
Se⸮Serial_0 Begin Setup ...
Serial_3 = 0
Serial_3 = 1
Serial_3 = 2
Serial_3 = 3
Serial_3 = 4
Serial_3 = 0
Serial_3 = 1
Serial_3 = 2
Serial_3 = 3
Serial_3 = 4

johnnypuke:
Does ALL serial activity get displayed in the Serial Monitor?

The Mega has 4 serial ports named Serial, Serial1, Serial2 and Serial3.

Serial is connected to pins 0 and 1 (like on an Uno) and to the PC via the USB cable.

The other serial ports are not joined in any way to the regular USB connection and their output does not go to the PC.

If you do want to connect any of the other serial ports to your PC you could do so with a separate USB-TTL cable.

...R