Contents

DashIO IoT Guide (ESP32)

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 ESP32 IoT device using the Arduino IDE.

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 ESP32 board, Arduino IDE and follow this guide. This example should work on ESP32 development boards available for the Arduino IDE.

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.

ESP32 Support (WiFi, TCP and mDNS)

If you haven't yet installed the ESP32 Arduino IDE support from espressif, please check the following link for instructions: https://github.com/espressif/arduino-esp32#installation-instructions

From the ESP32 espressif library, we will use the secure WiFi client (WiFiClientSecure.h) to enable SSL connections, which are required for the Dash server.

For mDNS we are using the ESPmDNS library, which is included in the ESP32 espressif library.

MQTT

For MQTT we are using the arduino-mqtt library created by Joël Gähwiler. This is available in the Arduino IDE Library Manager, by searching for "mqtt" and installing the library titled "MQTT" by Joël Gähwiler. This library is also stored on GitHub where there are examples and other useful information: https://github.com/256dpi/arduino-mqtt

BLE

We will use the ESP32 BLE Arduino library by Neil Kolban, which is included in the Arduino IDE Library manager. Seach the library manager for the library titled "ESP32 BLE Arduino" and install.

Guide

BLE Basics

Lets start with a simple BLE connection.

#include "DashioESP.h"

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

void setup() {
    Serial.begin(115200);
    
    ble_con.begin();
    dashioDevice.setup(ble_con.macAddress(), "Joe Name"); // 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 "DashioESP.h"

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

bool oneSecond = false; // Set by timer every second.

void oneSecondTimerTask(void *parameters) {
    for(;;) {
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        oneSecond = true;
    }
}

void setup() {
    Serial.begin(115200);
    
    ble_con.begin();
    dashioDevice.setup(ble_con.macAddress(), "Joe Name"); // unique deviceID, and device name

    // Setup 1 second timer task
    xTaskCreate(
        oneSecondTimerTask,
        "One Second",
        1024, // stack size
        NULL,
        1, // priority
        NULL // task handle
    );
}

void loop() {
    ble_con.run();

    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 "DashioESP.h"

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

bool oneSecond = false; // Set by timer every second.

void oneSecondTimerTask(void *parameters) {
    for(;;) {
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        oneSecond = true;
    }
}

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);
    
    ble_con.setCallback(&processIncomingMessage);
    ble_con.begin();
    dashioDevice.setup(ble_con.macAddress(), "Joe Name"); // unique deviceID, and device name

    // Setup 1 second timer task
    xTaskCreate(
        oneSecondTimerTask,
        "One Second",
        1024, // stack size
        NULL,
        1, // priority
        NULL // task handle
    );
}

void loop() {
    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:

#include "DashioESP.h"

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

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", "A Dial", "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;
    }
}

void setup() {
    Serial.begin(115200);
    
    ble_con.setCallback(&processIncomingMessage);
    ble_con.begin();
    dashioDevice.setup(ble_con.macAddress(), "Joe Name"); // unique deviceID, and device name
}

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

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 "DashioESP.h"

DashioDevice dashioDevice("ESP32_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", "A Dial", "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);
    
    ble_con.setCallback(&processIncomingMessage);
    ble_con.begin();
    dashioDevice.setup(ble_con.macAddress(), "Joe Name"); // unique deviceID, and device name
}

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

If you would like to use a secure BLE connection, simply add "true" as an parameter to the BLE begin:

ble_con.begin(true);

TCP Basics

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

#include "DashioESP.h"

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

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

void setup() {
    Serial.begin(115200);
    delay(1000);
    
    dashioDevice.setup(wifi.macAddress(), "Joe 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. This starts the wifi and TCP connection and sets up the the mDNS (also called Bonjour or Zeroconf) to make the device discoverable on the DashIO app.

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

This device is discoverable by the DashIO app. You can also discover your IoT device using a thiry party Bonjour/Zeroconf discovery tool. The mDNS service will be "_DashIO._tcp." and individual IoT devices on this service are identified by their mac address (device_ID).

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 "DashioESP.h"

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

DashioDevice dashioDevice("Nano33IoT_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(), "Joe 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 "DashioESP.h"

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

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

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

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

    dashioDevice.setup(wifi.macAddress(), "Joe 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. We also provide a buffer size (2048) and the third 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 "DashioESP.h"

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

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

DashioDevice dashioDevice("DashIO_Nano_Type");
DashioMQTT   mqtt_con(&dashioDevice, 2048, 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(), "Joe 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 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 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 ESP32 is capable of operating with any two of the three connection types at the same time. It just desn't have enough jibbers to run all three together. It's relativelty easy to combine to code for two connections in one ESP32 IoT device where each connection calls a common processIncomingMessage callback.

Note that the wifi mac address is not identical to the BLE mac addresss. Therefore, if you are combining connections, it is best to stick with the wifi mac address for your device_ID.

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.

A provisioning library is available within the DashIO library for the ESP32 (DashioProvisionESP.h and DashioProvisionESP.cpp) which stores the following provisioning data to EEPROM:

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

The 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 "DashioProvisionESP.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);

    if (commsChanged) {
        mqtt_con.setup(dashioProvision.dashUserName, dashioProvision.dashPassword);
        wifi.begin(dashioProvision.wifiSSID, dashioProvision.wifiPassword);
    }
}

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. In the above example we call begin on the WiFI and MQTT. Alternatively, you could reboot the processor for a fresh start.