Projects / Web Bluetooth dashboard

Web Bluetooth dashboard

Intermediate3 hGuided buildReviewed
ESP32MPU6050tilt 5°

Build it step by step

0 of 7 done
  1. ESP32MPU6050tilt 29°

    Test the IMU

    Print the three accelerations to serial.

  2. ESP32MPU6050tilt 29°

    Start BLE

    Advertise with the name ElectroLark-IMU.

  3. ESP32

    Check with a scanner app

    Confirm the service shows.

  4. ESP32

    Build the page

    Save the HTML and serve it from localhost.

  5. ESP32

    Connect from Chrome

    Pick the device in the chooser.

  6. ESP32

    Plot the values

    Tilt the board and watch the lines move.

  7. ESP32

    Change the rate

    Use the dropdown to write the command byte.

In short

How do you build a web Bluetooth dashboard?

A web page in Chrome connects straight to a BLE device and plots its data, no app needed. You need ESP32 DevKit V1 and MPU6050 IMU. It takes 7 steps and about 180 minutes.

Level
Intermediate
Time
About 180 min
Parts
2

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
MPU6050 VCC and GNDESP32 3V3 and GNDPower
MPU6050 SDAESP32 GPIO21I2C data
MPU6050 SCLESP32 GPIO22I2C clock
USBESP32Power and upload

Code

Chrome and Edge on a computer or an Android phone can talk to a BLE device straight from a web page, but only from a secure address: a page opened from localhost, for example by running python3 -m http.server in the folder, or from an https website. Safari and iPhone browsers do not support Web Bluetooth. The page asks you to pick the device, subscribes to notifications and plots the three acceleration values. Saving the page needs no installation, so anyone can use the dashboard. The second characteristic lets the page change the sample rate. There is no pairing or encryption, so anyone near can connect; do not use this for anything that controls something important. Make your own UUIDs for projects you publish. Arduino IDE with the ESP32 boards package, board "ESP32 Dev Module".

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// ESP32 side. Libraries: Adafruit MPU6050, Adafruit Unified Sensor, ESP32 BLE Arduino (built in).
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#include <Wire.h>
#include <Adafruit_MPU6050.h>

#define SERVICE_UUID "6e400001-b5a3-f393-e0a9-e50e24dcca9e"   // make your own UUIDs for real projects
#define DATA_UUID    "6e400002-b5a3-f393-e0a9-e50e24dcca9e"   // notify: ax, ay, az as int16 in milli-g
#define CMD_UUID     "6e400003-b5a3-f393-e0a9-e50e24dcca9e"   // write: one byte, sample interval in units of 10 ms

Adafruit_MPU6050 mpu;
BLECharacteristic *dataChar;
bool connected = false;
unsigned long intervalMs = 100;

class ServerCb : public BLEServerCallbacks {
  void onConnect(BLEServer*) { connected = true; }
  void onDisconnect(BLEServer* s) { connected = false; s->startAdvertising(); }
};
class CmdCb : public BLECharacteristicCallbacks {
  void onWrite(BLECharacteristic* c) {
    String v = c->getValue();
    if (v.length() > 0 && v[0] >= 2 && v[0] <= 100) intervalMs = v[0] * 10UL;   // 20 ms to 1 s
  }
};

void setup() {
  Wire.begin(21, 22);
  mpu.begin();
  BLEDevice::init("ElectroLark-IMU");
  BLEServer* server = BLEDevice::createServer();
  server->setCallbacks(new ServerCb());
  BLEService* svc = server->createService(SERVICE_UUID);
  dataChar = svc->createCharacteristic(DATA_UUID, BLECharacteristic::PROPERTY_NOTIFY);
  dataChar->addDescriptor(new BLE2902());
  BLECharacteristic* cmd = svc->createCharacteristic(CMD_UUID, BLECharacteristic::PROPERTY_WRITE);
  cmd->setCallbacks(new CmdCb());
  svc->start();
  BLEDevice::getAdvertising()->addServiceUUID(SERVICE_UUID);
  BLEDevice::getAdvertising()->start();
}

