# Hướng dẫn kết nối tới MQTT Broker của nền tảng

## Tạo mới thiết bị

Vui lòng tạo mới thiết bị trước khi thực hiện các bước tiếp theo để thuận tiện cho quá trình kết nối MQTT và xác thực thiết bị.

Để thực hiện tạo mới thiết bị, tham khảo hướng dẫn [Thiết bị](/innoway/tinh-nang/quan-li-thuc-the/thiet-bi.md).

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

## 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 của MQTT

Để lấy Token, người dùng nhấn vào mục tài khoản -> Copy token thiết bị

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

* Device ID : là mã định danh của thiết bị trên Innoway

<figure><img src="/files/04c1lasSvXWFw57ZUI9R" alt=""><figcaption></figcaption></figure>

Chọn **Sao chép ID** để lấy ID của thiết bị, VD : 72fe7191-06af-423d-abdd-311178a9bd34

## Thực hiện kết nối

Để đẩy dữ liệu thừ thiết bị lên nền tảng Innoway thông qua MQTT có 2 cách:

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

### Cách 1 : Sử dụng Topic của hệ thống

#### Định dạng topic mqtt của nền tảng : *<mark style="color:yellow;">**messages/{{device\_id}}/{{subtopic}}**</mark>*

Trong đó :&#x20;

* {{device\_id}} : là ID của thiết bị đã lấy được ở bên trên
* {{subtopic}} : là phần mở rộng của topic dùng cho các mục đích khác nhau
  * <mark style="color:yellow;">messages/{{device\_id}}/attribute</mark> : Lưu thuộc tính thiết bị, chỉ lưu giá trị mới nhất
  * <mark style="color:yellow;">messages/{{device\_id}}/attributets</mark> : Lưu thuộc tính thiết bị, lưu lịch sử thay đổi giá trị của thuộc tính

{% hint style="success" %}
Ví dụ:\ <mark style="color:$warning;">messages/72fe7191-06af-423d-abdd-311178a9bd34/attribute</mark>\ <mark style="color:$warning;">messages/72fe7191-06af-423d-abdd-311178a9bd34/attributets</mark>
{% endhint %}

{% hint style="warning" %}
Vui lòng sử dụng đúng ***device\_id*** của thiết bị, nếu ID bị sai hoặc không thuộc sở hữu của tài khoản sẽ gây mất kết nối mqtt
{% endhint %}

Sau khi đã có thông tin topic, người dùng có thể kết nối tới  MQTT broker với các thông tin sau :

{% hint style="success" %}
Host : <mark style="color:yellow;">mqttvcloud.innoway.vn</mark>

Port : <mark style="color:yellow;">1883</mark>

Username : Tùy ý

Password : Token (lấy ở mục [Bắt đầu kết nối](#bat-dau-ket-noi))
{% endhint %}

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

#### Thực hiện Publish dữ liệu&#x20;

Sau khi kết nối thành công, người dùng có thể thực hiện Publish dữ liệu với định dạng json lên nền tảng, ví dụ dữ liệu được Publish :&#x20;

```json
{
  "value_double": 12345.67898,
  "value_string": "string",
  "value_int": 1234567898,
  "value_bool": true,
  "value_json": {
    "bool": true,
    "number": 12345,
    "string": "value"
  }
}
```

Từ dữ liệu được đẩy lên hệ thống sẽ căn cứ vào các json key có cấp độ thấp nhất để lưu thành các thuộc tính riêng biệt(attribute)

Với ví dụ trên hệ thống sẽ tự động lưu dữ liệu thành 5 thuộc tính với tên là các  json key tương ứng:&#x20;

```yaml
attributes:
  - attribute_key: double_value
    logged: true
    value: 12345.67898
    last_update_ts: 1759138078420
    value_type: DBL (double)
    
  - attribute_key: string_value
    logged: true
    value: string
    last_update_ts: 1759138078420
    value_type: STR (string)
    
  - attribute_key: long_value
    logged: true
    value: 1234567898
    last_update_ts: 1759138078420
    value_type: LONG (long)
    
  - attribute_key: bool_value
    logged: true
    value: true
    last_update_ts: 1759138078420
    value_type: BOOL (bool)
    
  - attribute_key: json_value
    logged: true
    value:
      bool: true
      number: 12345
      string: value
    last_update_ts: 1759138078420
    value_type: JSON (json)
```

Sau khi gửi dữ liệu thành công, chọn mục Sửa thiết bị để xem chi tiết các thông tin của thiết bị

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

Chọn "Thuộc tính" để xem các thuộc tính đang có

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

Dữ liệu mới nhất sẽ luôn được hiển thị nếu thiết bị tiếp tục gửi bản tin đến. Nếu dữ liệu được gửi lên thông qua : ***messages/{{device\_id}}/**<mark style="color:yellow;">**attributets**</mark>* , các thuộc tính sẽ lưu lịch sử thay đổi (phần "Lưu lịch sử" sẽ có màu đỏ) và có thể xem lịch sử thuộc tính tại mục "Bản tin thuộc tính" :&#x20;

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

### Cách 2 : Sử dụng Topic do người dùng tự định nghĩa

Được sử dụng trong khi người dùng muốn tự định nghĩa topic, vui lòng tham khảo hướng dẫn  [Adapter MQTT](/innoway/tinh-nang/custom-adapter/mqtt.md#bat-dau-su-dung)&#x20;

{% hint style="warning" %}
Lưu ý, định dạng topic người dùng định nghĩa không được trùng với định dạng topic hệ thống
{% endhint %}

### Phần mở rộng

Để thực hiện tạo mới thiết bị và lấy mật khẩu kết nối MQTT tự động, tham khảo tính năng [Provision Device](/innoway/tinh-nang/tinh-nang-khac/provision-device.md).


---

# 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/bat-dau-su-dung/huong-dan-ket-noi-thiet-bi/huong-dan-ket-noi-toi-mqtt-broker-cua-nen-tang.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.
