Arduino Mega - NodeMcu - 2 way communication

Hello,

I am making a kind of home automation in which an arduino mega with relays and sensors connected communicates with a NodeMcu Esp8266. via the NodeMcu and Blynk I can see data on my mobile phone and have assignments executed. everything works except the communication between the NodeMcu and the arduino mega. I have tried I2C but due to the difference in voltage (NodeMcu-3.3V ArduinoMega-5V) will this not work. does anyone know of a communication in both directions that will work on these 2 microcontrollers?

Isaak

Use level shifters to connect 3.3 to 5 volt logic. Thet don't cist much on f ex Ebay.

So with a level shifter it would be possible to send and receive in both directions?
Would this one work : 5V-3V IIC UART SPI 4 Kanal Logic Level Converter Module Bi-Directiona, 0,26 €

Here is tested demo code for 2 way communication between a Mega and ESP8266-12. It should work with a NodeMCU.

Also a schematic for the required level shifter on the Mega TX (5V) to ESP RX (3.3V) line. The Mega RX to ESP TX requires no level shifting. The 3.3V signal from the ESP should be read by the Mega without trouble.

Make sure that line endings is set to Both NL & CR in serial monitor.

The code is modified from Robin2's serial input basics tutorial example 2.

Mega code:

// Example 2 - Receive with an end-marker

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup()
{
   Serial.begin(115200);
   Serial.println("<Arduino is ready>");
   Serial1.begin(19200);
}

void loop()
{
   recvWithEndMarker();
   showNewData();
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();
      Serial1.println("hello from Mr. Mega");
   }
   if (Serial.available())
   {
      Serial1.write(Serial.read());
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (Serial1.available() > 0 && newData == false)
   {
      rc = Serial1.read();

      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void showNewData()
{
   if (newData == true)
   {
      Serial.print("This just in ... ");
      Serial.println(receivedChars);
      newData = false;
   }
}

ESP8266 code:

// Example 2 - Receive with an end-marker

#include <SoftwareSerial.h>

SoftwareSerial ss(4, 5); // RX, TX

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup()
{
   Serial.begin(115200);
   Serial.println("<Arduino is ready>");
   ss.begin(19200);
}

void loop()
{
   recvWithEndMarker();
   showNewData();
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();
      ss.println("hello from ESP8266-12");
   }
   if (Serial.available())
   {
      ss.write(Serial.read());
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (ss.available() > 0 && newData == false)
   {
      rc = ss.read();

      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void showNewData()
{
   if (newData == true)
   {
      Serial.print("This just in ... ");
      Serial.println(receivedChars);
      newData = false;
   }
}

Level shifter:

ESP MEGA SER.jpg

ESP MEGA SER.jpg

You can use SerialTransfer.h to do the heavy work of @groundFungus's suggestion. The lib allows you to reliably transfer data via UART, I2C, or even SPI. It comes with a lot of examples and can be installed using the Arduino IDE's Libraries Manager.