Menu

Skip to content
Sytone's Ramblings
  • Home
  • About

Sytone's Ramblings

the occasional posts of a guy who plays with technology

Enabling MQTT abilities in OpenHab

Posted on October 25, 2015 by Sytone

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://<IP ADDRESS OF PI>:8080/openhab.app?sitemap=demo

In mqtt-spy publish a message to home/testing/message with a message of hello, you should see it on the OpenHAB site. You now have MQTT working with OpenHAB.

Share this:

  • Email
  • Print
  • Reddit
  • Twitter
  • Pocket
  • Pinterest
Posted in General Home Automation | Leave a comment

Installing OpenHAB

Posted on October 25, 2015 by Sytone

To install and run OpenHab on your Pi I am recommending you use the apt-get approach, these instructions are taken from the OpenHAB Wiki, so it may be more up to date and have additional options. I also contribute to changes in the wiki every now and then so this may get out of date but in general be correct. This page does highlight the addons needed for the guides on my site.

Shopping List – things you need to do this.

Just a Raspberry Pi for this, I still use V1 B+ as I have not needed to get a V2 yet. This pack has everything you need to get started.
  1. Install Java if 1.6 or higher is not already installed. The following will display your current Java version.
    java -version
    
  2. Add the openHAB Bintray Repositories key to the apt-keyring
    using wget:

    wget -qO - 'https://bintray.com/user/downloadSubjectPublicKey?username=openhab' | sudo apt-key add -
    

    or using curl:

    curl 'https://bintray.com/user/downloadSubjectPublicKey?username=openhab' | sudo apt-key add -
    
  3. Add openHAB apt repository to the apt sources list (Note: the current openhab.list file will be overwritten)
    echo "deb http://dl.bintray.com/openhab/apt-repo stable main" | sudo tee /etc/apt/sources.list.d/openhab.list
    
  4. Resynchronize the package index
    sudo apt-get update
    
  5. Install the openHAB runtime
    sudo apt-get install openhab-runtime
    
  6. If you have more than one OpenHAB binding with a USB device (Z-Wave, RFXCOM, etc), refer to [[symlinks]]
  7. Start openHAB – manually
    1. Init based on sysVinit (e.g. Debian 7 / Ubuntu 14.x and earlier)
      sudo /etc/init.d/openhab start
      sudo /etc/init.d/openhab status
      
    2. Init based on systemd (e.g. Debian 8 / Ubuntu 15.x and higher)
      sudo systemctl start openhab
      
  8. Start openHAB – at system startup
    1. Init based on sysVinit
      sudo update-rc.d openhab defaults
      
    2. Init based on systemd
      sudo systemctl daemon-reload
      sudo systemctl enable openhab
      
  9. Install the add-ons / bindings as you need them. To follow these guides install the addons below. Feel free to add any other you may need or want.
    sudo apt-get install openhab-addon-${addon-type}-${addon-name}
    

    Install these addons:

    
    sudo apt-get install openhab-addon-binding-http
    sudo apt-get install openhab-addon-binding-ntp
    sudo apt-get install openhab-addon-binding-weather
    sudo apt-get install openhab-addon-binding-zwave
    sudo apt-get install openhab-addon-persistence-logging
    sudo apt-get install openhab-addon-persistence-rrd4j 
    sudo apt-get install openhab-addon-binding-mqtt
    sudo apt-get install openhab-addon-binding-exec 
    sudo apt-get install openhab-addon-binding-squeezebox 
    sudo apt-get install openhab-addon-binding-xbmc 
    

    A list of all available packages can be retrieved with

    sudo apt-cache search openhab
    
  10. Configure your system as outlined here: configuration.
  11. If you wish to use a USB zwave stick or other USB/serial device you will need to add the “openhab” users to the “dialout” group:
    sudo usermod -a -G dialout openhab
    

    and then reboot.

  12. Test it, point your browser to http://localhost:8080/openhab.app?sitemap=yourname and you should see your sitemap. Replace localhost with the IP of your Pi if you are running headless.

Share this:

  • Email
  • Print
  • Reddit
  • Twitter
  • Pocket
  • Pinterest
Posted in General | Leave a comment

Setting up Mosquitto on your Pi as part of the home hub

Posted on October 25, 2015 by Sytone

To enable all your nodes to communicate we are using a protocol called MQTT, on the pi we are using Mosquitto as the broker to support this. The broker allows nodes to publish information or subscribe to get information when it is published.

MQTT uses simple channels to dictate where messages go, here are some examples:

HOME/Garage/DoorOne/State
 HOME/Garage/DoorTwo/State
 HOME/Garage/Temperature

The structure of the channel is up to you, just keep it short and logical! In these articles I will have them set to how I like them, feel free to change them as you need. To install Mosquitto on your Raspberry Pi run the following commands.

sudo apt-get install mosquitto mosquitto-clients

Now you have it installed lets test it to ensure it is working. You can do this in a few ways but I’m going to use mqtt-spy. Head to the github site and download the latest version (0.3.0 at the time of writing). Once you have it open it up and ensure a default configuration is created.

Now you can add in your nice new broker. In the connections menu select new connection. The Server URI is the IP address of your Pi running OpenHab. To get this if you do not have it run ifconfig in a SSH session. Click on Apply and the Open the Connection.

If everything is good you will have a new green tab at the top of the window, this is the connection to your Mosquitto broker instance. Right click on the tab and select Show Broker Statistics to see the $SYS channel and data. This is a good indication of traffice going to and from the broker and you have a connection up and running from another machine.

To test subscribing and publishing make a subscription to test/my/channel in the new subscription section and press enter. In the publish section enter test/my/channel as the topic and Hello World as the data (what else…). Press publish and you will see it turn up in the subscription you just made.

mqtt-spy is a handy tool to have around so keep it in your home automation toolbox.

You now have Mosquitto installed and running on your system. Next we need to configure OpenHab to work with MQTT to send and receive messages.

Share this:

  • Email
  • Print
  • Reddit
  • Twitter
  • Pocket
  • Pinterest
Posted in General | Leave a comment

Post navigation

  • Older posts
  • Newer posts

Tags

API autohotkey Binding c# cache Content Deployment development engineering forge Games General Gnomoria hack Home Assistant Home Automation hosting ISO ISY JSON libraries Microsoft Microsoft Flow minecraft MoCA mod network openhab outlook paper PowerShell REST Security SharePoint site SQL toodledo troubleshooting Tweak Visual Studio Visual Studio Code windows 7 Windows 8 Windows Phone winkhub XAML

Recent Posts

  • iPhone hotspot issues? Getting it to work with windows.
  • Getting a DietPi image running on Hyper-V
  • Getting HP Officejet Pro 8620 scan to network working…
  • Developing Home Assistant on Windows using the Subsystem for Linux
  • Using Microsoft Flow with Home Assistant

Recent Comments

  • Shrabean on Automating OneNote with AutoHotKey to be a Journal V1.1
  • danbutter on Integrating ISY into OpenHAB
  • Sytone on Integrating ISY into OpenHAB
  • rick Sumter on Integrating ISY into OpenHAB
  • Sytone on Automating OneNote with AutoHotKey to be a Journal V1.1

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
Proudly powered by WordPress
Theme: Flint by Star Verte LLC
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.