MQTT(S) và HTTP(S) protocol trên Magistrala
1. Giao tiếp qua MQTT (Message Queuing Telemetry Transport)
Đây là giao thức tối ưu cho thiết bị IoT nhờ đặc tính nhẹ và kết nối duy trì (persistent connection).
-
Cổng mặc định: 1883 (hoặc 8883 cho TLS).
-
Xác thực: Sử dụng Client ID làm Username và Client Key làm Password.
-
Cấu trúc Topic:
-
Gửi/nhận dữ liệu (Publish/Subscribe): m/<domain_id>/c/<channel_id>/<subtopic>
VD:
mosquitto_pub -I <client_name> -u <client_id> -P <client_secret> -t m/<domain_id>/c/<channel_id> -h localhost -p 1883 -m '[{"bn":"some-base-name:","bt":1.276020076001e+09, "bu":"A","bver":5, "n":"voltage","u":"V","v":120.1}, {"n":"current","t":-5,"v":1.2}, {"n":"current","t":-4,"v":1.3}]'
mosquitto_sub -I <client_name> -u <client_id> -P <client_secret> -t m/<domain_id>/c/<channel_id> -h localhost -p 1883
2. Giao tiếp qua HTTP (Hypertext Transfer Protocol)
Dùng cho các thiết bị không giữ được kết nối liên tục hoặc các ứng dụng web/mobile muốn gửi dữ liệu nhanh.
-
Cổng mặc định: 8008.
-
Xác thực: Sử dụng Client Key đặt trong Header Authorization.
-
Endpoint: POST /m/<domain_id>/c/<channel_id>/<subtopic>
VD:
curl -X POST -H "Content-Type: application/senml+json" -H "Authorization: Client <client_secret>" http://localhost:8008/m/<domain_id>/c/<channel_id> -d '[{"bn":"some-base-name:","bt":1.276020076001e+09, "bu":"A","bver":5, "n":"voltage","u":"V","v":120.1}, {"n":"current","t":-5,"v":1.2}, {"n":"current","t":-4,"v":1.3}]'
No Comments