ESP8266 not working with Arduino Mega

Hi everyone,

I am working on a project that should send data from the Arduino to a webserver using the esp8266-01. I used the code below, and when I used the Uno, it worked perfectly; however, I tried using the Mega and it didn't work.

Do I have to use another library, or should I change the pins I use?

Kind regards and thanks in advance,
Alex

#include "SoftwareSerial.h"
String ssid ="SSID";

String password="password";

SoftwareSerial esp(6, 7);// RX, TX

String data;

String server = "example.com"; // www.example.com

String uri = "/esppost.php";// our example is /esppost.php



byte dat [5];

String temp ,hum;

void setup() {





Serial.begin(9600);

esp.begin(9600);
Serial.println("About to reset");
reset();

connectWifi();

}

//reset the esp8266 module

void reset() {

esp.println("AT+RST");

delay(1000);

if(esp.find("OK") ) Serial.println("Module Reset");

}

//connect to your wifi network

void connectWifi() {

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

esp.println(cmd);

delay(4000);

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

Serial.println("Connected!");

}

else {

connectWifi();

Serial.println("Cannot connect to wifi"); }

}





void loop () {



// convert the bit data to string form

hum = 1;

temp= 5;

data = "temperature=" + temp + "&humidity=" + hum;// 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);

String postRequest =

"POST " + uri + " HTTP/1.0\r\n" +

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

"Accept: *" + "/" + "*\r\n" +

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

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

"\r\n" + data;

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

esp.print(sendCmd);

esp.println(postRequest.length() );

delay(500);

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");

}

}}

------SOLVED-------

I just had to use pin 10 as the RX pin.

you have 4 hardware Serial on Mega. why do you use SoftwareSerial?