Enabling MQTT abilities in OpenHab
Using your favorite editor in Linux or something like WinSCP we need to edit some OpenHAB configuration files. If you followed the guides on this site the configuration is located in /etc/openhab. Open up /etc/openhab/configurations/openhab.cfg to edit. If it does not exist you need to copy openhab_defaults.cfg in the same directory to openhab.cfg
sudo nano /etc/openhab/configurations/openhab.cfg
You need to navigate to the MQTT Transport section for transport settings. Ignore the MQTT Persistence section. The following three bolded configuration items need to be modified, I am using pimosquitto as the broker name, you can use whatever you want. Just be consistent.
################################# MQTT Transport ######################################
# Define your MQTT broker connections here for use in the MQTT Binding or MQTT # Persistence bundles. Replace [broker] with a id you choose.
# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883 mqtt:pimosquitto.url=tcp://localhost:1883
# Optional. Client id (max 23 chars) to use when connecting to the broker. # If not provided a default one is generated. mqtt:pimosquitto.clientId=localpi
# Optional. User id to authenticate with the broker. # mqtt:[broker].user=[user]
# Optional. Password to authenticate with the broker. #mqtt:[broker].pwd=[password]
# Optional. Set the quality of service level for sending messages to this broker. # Possible values are 0 (Deliver at most once),1 (Deliver at least once) or 2 # (Deliver exactly once). Defaults to 0. #mqtt:[broker].qos=[qos]
# Optional. True or false. Defines if the broker should retain the messages sent to # it. Defaults to false. mqtt:pimosquitto.retain=true
# Optional. True or false. Defines if messages are published asynchronously or # synchronously. Defaults to true. #mqtt:[broker].async=[async]
# Optional. Defines the last will and testament that is sent when this client goes offline # Format: topic:message:qos:retained #mqtt:[broker].lwt=[last will definition]
Save the file. You are now ready to create a item in openhab and add it to a sitemap. I will assume you have the demo items and sitemap in place for this or already have a file ready to go. Open up the items file in the items folder under /etc/openhab/configurations. Add in a new group for testing, you can remove/comment this out when you are happy with your real configuration. Also add in a String so we can see the values in the site map. Example:
/* MQTT Testing groups, string and switches */ Group MqttTesting /* Inbound String for topic home/testing/message */ String MqttTestMessage “Message: [%s]” (MqttTesting) {mqtt="<[pimosquitto:home/testing/message:state:default]"} /* Inbound Switch for topic home/testing/message */ Switch MqttTestSwitch “MQTT Switch” (MqttTesting) {mqtt="<[pimosquitto:home/testing/switch:command:ON]"}
Next we add this section to the sitemap. Open up the sitemap file in the sitemaps folder under /etc/openhab/configurations. This is a very simple frame that will list anything out in the group MqttTesting
Frame label=“Tests” { Group item=MqttTesting label=“MQTT Testing” }
Save the file and then open up your browser and navigate to http://