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