# MQTT Device API

## Bắt đầu kết nối

Người dùng cần chuẩn bị những thông tin sau:

* Token: là password sử dụng cho bảo mật TLS của MQTT
* Device ID: là mã định danh của thiết bị trên Innoway

## Lấy Token

Để lấy Token, người dùng tạo thiết bị mới, bấm và chọn Sao chép Token

<figure><img src="/files/gTi7HKlzmSHxaBZ35qK7" alt=""><figcaption></figcaption></figure>

## Thiết lập thiết bị gửi bản tin

Sau đó, thiết lập cho thiết bị gửi bản tin MQTT với thông tin sau:

|          |                 |                                                                    |
| -------- | --------------- | ------------------------------------------------------------------ |
| topic    |                 | subTopic được sử dụng cho Flow Engine theo bài toán của người dùng |
| payload  |                 | Nội dung nhận Strings/JSON/XML/Characters                          |
| url      | mqtt.innoway.vn | IP mặc định của Broker Innoway                                     |
| username |                 | Có thể sử dụng bất kỳ Username nào                                 |
| password | {{token}}       |                                                                    |

Người dùng có thể gửi bản tin JSON vào subTopic **attribute** để xem ngay ở trên hệ thống.

## Attributes API <a href="#attributes-api" id="attributes-api"></a>

Người dùng có thể gửi bản tin JSON vào subTopic **attribute** để xem ngay ở trên hệ thống

Người dùng có thể vào trong màn hình **Danh sách thuộc tính** của thiết bị để xem thông tin thiết bị đã gửi với subTopic **attribute** và Payload **JSON**.

<figure><img src="/files/MGFmhZajDxxPdROUukvT" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/BLKA7L1bbKVcl3kb1yLW" alt=""><figcaption></figcaption></figure>

## Provision Device

Vui lòng xem bài viết tương ứng để biết thêm thông tin về tính năng [Provision Device](/innoway/tinh-nang/tinh-nang-khac/provision-device.md).

Thực hiện kết nối với username: **provision**, sau đó subscribe vào topic:

```
provision/response
```

Để bắt đầu Provision Device, gửi Provision Request tới topic sau:

```
provision/request
```

Đinh dạng bản tin gửi đi:

```json
{
  "device_name": "DEVICE_NAME", //required
  "device_key": "DEVICE_KEY", //optional
  "provision_key": "u7piawkboq8v32dmcmpp", //required
  "provision_secret": "jpmwdn8ptlswmf4m29bw" //required
}
```

Thực hiện subscribe vào topic&#x20;

Nếu thành công, bản tin trả về sẽ có dạng:

```json
{
  "device_id": "bb56e5d7-96bc-4339-8780-6ebb67e5ac44",
  "status": "SUCCESS",
  "token": "zD8zxMrMaRVnhZhiGEVuRw2jid3oW721"
}
```

Sau khi provision thành công, thiết bị có thể lấy token để thực hiện kết nối MQTT tới nền tảng


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://innoway.gitbook.io/innoway/tai-lieu/api/mqtt-device-api.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
