Contents

DashIO IoT Guide (Arduino SAMD)

5 October 2021

So, what is DashIO? It is a quick effortless way to connect your IoT device to your phone. It allows easy setup of controls such as Dials, Text Boxes, Maps, Graphs, Notifications..., from your IoT device. You can define the look and layout of the controls on your phone from your IoT device. There are three methods to connect to your phone; Bluetooth Low Energy (BLE), TCP or MQTT. What's Dash then? Dash is a MQTT server with extra bits added in to allow you to send notifications, share your devices, and save your settings from your phone via the DashIO app.

This guide demonstractes how to make BLE, TCP and MQTT connections to an Arduino SAMD based IoT device using the Arduino IDE. The boards specifically covered in this guide are the Arduino NANO 33 IoT and Arduino MKR 1010.

The MQTT connection discussed in this guide is SSL/TLS enabled and we highly recommend that you only use SSL/TLS connections. The Dash MQTT server only accepts SSL connections.

Getting Started

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

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

Requirements

Grab an Arduino board, Arduino IDE and follow this guide.

You will need to install the DashIO app on your pobile phone or tablet. And if you'd like to connect to one IoT device for free, setup a free DashIO account from within the DashIO app.

Install

DashIO

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

Place the DashIO library files into your project directory or add them into your library.

BLE

We will use the ArduinoBLE library for BLE the connection, which is included in the Arduino IDE Library Manager. Search the library manager for the library titled "ArduinoBLE" and install.

WiFi and TCP

For WiFi and TCP connections we will use the WiFiNINA library, which is in the Arduino IDE Library Manager. Search the library manager for the library titled "WiFiNINA" and install.

MQTT

For MQTT we are using the ArduinoMqttClient library. This is available in the Arduino IDE Library Manager, by searching for "ArduinoMqttClient" and installing the library.

The arduino-timer library is also use for the MQTT connection and is available in the Arduino IDE Library Manager. Search the library manager for the library titled "arduino-timer" and install.

Provisioning

If you wish to use the DashIO provisioning library, you will need to install the Arduino Flash Storage library. Search the arduino library manager for the library titled "Flash Storage" and install.

Guide

BLE Basics

Lets start with a simple BLE connection.

#include "DashioSAMD_NINA.h"

DashioDevice dashioDevice("SAMD_NINA_Type");
DashioBLE    ble_con(&dashioDevice, true);

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

    ble_con.begin();
    dashioDevice.setup(ble_con.macAddress(), "Bob"); // unique deviceID, and device name
}

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

This is about the fewest number of lines of code to get talking to the DashIO app. There is a lot happening under the hood to make this work. After the include 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.

In the setup() function we start the BLE connection ble_conn.begin(). For BLE connections we start the connection before the device setup. This is to make sure we get a sensible value for the mac address, which is then supplied to the device as a unique device_ID.

We then setup the device with two parameters. These parameters describe the device to the DashIO app to allow it uniquely identify each device.

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

Lets add Dial control messages that are sent to the DashIO 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 "DashioSAMD_NINA.h"
#include <timer.h>

DashioDevice dashioDevice("SAMD_NINA_Type");
DashioBLE    ble_con(&dashioDevice, true);

auto timer = timer_create_default();
bool oneSecond = false; // Set by hardware timer every second.

// Timer Interrupt
static bool onTimerCallback(void *argument) {
  oneSecond = true;
  return true; // to repeat the timer action - false to stop
}

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

    ble_con.begin();
    dashioDevice.setup(ble_con.macAddress(), "Bob"); // unique deviceID, and device name

    timer.every(1000, onTimerCallback); // 1000ms
}

void loop() {
    timer.tick();
    ble_con.checkForMessage();

    if (oneSecond) { // Tasks to occur every second
        oneSecond = false;
        ble_con.sendMessage(dashioDevice.getDialMessage("D01", int(random(0, 100))));
    }
}

The dashioDevice.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 DashIO app and the second parameter is simply the Dial value.

