Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mforbiz opened this issue Aug 14, 2023 · 2 comments
Open

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

mforbiz opened this issue Aug 14, 2023 · 2 comments

Comments

@mforbiz
Copy link

mforbiz commented Aug 14, 2023

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
Copy link

Use mqttClient.setUsernamePassword(username, password)

@LateROnni
Copy link

LateROnni commented Mar 28, 2024

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants