I have used ESP8266 wifi not wok on mega

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 on serial port.

here is my code:

#include "SoftwareSerial.h"
#include <Adafruit_Sensor.h>

String ssid ="redmi";

String password="1234567890";

SoftwareSerial esp(0, 1);// RX, TX TX 2-> Blue 3 -> Green

String data;
//String server = "localhost";
String server = "inventorymanagement007.000webhostapp.com";

String uri = "/check.php";

void setup() {

esp.begin(115200);

Serial.begin(115200);

reset();

connectWifi();

}

//reset the esp8266 module

void reset() {

esp.println("AT+RST");

delay(1000);

}

//connect to your wifi network

void connectWifi() {

String cmd = "AT+CWJAP="" +ssid+"","" + password + """;

esp.println(cmd);

delay(7000);

if(esp.find("OK")) {
Serial.println("Connected!");

}
//
//else {
//
//connectWifi();
//
//Serial.println("Cannot connect to wifi"); }

}

void loop () {

data = "?dept=1&mach=1";// data sent must be under this form //name1=value1&name2=value2.

httppost();

delay(1000);

}

void httppost () {

esp.println("AT+CIPSTART="TCP","" + server + "",80");//start a TCP connection.

if( esp.find("OK")) {

Serial.println("TCP connection ready");

} delay(1000);delay(1500);

String postRequest =

"GET " + uri + data +" HTTP/1.1\r\n" +

"Host: " + server + "\r\n" +

"Accept: text/html" + "\r\n" +

//"Content-Length: " + data.length() + "\r\n" +

//"Content-Type: application/x-www-form-urlencoded\r\n" +

"\r\n";

String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.

esp.print(sendCmd);

esp.println(postRequest.length() );

if(esp.find(">")) { Serial.println("Sending.."); esp.print(postRequest);

if( esp.find("SEND OK")) { Serial.println("Packet sent");

while (esp.available()) {

String tmpResp = esp.readString();

Serial.println(tmpResp);

}

// close the connection

esp.println("AT+CIPCLOSE");

}

}}

Hi and welcome to the forum. Please read How to use the forum and edit you post to match that. Especially the part about code tags and the "what have you tried" etc bit.

shubhampatil09:

SoftwareSerial esp(0, 1);// RX, TX  TX 2-> Blue 3 -> Green

From the SoftwareSerial library documentation:
https://www.arduino.cc/en/Reference/SoftwareSerial

Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).

So you can't use pins 0 and 1 for software serial on the Mega.

Even on the Uno, that is extremely silly. Pins 0 and 1 have hardware serial capabilities so using software serial on them is crazy. Also, pins 0 and 1 are Serial, which you're using for debug output also so I'm surprised this even worked on the Uno.

The Mega has 4 hardware serial ports. You can continue to use 0 and 1 for Serial and use another one for the ESP8266. No need to use SoftwareSerial at all.

Thnk you sir for your help. my problem is now solve by some changes in my program.
Here is my new code

#include "SoftwareSerial.h"
#include <Adafruit_Sensor.h>

String ssid ="redmi";

String password="1234567890";

SoftwareSerial esp(2, 3);// RX, TX TX 2-> Blue 3 -> Green

String data;
//String server = "localhost";
String server = ".000webhostapp.com";

String uri = "/check.php";

void setup() {

esp.begin(115200);

Serial.begin(115200);

MODE3();

connectWifi();

}

//reset the esp8266 module

void MODE3() {

esp.println("AT+CWMODE=3");

delay(2000);

}

//connect to your wifi network

void connectWifi() {

String cmd = "AT+CWJAP="" +ssid+"","" + password + """;

esp.println(cmd);

//delay(7000);

if(esp.find("OK")) {
Serial.println("Connected!");

}
//
//else {
//
//connectWifi();
//
//Serial.println("Cannot connect to wifi"); }

}

void loop () {

data = "?dept=1&mach=1";// data sent must be under this form //name1=value1&name2=value2.

httppost();

delay(1000);

}

void httppost () {

esp.println("AT+CIPSTART="TCP","" + server + "",80");//start a TCP connection.

if( esp.find("OK")) {

Serial.println("TCP connection ready");

} delay(1000);delay(1500);

String postRequest =

"GET " + uri + data +" HTTP/1.1\r\n" +

"Host: " + server + "\r\n" +

"Accept: text/html" + "\r\n" +

//"Content-Length: " + data.length() + "\r\n" +

//"Content-Type: application/x-www-form-urlencoded\r\n" +

"\r\n";

String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.

esp.print(sendCmd);

esp.println(postRequest.length() );

if(esp.find(">")) { Serial.println("Sending.."); esp.print(postRequest);

if( esp.find("SEND OK")) { Serial.println("Packet sent");

while (esp.available()) {

String tmpResp = esp.readString();

Serial.println(tmpResp);

}

// close the connection

esp.println("AT+CIPCLOSE");

}

}}

I'm very surprised that code works on your Mega. I guess you didn't understand the documentation or my advice. You're still using software serial and you're using an RX pin that doesn't support software serial on the Mega.