Hướng dẫn kết nối thiết bị với thingsboard gateway (docker windows) thông qua mqtt connector
{tocify} $title = {Danh mục}
Trong đó:
Cài đặt docker
Tải và cài đặt bản docker desktop mới nhất tại https://www.docker.com/get-started (hiện tại mình đang dùng phiên bản 4.0.1)
Cài đặt mosquitto
Tải và cài đặt phần mềm mosquitto phiên bản mới nhất tại đường dẫn: https://mosquitto.org/download/
Sau khi tải xong, chúng ta tiến hành test xem quá trình cài đặt mosquitto thành công hay chưa bằng cách:
Test cài đặt mosquitto bằng Command Prompt
Tiến hành mở 03 tabs Command Prompt chế độ administrator và truy cập vào đường dẫn cài đặt của mosquitto bằng cách nhập lệnh:
cd ../../
cd "Program Files\Mosquitto"
Khởi chạy mosquitto mqtt broker
mosquitto -v
Subcribe mqtt topic
mosquitto_sub -t #
Publish to mqtt topic
mosquitto_pub -t "test" -m "hello world"
Cài đặt mosquitto
mở port mqtt 1883 trên windows
Để thiết bị khác có thể truy cập vào mosquitto broker trên máy tính windows đang cài mosquitto, bạn cần phải mở port trên máy tính (1883). Bạn hãy làm theo hướng dẫn: tại https://bytesofgigabytes.com/networking/how-to-open-port-in-windows/
Cho phép truy cập vào broker dưới dạng địa chỉ ip, chọn chế độ mqtt
Tiến hành mở file mosquitto.conf trong thư mục cài đặt (C:\Program Files\Mosquitto) và thêm 02 dòng dưới đây:
listener 1883 0.0.0.0
allow_anonymous true
Tạo user, password (Không cần thiết)
Tạo gateway trên Thingsboard
Tạo gateway device trên thingsboard như hình dưới:
Cài đặt Thingsboard Gateway trên docker
Tải docker thingsboard gateway image
docker pull thingsboard/tb-gateway
Khởi chạy docker image
Cấu hình thingsboard gateway
Liệt kê các container đang chạy:
docker ps -a
docker exec -it [container id] bash
Đi vào thư mục cấu hình của tb-gateway
cd thingsboard_gateway/config/
Cài đặt gói nano để edit file
apt install nano
Mở và chỉnh sửa file tb_gateway.yaml
nano tb_gateway.yaml
Trong đó:
- host: đường dẫn đến server thingsboard gateway
- accessToken: xem mục 4
- connectors: chọn mqtt
Lưu và thoát
Ctrl + X -> Y -> Enter
Chỉnh sửa mqtt connector
nano mqtt.json
- host: địa chỉ ip của máy tính khởi chạy mqtt broker (mosquitto), nếu máy tính của bạn chạy cả docker và mqtt broker thì bạn có thể điền host.docker.internal thay cho địa chỉ IP.
- port: 1883
- security: anonymous (không bảo mật) hoặc basic
- username, password của mqtt broker (đã tạo ở mục cài đặt mosquitto).
Lưu và thoát
Ctrl + X -> Y -> Enter
Restart thingsboard gateway
Test
Khởi chạy mosquitto mqtt broker
net start mosquitto
Restart thingsboard gateway
Tạo device với gateway
mosquitto_pub -h localhost -p 1883 -t "/sensor/data" -m "{\"serialNumber\":\"esp32-smart-switch\", \"temp\":12}"
hoặc
mosquitto_pub -h localhost -p 1883 -t "/sensor/data" -u user -P password -m "{\"serialNumber\":\"esp32-smart-switch\", \"temp\":12}"
Leave a Comment