Mega2560 can't connect to ESP8266

Hi all,

I am working on IoT project and decided to use next open source project as base: Temperature upload over MQTT using Arduino UNO, ESP8266 and DHT22 sensor | ThingsBoard Community Edition

Above sketch works properly for me on my Arduino uno, but when I change board to MEGA 2560, WiFiEsp lib can’t connect to ESP8266:

[WiFiEsp] Initializing ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Cannot initialize ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] No tag found
WiFi shield not present

Connecting scheme:

Only change I did is:

SoftwareSerial soft(2, 3); // RX, TX
//to
SoftwareSerial soft(19, 18); // RX, TX

so, I am a bit confused, why it does not work.
I tried all RX/TX pins available on Mega 2560, but it does not help.

Has somebody any ideas why it does not work and how it could be fixed?

Thank you for any help.

You have a couple of problems.

First you haven't specified which software serial library you are using.

Second, the UNO pins 2-3 are digital, where as pins 18-19 on the MEGA are hardware serial so you don't need software serial.

You need to use pins that are compatible with the library you are using.

Software Serial Reference with pin outs

AltSoftSerial Reference with pin outs

Mega has 4 hardware Serials. so use one of them. but set the baud rate to 9600 for AT firmware. WiFiEsp library can't catch-up with the AT firmware at 115200 baud

Thank you for help! I've solved the problem by switching pins 19 and 18 to 51,52 based on this doc: Arduino - SoftwareSerial

Why are you using SoftwareSerial at all? You only need that for the UNO because it doesn't have a spare serial port. The Mega has 4. Use one of them instead and you won't have any of these problems and your code will be faster and won't tie up an interrupt.