Contents

Dash IoT Guide (Adafruit Bluefruit SPI Friend)

18 October 2023

This guide demonstrates how to make BLE, TCP and MQTT connections to an Adafruit Bluefruit SPI friend using the Arduino or PlatformIO IDE and the DashIO Adruino library. It also shows how to load user controls (widgets) into your mobile device to monitor and control an IoT device.

Getting Started

For the big picture on Dash, take a look at our website: dashio.io

For the Dash arduino library: github.com/dashio-connect/arduino-dashio

Requirements

Grab a development board, Adafruit Bluefruit SPI friend, Arduino IDE and follow this guide.

You will need to install the Dash app on your mobile phone or tablet.

Install

Dash

You will need to add the Dash library into your project. Download the Dash library from GitHub with the following link: https://github.com/dashio-connect/arduino-dashio

You will need to copy the following bluefruit files from the arduino Dash library examples/DashIO_ArduinoBluefruit into your project directory:

  • DashioBluefruitSPI.h
  • DashioBluefruitSPI.cpp
  • bluefruitConfig.h

Make sure you edit the bluefruitConfig.h file so the pins and settings match your hardware wiring.

BLE

You will need to install the Adafruit library for the Bluefruit SPI friend, which are available in the Arduino IDE Library Manager. Search for the library "Adafruit BluefruitLE nRF51" and install.

PlatformIO

Using Dash with the PlatformIO IDE is easy.

  1. Install the Dash library into your project. The easiest way to do this is to download the arduino-dashio library into the lib directory of your project.
  2. From the arduino-dashio library, go to the directory arduino-dashio/examples/DashIO_ArduinoBluefruit and copy the three bluefruit files in the directory into the src directory of your project.
  3. Edit the bluefruitConfig.h file so the pins and settings match your hardware wiring.
  4. Install the Adafruit BluefruitLE nRF51 library using the PlatformIO Libraries manager.
  5. Edit the platformio.ini file for you project by adding lib_ldf_mode = deep to enable all the required files to be found and also add monitor_speed = 115200 to set the required serial monitor speed.

Your finished platformio.ini file should look something like this:

[env:teensy36]
platform = teensy
board = teensy36
framework = arduino
lib_deps = adafruit/Adafruit BluefruitLE nRF51@^1.10.0
lib_ldf_mode = deep
monitor_speed = 115200

Guide

BLE Basics

Lets start with a simple BLE connection.

#include "DashioBluefruitSPI.h"

#define DEVICE_TYPE "BF_SPI_BLE_Type"
#define DEVICE_NAME "Betty Name"

DashDevice dashDevice(DEVICE_TYPE);
DashBluefruit_BLE  ble_con(&dashDevice, true);

void setup() {
    Serial.begin(115200);

    dashDevice.name = DEVICE_NAME;
    ble_con.begin(true);
}

void loop() {
    ble_con.run();
}

This is about the fewest number of lines of code necessary to get talking to the Dash app. There is a lot happening under the hood to make this work. After the #include "DashioBluefruitSPI.h" we create a device with the device_type as its only attribute. We also create a BLE connection, with the newly created device being an attribute to the connection. The second attribute of the BLE connection enables the received and transmitted messages to be printed to the serial monitor to make debugging easier.

In the setup() function we assign the device name to the device and start the BLE connection ble_con.begin(true);. The device_ID is automagically obtained from the macAddress of the Bluefruit SPI Friend.

The "true" attribute of the ble_conn.begin(true); causes the Bluefruit SPI friend to perform a factory reset to make sure everything is in a known state. This can be set to "false" when you have finished your code.

This device is discoverable by the Dash app. You can also discover your IoT device using a third party BLE scanner (e.g. BlueCap or "nRF Connect"). The BLE advertised name of your IoT device will be a concatentation of "DashIO_" and the device_type.

Setup the Arduino IDE serial monitor with 115200 baud and run the above code. Then run the Dash app on your mobile device and you will see connection "WHO" messages on the Arduino serial monitor as the Dash app detects and communicates with your IoT device.

Troubleshooting: Occasionally, the Dash app is unable to discover a BLE connection to the IoT device. If this occurs, try deleting the the IoT device from the Bluetooth Settings of your phone or tablet.

If you like, you can assign your own device_ID by changing the BLE connection begin function call with the following:

dashDevice.deviceID = "myUniqueDevieID";
ble_con.begin(true, false);

Lets add Dial control messages that are sent to the Dash app every second. To do this we create a new task to provide a 1 second time tick and then send a Dial value message from the loop every second.

#include "DashioBluefruitSPI.h"

#define DEVICE_TYPE "BF_SPI_BLE_Type"
#define DEVICE_NAME "Betty Name"

DashDevice dashDevice(DEVICE_TYPE);
DashBluefruit_BLE  ble_con(&dashDevice, true);

void setup() {
    Serial.begin(115200);

    dashDevice.name = DEVICE_NAME;
    ble_con.begin(true);
}

void loop() {
    ble_con.run();
    ble_con.sendMessage(dashDevice.getDialMessage("D01", int(random(0, 100))));
    delay(1000);
}

The line dashDevice.getDialMessage("D01", int(random(0, 100))) creates the message with two parameters. The first parameter is the control_ID which identifies the specific Dial control in the Dash app and the second parameter is simply the Dial value.

Once again, run the above code and the Dash app. This time, a new "DIAL" messages will be seen on the serial monitor every second.

The next step is to show the Dial values from the messages on a control on the Dash app.

In the Dash app, tap the All Devices button, followed by the Find New Device button. Then select the BLE Scan option to show a list of new IoT devices that are BLE enabled. Your IoT device shpuld be shown in the list. Select your device and from the next menu select Create Device View. This will create an empty Device View for your new IoT deivce. You can now add controls to the Device View:

Adding Controls to Dash App

Once you have discovered your IoT device in the Dash app and have a Device View available for editing, you can add controls to the Device View:

  • Start Editing: Tap the Edit button (it not already in editing mode).
  • Add Dial Control: Tap the Add Control button and select the Dial control from the list.
  • Edit Controls: Tap the Dial control to select it. The Control Settings Menu button will appear in the middle of the Control. The Control can then be dragged and resized (pinch). Tapping the button allows you to edit the Control settings where you can setup the style, colors and other characteristics of your Control. Make sure the Control_ID is set to the same value that is used in the Dial messages (in this case it should be set to "D01").
  • Quit editing: Tap the Edit button again.

The Dial on the Dash app will now show the random Dial values as they arrive.

The next piece of the puzzle to consider is how your IoT device can receive data from the Dash app. Lets add a Knob and connect it to the Dial.

In the Dash app you will need to add a Knob control onto your Device View, next to your Dial control. Edit the Knob to make sure the Control ID of the Knob matches what you have used in your Knob messages (in this case it should be "KB01"), then quit edit mode.

Next, in the Arduino code we need to respond to messages coming in from a Knob control that we just added to the Dash app. To make the changes to your IoT device we add a callback, processIncomingMessage, into the BLE connection with the setCallback function.

#include "DashioBluefruitSPI.h"

#define DEVICE_TYPE "BF_SPI_BLE_Type"
#define DEVICE_NAME "Betty Name"

DashDevice dashDevice(DEVICE_TYPE);
DashBluefruit_BLE  ble_con(&dashDevice, true);

void processIncomingMessage(MessageData *messageData) {
    switch (messageData->control) {
    case knob:
        if (messageData->idStr == "KB01") {
            String message = dashDevice.getDialMessage("D01", (int)messageData->payloadStr.toFloat());
            ble_con.sendMessage(message);
        }
        break;
    }
}

void setup() {
    Serial.begin(115200);

    dashDevice.name = DEVICE_NAME;
    ble_con.setCallback(&processIncomingMessage);
    ble_con.begin(true);
}

void loop() {
    ble_con.run();
}

We obtain the Knob value from the message data payload that we receive in the processIncomingMessage function. We then create a Dial message with the value from the Knob and send this back to the Dash app. And remember to remove the timer and the horrible delay from the loop:

When you adjust the Knob on the Dash app, a message with the Knob value is sent your IoT device, which returns the Knob value into the Dial control, which you will see on the Dash app.

