Arduino Mega 2560 + WIFI

Hello, i've bought from AliExpress Arduino Mega 2560 (32M) with built-in ESP8266. Flashed it with
AiThinker_ESP8266_DIO_8M_8M_20160615_V1.5.4 (dont know why 32M version doesnt want to be uploaded). Opened Arduino IDE and used this sketch. The sketch is working, but mqtt stays connected for several seconds and then it reconnects ( withmessage : [WiFiEsp] Disconnecting 3
) . also received messages are not displayed in serial, but the sketch is publishing OK.

is it just too much for esp or what? I am trying to make a wifi mqtt controlled vehicle and it surprises me it cant handle it?

it's also my first time working with Mega 2560 + wifi, so it might be my fault...
but with normal Nodemcu V3 mqtt work flawless...

#include "WiFiEsp.h"

#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX

#include <PubSubClient.h>

char* wifi_ssid = "**";
char* wifi_password = "**";

char* mqtt_server = "**";
int mqtt_port = 1883;
char* mqtt_clientID = "ESP8266_1503489186_1";
char* mqtt_username = "**";
char* mqtt_password = "**";

char* mqtt_publish_topic = "test";
char* mqtt_subscribe_topic = "kosilnica/pogon";

WiFiEspClient espClient;
PubSubClient client(espClient);
int status = WL_IDLE_STATUS;     // the Wifi radio's status

void setup() {
  client.setServer(mqtt_server, mqtt_port);

void setup_wifi() {

  // initialize serial for ESP module
  // initialize ESP module
  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");

  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {

  Serial.println("WiFi connected");
  Serial.println("IP address: ");

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(mqtt_clientID,mqtt_username,mqtt_password)) {

      Serial.println("Subscribed to test");

    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

void loop() {
  if (!client.connected()) {
    //client.publish("test", "test"); 
    //Serial.println("Publishing test");

void callback(char* topic, byte* payload, unsigned int length) {
  payload[length] = '\0'; // Null terminator used to terminate the char array
  String message = (char*)payload;
  String strTopic = String((char*)topic);




The ESP is more capable than many Arduino boards.
I have never used this board. (In fact I have never heard of it). My favorite project board is the Wemos D1 Mini.

So, reconnect() is called every few minutes?

Is this the only MQTT device you have? The Client ID has to be unique on the network or else you will get this behavior.

Well, this sketch is just a basic MQTT connect, subsrcibe and i've tested same basic functionality with nodemcu board and it's working without a problem .
it's wierd also,because Mega 2560 doesnt even write serial after message is received ( and it should ). i've read online that if you put a delay(100) after client.loop(), it should help.. and it does, but it only show first xx messages and then it reconnects again.. I didnt measure time between reconnects, but yeah, it could be a minute and it seems that these disconnects are repeating constantly,

Well for this project i actually need lots of pins, many sensors and motor :slight_smile: so i wanted something big like Mega 2560...

yes, this device has unique client ID.

It keeps disconnecting every 30 second...


Use a port expander.

AT firmware 1.7+ with my WiFiEspAT library over hardware Serial will be much more stable,

thank you Juraj!
yeah, i can't make current setup to work... it's driving me nuts.
Since i am just a beginner - i flash AT firmware 1.7 the same way as I flashed AiThinker fw (with ESP flasher)? do you have any simple sketch example for this setup?

the library (WiFiEspAT) has examples. install the library in IDE Library Manager and then the examples will be in the IDE Examples menu

thanks, will try it out! :slight_smile: