1// Bluetooth lab B2 Be seen. uv run pio run -d labs/ble-lab-b2 -e ble-lab-b2 -t upload2// The board turns from listener into broadcaster: a beacon. It packs live values (button presses,3// the chip's temperature, uptime) into its advertisement, so any phone nearby can read them without4// ever connecting. Then the same 31 bytes as an iBeacon, as an Eddystone-URL, or just a name, and5// what interval and TX power cost in battery.6//7// Wiring: the screen from lesson 03. The push button on GPIO 5 is optional (it does what BOOT does).8// The scanner is your phone: nRF Connect (Android, iPhone) or LightBlue.9//10// Views: packet · air · budget · log (click = next view, double-click = press: the counter in the11// packet goes up, long press = advertising on/off)12// Serial: help lists the commands. Shared code: kit/lib/ble-kit; the packets: beacon_payload.h.13#include "b2_views.h"14#include "beacon.h"15#include "ble_lab.h"16#include "server_link.h"17 18namespace {19 20b2::Beacon beacon;21b2v::AirSim airSim;22ble::ServerLink links;23uint32_t liveAt = 0;24 25bool on() { return beacon.settings.on; }26 27void drawPacket(BleLab &, FrameBuffer &fb, float) { b2v::packet(fb, beacon, on()); }28void drawAir(BleLab &, FrameBuffer &fb, float t) { b2v::air(fb, beacon, airSim, on(), t); }29void drawBudget(BleLab &, FrameBuffer &fb, float) { b2v::budget(fb, beacon); }30void drawLog(BleLab &, FrameBuffer &fb, float) { bv::log(fb); }31 32const BleView kViews[] = {33 {"packet", drawPacket, "press"},34 {"air", drawAir, "press"},35 {"budget", drawBudget, "press"},36 {"log", drawLog, "press"},37};38 39// ---- Commands ---------------------------------------------------------------------------------40 41void cmdMode(BleLab &lab, const String &, const String &lower) {42 static const b2::Mode modes[] = {b2::Mode::Bench, b2::Mode::IBeacon, b2::Mode::Url, b2::Mode::Name};43 for (b2::Mode m : modes)44 if (lower == b2::modeName(m)) {45 const b2::Mode before = beacon.settings.mode;46 beacon.settings.mode = m;47 if (!beacon.apply(true)) beacon.settings.mode = before, beacon.apply(true);48 return lab.showView("packet");49 }50 lab.warn("mode bench | ibeacon | url | name");51}52 53void cmdUrl(BleLab &lab, const String &arg, const String &) {54 if (arg.isEmpty() || arg.length() >= sizeof(beacon.settings.url)) return lab.warn("url <https://...> (short: at most 17 bytes after the scheme)");55 char before[sizeof(beacon.settings.url)];56 strlcpy(before, beacon.settings.url, sizeof(before));57 strlcpy(beacon.settings.url, arg.c_str(), sizeof(beacon.settings.url));58 beacon.settings.mode = b2::Mode::Url;59 if (!beacon.apply(true)) {60 strlcpy(beacon.settings.url, before, sizeof(beacon.settings.url));61 beacon.apply(true);62 }63}64 65void cmdBeacon(BleLab &lab, const String &arg, const String &) {66 const int sp = arg.indexOf(' ');67 long major = 0, minor = 0;68 if (sp < 0 || !BleLab::number(arg.substring(0, sp), 0, 65535, major) || !BleLab::number(arg.substring(sp + 1), 0, 65535, minor))69 return lab.warn("beacon <major> <minor>, each 0-65535");70 beacon.settings.beacon.major = uint16_t(major);71 beacon.settings.beacon.minor = uint16_t(minor);72 beacon.settings.mode = b2::Mode::IBeacon;73 beacon.apply(true);74}75 76void cmdInterval(BleLab &lab, const String &arg, const String &) {77 long ms = 0;78 const long lo = b2::minIntervalMs(beacon.settings);79 if (!BleLab::number(arg, lo, 10240, ms))80 return lab.warn("interval <ms>, %ld-10240 (%s: %ld ms is the floor)", lo, beacon.settings.connectable ? "connectable" : "not connectable", lo);81 beacon.settings.intervalMs = uint16_t(ms);82 beacon.apply(true);83}84 85void cmdConnectable(BleLab &lab, const String &, const String &lower) {86 if (lower != "on" && lower != "off") return lab.warn("connectable on | connectable off");87 beacon.settings.connectable = lower == "on";88 if (beacon.settings.intervalMs < b2::minIntervalMs(beacon.settings)) {89 beacon.settings.intervalMs = b2::minIntervalMs(beacon.settings);90 BLE_NOTE(Warn, "interval raised to %u ms: the floor for non-connectable", beacon.settings.intervalMs);91 }92 beacon.apply(true);93}94 95void cmdScanResp(BleLab &lab, const String &, const String &lower) {96 if (lower != "on" && lower != "off") return lab.warn("scanresp on | scanresp off");97 beacon.settings.scanResponse = lower == "on";98 beacon.apply(true);99}100 101void cmdAdv(BleLab &lab, const String &, const String &lower) {102 if (!lower.isEmpty() && lower != "on" && lower != "off") return lab.warn("adv | adv on | adv off");103 beacon.settings.on = lower.isEmpty() ? !beacon.settings.on : lower == "on";104 NimBLEDevice::getServer()->advertiseOnDisconnect(beacon.settings.on);105 links.setAdvertiseAgain(beacon.settings.on);106 if (beacon.settings.on) beacon.apply(true);107 else beacon.stop();108}109 110void cmdPress(BleLab &, const String &, const String &) {111 beacon.live.presses++;112 beacon.apply(false);113 BLE_NOTE(Info, "presses %u: in the next packet", beacon.live.presses);114}115 116void printPacket(const char *what, const b2::Packet &p) {117 Serial.printf(" %s, %u of 31 bytes:\n", what, p.n);118 const ad::Parsed ps = ad::split(p.b, p.n);119 for (int f = 0; f < ps.count; f++) {120 const ad::Field &fl = ps.fields[f];121 Serial.printf(" [%02X %02X |", fl.len + 1, fl.type);122 for (int i = 0; i < fl.len; i++) Serial.printf(" %02X", p.b[fl.offset + i]);123 Serial.printf("] %s\n", ad::typeName(fl.type));124 }125}126 127void cmdBytes(BleLab &, const String &, const String &) {128 Serial.printf("%s · %s · every %u ms · TX %d dBm\n", b2::modeName(beacon.settings.mode), b2::pduName(beacon.settings), beacon.settings.intervalMs,129 NimBLEDevice::getPower());130 printPacket("advertisement", beacon.adv);131 if (beacon.rsp.n) printPacket("scan response", beacon.rsp);132 else Serial.println(beacon.settings.scanResponse ? " scan response: empty" : " scan response: off");133}134 135const BleCommand kCommands[] = {136 {"mode", "bench|ibeacon|url|name", "what the advertisement carries", cmdMode},137 {"url", "<https://...>", "the Eddystone-URL (switches to url mode)", cmdUrl},138 {"beacon", "<major> <minor>", "the iBeacon numbers (switches to ibeacon mode)", cmdBeacon},139 {"interval", "<ms>", "time between advertising events (default 100)", cmdInterval},140 {"connectable", "on|off", "may a phone connect? off = a pure beacon", cmdConnectable},141 {"scanresp", "on|off", "answer active scanners with a second packet", cmdScanResp},142 {"adv", "[on|off]", "advertise or go silent (alone: toggle; long press too)", cmdAdv},143 {"press", "", "count a press into the packet (double-click too)", cmdPress},144 {"bytes", "", "both packets, structure by structure", cmdBytes},145};146 147// ---- Hooks ------------------------------------------------------------------------------------148 149void setup(BleLab &) {150 // A server with no services of its own: in connectable mode a phone may connect, and the link151 // view's state stays right. Advertising starts again by itself when the phone hangs up.152 NimBLEServer *server = NimBLEDevice::createServer();153 server->setCallbacks(&links, false);154 beacon.live.tempCenti = int16_t(temperatureRead() * 100);155 beacon.apply(true);156}157 158// Once a second: fresh live values into the advertisement (the controller keeps sending the old159// bytes until we hand it new ones).160void tick(BleLab &) {161 airSim.tick(beacon.settings.intervalMs, on() && ble::link().get().radio != ble::Radio::Connected);162 if (millis() - liveAt < 1000) return;163 liveAt = millis();164 beacon.live.tempCenti = int16_t(temperatureRead() * 100);165 beacon.live.uptime = uint16_t(millis() / 1000);166 if (on()) beacon.apply(false);167}168 169const BleLabSpec kSpec{170 "ble-lab-b2", "B2", "Be seen", "Bench B2", kViews, sizeof(kViews) / sizeof(kViews[0]), 0,171 kCommands, sizeof(kCommands) / sizeof(kCommands[0]), setup, tick, nullptr, "adv",172};173 174BleLab lab(kSpec);175 176} // namespace177 178void setup() { lab.begin(); }179 180void loop() { lab.loop(); }
// Bluetooth lab B2 Be seen. uv run pio run -d labs/ble-lab-b2 -e ble-lab-b2 -t upload
// The board turns from listener into broadcaster: a beacon. It packs live values (button presses,
// the chip's temperature, uptime) into its advertisement, so any phone nearby can read them without
// ever connecting. Then the same 31 bytes as an iBeacon, as an Eddystone-URL, or just a name, and
// what interval and TX power cost in battery.
//
// Wiring: the screen from lesson 03. The push button on GPIO 5 is optional (it does what BOOT does).
// The scanner is your phone: nRF Connect (Android, iPhone) or LightBlue.
// Views: packet · air · budget · log (click = next view, double-click = press: the counter in the
// packet goes up, long press = advertising on/off)
// Serial: help lists the commands. Shared code: kit/lib/ble-kit; the packets: beacon_payload.h.
#include "b2_views.h"
#include "beacon.h"
#include "ble_lab.h"
#include "server_link.h"
namespace {
b2::Beacon beacon;
b2v::AirSim airSim;
ble::ServerLink links;
uint32_t liveAt = 0;
bool on() { return beacon.settings.on; }
void drawPacket(BleLab &, FrameBuffer &fb, float) { b2v::packet(fb, beacon, on()); }
void drawAir(BleLab &, FrameBuffer &fb, float t) { b2v::air(fb, beacon, airSim, on(), t); }
void drawBudget(BleLab &, FrameBuffer &fb, float) { b2v::budget(fb, beacon); }
void drawLog(BleLab &, FrameBuffer &fb, float) { bv::log(fb); }
const BleView kViews[] = {
{"packet", drawPacket, "press"},
{"air", drawAir, "press"},
{"budget", drawBudget, "press"},
{"log", drawLog, "press"},
};
// ---- Commands ---------------------------------------------------------------------------------
void cmdMode(BleLab &lab, const String &, const String &lower) {
static const b2::Mode modes[] = {b2::Mode::Bench, b2::Mode::IBeacon, b2::Mode::Url, b2::Mode::Name};
for (b2::Mode m : modes)
if (lower == b2::modeName(m)) {
const b2::Mode before = beacon.settings.mode;
beacon.settings.mode = m;
if (!beacon.apply(true)) beacon.settings.mode = before, beacon.apply(true);
return lab.showView("packet");
}
lab.warn("mode bench | ibeacon | url | name");
void cmdUrl(BleLab &lab, const String &arg, const String &) {
if (arg.isEmpty() || arg.length() >= sizeof(beacon.settings.url)) return lab.warn("url <https://...> (short: at most 17 bytes after the scheme)");
char before[sizeof(beacon.settings.url)];
strlcpy(before, beacon.settings.url, sizeof(before));
strlcpy(beacon.settings.url, arg.c_str(), sizeof(beacon.settings.url));
beacon.settings.mode = b2::Mode::Url;
if (!beacon.apply(true)) {
strlcpy(beacon.settings.url, before, sizeof(beacon.settings.url));
beacon.apply(true);
void cmdBeacon(BleLab &lab, const String &arg, const String &) {
const int sp = arg.indexOf(' ');
long major = 0, minor = 0;
if (sp < 0 || !BleLab::number(arg.substring(0, sp), 0, 65535, major) || !BleLab::number(arg.substring(sp + 1), 0, 65535, minor))
return lab.warn("beacon <major> <minor>, each 0-65535");
beacon.settings.beacon.major = uint16_t(major);
beacon.settings.beacon.minor = uint16_t(minor);
beacon.settings.mode = b2::Mode::IBeacon;
void cmdInterval(BleLab &lab, const String &arg, const String &) {
long ms = 0;
const long lo = b2::minIntervalMs(beacon.settings);
if (!BleLab::number(arg, lo, 10240, ms))
return lab.warn("interval <ms>, %ld-10240 (%s: %ld ms is the floor)", lo, beacon.settings.connectable ? "connectable" : "not connectable", lo);
beacon.settings.intervalMs = uint16_t(ms);
void cmdConnectable(BleLab &lab, const String &, const String &lower) {
if (lower != "on" && lower != "off") return lab.warn("connectable on | connectable off");
beacon.settings.connectable = lower == "on";
if (beacon.settings.intervalMs < b2::minIntervalMs(beacon.settings)) {
beacon.settings.intervalMs = b2::minIntervalMs(beacon.settings);
BLE_NOTE(Warn, "interval raised to %u ms: the floor for non-connectable", beacon.settings.intervalMs);
void cmdScanResp(BleLab &lab, const String &, const String &lower) {
if (lower != "on" && lower != "off") return lab.warn("scanresp on | scanresp off");
beacon.settings.scanResponse = lower == "on";
void cmdAdv(BleLab &lab, const String &, const String &lower) {
if (!lower.isEmpty() && lower != "on" && lower != "off") return lab.warn("adv | adv on | adv off");
beacon.settings.on = lower.isEmpty() ? !beacon.settings.on : lower == "on";
NimBLEDevice::getServer()->advertiseOnDisconnect(beacon.settings.on);
links.setAdvertiseAgain(beacon.settings.on);
if (beacon.settings.on) beacon.apply(true);
else beacon.stop();
void cmdPress(BleLab &, const String &, const String &) {
beacon.live.presses++;
beacon.apply(false);
BLE_NOTE(Info, "presses %u: in the next packet", beacon.live.presses);
void printPacket(const char *what, const b2::Packet &p) {
Serial.printf(" %s, %u of 31 bytes:\n", what, p.n);
const ad::Parsed ps = ad::split(p.b, p.n);
for (int f = 0; f < ps.count; f++) {
const ad::Field &fl = ps.fields[f];
Serial.printf(" [%02X %02X |", fl.len + 1, fl.type);
for (int i = 0; i < fl.len; i++) Serial.printf(" %02X", p.b[fl.offset + i]);
Serial.printf("] %s\n", ad::typeName(fl.type));
void cmdBytes(BleLab &, const String &, const String &) {
Serial.printf("%s · %s · every %u ms · TX %d dBm\n", b2::modeName(beacon.settings.mode), b2::pduName(beacon.settings), beacon.settings.intervalMs,
NimBLEDevice::getPower());
printPacket("advertisement", beacon.adv);
if (beacon.rsp.n) printPacket("scan response", beacon.rsp);
else Serial.println(beacon.settings.scanResponse ? " scan response: empty" : " scan response: off");
const BleCommand kCommands[] = {
{"mode", "bench|ibeacon|url|name", "what the advertisement carries", cmdMode},
{"url", "<https://...>", "the Eddystone-URL (switches to url mode)", cmdUrl},
{"beacon", "<major> <minor>", "the iBeacon numbers (switches to ibeacon mode)", cmdBeacon},
{"interval", "<ms>", "time between advertising events (default 100)", cmdInterval},
{"connectable", "on|off", "may a phone connect? off = a pure beacon", cmdConnectable},
{"scanresp", "on|off", "answer active scanners with a second packet", cmdScanResp},
{"adv", "[on|off]", "advertise or go silent (alone: toggle; long press too)", cmdAdv},
{"press", "", "count a press into the packet (double-click too)", cmdPress},
{"bytes", "", "both packets, structure by structure", cmdBytes},
// ---- Hooks ------------------------------------------------------------------------------------
void setup(BleLab &) {
// A server with no services of its own: in connectable mode a phone may connect, and the link
// view's state stays right. Advertising starts again by itself when the phone hangs up.
NimBLEServer *server = NimBLEDevice::createServer();
server->setCallbacks(&links, false);
beacon.live.tempCenti = int16_t(temperatureRead() * 100);
// Once a second: fresh live values into the advertisement (the controller keeps sending the old
// bytes until we hand it new ones).
void tick(BleLab &) {
airSim.tick(beacon.settings.intervalMs, on() && ble::link().get().radio != ble::Radio::Connected);
if (millis() - liveAt < 1000) return;
liveAt = millis();
beacon.live.uptime = uint16_t(millis() / 1000);
if (on()) beacon.apply(false);
const BleLabSpec kSpec{
"ble-lab-b2", "B2", "Be seen", "Bench B2", kViews, sizeof(kViews) / sizeof(kViews[0]), 0,
kCommands, sizeof(kCommands) / sizeof(kCommands[0]), setup, tick, nullptr, "adv",
BleLab lab(kSpec);
} // namespace
void setup() { lab.begin(); }
void loop() { lab.loop(); }
New word #include
#include
Pastes another file in here, so this file can use what it declares.
PlatformIO environment ble-lab-b2 includes C++ source files covering broadcaster role, 31-byte budget, AD structures, manufacturer data, iBeacon & Eddystone, ADV_IND · SCAN_IND · NONCONN, interval & advDelay, TX power vs battery.