Getting mqtt and password together

Hi All I was wondering if you could help I have a code to run Mqtt sensors etc but want to add password and user name. Ive tried loads but cant get it to work.

Here is the code
*/
#include <SPI.h>
#include <Ethernet.h>// Ethernet.h library
#include "PubSubClient.h" //PubSubClient.h Library from Knolleary
#include "DHT.h" //DHT.h library
#define CLIENT_ID "Test"
#define PUBLISH_DELAY 3000 // that is 3 seconds interval
#define DHTPIN 3
#define DHTTYPE DHT22
#define ledPin 13
#define relayPin 8
String ip = "";
bool statusKD = HIGH;
bool statusBD = HIGH;
bool statusGD = HIGH;
bool relaystate = LOW;
bool pir = LOW;
bool startsend = HIGH;// flag for sending at startup
int lichtstatus; //contains LDR reading
uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x06};

EthernetClient ethClient;
PubSubClient mqttClient;
DHT dht(DHTPIN, DHTTYPE);

long previousMillis;

void setup() {
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT);
pinMode(relayPin, OUTPUT);

// setup serial communication

Serial.begin(9600);
while (!Serial) {};
Serial.println(F("MQTT Arduino Demo"));
Serial.println();

// setup ethernet communication using DHCP
if (Ethernet.begin(mac) == 0) {
//Serial.println(F("Unable to configure Ethernet using DHCP"));
for (;;);
}

Serial.println(F("Ethernet configured via DHCP"));
Serial.print("IP address: ");
Serial.println(Ethernet.localIP());
Serial.println();
//convert ip Array into String
ip = String (Ethernet.localIP()[0]);
ip = ip + ".";
ip = ip + String (Ethernet.localIP()[1]);
ip = ip + ".";
ip = ip + String (Ethernet.localIP()[2]);
ip = ip + ".";
ip = ip + String (Ethernet.localIP()[3]);
//Serial.println(ip);

// setup mqtt client
mqttClient.setClient(ethClient);
// mqttClient.setServer("test.mosquitto.org", 1883);//use public broker
mqttClient.setServer( "192.168.8.100", 1883); // or local broker
//Serial.println(F("MQTT client configured"));
mqttClient.setCallback(callback);
// setup DHT sensor
dht.begin();
Serial.println(F("DHT sensor initialized"));
Serial.println();
Serial.println(F("Ready to send data"));
previousMillis = millis();
mqttClient.publish("home/br/nb/ip", ip.c_str());
}

void loop() {
statusBD = digitalRead(4);// FrontdoorSwitch
statusGD = digitalRead(5);// Garagedoor Switch
statusKD = (digitalRead(6));//LivingRoom Switch
lichtstatus = analogRead(A0);//Reads an LDR
pir = digitalRead(7);//Reads a PIR sensor
relaystate = digitalRead(relayPin);// Reads the state of a relay

// it's time to send new data?
if (millis() - previousMillis > PUBLISH_DELAY) {
sendData();
previousMillis = millis();
}
mqttClient.loop();
}

void sendData() {
char msgBuffer[20];
float h = dht.readHumidity();
float t = dht.readTemperature();
Serial.print("Temperature: ");
Serial.print(t);
Serial.println("oC");
Serial.print("Humidity: ");
Serial.print(h);
Serial.println("%");
Serial.print("Relay is: ");
Serial.println((relaystate == LOW) ? "OPEN" : "CLOSED");
if (mqttClient.connect(CLIENT_ID)) {
mqttClient.publish("home/br/nb/temp", dtostrf(t, 6, 2, msgBuffer));
mqttClient.publish("home/br/nb/humid", dtostrf(h, 6, 2, msgBuffer));
mqttClient.publish("home/br/nb/deur", (statusBD == HIGH) ? "OPEN" : "CLOSED");
mqttClient.publish("home/br/nb/garage", (statusGD == HIGH) ? "OPEN" : "CLOSED");
mqttClient.publish("home/br/nb/bel", (statusKD == HIGH) ? "OPEN" : "CLOSED");
mqttClient.publish("home/br/nb/l", dtostrf(lichtstatus, 4, 0, msgBuffer));
mqttClient.publish("home/br/nb/pr", (pir == HIGH) ? "OPEN" : "CLOSED");
mqttClient.publish("home/br/nb/relay", (relaystate == LOW) ? "OPEN" : "CLOSED");
mqttClient.subscribe("home/br/sb");
if (startsend) {
mqttClient.publish("home/br/nb/ip", ip.c_str());
startsend = LOW;
}
}
}

void callback(char* topic, byte* payload, unsigned int length) {
char msgBuffer[20];
// I am only using one ascii character as command, so do not need to take an entire word as payload
// However, if you want to send full word commands, uncomment the next line and use for string comparison
// payload[length]='\0';// terminate string with 0
//String strPayload = String((char*)payload); // convert to string
// Serial.println(strPayload);
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");//MQTT_BROKER
for (int i = 0; i < length; i++) {
Serial.print((char)payload*);*

  • }*

  • Serial.println();*

  • Serial.println(payload[0]);*

  • // Examine only the first character of the message*

  • if (payload[0] == 49) // Message "1" in ASCII (turn output ON)*

  • {*

  • digitalWrite(LED_BUILTIN, HIGH); //*

  • digitalWrite(relayPin, HIGH);*

  • } else if (payload[0] == 48) // Message "0" in ASCII (turn output OFF)*

  • {*

  • digitalWrite(relayPin, LOW); //*

  • digitalWrite(LED_BUILTIN, LOW);*

  • } else if (payload[0] == 50)*

  • {*

  • mqttClient.publish("home/br/nb/ip", ip.c_str());// publish IP nr*

  • } else {*

  • Serial.println("Unknown value");*

  • mqttClient.publish("home/br/nb", "Syntax Error");*

  • }*
    }
    I have been told that you can use this code:
    /*
    Basic MQTT example with Authentication

    • connects to an MQTT server, providing username*
  • and password*

    • publishes "hello world" to the topic "outTopic"*
    • subscribes to the topic "inTopic"*
      */
      #include <Ethernet.h>
      #include <PubSubClient.h>
      // Update these with values suitable for your network.
      byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
      IPAddress ip(192, 168, 8, 100);
      IPAddress server(192, 168, 8, 29);
      void callback(char* topic, byte* payload, unsigned int length) {
  • // handle message arrived*
    }
    EthernetClient ethClient;
    PubSubClient client(server, 1883, callback, ethClient);
    void setup()
    {

  • Ethernet.begin(mac, ip);*

  • // Note - the default maximum packet size is 128 bytes. If the*

  • // combined length of clientId, username and password exceed this,*

  • // you will need to increase the value of MQTT_MAX_PACKET_SIZE in*

  • // PubSubClient.h*

  • if (client.connect("arduinoClient", "pab", "Cdrom123")) {*

  • client.publish("outTopic","this is a test for the password code");*

  • client.subscribe("inTopic");*

  • }*
    }
    void loop()
    {

  • client.loop();*
    }
    But i cant seem to integrate them can anyone help please.
    Thanks
    Phil