3

I am using ESP8266 with arduino using WiFiEsp library.I want to make MQTT connection with arduino so I use PubSubclient library.i got error:

Attempting MQTT connection...failed, rc=-2 try again in 5 seconds

my code is:

#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <WiFiEspUdp.h>
#include "SoftwareSerial.h"
#include <PubSubClient.h>

IPAddress server(212, 72, 74, 21);
char ssid[] = "atmel";           // your network SSID (name)
char pass[] = "bets56789";           // your network password
int status = WL_IDLE_STATUS;   // the Wifi radio's status

// Initialize the Ethernet client object
WiFiEspClient espClient;

PubSubClient client(espClient);

SoftwareSerial soft(2,3); // RX, TX
void setup() {
// initialize serial for debugging
Serial.begin(9600);
// initialize serial for ESP module
soft.begin(115200);
// initialize ESP module
 WiFi.init(&soft);

// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}

// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}

// you're connected now, so print out the data
Serial.println("You're connected to the network");
//delay(2000);
//connect to MQTT server
client.setServer(server, 1883);
client.setCallback(callback);
}

//print any message received for subscribed topic
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}

void loop() {
// put your main code here, to run repeatedly:
if (!client.connected()) {
reconnect();
}
client.loop();
}

 void reconnect() {
 // Loop until we're reconnected
 while (!client.connected()) {
 Serial.print("Attempting MQTT connection...");
 // Attempt to connect, just a name to identify the client
 if (client.connect("arduinoClient")) {
  Serial.println("connected");
  // Once connected, publish an announcement...
  client.publish("outSHADAB","hello world");
  // ... and resubscribe
  client.subscribe("inShadab");
  } else {
  Serial.print("failed, rc=");
  Serial.print(client.state());
  Serial.println(" try again in 5 seconds");
  // Wait 5 seconds before retrying
  delay(5000);
  }
  }
  }

I am using ESP8266 with arduino using WiFiEsp library.I want to make MQTT connection with arduino so I use PubSubclient library

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
shadab
  • 391
  • 4
  • 15
  • i got error: rc=-2 – shadab May 04 '16 at 14:02
  • I want to use Arduino UNO as a controller,and esp8266 as wifi module to work on MQTT protocol, so I use pubsubclient library for MQTT support.and I connect esp8266 with software serial library.But the problem is that pubsub library client is not working on softwreserial .so i use WiFiEsp library, as code is described.please help me. – shadab May 05 '16 at 13:47
  • I have the same problem. do you have Any solution yet? – Pranav Goswami Sep 15 '19 at 21:21
  • not yet found the solution – shadab May 19 '21 at 11:13

1 Answers1

1

I don't think the PubSubClient supports the WiFiEsp as a network layer from an arduino.

While the doc list the ESP8266 I believe this is running the PubSubClient directly on the ESP8266 hardware.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • i am using this example shown in blog:[link](https://sonyarouje.com/2016/03/15/mqtt-communication-with-arduino-using-esp8266-esp-01/) – shadab May 05 '16 at 07:23
  • This appears to be the case, an issue has been raised in the WiFiEsp repository: https://github.com/bportaluri/WiFiEsp/issues/40 – Brett Y May 19 '16 at 23:47