0

I am trying to use the ESP8266 as the wifi module. 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;

//MQTT
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(topic);
    Serial.println("]");
    Serial.print("Message is:");
    String message = (char *)payload;
    Serial.println(message);

    Serial.println();
    if (!strncmp((char *)payload, "Start", length)) //Start code can be changed to any string value in place of 1
    {
      randomNumberGenerator();
      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))
    {
      Serial.println("connected");
      //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
    }
    else
    {
      Serial.print("failed, rc = ");
      Serial.print(client.state());
      Serial.println("Trying again in 5 seconds");
      //Wait for 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);

  //initilize the ESP module
  WiFi.init(&Serial1);
  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
  else
  {
    Serial.println("Connected to the network");
  }
  //client.setServer(server, 1883);
  client.setCallback(callback);
  //Allow the hardware to sort itself
  delay(1500);
  randomSeed(25);
}

void loop() {
  if (!client.connected())
  {
    reconnect();
  }
  //delay(1000);
  client.loop();
  delay(50);
}

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);
    Serial.println(randNumber3);
    rn3 = String(randNumber3);
    rn3.toCharArray(rn3_char, rn1.length() + 1);
    randNumber4 = random (40,50);
    Serial.println(randNumber4);
    rn4 = String(randNumber4);
    rn4.toCharArray(rn4_char, rn4.length() + 1);
    randNumber5 = random (50,60);
    Serial.println(randNumber5);
    rn5 = String(randNumber5);
    rn5.toCharArray(rn5_char, rn5.length() + 1);
    randNumber6 = random (60,70);
    Serial.println(randNumber6);
    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. However, I keep getting Timeout Error and the ESP8266 keeps disconnecting from the broker.

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

  • use 9600 baud over hardware Serial too. it slows down the data sent by AT firmware so the WiFiEsp library can handle it. or see if you can use my new WiFiEspAT library. it can run at 500000 baud github.com/bportaluri/WiFiEsp/issues/184 – Juraj Nov 29 '19 at 8:30

Your Answer

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

Browse other questions tagged or ask your own question.