Interfacing of ESP8266 with arduino MEGA 2560

I have used ESP8266 wifi module with Arduino UNO for IOT but i want run same program in ARDUINO MEGA 2560 but I i did not get result and did not upload data on server with arduino mega 2560 with esp8266 wifi module..

Please help me why is it not uploading data on server by arduino MEGA 2560.

#include <SoftwareSerial.h>
#include <stdlib.h>

// LED
int ledPin = 13;
// LM35 analog input
int lm35Pin = 0;

\

// replace with your channel's thingspeak API key
String apiKey = "NR8VOW08K766X1PW";

// connect 2 to TX of Serial USB
// connect 3 to RX of serial USB
SoftwareSerial ser(2, 3); // RX, TX

// this runs once
void setup() {
// initialize the digital pin as an output.
pinMode(ledPin, OUTPUT);

// enable debug serial
Serial.begin(9600);
// enable software serial
ser.begin(115200);

// reset ESP8266
ser.println("AT+RST");
}

// the loop
void loop() {

// blink LED on board
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);

// read the value from LM35.
// read 10 values for averaging.
int val = 0;
for(int i = 0; i < 10; i++) {
val += analogRead(lm35Pin);
delay(500);
}

// convert to temp:
// temp value is in 0-1023 range
// LM35 outputs 10mV/degree C. ie, 1 Volt => 100 degrees C
// So Temp = (avg_val/1023)5 Volts * 100 degrees/Volt
float temp = val
50.0f/1023.0f;

// convert to string
char buf[16];
String strTemp = dtostrf(temp, 4, 1, buf);

Serial.println(strTemp);

// TCP connection
String cmd = "AT+CIPSTART="TCP","";
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "",80";
ser.println(cmd);

if(ser.find("Error")){
Serial.println("AT+CIPSTART error");
return;
}

// prepare GET string
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr +="&field1=";
getStr += String(strTemp);
getStr += "\r\n\r\n";

// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
ser.println(cmd);

if(ser.find(">")){
ser.print(getStr);

}
else{
ser.println("AT+CIPCLOSE");
// alert user

}

// thingspeak needs 15 sec delay between updates
delay(16000);
}

Don't use software serial on your mega as you have 3 other hardware serial ports that,will perform much better

Are you sure all is wired and powered correctly? (Explain which esp, connexion pins and power)

SoftwareSerial ser(2, 3); // RX, TX

If you are going to persist in using SoftwareSerial, uselessly, on the Mega, RTFM.

I am trying again and again but not successful to do same work in Mega 2560.
Please send me code of esp8266 for mega2560...
My mail id is: jitendratomar49@gmail.com

jitendratomar:
I am trying again and again but not successful to do same work in Mega 2560.
Please send me code of esp8266 for mega2560...
My mail id is: jitendratomar49@gmail.com

Will you be paying by PayPal or wire transfer?

Come on - no one will do you work here as you don't even show what you have tried, describe how you tried to debug etc...

---> again don't use Software Serial get rid of the

// connect 2 to TX of Serial USB
// connect 3 to RX of serial USB
SoftwareSerial ser(2, 3); // RX, TX

and use Serial2 for example instead of the ser variable. Serial2 is a physical Hardware Serial port (Rx and Tx) working just like Serial (which is on pins 0 and 1)

for example change ser.println("AT+RST"); into Serial2.println("AT+RST"); --> do that everywhere you see the ser variable and of course find in the documentation (do some work) where Serial2 Rx and Tx pins are on a Mega and connect them correctly (possibly with voltage adaptor if you ESP is 3.3V) to your ESP Tx and Rx.

then test and debug...

Thank you so much for helping me finally i have succeed to upload data on sever.

Same problem dear with mega and esp8266. my code working with nano, uno but not working with mega any help.

Your statement is totally non actionable

Post your code within code tags
Describe how you wired and powered elements
Describe what you expect
Describe what you see

Bring a bit of your brain into the conversation