knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.82k stars 1.47k forks source link

Attempting MQTT connection.. fails rc = -2 #944

Open annadierothe opened 2 years ago

annadierothe commented 2 years ago

Hello, I have a similar problem as a lot of people here, however the fixes did not work for me until now. I use an ESP32 with platformio and I connect it to the eduroam network. Whenever I try to connect, the wifi connection does work, however the MQTT connection fails with the code -2 (More precisely Attempting MQTT connection [720604][E][WiFiClientSecure.cpp:135] connect(): start_ssl_client: -1 Trying connect again -2)

!!!The code worked before but I had to update platformio, so I went into the error, that esp_wpa2_config_t is undefined. I could solve that problem by reducing the following code: esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT(); if (esp_wifi_sta_wpa2_ent_enable(&config) != ESP_OK) { Serial.println("WPA2 Settings Not OK"); } to this code: if (esp_wifi_sta_wpa2_ent_enable() != ESP_OK) { Serial.println("WPA2 Settings Not OK"); } I am not sure whether this can be also the reason, that is why I mentioned it, however the Wificonnection works successfully with following (and code throws same error if it is connected to local network):

WiFi connected
IP address set:
192.168.162.18

What I tried so far: I used before only WiFiClient, that ended up with state -4, WiFiClientSecure ends with state -2; I changed the MQTT_MAX_PACKET_SIZE to 1024 and 2048, did not change anything, I tried Port 1883, also did not help. Password and username are corrected, I have also a Python code which runs successfully and as I mentioned, this code also worked before.

I would be really happy if somebody could help!

The following is my code:

#include <Arduino.h>
#include "Mqtt.h"
#include "../PubSubClient/PubSubClient.h"
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <esp_wpa2.h>
#include "../Profiles/test_profile.h"
#

const char* mqttServer = "eduroam"; //real server cannot be given due to privacy reasons
const int mqttPort = 8883;
const char* mqttUser = "username";
const char* mqttPass = "password";

#define EAP_IDENTITY "myusername"
#define EAP_PASSWORD "mypassword"
const char* ssid = "eduroam";

#define MQTT_SERIAL_PUBLISH_CH "r4"
#define MQTT_SERIAL_RECEIVER_CH "r4"

WiFiClientSecure wifiClient;
PubSubClient client(wifiClient);

void setup_wifi(){
  byte error = 0;
  delay(10);
  Serial.println();
  Serial.print("Connection to");
  Serial.println(ssid);
  WiFi.disconnect(true);
  WiFi.mode(WIFI_STA);
  error += esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY));
    error += esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY));
    error += esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD)); //Following times, it ran fine with just this line (connects very fast).
    if (error != 0) {
        Serial.println("Error setting WPA properties.");
    }
    WiFi.enableSTA(true);

    esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();
    if (esp_wifi_sta_wpa2_ent_enable(&config) != ESP_OK) {
        Serial.println("WPA2 Settings Not OK");
    }

    WiFi.begin(ssid); //connect to Eduroam function
    WiFi.setHostname("RandomHostname"); //set Hostname for your device - not neccesary
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address set: ");
    Serial.println(WiFi.localIP()); //print LAN IP
}

void reconnect(){
  while(!client.connected()){
    Serial.print("Attempting MQTT connection");
    String clientID = "ESP32Client-";
    clientID += String(random(0xffff),HEX);
    if(client.connect(clientID.c_str(), mqttUser, mqttPass)){
      Serial.print("Connected");
      client.subscribe(MQTT_SERIAL_RECEIVER_CH);
    } else {
      Serial.println("Trying connect agian");
      Serial.println(client.state());
      delay(5000);
    }
  }
}

void callback(char* topic, byte *payload, unsigned int length){
  Serial.println("---new message from broker ---");
  Serial.print ("channel:");
  Serial.println(topic);
  Serial.print("data:");
  Serial.write(payload,length);
  Serial.println();
}

void publishSerialData(char *serialData){
  if(!client.connected()){
    reconnect();
  }
  client.publish(MQTT_SERIAL_PUBLISH_CH, serialData);
}

void initMqtt(){
    setup_wifi();
    client.setServer(mqttServer, mqttPort);
    client.setCallback(callback);
    reconnect();
    client.publish("r4", "initialized MQTT");
}

void loopMqtt(){
    client.loop();
    client.publish("r4", "hello world");
}

and in my main file:

#include <Arduino.h>
#include "../lib/PubSubClient/PubSubClient.h"
#include "../lib/MQTT/Mqtt.h"
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <esp_wpa2.h>

void setup(){
   Serial.begin(115200);
   initMqtt();
}

void loop(){
   loopMqtt();
}
IronBamBam1990 commented 2 years ago

const char* mqttServer = "eduroam"; <----- you cant name it like that it must be IP number or you must create a dns .

define WM_ASYNC

define WM_MDNS

define MQTT_TASK_PRIO 1

define MQTT_TASK_QUEUE_SIZE 16

define MQTT_BUF_SIZE 256

define QUEUE_BUFFER_SIZE 512

char _tpm[3]; sprintf(_tpm, "Simon%06u", ESP.getChipId()); if(WiFi.status() == WL_CONNECTED){ if(MDNS.begin(_tpm)){ //Start mDNS with name esp8266 ////Serial.println("MDNS started"); MDNS.addService("http", "tcp", 80); ////Serial.println("MDNS Service Started"); MDNS.update(); } }

WiFi.begin();