Menu
Cart

Setting up MQTT server and connecting to OpenHAB

So at this point you should have the Arduino IDE up and running with the ability to compile applications for your engimusing boards. You should also have the openHAB server up and running on your PC. Now we need to be able to connect up the engimusing board up to the openHAB server so it can be controlled remotely. This is where MQTT (MQ Telemetry Transport) comes in. See http://mqtt.org/faq for a description of what MQTT is.

Installing mosquitto:

Download the appropriate binary installation for your operating system from here: https://mosquitto.org/download/ (Note: for windows select the Native build unless you know what cygwin is and really want to use it.)

Run the installer.

Open the mosquitto install directory (C:\Program Files (x86)\mosquitto by default) and run the mosquitto.exe file.

This will open up a blank console window with mosquitto running using the default port of 1883 and accepting connections from any network interface.

Mosquitto No Log Messages

(Optional) Turning on mosquitto log messages in the console:

Notice how the previous screenshot just shows a blank console. This section will describe how to turn on console messages for Mosquitto so we can tell what the MQTT server is doing.

Open the mosquitto.config file in a text editor and add the following to the bottom:

# =========================================================
# Engimusing suggested options
# =========================================================
#turn on logging to the console
log_dest stderr

#log all message types
log_type all

#log all connection messages
connection_messages true

 Then to run using this configuration run mosquitto.exe from the command line using this command:

mosquitto.exe -c mosquitto.conf

 

You should now see messages on the console about what is happening on the MQTT server. Like this:

Mosquitto With Console Messages

Connecting OpenHAB to Mosquitto

Open the OpenHAB addons folder.

Open the addons zip file we copied there in the OpenHAB configuration tutorial.

Copy the following two files out of the zip file and into the addons folder:

  • org.openhab.binding.mqtt-1.8.3.jar
  • org.openhab.binding.mqttitude-1.8.3.jar

OpenHAB Addons Zip

These two bindings will allow us to configure OpenHAB to connect to the Mosquitto MQTT server that is running.

Next we need to add the mqtt configuration options to the openhab.cfg file that is in the configurations directory of the OpenHAB installation. 

Add the following to the bottom of the openhab.cfg file:

##################### 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:localBroker.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:localBroker.clientId=openHab

# 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>
mqtt:localBroker.qos=1

# Optional. True or false. Defines if the broker should retain the messages sent to
# it. Defaults to false.
#mqtt:<broker>.retain=<retain>

# 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 <br/>
#mqtt:<broker>.lwt=<last will definition>

 At this point the OpenHAB server is connected to the MQTT server and it is ready to setup the links between MQTT topics and the OpenHAB widgets. 

If OpenHAB is not already running that start it up by running start.bat in the OpenHab main folder.

Next Tutorial: http://www.engimusing.com/pages/link-device-to-openhab-over-mqtt