Slack Button Tutorial

Requirements:

Raspberry Pi 3 B running Raspbian 8 (Jessie) nRF51-Dongle

Other Debian based Distros may work to, but package names may differ. Also we assume that your pi runs safely behind your firewall - Security is not covered by this tutorial.

Other BLE Hardware can also be used, but in this case you need different firmware. If you plan to implement your own, please have a look at the updateManufacturerData function.

Raspberry Pi setup

Logon to your raspberry as user with sudo rights (usually pi)

Add Mono and mosquitto repositories

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list

sudo wget -O /etc/apt/sources.list.d/mosquitto.list http://repo.mosquitto.org/debian/mosquitto-jessie.list
wget -O- http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key | sudo apt-key add -

Update your system and install required packages

sudo apt-get update
sudo apt-get upgrade

sudo apt-get install mono-complete nuget nunit-console mosquitto-clients mosquitto bluez-hcidump bluez-tools nodered git

Download and compile otraw2q

wget https://raw.githubusercontent.com/acolono/opentrigger-otraw2q/master/otraw2q.c
gcc -o otraw2q -O3 -Wall otraw2q.c
sudo install -s -m 755 -o root -g root otraw2q /usr/local/bin/
rm otraw2q otraw2q.c

Clone and build distributor

cd ~ && mkdir src && cd src
git clone https://github.com/acolono/opentrigger-distributor
cd opentrigger-distributor

nuget restore
xbuild /tv:4.0 /p:Configuration=Release /t:Rebuild

Prepare your nRF51-Dongle

Connect up to 6 buttons

nRF51 comes with internal pullups, so you need one resistor:

For a generic introduction on this please have a look at

this tutorial

.

Programm the firmware

Go to the opentrigger-nRF51 firmware repository and click Build repository. After building is complete you can download the Firmware as .hex file. Plug your nRF51 into an usb port and copy the .hex file there.

  sudo mkdir /mnt/jlink
  sudo mount /dev/disk/by-label/JLINK /mnt/jlink
  sudo cp firmware.hex /mnt/jlink/
  sudo umount /mnt/jlink/

Test your Button

While the button is pressed, the red and blue LEDs should be lit. One you release it, the red LED should go dark. The blue LED will go dark a couple of seconds later.

Configuration and Services

Listen for Bluetooth Advertisements

Copy and paste this into any terminal, make sure you include the curly braces.

{
  hciconfig | grep -Po '^hci[0-9](?=:)' | while read -r hcidevice ; do
    hcimac=$(hciconfig $hcidevice | grep -m1 -Po '(?<=Address: )[0-F:]+')
    echo $hcidevice $hcimac
    sudo hcitool -i $hcidevice lescan --duplicates > /dev/null &
    sudo hcidump -i $hcidevice --raw | otraw2q | mosquitto_pub -t "/opentrigger/rawhex/$hcimac" -l &
  done
}

to check if it is working issue mosquitto_sub -t '/opentrigger/rawhex/+' and push on your button, you should see some hex strings fly by.

Distributor

create a file called ~/my-distributor-config.json and add the MAC address of your button to the IncludedMacs parameter. If you dont want to filter for MAC addresses you can set it to null (or remove the line).

{
  "QueueDistributorConfigs": [

    // one or mor DistributorConfig/s
    {
      "Connection": "tcp://127.0.0.1:1883", /* tcp://host:port of mqtt server */
      "QosLevels": [ "AtMostOnce" ], /* AtMostOnce | AtLeastOnce | ExactlyOnce */
      "RawHexTopics": [ "/opentrigger/rawhex/#" ], /* topics to subscribe to, expecting bluetooth packages in HexStream Format */
      "TriggerTopic": "/opentrigger/signals/trigger", /* trigger messages will be published to this topic */
      "ReleaseTopic": "/opentrigger/signals/release", /* release messages will be published to this topic */
      "PublishFormat": "JsonPretty", /* Json | JsonPretty | HexString */
      "ClientId": null, /* null will generate a random ClinetÍd */
      "Distance": 100, /* possible delay between signals */
      "Skip": 0,
      "UniqueIdentifier": "MacAndTokenCubePir", /* Mac | MacAndAdvertisingData | MacAndTokenCubePir */
      "IncludedMacs": [ "00:00:00:00:00:00" ], /* Mac Address of your Button/s */
    },

  ],
  "Verbosity": 0, /* 0-5 */
  "IdleCycle": 500,
}

and than start it:

~/src/opentrigger-distributor/com.opentrigger.distributor/cli/bin/Release/distributord.exe -c ~/my-distributor-config.json &> ~/my-distributor.log &

for testing, subscribe to the release topic and press a button:

mosquitto_sub -t '/opentrigger/signals/release'

you should see something like this:

{
  "Age": 1645,
  "Timestamp": "2016-06-21T12:46:34.780657+00:00",
  "Packet": {
    "Mac": "CA:9F:0A:00:66:20",
    "Rssi": -33,
    "RawData": "BD4gAgEAASBmAAqfyhQCAQYQ/+7/BAERBwEPQQEBAQABAd8=",
    "AdvertisingData": "AgEGEP/u/wQBEQcBD0EBAQEAAQE=",
    "ManufacturerSpecific": {
      "CompanyId": "7v8=",
      "Data": "BAERBwEPQQEBAQABAQ==",
      "SensorData": {
        "PIR": "True",
        "EventId": "65",
        "DigitalInput0": "1",
        "DigitalInput1": "1",
        "DigitalInput2": "1",
        "DigitalInput3": "0",
        "DigitalInput4": "1",
        "DigitalInput5": "1",
        "DigitalInputs": "1,1,1,0,1,1",
        "UnknownData": ""
      }
    }
  },
  "UniqueIdentifier": "CA:9F:0A:00:66:20",
  "OriginTopic": "/opentrigger/rawhex/B8:27:EB:9A:2B:91",
  "Rssi": 0
}

Node-RED

enable Node-Red Service

sudo systemctl enable nodered.service

and start it

sudo service nodered start

Browse to http://raspberrypi:1880/ and import this Flow: http://flows.nodered.org/flow/da794433d3ef6335d79af75d0b7dcb26

now go to https://my.slack.com/services/new/bot and add a new bot to the channel you want to post to. You will get an Url wich looks like that:

https://hooks.slack.com/services/SDA28HD/ASDK83JA/S9ADSk99DDASDlka

Modify the slack node and paste your Url in there

You can costumize the message by editig the Coffee is ready! node. Slack Webhook documentation can be found here:

https://api.slack.com/incoming-webhooks

Now press your Button Connected to P0.15 and you should see a Message on Slack!

Last updated