MQTT, Arduino & Openhab & RGB

if you do not see the MQTT binding, it is not installed

with regard to mqtt.cfg : the only essential info there is the broker name. other things like client etc will default and unless you want to set up a password/user you can leave that totally empty. Anything besides the broker name and ip is optional

He says he can use those credentials in his other client, so I am assuming he has user/password authentication, in which case, he needs those lines.

So if the MQTT says it’s installed but I do not see the MQTT in the Binding main tab how do I correct that?

I corrected the MQTT.config and still nothing from the Switch test

thats might have escaped me. Generally though that would be the first thing to switch off if connction problems :wink:

i am not sure what you mean with 'the mqtt says it is installed’
I suggest to install it again from paper ui

Ok, a few things confuse me with this entire installation.

Are you using superuser credentials to connect to the broker? If so, bad idea.

You need to add the user openhab to the mosquitto configuration file (not the openhab one) and either supply a plain text password, or use a password file. I am beginning to think you followed a bad tutorial of setting up the broker.

If I go to paper UI and click config then bindings I see the NTP biding and the Network binding but not the MQTT binding.

I then click the Blue + and go to bindings and scroll to MQTT and it says installed. So for some reason it is not installing the binding correctly to put the MQTT binding in the Config, Binding tab.

No that is quite normal. what counts is that the binding says it is installed. I do also not have it pop up in the config
Do you have anything else working with MQTT?

not sure i understand the question. This is just a start to the MQTT setup I was hoping to get some accent lighting to start learning MQTT and openhab

I know it might be hard to follow two people giving advice, but as far as i can see, (unless I missed something) the problem stll is not pinpointed to any specific device. Is it your broker set up, is it openhab or is it the arduino (i think the broker setup)
So, may i suggest you use a clear mqtt monitor to actually see the MQTT traffic, then you can clearly see what is going on.

What also would helpis to know if your rgb is the only thing not working. Are you using MQTT for anything lse in openhab? and if yes, is that working? (I guess not considering the faults in yr cfg)

When I load the Arduino Program adjusted from your comments listed below.

#include <UIPEthernet.h>
#include <utility/logging.h>
#include <PubSubClient.h>

// Set the MAC address
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x5E, 0x69 };

// Set fallback IP address if DHCP fails
IPAddress ip(192,168,3,20);

// Set the broker server IP
byte server[] = { 192,168,3,3 };

EthernetClient ethClient;

// Callback function header
void callback(char* topic, byte* payload, unsigned int length);

PubSubClient client(server, 1883, callback, ethClient);

int SoffitR;
int SoffitG;
int SoffitB;
int BLUE = 5;                   
int GREEN = 7; 
int RED = 3; 

void setup()
{
    // Open serial communications
  Serial.begin(9600);
  
    // Start with a hard-coded address:
    Serial.println("Assigning Static IP address:");
    Ethernet.begin(mac, ip);
  
     Serial.print("My address:");
  Serial.println(Ethernet.localIP());
  
  // Connect to Broker, give it arduino as the name
  if (client.connect("arduino_LED2")) {
    
    // Publish a message to the status topic
    client.publish("status/arduino_LED2","Arduino LED2 is now online");
    
    // Listen for messages on the control topic
    client.subscribe("control/arduino_LED2/#");
  }
}


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

void callback(char* topic, byte* payload, unsigned int length) {
  // check for messages on subscribed topics
  payload[length] = '\0';
  Serial.print("Topic: ");
  Serial.println(String(topic));
  


  // check topic to identify type of content
  if(String(topic) == "control/arduino_LED2/livingroom/color") {
    // convert payload to String
    String value = String((char*)payload);
    //value.trim();
    Serial.print (value);
    //Serial.flush();
    // split string at every "," and store in proper variable
    // convert final result to integer
    SoffitR = value.substring(0,value.indexOf(',')).toInt();
    SoffitG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt();
    SoffitB = value.substring(value.lastIndexOf(',')+1).toInt();

    // print obtained values for debugging
    Serial.print("RED: ");
    Serial.println(SoffitR);
    //client.publish("status/arduino_LED2", SoffitR);

    Serial.print("GREEN: ");
    Serial.println(SoffitG);
    //client.publish("status/arduino_LED2", SoffitG);
    
    Serial.print("BLUE: ");
    Serial.println(SoffitB);
    //client.publish("status/arduino_LED2/soffit/color/blue", int SoffitB);
    //Serial.flush();
   
  analogWrite(GREEN, SoffitG);
  analogWrite(RED, SoffitR);
  analogWrite(BLUE, SoffitB);
  
   while(Serial.available())
  Serial.read();
    
  }
}

I get a status/arduino_LED2 Arduino LED2 is now online status on the MQTT Broker listening to

pi@MQTT:~ $ mosquitto_sub -h localhost -t \# -v 

I added a item to the Openhab items file:

TestMQTT "MQTT TEST" <switch> ["Switchable"] {mqtt=">[mosquitto:topic:command:ON:1],>[mosquitto:topic:command:OFF:0],<[mosquitto:topic:state:ON:1],<[mosquitto:topic:state:OFF:0]"}

With corresponding sitemap setup.
This switch is not generating anything on the MQTT broker.

This need to be an actual topic, in the form of a path, I dont think 1 word topics are permitted.

1 Like

Expanded the topics still no change to anything on the MQTT broker. I think there is still a disconnect with the MQTT and Openhab

Switch TestMQTT "MQTT TEST" <switch> ["Switchable"] {mqtt=">[mosquitto:livingroom/lighting/accent:command:ON:1],>[mosquitto:livingroom/lighting/accent:command:OFF:0],<[mosquitto:livingroom/lighting/accent:state:ON:1],<[mosquitto:livingroom/lighting/accent:state:OFF:0]"}

I think so too, and I am 99% sure it’s your credentials.

Post your mosquitto.conf file from the /etc/mosquitto folder.

i took the user name and password out didnt make a difference

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

OK, change it to this:

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

allow_anonymous false
password_file /etc/mosquitto/pwfile
listener 1883

Then at the command line:

sudo mosquitto_passwd -c /etc/mosquitto/pwfile openhab

at the prompt, type any password you like.

sudo reboot

go back to your OH mqtt.cfg and update the username to openhab, and the password as what you chose.

Made those changes now when i run the following command on the MQTT

mosquitto_sub -h localhost -t \# -v

I’m getting a connection refused not authorised over and over

I had to change directory onwer to pi (The user I login with) to make these changes on MQTT broker

That’s because your username and password have changed

Username is now ‘openhab’