Raspbian Jessie as OpenHAB + MQTT Server: Part 3: Add Websockets

Nowhere near completely usable, BUT, what IS working so far:

  • Websocket javascript with Paho library subscribes succesfully to the Websocket listener on Mosquitto
  • I can subscribe, and recieve all the published messages on the HAB
  • I am using the data received, to dynamically, and automatically, build up a device tree – it looks at the topic path (remember how a year a ago I wrote a post about why the topic name is important (; – here you see why – from the topic names i can deduce the entire hierarchy automatically)
  • Clicking any item in the tree brings ip a CONTROL interface – to be extended with sensorvalues, etc
  • The basic concept here is working great – by setting my devices to announce themselves, or response to a broadcast requesting them to announce themselves, I can cut a LOT of configuration out – theoretically I can have a new device connect to MQTT, send the page its name, location, connected devices, what those devices are (switch, dimmer, sensor, or actuator) and the javascript can automagically make a UI for it. This means you can mostly get away without a server, without a database.

Source being shared here: https://github.com/openhardwarecoza/ESP8266-Sketches/tree/master/htt-websocket-esp8266


Raspbian Jessie as OpenHAB + MQTT Server: Part 2: Add OpenHAB

I’ll be very brief on Part 2:  Since I already wrote extensively on the install and configuration:

sudo apt-get update
 sudo apt-get install oracle-java8-jdk
sudo update-java-alternatives -s jdk-8-oracle-arm-vfp-hflt
wget -qO - 'https://bintray.com/user/downloadSubjectPublicKey?username=openhab' | sudo apt-key add -
echo "deb http://dl.bintray.com/openhab/apt-repo stable main" | sudo tee /etc/apt/sources.list.d/openhab.list
sudo apt-get update
sudo apt-get install openhab-runtime
sudo service openhab status
sudo service openhab start

See https://openhardwarecoza.wordpress.com/2015/03/29/openhab-mqtt-arduino-and-esp8266-part-1-setting-up-your-environment/ for more details on configuring OpenHAB


Raspbian Jessie as OpenHAB + MQTT Server: Part 1: Add Websockets!

My home automation system has been running stable for well over a year, but the versions of the software in use have started to show age.   For that matter, the hardware too.   I have been using an old Cubieboard A10 board to run OpenHAB +Mosquitto on, and lately, its been needing a reboot every now and then to keep on working

The friendly folks at FabCreator.com donated a new Raspberry Pi 3 to the LaserWeb project, so with that upgrade, I now have a spare Raspberry Pi B+ available (no longer in use for LaserWeb development) so I decided to repurpose this old B+ into a new OpenHAB+Mosquitto server.  Also, in the year or so since my last install, Mosquitto now comes with WebSocket support.  This is something I REALLY want to play with – would help a lot for adding quick dashboards onto the HAB (Not HAB – as in Home Automation Bus…  not OpenHAB – with WebSockets I can choose to listen in on the MQTT layer, and either just display updates, or also send MQTT messages to the broker, and in turn to the devices or the OpenHAB server. )

So, here we go:

1.  I downloaded Rasbian Jessie from Raspberrypi.org

2.  I burned it to an SD card with Win32DiskImager and booted up the Raspberry Pi B+

3.  Next, I configured a static IP, and did some standard setup (expand filesystem, allocate memory for headless use, overclock to medium, etc)

Install Mosquitto with WebSocket Support

The version of Mosquitto in the RPi repos doesnt support Websockets, so first we need to add a Repo from mosquitto.org, then install Mosquitto

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-jessie.list
sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients

Note: At the time of writing this gave me  Mosquitto version 1.4.9 (build date Fri, 03 Jun 2016 09:02:12 +0100)

Enable Websocket Support

Open the Mosquitto config in your favourite editor

sudo nano /etc/mosquitto/mosquitto.conf

By default, mosquitto comes without any listeners.  We want to add two listeners:  one standard MQTT protocol listener on port 1883,    and a second listener on port 1884 (for the Websocket protocol)

Your config file should look something like this:  Once done, save and exit.














Restart mosquitto:

sudo service mosquitto restart

If you want to confirm that worked, run:

sudo netstat -nlp | grep mosquitto






As you can see in the screenshot above, both :1883 and :1884 are listening (:

Now, if you are anything like me, you’re probably anxious to test that newfound feature first right? Lets test that Websocket Connection!
But i’m also lazy, so lets test it without a single line of code (;

Head over to http://mitsuruog.github.io/what-mqtt/

Enter your local Websocket IP and connect. Subscribe and publish – see if it works (: – mine did! First try, this went easier than I expected