Hi all
I’m trying to run a simple program on my esp32 (wroom) but keep getting -2 error (connection refused).
I’ve defined a sensor in HA via the configuration.yaml
Running MQTT explorer on my laptop is working and I see the sensor data in HA.
The esp32 is managing to connect to the same wifi network as my laptop and successfully connect and publish a message to test.mosquitto.org. But when I give it the MQTT broker IP, I get the -2 error.
I’ve verified that I’m using the same user and password as I’ve used with MQTT explorer. Even tried using another user. Still, it works with MQTT explorer but not with the esp32.
I’ve also tried disabling the firewall in the router…nothing
This looks to me as a MQTT configuration issue
Went through many forums and examples, tried using PubSubClient and mqttClient libs but none helped.
Pasting the code here for reference
#include <WiFi.h>
#include <ArduinoMqttClient.h>
#include <ArduinoJson.h>
const char *ssid_Router = "*******"; //Enter the router name
const char *password_Router = "******"; //Enter the router password
//const char topic[] = "chicken_coop";
const char* mqtt_server = "192.168.0.99";
int port = 1883;
const char* mqtt_user = "esp32_user";
const char* mqtt_pass = "esp32_user123";
//set interval for sending messages (milliseconds)
const long interval = 8000;
unsigned long previousMillis = 0;
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
void setupWIFI() {
Serial.println("WIFI setup start");
WiFi.begin(ssid_Router, password_Router);
Serial.println(String("Connecting to ")+ssid_Router);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("\nConnected, IP address: ");
Serial.println(WiFi.localIP());
Serial.print("RRSI: ");
Serial.println(WiFi.RSSI());
// bool ret = Ping.ping("www.google.com");
// if (ret) {
// Serial.println("Did a ping to google.com");
// }
// else {
// Serial.println("Could not do a ping to google.com");
// }
Serial.println("WIFI setup End");
}
void setupMQTT() {
Serial.print("Attempting to connect to the MQTT broker: ");
Serial.println(mqtt_server);
mqttClient.setUsernamePassword(mqtt_user, mqtt_pass);
IPAddress ip(192,168,0,99);
Serial.println("mqtt ip:");
Serial.println(ip.toString());
Serial.println("esp32id0019");
mqttClient.setId("esp32id0019");
while (!mqttClient.connect(ip, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
Serial.println("Retrying in 3 seconds...");
Serial.println();
delay(3000);
}
Serial.println("You're connected to the MQTT broker!");
Serial.println();
}
void reconnectMQTT() {
// Loop until we're reconnected
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
if (!mqttClient.connect(mqtt_server, port)) {
Serial.print("MQTT connection failed! Error code = ");
Serial.println(mqttClient.connectError());
// Wait 5 seconds before retrying
delay(5000);
}
else {
Serial.println("connected");
// Subscribe
//client.subscribe("esp32/output");
}
}
}
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println("Wifi Setup...");
setupWIFI();
Serial.println("MQTT Setup...");
setupMQTT();
}
float temperature = 0;
long lastMsg = 0;
char msg[50];
void loop() {
if (!mqttClient.connected()) {
reconnectMQTT();
}
mqttClient.poll();
long now = millis();
if (now - lastMsg > 5000) {
lastMsg = now;
// Temperature in Celsius
//temperature = bme.readTemperature();
// Uncomment the next line to set temperature in Fahrenheit
// (and comment the previous temperature line)
//temperature = 1.8 * bme.readTemperature() + 32; // Temperature in Fahrenheit
temperature = 36.1;
// Convert the value to a char array
char tempString[8];
dtostrf(temperature, 1, 2, tempString);
Serial.print("Temperature: ");
Serial.println(tempString);
String jsonTemperatureStr1 = String("{\"temperature\":");
String jsonTemperatureStr2 = jsonTemperatureStr1 + tempString + "}";
//mqttClient.publish("chicken_coop/temperature", jsonTemperatureStr2.c_str());
//mqttClient.beginMessage("chicken_coop/temperature");
mqttClient.beginMessage("arduino/simple");
mqttClient.print(jsonTemperatureStr2.c_str());
mqttClient.endMessage();
// humidity = bme.readHumidity();
// // Convert the value to a char array
// char humString[8];
// dtostrf(humidity, 1, 2, humString);
// Serial.print("Humidity: ");
// Serial.println(humString);
// mqttClient.publish("esp32/humidity", humString);
}
}