OpenHAB, MQTT, Arduino and ESP8266: Part 5.1: Graphing Sensor Data

The following series of posts will document my journey to full Home Automation using the MQTT protocol, all custom hardware (Arduino and ESP8266 based), and all tied together using OpenHAB

  • Part 1:  Setting up the server / environment
  • Part 2:  Publish, Subscribe, Command, State, and WTFs
  • Part 3:  Hardware:  Arduino with Ethernet Shield
  • Part 4:  Hardware:  ESP8266 with NodeMCU firmware
  • Part 5:  Hardware:  Sensors
    • Part 5.1:  Graphing Sensor Data
  • Part 6:  OpenHAB Automation Rules

So now that we have a pretty basic “remote control” system – not quite Home Automation yet, but pretty nifty already – you should have enough knowledge to add several switches and sensors at the very least. Keep in mind any value you could have an Arduino find and publish to MQTT can be used, whether its an analogRead() or a simple ON/OFF status message, anything can be used.

OpenHAB comes with a functionality called Persistence, which basically means storing values/states in a database.   This could be as complicated a MySQL, or – the way I prefer for now – a simple RRD deployment.   RRD is used in a LOT open source project to provide graphing – the first time I encountered RRD was on PfSense’s traffic logs for example (:

To configure RRD4J as a Persistance add-on for OpenHAB is extremely simple!

1.  Install the rrd4j add-on:

sudo apt-get install openhab-addon-persistence-rrd4j

2.  Edit the Persistence file:

sudo vi /etc/openhab/configurations/persistence/rrd4j.persist

Add:

/ Configuration file for "rrd4j" persistence module

// persistence strategies have a name and a definition and are referred to in the "Items" section
Strategies {

// for rrd charts, we need a cron strategy
everyMinute : "0 * * * * ?"
}

Items {

// let's store EVERYTHING - we may need it later (:
* : strategy = everyMinute
}

You could also choose to only share specific states:

/ Configuration file for "rrd4j" persistence module

// persistence strategies have a name and a definition and are referred to in the "Items" section
Strategies {

// for rrd charts, we need a cron strategy
everyMinute : "0 * * * * ?"
}

Items {

// let's only store temperature values in rrd
Office_temp : strategy = everyMinute
}

3.  Add a chart to your sitemap:

Notes:  You can change the Period to h,D,W,M,Y etc (Hour, Day, Week, Month, Year

sitemap dolphin label="Main Menu"
{
        Frame label="MQTT" {
        Switch item=mqttsw1 label="MQTT Switch 1"
        Switch item=mqttsw2 label="MQTT Switch 2"
        Switch item=lamp1 label="Office Lamp"
        Text item=Office_temp
        }

        Frame label="Temperature Graph" {
        Chart label="Last Hour Temp" item=Office_temp period=h refresh=30000
        }
}

Save, exit and restart OpenHAB

sudo /etc/init.d/openhab restart

Give it a minute or two, and you should see it has created a RRD database for your items:

peter@www:/$ cd /usr/share/openhab/etc/rrd4j
peter@www:/usr/share/openhab/etc/rrd4j$ ls
Office_temp.rrd

Next, give it 3-4 minutes and test your Sitemap:

tempgraph

Easy?   Definately the easiest way I have ever used to map a value (:

To recap how this works:

  1. Arduino with MQTT Publishes temperature to a MQTT Broker
  2. OpenHAB pulls this value into a “Number” Item’s state from a configuration in your .items file
  3. We display the current value in a Text item in the .sitemap
  4. We enable the RRD4J module by apt-get installing it, and then placing a configuration in /etc/openhab/configurations/persistance/rrd4j.persist
  5. It creates .rrd files in /usr/share/openhab/etc/rrd4j/
  6. We can access the graph from a Graph entry in the sitemap
Advertisements