Password protected MQTT for LED Strip Control


#1

Hey all,
I’m following along with Matt’s video and so far so good.

The only snag I’m running into, is that my MQTT server is purposefully password protected. It’s also going to use SSL, but for now I don’t have any certificates installed just for testing my LED strip.

Matt’s code for the ESP8266 doesn’t seem to have provisions for an MQTT login and password. I’ve gone ahead and defined them, but I’m not sure on the syntax as far as how or where they need to be called in order to connect.

Any tips?

I’ve added this commented code to the beginning of the ESP8266 Sketch

//MQTT Login
const char* mqtt_username = "usernamehere";
const char* mqtt_password = "PasswordHere";

I want to say that the relevant snippet is here:

Code Snippet
void setup() 
{

  WiFi.mode(WIFI_STA);
  
  WiFi.begin(ssid, password);

  client.begin(server, net);

  client.onMessage(messageReceived);

  connect();

  MDNS.begin(host);

  httpUpdater.setup(&httpServer, update_path, update_username, update_password);
  httpServer.begin();

  MDNS.addService("http", "tcp", 80);
}

Specifically the client.begin(server, net); part, but this is literally my first foray into the esp8266.


MK-Blinds config - MQTT login setup?
#2

NEVERMIND!

lol I figured it out:

Code Snippet
void connect() 
{
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(1000);
  }

  while (!client.connect(mqttDeviceID, mqtt_username, mqtt_password )) 
  {
    delay(1000);
  }

  client.subscribe(subscribeTopic);
}

In my case, since I defined the login and password already as mqtt_username and mqtt_password, I just needed to add them to the while statement loop that checks to see if we’re connected or not, and seperate each defined value with a comma.


#3

Thank you for posting this in the forum for future people who also want authentication!


#4

Glad to help out Matt, I may post back here after I get the SSL version going too, but that may be a bit of a ways out.