# Connect your first device

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](https://app.qualithm.com/).

## Create a scope

A [scope](https://docs.qualithm.com/scopes.md) groups devices in one [zone](https://docs.qualithm.com/zones.md). Every device belongs to one.

In the app, open [Scopes](https://app.qualithm.com/scopes). 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](https://app.qualithm.com/scopes) 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**:

1. Choose your scope under **Scope**.
2. Choose **Create claim code**.
3. Copy the code. It is shown once.
4. 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:

```bash
mkdir my-first-device
cd my-first-device
npm init -y
npm install @qualithm/device
```

## Write the device

Create `device.mjs` in the same folder. Replace `<your-api-url>` and `<your-claim-code>` with the
values you copied:

```js


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:

- `capabilities` declares what the device reports: here, one `temperature` reading. 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 the `telemetry` topic. `ts` is the reading time in epoch
  milliseconds. `metrics` maps each metric name to a number. With `qos: 1`, the call resolves once
  the gateway has stored the reading.

## Run it

```bash
node device.mjs
```

The 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

1. Open [Dashboards](https://app.qualithm.com/dashboards) and choose **Create dashboard**. Under
   **Start from**, keep **Blank**, then choose **Create dashboard**. The new dashboard opens empty.
2. Choose **Edit layout**, then **Add widget**.
3. Choose the **Custom** tab. Under **Type**, choose **Current value**.
4. Under **Device**, choose **My first device**. Under **Reading**, choose **temperature**.
5. 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](https://docs.qualithm.com/micropython.md): run the same steps on a Raspberry Pi Pico 2 W or an ESP32 board.
- [Devices](https://docs.qualithm.com/devices.md): how a device joins, connects and reaches production.
- [Dashboards](https://docs.qualithm.com/dashboards.md): the other widgets, and how to arrange them.
- [Declare capabilities](https://docs.qualithm.com/mqtt.md#declare-capabilities): add controls the dashboard can set.
- [Automations](https://docs.qualithm.com/automations.md): act on a reading as it arrives.
