MQTT and Websockets


I have been experimenting with a new web site to control my house as the current one, written in PHP, is very slow as it gets data using TCP connections to HouseControl.

I wanted to get as much of the data from MQTT as possible. I experimented with a node.js web site, which read the MQTT data in the server and sent it to the browser in a refreshing page. This worked but the data was slightly out of date, and the page refresh flash was annoying.

So I need a Javascript-based web site to avoid these problems. The easiest way to do this was to use a version of the Mosquitto MQTT server, that supported Websockets. The 1.4 version supports it, but only if you build it with Websocket support. So I followed Martin Harizanov’s guide, and installed it on a Raspberry Pi 2. I had slight problem with this, as the service script gave a syntax error, and as I had lost the indentation, it was hard to diagnose, so I copied the one from my emonPi. Also, as the script puts things in /usr/local, not /usr, I had to run “ldconfig”, to update symbolic links.

The web app works well and data is updated in real time.

I configure the page with a json file, rooms.json on the server:

"emonhub/rx/5/values":{"Total": null},
"/house/rooms/first-floor/kitchen/dishwasher/power":{"Dish washer":null},
"Utility room":{
"/weather/temperature":{"Temperature":null, "scale": 0.1},
"/weather/pressure":{"Pressure":null, "scale": 0.01}
"/house/rooms/first-floor/kitchen/light-level":{"Light Level":null},
"Living Room":{
"/house/rooms/first-floor/living-room/front/temperature":{"Front temperature":null},
"/house/rooms/first-floor/living-room/back/temperature":{"Back temperature":null},
"/house/rooms/first-floor/living-room/front/light-level":{"Front Light Level":null},
"/house/rooms/first-floor/living-room/back/light-level":{"Back Light Level":null},
"/house/rooms/first-floor/living-room/front/occupied":{"Front Occupied":null},
"/house/rooms/first-floor/living-room/back/occupied":{"Back Occupied":null},
"/house/rooms/first-floor/living-room/couch/occupied":{"Couch Occupied":null}},
"/house/rooms/second-floor/landing/lightlevel":{"Light Level":null},
"/house/rooms/second-floor/bathroom/temperature":{"Temperature":null, "scale": 0.1}
"Second bedroom":{
"/house/rooms/second-floor/second-bedroom/temperature":{"Temperature":null,"scale": 0.1},
"/house/rooms/second-floor/second-bedroom/humidity":{"Humidity":null, "scale":0.1}
"Master bedroom":{
"/house/rooms/second-floor/master-bedroom/temperature":{"Temperature":null, "scale": 0.1},
"/house/rooms/second-floor/master-bedroom/light-level":{"Light level":null}

I could have easier configuration if I made more use of the topic hierarchy, e.g. show everything under /house/rooms, but the way I have things is more flexible and lets me choose which items to display.

Currently this web site only supports monitoring devices, not controlling them. I think I might add Websocket support to HouseControl, or I might do a node.js gateway that converts a Websocket to the tcp socket that HouseControl currently supports. HouseControl does support controlling things by publishing to MQTT, but you get no reply from that, and sometimes I need the reply.

This entry was posted in Home automation and tagged . Bookmark the permalink.

One Response to MQTT and Websockets

  1. hi thanks for the detailed description of working with NodeMCU. Try this system to control this . The only thing that is on the development of Russian. It may be difficult to understand.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s