Introduction: How to Bridge Homie Nodes to a PiDome Server?

If you have an interest in Home Automation, IoT and ESP8266; you likely have crossed path of things like :

You might also have crossed the path of MQTT and brokers like Mosquitto. Other components like RFM69 and NRF24L01+.

Basically, it's a world of buzz words lacking concrete consistency.

My own devices are, well, just a few. Only 3 D1 Mini on breadboards to try things.

One thing I was not able to achieve was to have good WiFi based devices, two-way communication and maximum flexibility over controllers sketches. I wasn't willing to just buy expensive stuff and not learn anything.

I ended up by setting :

  • PiDome on my Raspberry Pi, enabling it's builtin MQTT broker
  • Two D1 Mini devices speaking MQTT. One running custom Arduino software and the other running Homie ESP8266 2.x from Marvin Roger

Only issue was... it's pretty easy to write custom code to match PiDome MQTT topics' format. It's quite harder to make 2 "proprietary" MQTT topic-based protocols speak to each other. Namely PiDome and Homie.

This is where it all began...

Step 1: Background

My Homie sketch advertises a DHT22 sensor and a LED to simulate a light switch.

You can find the complete source at my GitHub repository Homie-DHT22.

  • Homie will publish temperature to a topic like : homie/5ccf7fd3945b/temperature/degrees (19.20)
  • To enabled the LED, we have to publish a topic like : homie/5ccf7fd3945b/switch/on/set (true)

My PiDome instance has a custom MQTT device created in order to access the DHT22 temperature (Celcius) / humidity (relative) and light switch state (on/off).

PiDome speaks another language, it wants previous Homie topics in these formats:

  • /hooks/devices/17/dht/temp (19.20)

  • /hooks/devices/17/LED/on (true)

Am I seeking for troubles? Yes sir.

Step 2: The Logic

We already have 3 parties playing here:

  1. A Homie node
  2. A PiDome server
  3. A MQTT broker

What we need is a translator between Homie language and PiDome language (well, topics).

Here comes a new challenger! The "MQTT Message Translator".

In the sequence diagram you can see that we are dealing with:

  1. Homie device sends temperature to broker
  2. Translator receives this information
  3. Translator converts this information so that PiDome can understand it
  4. Translator sends back temperature to broker using PiDome format
  5. PiDome can now happily read the temperature

We afterward have a new message:

  1. We pressed some button in PiDome, this is the light switch! PiDome sends command to broker
  2. Translator receives this information
  3. Translator converts this information so that Homie node can understand it
  4. Translator sends back the command to broker using Homie format
  5. Homie node can now react and turns the light on or off

Step 3: Creating the Translator

The translator is a quite simple Python script built using Paho MQTT Client for Python.

In order to translate messages we need to :

  1. Subscribe to published messages from Homie node
  2. Subscribe to published messages from PiDome
  3. NOT subscribe to translated messages

The translation process works by:

  • Detecting if its a Homie or Pidome topic
  • Splitting source topic into parts
  • Convert between Homie device ID ("5ccf7fd3945b") and PiDome device ID ("17")
  • Convert commands from:
    1. Homie "temperature/degrees" to PiDome "dht/temp"
    2. PiDome "LED/on" to Homie "switch/on/set". The extra "/set" says its a setter.
  • Compose new topic based on converted device ID and verb/action

You can find all details at my Homie-PiDome-MQTT-translator repository.