Hello World — Kết nối thiết bị đầu tiên

Hướng dẫn từng bước kết nối thiết bị IoT đầu tiên và xem dữ liệu trên dashboard trong 5 phút.

Hello World — Kết nối thiết bị đầu tiên

Hướng dẫn này sẽ giúp bạn kết nối thiết bị IoT đầu tiên và xem dữ liệu telemetry trên dashboard trong khoảng 5 phút.

Bước 1: Tạo thiết bị

  1. Đăng nhập vào Dashboard tại http://localhost:3000
  2. Vào Devices → nhấn nút + Add device
  3. Nhập tên thiết bị: My First Device
  4. Chọn Device profile: default
  5. Nhấn Add

Sau khi tạo, hệ thống tự động tạo Access Token cho thiết bị.

Bước 2: Lấy Access Token

  1. Click vào thiết bị vừa tạo
  2. Vào tab Credentials
  3. Copy giá trị Access Token (ví dụ: A1_TEST_TOKEN)

Bước 3: Gửi telemetry đầu tiên

Mở terminal và gửi dữ liệu qua MQTT:

# Cài mosquitto client nếu chưa có # macOS: brew install mosquitto # Ubuntu: sudo apt install mosquitto-clients mosquitto_pub \ -h localhost -p 1883 \ -u "A1_TEST_TOKEN" \ -t "v1/devices/me/telemetry" \ -m '{"temperature": 25.5, "humidity": 68, "status": "online"}'

Hoặc qua HTTP:

curl -X POST http://localhost:8080/api/v1/A1_TEST_TOKEN/telemetry \ -H "Content-Type: application/json" \ -d '{"temperature": 25.5, "humidity": 68, "status": "online"}'

Nếu thành công, bạn sẽ thấy dữ liệu xuất hiện ngay trên trang chi tiết thiết bị, tab Latest Telemetry.

Bước 4: Tạo Dashboard

  1. Vào Dashboards+ Add dashboard
  2. Đặt tên: My First Dashboard
  3. Nhấn Add → mở dashboard

Thêm widget bảng telemetry

  1. Nhấn Edit (nút bút chì)
  2. Nhấn + Add widget
  3. Chọn Entities tableLatest values
  4. Phần Datasource: chọn Device → chọn My First Device
  5. Thêm các key: temperature, humidity, status
  6. Nhấn AddSave

Thêm widget biểu đồ

  1. Tiếp tục Edit+ Add widget
  2. Chọn ChartsTime series chart
  3. Datasource: chọn My First Device → key temperature
  4. Nhấn AddSave

Bước 5: Tạo Alarm Rule

Tự động cảnh báo khi nhiệt độ vượt 30°C:

  1. Vào Device Profiles → chọn default
  2. Tab Alarm rules+ Add alarm rule
  3. Cấu hình:
    • Alarm type: High Temperature
    • Severity: Critical
    • Condition: temperature > 30
  4. Lưu lại

Thử kích hoạt alarm:

mosquitto_pub \ -h localhost -p 1883 \ -u "A1_TEST_TOKEN" \ -t "v1/devices/me/telemetry" \ -m '{"temperature": 35}'

Vào Alarms để xem cảnh báo mới.

Bước 6: Gửi dữ liệu liên tục (demo)

Script Python gửi dữ liệu mỗi 5 giây:

import paho.mqtt.client as mqtt import json, time, random ACCESS_TOKEN = "A1_TEST_TOKEN" client = mqtt.Client() client.username_pw_set(ACCESS_TOKEN) client.connect("localhost", 1883, 60) client.loop_start() print("Đang gửi dữ liệu... Ctrl+C để dừng") try: while True: payload = { "temperature": round(20 + random.random() * 20, 1), "humidity": round(50 + random.random() * 30, 1), } client.publish("v1/devices/me/telemetry", json.dumps(payload)) print(f"Sent: {payload}") time.sleep(5) except KeyboardInterrupt: client.loop_stop()

Bước tiếp theo