Home

Awesome

Nordic Thingy:52 for Web Bluetooth

About Thingy:52

The Nordic Thingy:52™ is a compact, power-optimized, multi-sensor development kit. It is an easy-to-use development platform, designed to help you build IoT prototypes and demos, without the need to build hardware or write firmware. Read more about it here.

This repository

This repository is an attempt to make it easier to start developing applications for Thingy:52 using Web Bluetooth. Web Bluetooth is a JavaScript API that makes it possible to communicate with Bluetooth Low Energy devices in web browsers. The implementation status for different browsers and platforms can be seen here.

This is work in progress, and for now this repository will help you connect to a Thingy:52 and access all services and characteristics except for MTU (Maximum Transmission Unit):

Get started

Examples

The following example will first connect to a thingy:52 with the option logEnabled set to false, then subscribe to and log all incoming data from the temperature sensor. After that we will request the device's name, before setting it to "Thingy". In the end, we set a timeout for 10 secounds before we disconnect from the device.


import Thingy from "./js/thingy.js";

const thingy = new Thingy({logEnabled: false});

function myLoggingFunction(data) {
    const temperatureData = data.detail;
    console.log(temperatureData);
}

async function start(device) {
    try {
        await device.connect();
        
        device.addEventListener("temperature", myLoggingFunction);
        await device.temperature.start();
        
        const deviceName = await device.name.read();
        console.log(deviceName);

        await device.name.write("Thingy");

        setTimeout(async () => {
        await device.disconnect();
        console.log("Disconnected from the device");
        }, 10000);
        
    } catch (error) {
        console.error(error);
    }
}

start(thingy);

The following example will first connect to a thingy:52, and then read the current LED configuration. In the end we will set the LED to breathe mode, with an intensity of 50% and a delay of 1 second.


import Thingy from "./js/thingy.js";

const thingy = new Thingy();

async function start(device) {
    try {
        await device.connect();

        const currentLedConfiguration = await device.led.read();
        console.log(currentLedConfiguration);
        
        const newLedConfiguration = {
            mode: "breathe",
            color: "cyan",
            intensity: 50,
            delay: 1000,
        }
        
        await device.led.write(newLedConfiguration);  
    } catch (error) {
        console.error(error);
    }
}

start(thingy);

Note: the Web Bluetooth API requires that a function trying to connect to a BLE device is initiated by a user action such as a mouse click.

API documentation

Thingy offers several features, all of which rely on established BLE protocols for sending and receiving data. The BLE operations are abstracted away through the following operations:

OperationDescription
start/stopSubscribes to a feature and relays any incoming data from that feature as an event to the device object.
readReads data from the specified feature on Thingy.
writeWrites data to the specified feature on Thingy.

Note: If any connect/start/stop operation is unsuccessful due to network congestion, the method is queued and and re-tried at a later time. If the operation continues to complete unsuccessfully, an event will be dispatched on thingy.operationdiscarded.

EventDescription
thingy.errorAn event with this name is emitted on any and all errors. The event contains information about the error, as well as the feature and operation that caused the error to occur.
thingy.operationdiscardedAn event with this name is emitted whenever a connect/start/stop operation is discarded on one of Thingy's features. The event contains information about the operation that was cancelled along with a function call.
thingy.writeAn event with this name is emitted whenever a successful write operation has been performed. The event contains information about the feature that triggered the write operation, as well as the value that written.

Supported operations

FeatureStart/StopReadWrite
Absolute orientationYesNoNo
Advertising parametersNoYesYes
BatteryYesYesNo
ButtonYesNoNo
Cloud tokenNoYesYes
ColorYesNoNo
Connection parametersNoYesYes
DFUYesNoYes
Eddystone urlNoYesYes
Environment configurationNoYesYes
Euler orientationYesNoNo
FirmwareNoYesNo
GasYesNoNo
Gravity vectorYesNoNo
HeadingYesNoNo
HumidityYesNoNo
LEDNoYesYes
MicrophoneYesNoNo
Motion configurationNoYesYes
MTUNoYesYes
NameNoYesYes
PressureYesNoNo
Quaternion orientationYesNoNo
Raw dataYesNoNo
Rotation matrix orientationYesNoNo
Sound configurationNoYesYes
Speaker DataNoNoYes
Speaker StatusYesNoNo
Step counterYesNoNo
TapYesNoNo
TemperatureYesNoNo

Below you can find extended information on each feature Thingy supports, as well as information about the parameters required to interact with them.

Thingy

Parameters

Absolute Orientation

thingy.absoluteorientation

event: absoluteorientation

Allows interaction with the connected device's absolute orientation sensor

Supported operations

Advertising Parameters

thingy.advertisingparameters

Allows interaction with the connected device's advertising parameters

Supported operations

Battery

thingy.battery

event: battery

Allows interaction with the connected device's battery level

Supported operations

Button

thingy.button

event: button

Allows interaction with the connected device's button

Supported operations

Cloud Token

thingy.cloudtoken

Allows interaction with the connected device's cloud token service

Supported operations

Color

thingy.color

event: color

Allows interaction with the connected device's color sensor (not LED)

Supported operations

Connection Parameters

thingy.connectionparameters

Allows interaction with the connected device's connection parameters

Supported operations

DFU

thingy.dfucontrolpoint_v1 thingy.dfucontrolpoint_v2

Allows interaction with the connected device's DFU service (Device Firmware Upgrade). Note that depending on which firmware version the Thingy has, the characteristic is different. If the firmware is version 1.x, use the 'thingy.dfucontrolpoint_v1' characteristic. If it is 2.x, use the 'thingy.dfucontrolpoint_v2' characteristic. This is due to a change in the UUID of the service from firmware version 1.1.0 to 2.0.0.

Supported operations

Eddystone Url

thingy.eddystone

Allows interaction with the connected device's eddystone url service

Supported operations

Environment Configuration

thingy.environmentconfiguration

Allows interaction with the connected device's environment configuration

Supported operations

Euler Orientation

thingy.eulerorientation

event: eulerorientation

Allows interaction with the connected device's euler orientation sensor

Supported operations

Firmware

thingy.firmware

Allows interaction with the connected device's firmware service

Supported operations

Gas

thingy.gas

event: gas

Allows interaction with the connected device's gas sensor (co2 and tvoc)

Supported operations

Gravity vector

thingy.gravityvector

event: gravityvector

Allows interaction with the connected device's gravity vector sensor

Supported operations

Heading

thingy.heading

event: heading

Allows interaction with the connected device's heading sensor

Supported operations

Humidity

thingy.humidity

event: humidity

Allows interaction with the connected device's humidity sensor

Supported operations

LED

thingy.led

Allows interaction with the connected device's LED

Supported operations

Microphone

thingy.microphone

event: microphone

Allows interaction with the connected device's microphone

Supported operations

Motion Configuration

thingy.motionconfiguration

Allows interaction with the connected device's motion configuration

Supported operations

MTU

thingy.mtu

Allows interaction with the connected device's MTU service (Maximum Transmission Unit)

Supported operations

Name

thingy.name

Allows interaction with the connected device's name service

Supported operations

Pressure

thingy.pressure

event: pressure

Allows interaction with the connected device's pressure sensor

Supported operations

Quaternion orientation

thingy.quaternionorientation

event: quaternionorientation

Allows interaction with the connected device's quaternion orientation sensor

Supported operations

Raw data

thingy.rawdata

event: rawdata

Allows interaction with the connected device's raw data sensor (includes accelerometer, gyroscope, and compass)

Supported operations

Rotation matrix orientation

thingy.rotationmatrixorientation

event: rotationmatrixorientation

Allows interaction with the connected device's rotation matrix orientation sensor

Supported operations

Sound Configuration

thingy.soundconfiguration

Allows interaction with the connected device's sound configuration

Supported operations

Speaker Data

thingy.speakerdata

Allows interaction with the connected device's speaker data. Note that speaker mode has to be set to the desired mode beforehand by writing to Sound Configuration.

Supported operations

Speaker Status

thingy.speakerstatus

event: speakerstatus

Allows interaction with the connected device's speaker status

Supported operations

Step counter

thingy.stepcounter

event: stepcounter

Allows interaction with the connected device's step counter sensor

Supported operations

Tap

thingy.tap

event: tap

Allows interaction with the connected device's tap sensor

Supported operations

Temperature

thingy.temperature

event: temperature

Allows interaction with the connected device's temperature sensor

Supported operations