Finally, we should respond to the STATUS message from the Dash app. STATUS messages allows the IoT device to send initial conditions for each control to the Dash app as soon as a connection becomes active. Once again, we do this from the processIncomingMessage function and our complete code looks like this:

#include "DashioBluefruitSPI.h"

#define DEVICE_TYPE "BF_SPI_BLE_Type"
#define DEVICE_NAME "Betty Name"

DashDevice dashDevice(DEVICE_TYPE);
DashBluefruit_BLE  ble_con(&dashDevice, true);

int dialValue = 0;

void processStatus(ConnectionType connectionType) {
    String message((char *)0);
    message.reserve(1024);

    message = dashDevice.getKnobMessage("KB01", dialValue);
    message += dashDevice.getDialMessage("D01", dialValue);

    ble_con.sendMessage(message);
}

void processIncomingMessage(MessageData *messageData) {
    switch (messageData->control) {
    case status:
        processStatus(messageData->connectionType);
        break;
    case knob:
        if (messageData->idStr == "KB01") {
              dialValue = messageData->payloadStr.toFloat();
            String message = dashDevice.getDialMessage("D01", dialValue);
            ble_con.sendMessage(message);
        }
        break;
    }
}

void setup() {
    Serial.begin(115200);

    dashDevice.name = DEVICE_NAME;
    ble_con.setCallback(&processIncomingMessage);
    ble_con.begin(true);
}

void loop() {
    ble_con.run();
}

This is just the beginning and there is a lot more we can do. Take a look at the examples in the library to see more details.

Layout Configuration

Layout configuration allows the IoT device to hold a copy of the complete layout of the device as it should appear on the Dash app. It includes all infomration for the device, controls (size, colour, style etc.), device views and connections.

When the Dash app discovers a new IoT device, it will download the Layout Configuration from the IoT device to display the device controls they way they have been designed by the developer. This is particularly useful developing commercial products and distributing your IoT devices to other users.

To include the Layout Configuration in your IoT device, simply follow these steps:

  • Design your layout in the Dash app and include all controls and connections that you need in your layout.
  • Export the layout: Tap on the Device button, then tap the Export Layout button.
  • Select the provisioning setup that you want (see below for provisioning details) and tap the Export button. The Layout Configuration will be emailed to you.
  • Copy and paste the C64 configuration text from the email into your Arduino code, assigning it to a pointer to store the text in program memory. Your C64 configuration text will be different to that shown below.
  • Add the pointer to the C64 configuration text (configC64Str) as a second attribute to the DashDevice object.
  • Add the Layout Config Revision integer (CONFIG_REV) as a third attribute to the DashDevice object.
const char configC64Str[] PROGMEM =
"lVNNb9swDP0rhQ47CUPiLM2WW2S7bhEndh3V3TAMgxursRZH8mQ5Hy3y30f5I83W7lD4QpOPpPge+Yx84qPx9x8YTecBAesZVRVP"
"0RhNh2RbrarLW5HE9zNvE4ZeFa8QRqU+5AwA8yCaTXxwLKXQSuY3jskivb7BMJEGIj8EImI5S0rAa1UxjDbJHo37vR5GRaKY0HWS"
"E9dJiqVxkleAHUFcc123mQr5AMGM8VWmo0Rzica9j9ZliwhlycEnAEmD0LTO5G7Gxcw0anoeOswpe/gJviFGa6hty1wq8wjGiouQ"
"izXUSHmSX8k8l7uybt8WMu4O/o2ZMGB3PNXZWWWM9q/6WdbIGg1geg7v7B2B7YXvRA3v1P1KG8u5aV2zSdga7vyusfzAa4xJ7DTG"
"wrdpi7f91nDi+L7WsNOIBJHjRgsjklY5cHIFWi34E8QGwPFK8RQGqjaiRGPLMuSBKo2nG1pUmxOk/7fYrW6mNJEqZaoj5z7jmnWR"
"9UqkXYAYeRv/f7FUJaKst2N5ALZMyy45WRp1us1wQI6LDxftgph8aiJE7t9o14VeVTccRCA0MDI4g74Q1f+MEYex58nGdL0r0LEW"
"a+Kfn8uXXqx+E3JNPe/GyTLuFtNf+9uzcyGTCP4KxZa8rPd18A+ZNZfvvw/Dwnvuo95tnp/49xRjAr11Jf2h+eAlkgv9Iu5p82Ez"
"Hpg6a2C7c+pGb97ge66kElyDHAidH4x9HbVnQr0ovG5MQum8s7z2ZuwruJRnlLItX7IF01UBpQRoh3f8kWO9LH4WUmmcJmWGZ7eU"
"YuK7mNphM5FT58Wc7dp9f1xFbAvm8fgH";


