Professional Documents
Culture Documents
IoT_Cuoi_Ki
IoT_Cuoi_Ki
IoT_Cuoi_Ki
----- -----
PROJECT IOT
……
- Ở giao diện chính, chọn mục Devices => Add device => Add new device ,
sau đó nhập tên của dự án :
- Bước tiếp theo, chọn mục Next : Credentials => MQTT Basic, nhập các
dữ liệu Client ID, Username, Password , lưu lại để nhập vào chương trình
gửi dữ liệu lên Thingsboard :
- Để cấu hình bảng diều khiển hiển thị dữ liệu được gửi lên, ở giao diện
chính Thingsboard, chọn mục Dashboards => Add dashboard => Create
new dashboard , sau đó nhập tên của dữ liệu muốn hiển thị:
- Giao diện của bảng điều khiển hiển thị dữ liệu:
3. Chạy chương trình mô phỏng trên Wokwi gửi dữ liệu lên Thingsboard
#include <EspMQTTClient.h>
#include <WiFi.h>
#include <DHT.h>
#include <PubSubClient.h>
EspMQTTClient client(
WIFI_NAME,
WIFI_PASSWORD,
"thingsboard.cloud", // MQTT Broker server ip
MQTT_USERNAME,
MQTT_PASSWORD,
MQTT_CLIENT_ID
);
// DHT information
#define DHTPIN 5
#define DHTTYPE DHT22 // or set DHT11 type if you use DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
// Begin DHT
dht.begin();
}
void onConnectionEstablished() {
Serial.println("MQTT Client is connected to ThingsBoard!");
}
void publishData() {
if (client.isConnected() && (millis() - lastTime > delayTime)) {
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("\nTemperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.println(F("% "));
Serial.println("Data published");
lastTime = millis();
}
}
void loop() {
client.loop();
publishData();
}
- Ở phần kết quả sau khi chạy chương trình, dữ liệu nhiệt độ và độ ẩm đã
được hiển thị, Publising data to Thingsboard báo dữ liệu đã được gửi
Thingsboard :