arduino-libraries / ArduinoMqttClient

ArduinoMqttClient Library for Arduino
GNU Lesser General Public License v2.1
186 stars 73 forks source link

How to send MQTT server's username and password #91

Open mforbiz opened 1 year ago

mforbiz commented 1 year ago

Hi. Using the library, is it possible to connect to an MQTT server (Mosquitto) thas requires authentication based on username and password? Actually I'm using mqttClient.connect(broker, port), but I don't know how to provide MQTT server's username and password. Any idea?

WL-Richards commented 12 months ago

Use mqttClient.setUsernamePassword(username, password)

LateROnni commented 4 months ago

Sorry to hijack but it's still open and the same issue. Any hints on where? I saw that code in the examples but it doesn't seem to want to work when I insert it in the (what I think is the) equivalent place in my code. I am using the env board on a mkr so I have the Mqtt stuff in modules. Is that the issue? Even was running fantastic on the mosquitto.org, but I set up a raspberry pi (which I know is working) and need to switch this to that server.

Here is the snippet of where I inserted it but tbh I have tried about 20 places to no avail. For the moment I have set my username to mummy and password is raspberry. I tried defining them at the top and using mqttClient.setUsernamePassword(username, password) no love, so now just using them directly mqttClient.setUsernamePassword("mummy", "raspberry")

//start of my code that was previously connecting to mosquitto.org without needing credentials
void mqttSetup()
{
// You can provide a username and password for authentication
mqttClient.setUsernamePassword("mummy", "raspberry");  //where does this line go?
Serial.print("Attempting to connect to the MQTT server: ");
  Serial.println(broker);

  if (!mqttClient.connect(broker, port));{
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());

    while (1);
  }

  Serial.println("You're connected to the MQTT server!");
  Serial.println();
  }

void mqttRunning()
{  // call poll() regularly to allow the library to send MQTT keep alive which
  // avoids being disconnected by the broker
  mqttClient.poll();

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time a message was sent
    previousMillis = currentMillis;

    //record mkr env values
    float Rvalue = ENV.readTemperature();
    float Rvalue2 = ENV.readHumidity();
    float Rvalue3 = (ENV.readPressure()*10.5);
    float Rvalue4 = ENV.readIlluminance();
    float Rvalue5 = ENV.readUVA();
    float Rvalue6 = ENV.readUVB();
    float Rvalue7 = ENV.readUVIndex();

void mqttSetup()
{
// You can provide a username and password for authentication
mqttClient.setUsernamePassword("mummy", "raspberry");  
Serial.print("Attempting to connect to the MQTT server: ");
  Serial.println(broker);

  if (!mqttClient.connect(broker, port));{
    Serial.print("MQTT connection failed! Error code = ");
    Serial.println(mqttClient.connectError());

    while (1);
  }

  Serial.println("You're connected to the MQTT server!");
  Serial.println();
  }

void mqttRunning()
{  // call poll() regularly to allow the library to send MQTT keep alive which
  // avoids being disconnected by the broker
  mqttClient.poll();

  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time a message was sent
    previousMillis = currentMillis;

    //record mkr env values
    float Rvalue = ENV.readTemperature();
    float Rvalue2 = ENV.readHumidity();
    float Rvalue3 = (ENV.readPressure()*10.5);
    float Rvalue4 = ENV.readIlluminance();
    float Rvalue5 = ENV.readUVA();
    float Rvalue6 = ENV.readUVB();
    float Rvalue7 = ENV.readUVIndex();