I am trying to use the ESP8266 as the wifi module based on this tutorial. My connections are as follows:

ESP8266 <-----------------> Arduino Mega
VCC     ------------------> 3.3V (converted 5V from Mega to 3.3V using LD33V)
CH_PD   ------------------> 3.3V
GND     ------------------> GND
RX      ------------------> TX1(Pin 18)
TX      ------------------> RX1(Pin 19)

I have checked the baud rate by issuing the AT commands at 115200 baudrate and the ESP is responding. Below is my code which I am trying to use:

#include "WiFiEsp.h"
#include <PubSubClient.h>

const char* ssid = "MySSID";
const char* password = "MyPassword";
int status = WL_IDLE_STATUS; //wifi radio's status
byte* messageBuff;

const char* mqtt_server = "192.xxx.x.xxx";
//const char* mqtt_topic = "Rpi_Master";
const char* mqtt_username = "username";
const char* mqtt_password = "password";
//client Id
const char* clientID = "A_1";

//Variables for numbers
long randNumber1;
String rn1;
char rn1_char[50];
long randNumber2;
String rn2;
char rn2_char[50];
long randNumber3;
String rn3;
char rn3_char[50];
long randNumber4;
String rn4;
char rn4_char[50];
long randNumber5;
String rn5;
char rn5_char[50];
long randNumber6;
String rn6;
char rn6_char[50];

WiFiEspClient wifiClient;
PubSubClient client(mqtt_server, 1883, wifiClient); //1883 is the listener port for the broker

void callback(char* topic, byte* payload, unsigned int length) {

    Serial.print("Message Received: [");
    Serial.print("Message is:");
    String message = (char *)payload;

    if (!strncmp((char *)payload, "Start", length)) //Start code can be changed to any string value in place of 1
      client.publish("Arduino_A", "OK");


void reconnect() {
  //Loop until we are reconnected
  while (!client.connected())
    Serial.print("Attempting MQTT Connection...");
    //Attempt to Connect
    if (client.connect(clientID, mqtt_username, mqtt_password))
      //Once connected publish an announcement
      client.publish("Arduino_A", "Please send the pass code to send the values");
      //and resubscribe
      client.subscribe("Rpi_Master"); //This name can be changed
      Serial.print("failed, rc = ");
      Serial.println("Trying again in 5 seconds");
      //Wait for 5 seconds before retrying

void setup() {

  //initilize the ESP module
  Serial.println("Attempting to connect to WPA network");
  status = WiFi.begin(ssid, password);
  //if you are not connected stop here
  if (status != WL_CONNECTED)
    Serial.println("Couldn't get a wifi network");
    while (true);
  //if you are connected, print out the info about the connection
    Serial.println("Connected to the network");
  //client.setServer(server, 1883);
  //Allow the hardware to sort itself

void loop() {
  if (!client.connected())

void randomNumberGenerator()
    randNumber1 = random(10,20);
    Serial.println(randNumber1); // print a random number from 0to 299
    rn1 = String(randNumber1);
    rn1.toCharArray(rn1_char, rn1.length() + 1);
    randNumber2 = random(20, 30);// print a random number from 10 to 19
    Serial.println (randNumber2);
    rn2 = String(randNumber2);
    rn2.toCharArray(rn2_char, rn2.length() + 1);
    randNumber3 = random(30,40);
    rn3 = String(randNumber3);
    rn3.toCharArray(rn3_char, rn1.length() + 1);
    randNumber4 = random (40,50);
    rn4 = String(randNumber4);
    rn4.toCharArray(rn4_char, rn4.length() + 1);
    randNumber5 = random (50,60);
    rn5 = String(randNumber5);
    rn5.toCharArray(rn5_char, rn5.length() + 1);
    randNumber6 = random (60,70);
    rn6 = String(randNumber6);
    rn6.toCharArray(rn6_char, rn6.length() + 1);
    client.publish("LC_1", rn1_char);
    client.publish("LC_2", rn2_char);
    client.publish("LC_3", rn3_char);
    client.publish("LC_4", rn4_char);
    client.publish("LC_5", rn5_char);
    client.publish("LC_6", rn6_char);
    client.publish("Arduino_A", "End Transaction");

Currently am using this code to send 6 random values whenever there is a request being sent from the broker over MQTT. I intend to replace this code once the error issue is resolved. The code works fine and the ESP connects to the broker (Raspberry Pi in my case) initially. However after some time I keep getting Timeout Error and the ESP8266 keeps diconnecting from the server.

I read in other forums that ESP8266 will not be handle high baudrates such as 115200 and should be set to 9600. Could this be a reason for the timeout error? Please let me know what changes I need to rectify this issue. Thanks a lot for your time in advance. Attached the screenshot of the error.enter image description here


The old WiFiEsp by Bruno Poltaruri library can't do much with larger data received. The AT firmware sends all the data at once and the serial RX buffer overflows. It works better with 9600 baud which slows down the AT firmware.

It is hard to receive more data over network with AT firmware without UART hardware flow control and Arduino AVR boards don't have flow control and simple esp8266 modules don't have the flow control pins exposed.

My new WiFiEspAT library uses the new passive receive mode implemented in AT firmware 1.7.0 (SDK 3). The new passive receive mode of the AT firmware is not supported for UDP and secure connection (SSL) yet. For this reason in the new library UDP received message size is limited to configured buffer size and secure connection (SSL, https) is not supported.

With my new library over hardware serial high baud rates can be used. For Arduino Mega 500000 baud works good. For example a WebServer serving web files from SD card requires high baud rate to be able to serve multiple files one after other fast enough.

  • Thanks Juraj. Will try it out.
    – Prashant
    Nov 20 '19 at 6:16
  • @Prashant, did the answer help?
    – Juraj
    Dec 25 '19 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.