Thank you Mr.Martin, you were right, a single serial port in esp8266, either connects to serial monitor or connects with arduino. I was doing both. Unplugged the USB cable and powered it independently. It works.
May be it will be useful for reference:
The arduino code I used is:
#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);
}
I am using Arduino mega, so I have 4 serial ports, I used TX1 and RX1.
On the NodeMCU Esp8266, I used the below 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();
}
Your String decoding logic worked in the first go Mr. Martin, Thank you..!!!
With that done, I have one final issue.
My ESP8266 is disconnecting from the network after updating a little data.
Is this a module power supply issue?
It is even taking a long time to connect to wifi, like 15mins..!!!!
I am using a power supply chip (rated for upto 700mA) to supply both arduino and my esp8266..!!!!