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.