Problem sending data from arduino TX to esp8288 RX

Hi all,

I need some help.

My requirement is, I am writing some data on arduino's TX pin using the command

Serial.write(msg);

my msg packet is sprintf(msg, "SMKBXX,SMCXXX,%d,%d,%d,%d",pPconL,pPerL,0,0);

I need to receive this "msg" packet on my nodeMCU esp8266.

I connected the TX of arduino to my nodemcu RX through a voltage divider of 1.2k and 2.4k resistors.

But data is not being received on my nodemcu.

Baudrate for both i have set as 9600.

What can be done..??

Thanks in advance.

On Nodemcu esp8266 i am using the following code:

// Import required libraries
#include "ESP8266WiFi.h"

String msg;

// WiFi parameters
const char* ssid = "lenovo";
const char* password = "password";
void setup(void)
{ 
// Start Serial
Serial.begin(9600);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
Serial.println("I am in loop, now the data part");
msg = Serial.readString();
Serial.println(msg);

}

I noticed that when i write data on my "Serial" port, my TX0 is giving me 2V.
When I write data on "Serial1" port, my TX0 is giving me 5V.

Why is this happening?

For commands as "Serial.write" TX0 gives around 2V.

For commands as "Serial1.write" TX0 gives around 5V.

Which is better to communicate with me esp8266

Serial or Serial1?

I connected the TX of arduino

Which Arduino?

Why is this happening?

You need to hold your mysterious Arduino up closer to the camera. We can't see what you have attached to it, or see how you are measuring the voltage.

Sorry.. It is working now,
the issue was that my esp8266 chip has a single serial port.
So either i communicate with arduino or i use it for serial monitor.

I was doing both, waiting for data on serial monitor, while also sending data from arduino.

Unplugged the serial monitor and now it is working.
For reference, Here's my working code

my arduino code:

#include <SPI.h>        

char msg[1000];
int pPL=3,pPrL=4; 

void setup() {
// initialize serial communications at 9600 bps:
Serial1.begin(9600);
}

void loop() {
sprintf(msg, "MMKBXX,MMCXXX,%d,%d,%d,%d",pPL,pPrL,0,0);
pPerL++;
Serial1.write(msg);
}

And my esp8266 code:

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

char msg[1000];
int i=0;


// WiFi parameters
const char* ssid = "lenovo";
const char* password = "password";

IPAddress remoteIp(192, 168, 43, 55); // address to send UDP packe to
int Port = 7000;
int localPort = 7000; 

WiFiUDP Udp;  // A UDP instance to let us send and receive packets over UDP

void setup(void)
{ 
// Start Serial
Serial.begin(9600);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.println(WiFi.localIP());
Serial.println(localPort);
Udp.begin(localPort);
}

void loop()
{
  
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial.write(inByte);
    msg[i] = (char)inByte;
    if (inByte == '\r' or inByte == '\n' or i > (sizeof(msg) - 1)) {
      msg[i] = 0;
      i = 0;
      Serial.print("echo = ");
      Serial.println(msg);
    }
    else
      i++;
  }
    Udp.beginPacket(remoteIp,Port); //Send Answer to Network     
    Udp.write(msg);
    Udp.endPacket();
}

But I am having stability issues with my esp8266. It disconnects from the network after like a minute.

Any insights to this issue..?