Skip to content
perrin7 edited this page Sep 19, 2015 · 7 revisions

NinjaCape MQTT bridge

Written to replace the Ninjablock firmware with openHAB. This script grabs the data from the Arduino (NinjaCape) over the serial port and publishes it as MQTT messages. It also monitors (subscribes) the MQTT topic "ninjaCape/output/#" and outputs any messages to the serial port.

You don't need to use openHAB, you could use anything that can publish/subscribe MQTT messages.

Prerequisites

  1. mosquitto installed on the local network (I've got it installed on the NinjaBlock)
  2. Python and the following packages:

openHAB item configuration examples

The MQTT messages from the ninjaCape are structure as follows:

topic: ninjaCape/input/<DeviceType>
payload: <DeviceAddress>
or
topic: ninjaCape/output/<DeviceType>
payload: <DeviceAddress>

Where <DeviceType> is the ID number specified by the ninjaCape. The one's I've used are below. A full list is provided here

Device Type Device
11 Generic switch device (Sockets, Buttons, PIRs)
30 Humidity Sensor
31 Temperature Sensor
999 NinjaBlock Status Light
1007 NinjaBlock Eyes

To find your <DeviceAddress> the easiest way is to run this in the command line (on the same machine mosquitto is running)

mosquitto_sub -d -t "ninjaCape/input/#"

This will stream all the data received 433Mhz, you can then copy/paste the DeviceType and DeviceAddress for use in the item definitions below.

You can read about how to structure the MQTT item definition here

Sockets

This defines a 433MHz 'socket'. It has 4 separate items, 2 inputs for state-tracking and 2 outputs for the commands. The inputs are there so if I used the external remote, openHAB still tracks the state.

Switch Socket1 "Socket 1" 	{mqtt="<[mosquitto:ninjaCape/input/11:state:ON:110111110111100100001110], 
							 <[mosquitto:ninjaCape/input/11:state:OFF:110111110111100100000110],
							 >[mosquitto:ninjaCape/output/11:command:OFF:110111110111100100000110],
							 >[mosquitto:ninjaCape/output/11:command:ON:110111110111100100001110]"
	    					}

Temperature/Humidity Sensor

Number Temperature	"Temperature [%.1f °C]"	(climate)	{mqtt="<[mosquitto:ninjaCape/input/31:state:default]"}
Number Humidity		"Humidity [%.1f %%]"		(climate)	{mqtt="<[mosquitto:ninjaCape/input/30:state:default]"}

PIR's

Switch PIR_1 "Motion Sensor 1" (Persist_Change) {mqtt="<[mosquitto:ninjaCape/input/11:state:ON:010101010101010101010101]"}

Ninja Eyes / Ninja Status Light

These are a bit tricky. I had to write two JavaScript 'transformation' functions to convert from RGB (for the NinjaCape) to HSB color (for openHAB) and vice-versa. The JavaScript functions are ninjaEyesColor for inputs and ninjaEyesOutput for outputs. You have to add the transformation files to the configurations/transform sub-folder in your openHAB directory.

Color NinjaEyes    {mqtt="<[mosquitto:ninjaCape/input/1007:state:JS(ninjaEyesColor.js)],
						  >[mosquitto:ninjaCape/output/1007:command:*:JS(ninjaEyesOutput.js)]"}

Color StatusLight   {mqtt="<[mosquitto:ninjaCape/input/999:state:JS(ninjaEyesColor.js)],
						   >[mosquitto:ninjaCape/output/999:command:*:JS(ninjaEyesOutput.js)]"}