It would be nice to have the DashIO app automagically setup a new Device View and place your control on the new Device View. To do that we need to add a few more lines of code. Most importantly, we need to respond to messages coming in from the DashIO app. To do this we add a callback into the BLE connection with the setCallback function:

#include "DashioSAMD_NINA.h"
#include <timer.h>

DashioDevice dashioDevice("SAMD_NINA_Type");
DashioBLE    ble_con(&dashioDevice, true);

auto timer = timer_create_default();
bool oneSecond = false; // Set by hardware timer every second.

// Timer Interrupt
static bool onTimerCallback(void *argument) {
  oneSecond = true;
  return true; // to repeat the timer action - false to stop
}

void processConfig(ConnectionType connectionType) {
    String message((char *)0);
    message.reserve(2048);

    message += dashioDevice.getConfigMessage(DeviceCfg(1)); // One Device Views

    DialCfg first_dial_control("D01", "DV01", "Dial", {0.24, 0.14, 0.54, 0.26});
    message += dashioDevice.getConfigMessage(first_dial_control);

    DeviceViewCfg deviceView("DV01", "A Dial", "up", "0");
    message += dashioDevice.getConfigMessage(deviceView);

    ble_con.sendMessage(message);
}

void processIncomingMessage(MessageData *messageData) {
    switch (messageData->control) {
    case config:
        processConfig(messageData->connectionType);
        break;
    }
}

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

    ble_con.setCallback(&processIncomingMessage);
    ble_con.begin();
    dashioDevice.setup(ble_con.macAddress(), "Bob"); // unique deviceID, and device name

    timer.every(1000, onTimerCallback); // 1000ms
}

void loop() {
    timer.tick();
    ble_con.run();

    if (oneSecond) { // Tasks to occur every second
        oneSecond = false;
        ble_con.sendMessage(dashioDevice.getDialMessage("D01", int(random(0, 100))));
    }
}

The processIncomingMessage callback checks the message type and calls the appropriate function. At this stage we are only interested in CONFIG messages and the processConfig function is called appropriately. This part of the code creates quite long messages, so we allocate enough space for the message, 2048 bytes in this case. We create and send three config response messages:

  1. Device: lets the DashIO app know there is a device with one Device View.
  2. Dial: includes the control_ID of the Dial, the control_ID of the Device View it will be displayed on, the title and the position of the dial on the Device View.
  3. DeviceView: includes the control_ID, title, icon name and colour of the Device View.

The next piece of the puzzle to consider is how we receive data from the DashIO app. Lets add a Knob and connect it to the Dial. We do this by adding the Knob to the processIncomingMessage function and send the Knob value to the Dial in the processConfig function. And remember to remove the timer and the Dial ble_con.sendMessage from the loop:

void processConfig(ConnectionType connectionType) {
    String message((char *)0);
    message.reserve(2048);

    message += dashioDevice.getConfigMessage(DeviceCfg(1)); // One Device Views

    DialCfg first_dial_control("D01", "DV01", "Dial", {0.24, 0.14, 0.54, 0.26});
    message += dashioDevice.getConfigMessage(first_dial_control);

    KnobCfg aKnob("KB01", "DV01", "Knob", {0.24, 0.42, 0.54, 0.26});
    message += dashioDevice.getConfigMessage(aKnob);

    DeviceViewCfg deviceView("DV01", "Dial & Knob", "up", "black");
    message += dashioDevice.getConfigMessage(deviceView);
    
    ble_con.sendMessage(message);
}

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

Finally, we should respond to the STATUS message from the DashIO app. STATUS messages allows the IoT device to send initial conditions for each control to the DashIO 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 "DashioSAMD_NINA.h"

DashioDevice dashioDevice("SAMD_NINA_Type");
DashioBLE    ble_con(&dashioDevice, true);

int dialValue = 0;

