Using MQTT Binding on OpenHAB 2

MQTT 1.9 is legacy binding for OH2 so you will have to configure it using configuration files:

My example on OH2 running on RP3:

  1. Install mosquitto, the new version not the one in the original repo
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
sudo apt-get install mosquitto

systemctl enable mosquitto
systemctl start mosquitto
systemctl status mosquitto
  1. Check your MQTT server is working using the free MQTT.fx http://mqttfx.jfx4ee.org

  2. Configure mqtt binding on OH2:
    /etc/openhab2/services/mqtt.cfg

mqtt:broker.url=tcp://localhost:1883
mqtt:broker.clientId=openhab
mqtt:broker.retain=true
mqtt:broker.async=false
  1. Add two switches:
    /etc/openhab2/items/switch.items
Switch Switch1 {mqtt=">[broker:myhome/bedroom/switch1:command:ON:1],>[broker:myhome/bedroom/switch1:command:OFF:0],<[broker:myhome/bedroom/switch1:state:ON:1],<[broker:myhome/bedroom/switch1:state:OFF:0"}
Switch Switch2 {mqtt=">[broker:myhome/bedroom/switch2:command:ON:1],>[broker:myhome/bedroom/switch2:command:OFF:0],<[broker:myhome/bedroom/switch2:state:ON:1],<[broker:myhome/bedroom/switch2:state:OFF:0]"}

Update the sitemap to the new switches and you are ready to go…