Skip to content

Instantly share code, notes, and snippets.

@craigt543nz
Last active September 20, 2023 23:55
Show Gist options
  • Save craigt543nz/c79dd6401b3875d51eabd3db75752633 to your computer and use it in GitHub Desktop.
Save craigt543nz/c79dd6401b3875d51eabd3db75752633 to your computer and use it in GitHub Desktop.
ESP23 LED Toggle with the Dash IoT mobile app

Dash project to demonstrate toggling an LED on an ESP32 development board. Details about Dash and how it works are available here: https://dashio.io/

This project has the following features:

  • Tapping a Button/Indicator control on the Dash IoT app toggles an LED on the ESP32 and increments a counter.
  • The counter value is displayed in a TextBox control on the **Dash IoT ** app.
  • Communicates via BLE and dash MQTT connections.
  • Uses LED on pin 13, but can be changed as required.
  • Uses the serial monitor to show what is going on (115200 baud).

Before you begin, make sure you have the following:

  • An ESP32 dev board with LED on pin 13.
  • Arduino IDE with the arduino-dashio library installed, available on GitHub.
  • Using the BLE connection is free. To make use of the dash MQTT connecction, you will need a dash account and subscription, which you can create on the Dash app.
  • The Dash IoT app is available here:
Apple Android

Install and run the code below in your ESP32. On the Dash IoT app, search for the device named "Toggle Button" as follows: 'All Devices' -> 'Find New Device' -> 'BLE Scan'

#include "DashioESP.h"

const char configC64Str[] PROGMEM =
"lVPbctowEP0VRq/1NFzTJm/4EqAYQ2yVdKb0wRdha2IkRpa5hOHfu5LthITmoaNhOOweLdqze07INV10//uPgabe3KxQ4Np+hbDz"
"CwM6obKkCbpH0Rc7zV5+7Pz9rT2kq5vBrp867BEZKOZMCp5PbGDhdgci21AQJnXAXqpAyags4BdASWVOFFN/GygjNM2kH0rK0X37"
"a2egTk1b8IJCnAHdm3uOuk0OcpjTVIUsx8OOD8E1F5tQvpGOzb2rqltBYlroim0DPUcJPm7J28U9TWT2eqnf/T741jHQ4aqcTvSh"
"85wXZBolcxYQBiJJUZIzaGdPahFnw0UNHO9nhdz5qALDpd1obuGab7k1sJfLJ61+IY9aLnMYTCwlthT5LDw8gOYBfYFMpwudpIIm"
"Fs/LDQOVu/DmIoMRVJHqWQZi5eaV0nk/tWpIqrTJRUIE8LhQf5qH8XOTeU5Z0iSeMipJnfiUjEXICr0K8VHrHdfMUUhZEXHBL9ah"
"vqi3wuSHpugsFBym9T55VVi17/M9NNbrXVAvRLo1EIWOvXCjx80ZQdWghrUFrLGP68Uf+YtxBU2MvUsPOPt59hDfRsHUWt3EZHWz"
"dLuDhatM0MxpPBmNXfhgiPH1uunDppvWSITHD3YxtV2iUkrOHBZGOUmacXH2qgFJaLlpBXkoScvMS/Ivh32wUa8N59psn7jq2jC9"
"O3Uqv1XT+V9vXMjtqqe1zDKPtOgmHtWusB7ACyeUkB2NSUBkuVXtPmJsmK56F6ysrXNLSvb12q5Tn+wAns9/AQ==";

char WIFI_SSID[] = "yourWiFiSSID";
char WIFI_PASSWORD[] = "yourWiFiPassword";

char MQTT_USER[] = "yourMQTTuserName";
char MQTT_PASSWORD[] = "yourMQTTpassword";

int ledPin = 13;
const char *BUTTON_ID = "B01";
const char *TEXT_BOX_ID = "T01";

DashDevice dashDevice("ESP32_Type", configC64Str, 1);
DashBLE    ble_con(&dashDevice, true);
DashMQTT   mqtt_con(&dashDevice, true, true);
DashWiFi   wifi;

bool buttonValue = false;
int counter = 0;

void sendMessage(const String& message) {
    // Send all messages through both connections in this simple example.
    ble_con.sendMessage(message);
    mqtt_con.sendMessage(message);
}

void sendData() {
    String message = dashDevice.getButtonMessage(BUTTON_ID, buttonValue);
    message += dashDevice.getTextBoxMessage(TEXT_BOX_ID, String(counter));
    sendMessage(message);
}

void processButtonPress() {
    counter += 1;
    buttonValue = !buttonValue;
    digitalWrite(ledPin, buttonValue);
    sendData();
}

void processIncomingMessage(MessageData *messageData) {
    switch (messageData->control) {
    case status:
        sendData();
        break;
    case button:
        if (messageData->idStr == BUTTON_ID) {
            processButtonPress();
        }
        break;
    }
}

void setup() {
    pinMode(ledPin, OUTPUT);
    
    Serial.begin(115200);
    delay(1000);
    
    dashDevice.setup(wifi.macAddress(), "Toggle Button"); // 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);

    ble_con.setCallback(&processIncomingMessage);
    ble_con.begin();
}

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

More dash gists here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment