- Build
- Connect your first device
Connect your first device
Run a device on your own computer with the JavaScript device SDK, send it a reading, and see the reading on a dashboard. No hardware needed.
Your computer stands in for the device. You run a short program with the Qualithm Device SDK. It joins your team, connects, and sends a temperature reading every five seconds. Then you watch the reading arrive on a dashboard.
Before you start
#- Node.js 20 or later.
- A Qualithm account. Sign in to the Qualithm app (opens in a new tab).
Create a scope
#A scope groups devices in one zone. Every device belongs to one.
In the app, open Scopes (opens in a new tab). Under Scopes, enter a Name, such
as Desk, and choose Create scope. If the app asks for a Zone, choose the one nearest you.
The app opens the new scope. Go back to Scopes (opens in a new tab) for the next step.
Create a claim code
#A claim code is a single-use code that lets a device add itself to a scope. The device exchanges it for its own credential the first time it connects.
On the Scopes page, under Claim codes:
- Choose your scope under Scope.
- Choose Create claim code.
- Copy the code. It is shown once.
- Copy the API URL under Connection details. The device claims its credential there.
Keep the page open. It shows when the device claims the code.
Install the SDK
#Create a folder for the device and install the SDK in it:
mkdir my-first-device
cd my-first-device
npm init -y
npm install @qualithm/deviceWrite the device
#Create device.mjs in the same folder. Replace <your-api-url> and <your-claim-code> with the
values you copied:
import { setTimeout as sleep } from "node:timers/promises"
import { Device } from "@qualithm/device"
const device = new Device({
provisioningUrl: "<your-api-url>",
claimCode: "<your-claim-code>",
name: "My first device",
capabilities: [{ key: "temperature", type: "sensor", metric: "temperature", unit: "celsius" }]
})
await device.connect()
console.log("connected")
while (true) {
const temperature = Math.round((20 + Math.random() * 5) * 10) / 10
await device.publish("telemetry", JSON.stringify({ ts: Date.now(), metrics: { temperature } }), {
qos: 1
})
console.log(`sent temperature ${temperature}`)
await sleep(5000)
}What each part does:
capabilitiesdeclares what the device reports: here, onetemperaturereading. A dashboard offers only readings the device declared.connect()claims the device with the claim code, stores its credential, and opens a connection to the gateway for its zone.publish()sends a reading to thetelemetrytopic.tsis the reading time in epoch milliseconds.metricsmaps each metric name to a number. Withqos: 1, the call resolves once the gateway has stored the reading.
Run it
#node device.mjsThe program prints connected, then a line for each reading it sends. In the app, the claim code
shows Claimed, and My first device claimed appears with a link to the device.
The SDK stores the credential in .qualithm/credential.json. Later runs connect with it and skip
the claim, so the used claim code is no longer needed. Keep .qualithm/ out of version control: it
holds the device's token.
See the reading on a dashboard
#- Open Dashboards (opens in a new tab) and choose Create dashboard. Under Start from, keep Blank, then choose Create dashboard. The new dashboard opens empty.
- Choose Edit layout, then Add widget.
- Choose the Custom tab. Under Type, choose Current value.
- Under Device, choose My first device. Under Reading, choose temperature.
- Choose Add widget, then Done.
The widget shows the latest temperature and whether the device is online. It updates with each reading the device sends. Stop the program with Ctrl+C.
Next steps
#- MicroPython: run the same steps on a Raspberry Pi Pico 2 W or an ESP32 board.
- Devices: how a device joins, connects and reaches production.
- Dashboards: the other widgets, and how to arrange them.
- Declare capabilities: add controls the dashboard can set.
- Automations: act on a reading as it arrives.