void processConfig(ConnectionType connectionType) {
    String message((char *)0);
    message.reserve(2048);

    message += dashioDevice.getConfigMessage(DeviceCfg(1)); // One Device Views

    DialCfg first_dial_control("D01", "DV01", "Dial", {0.24, 0.14, 0.54, 0.26});
    message += dashioDevice.getConfigMessage(first_dial_control);

    KnobCfg aKnob("KB01", "DV01", "Knob", {0.24, 0.42, 0.54, 0.26});
    message += dashioDevice.getConfigMessage(aKnob);

    DeviceViewCfg deviceView("DV01", "Dial & Knob", "up", "black");
    message += dashioDevice.getConfigMessage(deviceView);
    
    ble_con.sendMessage(message);
}

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

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

    ble_con.sendMessage(message);
}

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

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

    ble_con.setCallback(&processIncomingMessage);
    ble_con.begin();
    dashioDevice.setup(ble_con.macAddress(), "Bob"); // unique deviceID, and device name
}

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

TCP Basics

Lets have a look at a TCP connection. For this we also need WiFi so it's very slightly more complicated than BLE.

#include "DashioSAMD_NINA.h"

#define WIFI_SSID      "yourWiFiSSID"
#define WIFI_PASSWORD  "yourWiFiPassword"
#define TCP_PORT 5000

DashioDevice dashioDevice("SAMD_NINA_Type");
DashioTCP    tcp_con(&dashioDevice, TCP_PORT, true);
DashioWiFi   wifi;

void setup() {
    Serial.begin(115200);
    delay(1000);
    
    dashioDevice.setup(wifi.macAddress(), "Bob Name"); // unique deviceID, and device name
    wifi.attachConnection(&tcp_con);
    wifi.begin(WIFI_SSID, WIFI_PASSWORD);
}

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

Similar to BLE communications, we create a device and a TCP connection, with the device being an attribute to the connection. We also provide a port and the third attribute of the TCP connection enables the received and transmitted messages to be printed to the serial monitor.

In the setup function we setup the device with two parameters. These parameters describe the device to the DashIO app to allow it uniquely identify each device. The wifi object supplies the mac to the device, to be used as a unique device_ID.

We then attach the TCP connection to the wifi with wifi.attachConnection(&tcp_con);. This enables to wifi object to control the TCP connection from here on in.

We begin the wifi with the SSID and password.

Unfortunately, this device is NOT discoverable by the DashIO app because a suitable mDNS library is not yet available. To use this connection in the DashIO app you need to manually create a device with a TCP connection and enter the device_ID and IP address.

wifi.run(); is used to regularly check that the WiFi is connected and manage the attached connection.

Message processing for a TCP connection is done exactly the samy way as for the BLE connection, by adding the processIncomingMessage callback into the TCP connection during setup function. Our finished code, with Dial and Knob added is:

#include "DashioSAMD_NINA.h"

#define WIFI_SSID      "yourWiFiSSID"
#define WIFI_PASSWORD  "yourWiFiPassword"
#define TCP_PORT 5000

DashioDevice dashioDevice("SAMD_NINA_Type");
DashioTCP    tcp_con(&dashioDevice, TCP_PORT, true);
DashioWiFi   wifi;

int dialValue = 0;

void processConfig(ConnectionType connectionType) {
    String message((char *)0);
    message.reserve(2048);

    message += dashioDevice.getConfigMessage(DeviceCfg(1)); // One Device Views

    DialCfg first_dial_control("D01", "DV01", "Dial", {0.24, 0.14, 0.54, 0.26});
    message += dashioDevice.getConfigMessage(first_dial_control);

    KnobCfg aKnob("KB01", "DV01", "Knob", {0.24, 0.42, 0.54, 0.26});
    message += dashioDevice.getConfigMessage(aKnob);

    DeviceViewCfg deviceView("DV01", "Dial & Knob", "up", "black");
    message += dashioDevice.getConfigMessage(deviceView);
    
    tcp_con.sendMessage(message);
}

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

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

    tcp_con.sendMessage(message);
}

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

