Arduino Nano ESP32 MQTT connection with username, password

Hey together,

I want to establish a connection to an MQTT broker which is only accessible with a username and a password, port is still 1883.

I wanted to use the ESP MQTT client library GitHub - plapointe6/EspMQTTClient: Wifi and MQTT handling for ESP8266 and ESP32

But I get an error during compiling:

Alternatives for ETH.h: []
ResolveLibrary(ETH.h)
-> candidates: []
In file included from c:\Users\Arduino\libraries\Bootstrapper\src\BootstrapManager.h:29,
from c:\Users\Arduino\libraries\Bootstrapper\src\BootstrapManager.cpp:20:
c:\Users\Arduino\libraries\Bootstrapper\src\Configuration.h:33:10: fatal error: ETH.h: No such file or directory
#include <ETH.h>
^~~~~~~
compilation terminated.
exit status 1
Compilation error: exit status 1

Are there any other libraries I could use, or do I need to switch to micropython?

Thanks in advance!

I have used the PubSubClient library for a number of projects, but I have not tried it with the Nano ESP32

If you want help with the library that you are currently using then please post your full sketch, using code tags when you do

I am using PubSubClient on my ESP32-C6 dev board. Works fine.

// Libraries
#include <EspMQTTClient.h>
#include <ArduinoJson.h>                                // Import JSON functionality
#include <Arduino.h>                                    // Import Arduino functionality
#include <Secrets.h>                                    // Import data from secrets file

// Variables
const int       HwLiftRelease       = 2;                // D2 digital output
const int       HwLiftUp            = 3;                // D3 digital output
const int       HwLiftDown          = 4;                // D4 digital output

const char WifiSsid[] = WIFI_SSID;
const char WifiPwd[] = WIFI_PWD;
const char MqttAdr[] = MQTT_ADR;
const short MqttPort = MQTT_PORT;
const char MqttUser[] = MQTT_USER;
const char MqttPwd[] = MQTT_PWD;
const char MqttClient[] = MQTT_CLIENT_ID;
const char MqttTopicWill[] = MQTT_TOPIC_WILL;
const char MqttTopicSub[] = MQTT_TOPIC_SUB;
const char MqttTopicPub[] = MQTT_TOPIC_PUB;
const char MqttMessageWill[] = MQTT_MESSAGE_WILL;



EspMQTTClient client(
  WifiSsid,
  WifiPwd,
  MqttAdr,  // MQTT Broker server ip
  MqttUser,   // Can be omitted if not needed
  MqttPwd,   // Can be omitted if not needed
  MqttClient,     // Client name that uniquely identify your device
  MqttPort              // The MQTT port, default to 1883. this line can be omitted
);

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

  // Optional functionalities of EspMQTTClient
  client.enableDebuggingMessages(); // Enable debugging messages sent to serial output
  client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overridded with enableHTTPWebUpdater("user", "password").
  client.enableOTA(); // Enable OTA (Over The Air) updates. Password defaults to MQTTPassword. Port is the default OTA port. Can be overridden with enableOTA("password", port).
  client.enableLastWillMessage(MqttTopicWill, MqttMessageWill);  // You can activate the retain flag by setting the third parameter to true
}

// This function is called once everything is connected (Wifi and MQTT)
// WARNING : YOU MUST IMPLEMENT IT IF YOU USE EspMQTTClient
void onConnectionEstablished()
{
  // Subscribe to topic and display received message to Serial
  client.subscribe(MqttTopicSub, [](const String & payload) {
    Serial.println(payload);
  });

  // Publish a message to topic
  client.publish(MqttTopicPub, "Online"); // You can activate the retain flag by setting the third parameter to true
}

void loop()
{
  client.loop();
}

Example I used:
EspMQTTClient/examples/SimpleMQTTClient/SimpleMQTTClient.ino at master · plapointe6/EspMQTTClient · GitHub

Didnt see that the ArduinoMqttClient has a password and user function..

I used this now: ArduinoMqttClient/examples/WiFiAdvancedCallback/WiFiAdvancedCallback.ino at master · arduino-libraries/ArduinoMqttClient · GitHub

Initial issue was <Secrets.h> which should be written "Secrets.h"