OpenHAB2, Mosquitto and MySensors Gateway post

my first sensor is a DHT22 (temperature/humidity, very good accuracy), attached to a 3.3V pro mini (ATMEGA 328p), connected via a NRF24 l 01+ (2.4GHz) which is capable of low-power transmission. This enables the arduino to later work on battery power only. My goal is to build three of these sensors. The assembly is straight forward:

pro mini with sensor and transceiver attached

I had many setbacks, connecting the sensor to the Raspberry Pi, though. This forced me to understand how a basic MQTT infrastructure has to interact with its data sources and OpenHAB2:

infrastructure with DHT22 attached

Steps to connect:

  • add node ID and parent ID to sketch
  • define lower baud rate for later running the board at a lowered clock speed
  • get MySensors Gateway working on RPi
  • Connect GateWay to Mosquitto
  • Connect Mosquitto to the OpenHAB MQTT binding

Step by step

Following the basic steps in more detail. Keep in mind that starting with OpenHAB2, the "old" bindings like MQTT are not shown in PaperUI, even when installed. So you'd have to configure all items manually (which took me a few hours to find out)!

Sensor IDs and baud settings in sketch

#define MY_NODE_ID 101

// Define a lower baud rate for Arduino's running on 8 MHz (Arduino Pro Mini 3.3V & SenseBender)
#if F_CPU == 8000000L
#define MY_BAUD_RATE 9600

I've set up my sensor node IDs in "namespaces" like that:

  • 1xx environmental sensor nodes
  • 2xx extra nodes like elecrity IR reader

Attaching the NRF24 is relatively straight forward, as is connecting the DHT22.

Install and configure mosquitto

The current version, provided by apt is 1.4.x - I only managed to get it running in a stable manner when using 1.5.

- name: Add mosquitto repository
    state: present

- apt_repository:
    repo: "deb stretch main"
    update_cache: True
    state: present 

- name: install packages
  apt: pkg= state=latest
    - mosquitto
    - mosquitto-clients

Install MySensors gateway

In hindsight this is quite easy. Most of the time I needed, went into a reproducable ansible script ;-) You can find instructions here. Since I won't be exposing my OpenHAB instance to the internet - only available via VPN - I will not introduce authentication.

So, follow the instructions and install/compile the gateway. Keep in mind, that you will need mosquitto installed, too. I'd also opt for the MQTT binding for OpenHAB, since it's working fine with mosquitto. You can use the defaults @ mosquitto.

Some ansible help:

- name: get mysensors mqtt repo
    dest: /home/pi/MySensors
    accept_hostkey: yes
    version: master

- name: configure
  command: "./configure --my-transport=nrf24 --my-gateway=mqtt --my-controller-ip-address= --my-mqtt-publish-topic-prefix=mysensors-out --my-mqtt-subscribe-topic-prefix=mysensors-in --my-mqtt-client-id=mysensors-gateway"
    chdir: /home/pi/MySensors

- name: copy mysensors config
  template: src=./mysensors.conf.j2 dest=/etc/mysensors.conf owner=root group=root mode=755

- name: make
  command: "make"
    chdir: /home/pi/MySensors

- name: install
  command: "make install"
    chdir: /home/pi/MySensors
    - start mysqw

It's that easy, since all components reside within the RPi. Don't forget to attach a NRF24 to your RPi aswell!

Install the MQTT binding and influxdb

Two parts: Install influxdb and client and manually install the bindings in OpenHAB.

- name: Add influxdb apt key
    state: present

- apt_repository:
    repo: "deb stretch stable"
    update_cache: True
    state: present

 - name: install base packages
  apt: pkg= state=latest
    - influxdb
    - influxdb-client
    - start influxdb

For influxdb, use the defaults provided here. Don't forget to configure persistence for OpenHAB (influxdb.persist)!

Strategies { default = everyChange }
Items { * : strategy = everyUpdate, restoreOnStartup }

Configure OpenHAB

I installed OpenHAB as a package, so no OpenHABbian. This means: All configuration resides in /etc/openhab2 I strongly(!!!) suggest you to pack the whole directory into a separate git repository! You will configure a lot via config files and since PaperUI persists only in json files, which most of the times are generated from the config files, the latter are the safer bet.






Group    Home                    "home"                   <house>

Group    GF                      "Ground Floor"           <groundfloor>   (Home)
Group    FF                      "First Floor"            <firstfloor>    (Home)
Group    F2                      "Second Floor"           <attic>         (Home)
Group    F3                      "Third Floor"            <attic>         (Home)

Group    GF_Cellar               "Cellar"                 <cellar>        (Home, GF)
Group    FF_LivingDining         "Living & Dining Room"   <sofa>          (Home, FF)
Group    F2_Bedroom              "Bedroom"                <bedroom>       (Home, F2)
Group    F2_KidsRoom             "Kids Room"              <girl_3>        (Home, F2)
Group    F3_GuestRoom            "Guest Room"             <parents_4_3>   (Home, F3)

Number DHT22_101_01_cellar_temp  "temperature [%.1f °C]" <temperature> (gTemperature, GF_Cellar) {mqtt="<[mosquitto:mysensors-out/101/1/1/0/0:state:default]"}
Number DHT22_101_02_cellar_hum  "humidity [%.1f %%]" <humidity> (gHumidity, GF_Cellar) {mqtt="<[mosquitto:mysensors-out/101/0/1/0/1:state:default]"}

Group:Number:AVG   gTemperature   "Temperature"   <temperature>   (Home)
Group:Number:AVG   gHumidity      "Humidity"      <humidity>      (Home)

That's a lot in the items but mostly it's auto-generated grouping. The really interesting lines are the ones starting with "Number". They actually handle the processing of incoming sensor values for the temp/hum DHT22 sensor. Addotional sensor definitions (for DHT22s) would be located in here, too.

If you've followed this guide, you should have a working sensor with MySensors framework communicating with a RPi, having a MQTT gateway, broker and binding, pushing data into an influxdb.

Categories: tinkering, arduino, openhab

Tags: tinkering, IoT, smart home, arduino, mysensors