void loop() {
  if (connected) {
    sensors_event_t a, g, t; mpu.getEvent(&a, &g, &t);
    int16_t v[3] = { (int16_t)(a.acceleration.x / 9.81 * 1000), (int16_t)(a.acceleration.y / 9.81 * 1000), (int16_t)(a.acceleration.z / 9.81 * 1000) };
    dataChar->setValue((uint8_t*)v, 6);
    dataChar->notify();
  }
  delay(intervalMs);
}

/* ---- The web page: save as index.html and open it from a local server or an https address ----
<!DOCTYPE html>
<html><body style="font-family:sans-serif">
<h3>ElectroLark IMU</h3>
<button id="connect">Connect</button>
<label> Rate <select id="rate"><option value="10">100 ms</option><option value="5">50 ms</option><option value="50">500 ms</option></select></label>
<canvas id="plot" width="600" height="240" style="border:1px solid #999;display:block;margin-top:8px"></canvas>
<script>
var SERVICE = "6e400001-b5a3-f393-e0a9-e50e24dcca9e";
var DATA = "6e400002-b5a3-f393-e0a9-e50e24dcca9e";
var CMD = "6e400003-b5a3-f393-e0a9-e50e24dcca9e";
var cmdChar = null, history = [[], [], []];
var colours = ["#d33", "#3a3", "#33d"];
var ctx = document.getElementById("plot").getContext("2d");

function draw() {
  ctx.clearRect(0, 0, 600, 240);
  ctx.beginPath(); ctx.moveTo(0, 120); ctx.lineTo(600, 120); ctx.strokeStyle = "#ccc"; ctx.stroke();
  for (var s = 0; s < 3; s++) {
    ctx.beginPath(); ctx.strokeStyle = colours[s];
    for (var i = 0; i < history[s].length; i++) {
      var y = 120 - history[s][i] / 2000 * 120;            // plus or minus 2 g across the plot
      if (i == 0) ctx.moveTo(i * 3, y); else ctx.lineTo(i * 3, y);
    }
    ctx.stroke();
  }
}

document.getElementById("connect").onclick = async function () {
  var dev = await navigator.bluetooth.requestDevice({ filters: [{ name: "ElectroLark-IMU" }], optionalServices: [SERVICE] });
  var server = await dev.gatt.connect();
  var svc = await server.getPrimaryService(SERVICE);
  var data = await svc.getCharacteristic(DATA);
  cmdChar = await svc.getCharacteristic(CMD);
  await data.startNotifications();
  data.addEventListener("characteristicvaluechanged", function (e) {
    for (var s = 0; s < 3; s++) {
      history[s].push(e.target.value.getInt16(s * 2, true));
      if (history[s].length > 200) history[s].shift();
    }
    draw();
  });
};
document.getElementById("rate").onchange = function () {
  if (cmdChar) cmdChar.writeValue(new Uint8Array([parseInt(this.value)]));
};
</script>
</body></html>
*/

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Open the pageThe Connect button works in Chrome
Tilt the boardThe three lines move
Lay it flatThe blue line sits near 1000 (1 g)
Change the rateUpdates speed up or slow down

If it does not work

No device chooser

Use Chrome or Edge and an https or localhost address.

Device not listed

Check it is advertising and the name filter matches.

Values look scrambled

Read int16 values in little-endian order.

Disconnects often

Move closer and slow the rate.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the web Bluetooth dashboard?

ESP32 DevKit V1 and MPU6050 IMU.

How long does the web Bluetooth dashboard take to build?

About 180 minutes for a intermediate maker, in 7 steps.

How do I know the web Bluetooth dashboard works?

Open the page: The Connect button works in Chrome. Tilt the board: The three lines move. Lay it flat: The blue line sits near 1000 (1 g).

No device chooser: what should I check?

Use Chrome or Edge and an https or localhost address.

Device not listed: what should I check?

Check it is advertising and the name filter matches.

Values look scrambled: what should I check?

Read int16 values in little-endian order.

Disconnects often: what should I check?

Move closer and slow the rate.