void setup() {
    Serial.begin(115200);
    delay(1000);
    
    tcp_con.setCallback(&processIncomingMessage);
    dashioDevice.setup(wifi.macAddress(), "Bob Name"); // unique deviceID, and device name
    wifi.attachConnection(&tcp_con);
    wifi.begin(WIFI_SSID, WIFI_PASSWORD);
}

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

MQTT Basics

Lastly, lets look at a MQTT connection. We also need the WiFi for MQTT.

#include "DashioSAMD_NINA.h"

// WiFi
#define WIFI_SSID      "yourWiFiSSID"
#define WIFI_PASSWORD  "yourWiFiPassword"

// MQTT
#define MQTT_USER      "yourMQTTuserName"
#define MQTT_PASSWORD  "yourMQTTpassword"

DashioDevice dashioDevice("DashIO_Type");
DashioMQTT   mqtt_con(&dashioDevice, true, true);
DashioWiFi   wifi;

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

    dashioDevice.setup(wifi.macAddress(), "Bob Name"); // unique deviceID, and device name
    mqtt_con.setup(MQTT_USER, MQTT_PASSWORD);
    wifi.attachConnection(&mqtt_con);
    wifi.begin(WIFI_SSID, WIFI_PASSWORD);
}

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

Once again we create a device and this time a MQTT connection, with the device being an attribute to the connection. The secons attribute of the MQTT connection enables a push notification to be sent to your mobile phone when the IoT device reboots (Dash MQTT only). The final attribute enables the received and transmitted messages to be printed to the serial monitor.

In the setup function we setup the device with two parameters. These parameters describe the device to the DashIO app to allow it uniquely identify each device. The wifi object supplies the mac address to the device, to be used as a unique device_ID.

For MQTT connections, the device must be setup before we begin the wifi.

The MQTT connection setup requires the MQTT broker username and password, supplied in the mqtt_con.setup(MQTT_USER, MQTT_PASSWORD); method.

We then attach the MQTT connection to the wifi with wifi.attachConnection(&mqtt_con);. This enables to wifi object to control the MQTT connection from here on in.

Finally, we begin the wifi with the SSID and password.

wifi.run(); is used to regularly check that the WiFi is connected and to manage the attached connection.

This device is discoverable by the DashIO app.

Message processing for a MQTT connection is done exactly the samy way as for the BLE connection, by adding the processIncomingMessage callback into the MQTT connection during setup function. Our finished code, with Dial and Knob added is:

#include "DashioSAMD_NINA.h"

// WiFi
#define WIFI_SSID      "yourWiFiSSID"
#define WIFI_PASSWORD  "yourWiFiPassword"

// MQTT
#define MQTT_USER      "yourMQTTuserName"
#define MQTT_PASSWORD  "yourMQTTpassword"

DashioDevice dashioDevice("DashIO_Type");
DashioMQTT   mqtt_con(&dashioDevice, true, true);
DashioWiFi   wifi;

int dialValue = 0;

void processConfig(ConnectionType connectionType) {
    String message((char *)0);
    message.reserve(2048);

    message += dashioDevice.getConfigMessage(DeviceCfg(1)); // One Device Views

    DialCfg first_dial_control("D01", "DV01", "Dial", {0.24, 0.14, 0.54, 0.26});
    message += dashioDevice.getConfigMessage(first_dial_control);

    KnobCfg aKnob("KB01", "DV01", "Knob", {0.24, 0.42, 0.54, 0.26});
    message += dashioDevice.getConfigMessage(aKnob);

    DeviceViewCfg deviceView("DV01", "Dial & Knob", "up", "black");
    message += dashioDevice.getConfigMessage(deviceView);
    
    mqtt_con.sendMessage(message);
}

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

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

    mqtt_con.sendMessage(message);
}

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

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

    dashioDevice.setup(wifi.macAddress(), "Bob Name"); // unique deviceID, and device name
    mqtt_con.setup(MQTT_USER, MQTT_PASSWORD);
    mqtt_con.setCallback(&processIncomingMessage);
    wifi.attachConnection(&mqtt_con);
    wifi.begin(WIFI_SSID, WIFI_PASSWORD);
}

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

For MQTT connections operating through the DashIO MQTT broker, a service is available for sending Alarms (push notifications) to your mobile device. These are handy for sending messages or alarms to the user when they are not currently using the DashIO app. To send an alarm, simply call the MQTT connection's sendMessage method with two attrbutes; the first being any message String and the second having alarm_topic specified.

mqtt_con.sendMessage("message", alarm_topic);

Combining BLE, TCP and MQTT

The Arduino NANO 33 IoT and Arduino MKR 1010 are not capable of operating with bith BLE and WiFi operating at the same time. However, it is easy to switch between BLE and TCP or MQTT.

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.

Provisioning

Provisioning is the method by which the DashIO app can send user credentials and other setup information to the IoT device and the IoT device will store this data to non-volatile memory.

You will need to install the Arduino Flash Storage library. Search the arduino library manager for the library titled "Flash Storage" and install.

A provisioning library is available within the DashIO library for the Arduino SAMD boards (DashioProvisionSAMD.h and DashioProvisionSAMD.cpp) which stores the following provisioning data to Flash memory:

  • Device Name
  • WiFi SSID
  • WiFI Password
  • Dash MQTT broker User Name
  • Dash MQTT broker Password

WARNING: Flash memory has a limited amount of write cycles. Typical, flash memory can perform about 10000 writes cycles to the same flash block before starting to "wear out" and begin to lose the ability to retain data. Also, when you reprogram the microcontroller, the provisioning data will be erased.

The provisioning library exposes the following structure to manade the provisioning data:

struct DeviceData {
    char deviceName[32];
    char wifiSSID[32];
    char wifiPassword[63];
    char dashUserName[32];
    char dashPassword[32];
    char saved;
};

Use the provisioning library as follows:

Include the library

#include "DashioProvisionSAMD.h"

Create a provisioning object

DashioProvision dashioProvision(&dashioDevice);

Setup provisioning in the setup method. Here we update the provisioning object with the default provisioning data (using the DeviceData structure), then load the provisioning data. If the provisioning data has never been saved, then the the default provisionig data will be used. We also include a provision callback (discussed later).

We then use the provisioning data in the WiFi and MQTT setup.

// setup
DeviceData defaultDeviceData = {DEVICE_NAME, WIFI_SSID, WIFI_PASSWORD, MQTT_USER, MQTT_PASSWORD};
dashioProvision.load(&defaultDeviceData, &onProvisionCallback);

dashioDevice.setup(wifi.macAddress());
    
mqtt_con.setup(dashioProvision.dashUserName, dashioProvision.dashPassword);
mqtt_con.setCallback(&processIncomingMessage);
    
wifi.attachConnection(&mqtt_con);
wifi.begin(dashioProvision.wifiSSID, dashioProvision.wifiPassword);

Add provisioning process message in the processIncomingMessage callback from the example above becomes:

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

Make sure your IoT device lets the DashIO app know what provisioning services are available. This is done in the device configuration message:

message += dashioDevice.getConfigMessage(DeviceCfg(1), "name, wifi, dashio"); // One Device Views

The second attribute of the device configuration message contains a comma separated string where each field defines a provisioning service that is available in the IoT device. The possible options are:

  • "name" (set the device name)
  • "wifi" (set the WiFI SSID and password)
  • "dashio" (set the dashio username and password)

And finally, here is the onProvisionCallback:

void onProvisionCallback(ConnectionType connectionType, const String& message, bool commsChanged) {
    sendMessage(connectionType, message);

    // Restart WiFi and MQTT
}

The callback has two purposes. Firstly we send a message back to the DashIO app to let it know the provisioning is complete. And secondly, if the WiFi or MQTT connection credentials have changed, we need to restart the WiFi or MQTT connection or reboot the processor for a fresh start.