DashDevice    dashDevice(DEVICE_TYPE, configC64Str, CONFIG_REV);

Here is our Knob and Dial example, with Layout Configuration added:

#include "DashioBluefruitSPI.h"

#define DEVICE_TYPE "BF_SPI_BLE_Type"
#define DEVICE_NAME "Betty Name"

const char configC64Str[] PROGMEM =
"lVNNb9swDP0rhQ47CUPiLM2WW2S7bhEndh3V3TAMgxursRZH8mQ5Hy3y30f5I83W7lD4QpOPpPge+Yx84qPx9x8YTecBAesZVRVP"
"0RhNh2RbrarLW5HE9zNvE4ZeFa8QRqU+5AwA8yCaTXxwLKXQSuY3jskivb7BMJEGIj8EImI5S0rAa1UxjDbJHo37vR5GRaKY0HWS"
"E9dJiqVxkleAHUFcc123mQr5AMGM8VWmo0Rzica9j9ZliwhlycEnAEmD0LTO5G7Gxcw0anoeOswpe/gJviFGa6hty1wq8wjGiouQ"
"izXUSHmSX8k8l7uybt8WMu4O/o2ZMGB3PNXZWWWM9q/6WdbIGg1geg7v7B2B7YXvRA3v1P1KG8u5aV2zSdga7vyusfzAa4xJ7DTG"
"wrdpi7f91nDi+L7WsNOIBJHjRgsjklY5cHIFWi34E8QGwPFK8RQGqjaiRGPLMuSBKo2nG1pUmxOk/7fYrW6mNJEqZaoj5z7jmnWR"
"9UqkXYAYeRv/f7FUJaKst2N5ALZMyy45WRp1us1wQI6LDxftgph8aiJE7t9o14VeVTccRCA0MDI4g74Q1f+MEYex58nGdL0r0LEW"
"a+Kfn8uXXqx+E3JNPe/GyTLuFtNf+9uzcyGTCP4KxZa8rPd18A+ZNZfvvw/Dwnvuo95tnp/49xRjAr11Jf2h+eAlkgv9Iu5p82Ez"
"Hpg6a2C7c+pGb97ge66kElyDHAidH4x9HbVnQr0ovG5MQum8s7z2ZuwruJRnlLItX7IF01UBpQRoh3f8kWO9LH4WUmmcJmWGZ7eU"
"YuK7mNphM5FT58Wc7dp9f1xFbAvm8fgH";

DashDevice dashDevice(DEVICE_TYPE, configC64Str, 1);
DashBluefruit_BLE  ble_con(&dashDevice, true);

int dialValue = 0;

void processStatus(ConnectionType connectionType) {
    String message((char *)0);
    message.reserve(1024);

    message = dashDevice.getKnobMessage("KB01", dialValue);
    message += dashDevice.getDialMessage("D01", dialValue);

    ble_con.sendMessage(message);
}

void processIncomingMessage(MessageData *messageData) {
    switch (messageData->control) {
    case status:
        processStatus(messageData->connectionType);
        break;
    case knob:
        if (messageData->idStr == "KB01") {
              dialValue = messageData->payloadStr.toFloat();
            String message = dashDevice.getDialMessage("D01", dialValue);
            ble_con.sendMessage(message);
        }
        break;
    }
}

void setup() {
    Serial.begin(115200);

    dashDevice.name = DEVICE_NAME;
    ble_con.setCallback(&processIncomingMessage);
    ble_con.begin(true);
}

void loop() {
    ble_con.run();
}

Jump In and Build Your Own IoT Device

When you are ready to create your own IoT device, the Dash Arduino C++ Library will provide you with more details about what you need to know:

https://dashio.io/arduino-library/