For a project I am working on, I need to connect my ESP32 board to another server via MQTT protocol. I am using the Mosquitto MQTT Broker and it is presently on my laptop. Following is the test code I am using, which I'll build around this program.
#include <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "*****"; //WiFi Name
const char* password = "********"; //WiFi Password
const char* server= "***.***.**.***"; //RPi or Machine IP on which the broker is
WiFiClient espClient;
PubSubClient client(espClient);
int setup_WiFi(){
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Attempting MQTT connection...");
client.connect("esp32");
if (client.connect("esp32")){
Serial.println("connected");
}
else {
Serial.print("failed, rc=");
Serial.println(client.state());
}
return 0;
}
int reconnect() {
unsigned long startAttemptTime = millis();
while (!client.connected())
{
Serial.println("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("esp32")){ //if (client.connect("esp32", MQTT_USER, MQTT_PASS)) {
Serial.println("connected");
}
else {
Serial.print("failed, rc=");
Serial.println(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
return 0;
}
int send_mqtt(){
setup_WiFi();
char sss[15]="Hello World";
if (!client.connected()){
reconnect();
}
client.publish("esp32/test", sss); //send message
WiFi.disconnect(true);
Serial.println("Sent");
return 0;
}
void setup() {
Serial.begin(115200);
client.setServer(server, 1883); //mqtt server details
setup_WiFi();
reconnect();
send_mqtt();
}
void loop() {
send_mqtt();
delay(10000); //Wait 10 secs before next transmission
}
I tried connection but got this error
Attempting MQTT connection...failed, rc= -2
What am I doing wrong? Any changes I need to make to the .conf file about which I don't know? Guide me. Thank You in advance
-
Here, const char* server= "..**.***"; , I'm putting the IPv4 address of the machine on which Mosquitto is installed.P_K– P_K04/15/2021 07:20:00Commented Apr 15, 2021 at 7:20
-
Can your laptop ping the ESP32?Majenko– Majenko04/15/2021 09:54:29Commented Apr 15, 2021 at 9:54
-
Can you connect with an MQTT client running on the broker itself? Do the log files on the MQTT Broker say anything? Are there other MQTT Clients successfully connecting?StarCat– StarCat04/15/2021 13:21:43Commented Apr 15, 2021 at 13:21
-
@starcat I am able to connect to "localhost". I am using MQTT Explorer to see the connections. Don't actually know if the other devices are connecting, because I haven't tried that and nor do I have the resources. And I am trying to figure out what IP I should connect to.P_K– P_K04/15/2021 14:39:13Commented Apr 15, 2021 at 14:39
-
@Majenko From within the same network, yes I am able to ping. From a different network (different WiFi), NoP_K– P_K04/15/2021 14:48:51Commented Apr 15, 2021 at 14:48
1 Answer 1
The code in your sketch is unlikely to be the problem. It works fine for me after I enter the IP addresses and password for my network.
According to the pubSubClient API documentation, the rc = -2
code returned by client.state()
is due to the network connection having failed. As your code has already checked that it is connected to WiFi before trying to contact the MQTT broker, it is probably the communication between the two devices that is the issue, not the WiFi connection itself. Things to check:
- Are both devices are on the same network, and do they have a route to contact each other? From the laptop running the MQTT broker, try running
ping XXX.XXX.XXX.XXX
(replacing the XXXs with the IP address reported by the ESP32), and check you get a response. You might wish to comment out line 67 of your code so it doesn't disconnect from WiFi after every attempt at sending a message, otherwise you will see lots of lost packets. - Have you got the right IP address for the MQTT broker in your sketch? It should be the IP address for your laptop, not the loopback address,
127.0.0.1
. - Can you use another device on the same network to connect to the MQTT server? (e.g. a cellphone running a MQTT dashboard app, or MQTT Explorer in Windows)
- Is Mosquitto configured to listen out for remote connections? If you are running v2 of Mosquitto, you have to deliberately configure it to accept incoming connections, otherwise it will only listen to the
localhost
interface. You would also have to either set up an authentication mechanism or explicitly configure it to allow anonymous connections. This might be why you can connect from the laptop running the broker, but not from other devices. - On the MQTT server, can you see any connection attempts in Mosquitto's log file (
/var/log/mosquitto/mosquitto.log
)? There might be some error messages that give you further clues.
-
I tried all that you suggested. Except for "Ping" all the measures failed. I was able to ping. Sorry for the late response.P_K– P_K06/22/2021 14:04:40Commented Jun 22, 2021 at 14:04
-
So if you can ping the ESP32, and it's using the right address for the MQTT broker, the next thing to check is the broker itself. Could you update your question to include the version of Mosquitto you are running, and a copy of its
mosquitto.conf
file? What happens when you try to connect to it from a different device on the same wifi?JRI– JRI06/23/2021 06:50:38Commented Jun 23, 2021 at 6:50 -
When connecting another device it gives an error "connection failed"P_K– P_K06/23/2021 11:12:00Commented Jun 23, 2021 at 11:12
-
So it looks like the MQTT broker isn't working. Maybe it isn't configured to allow remote connections. Can you edit your question to include the Mosquitto version number and config file? That would help to debug the problem.JRI– JRI06/26/2021 20:27:13Commented Jun 26, 2021 at 20:27
-