Hướng dẫn làm dự án đọc và hiển thị nhiệt độ & độ ẩm trên LOTODA Mobile app
Hướng dẫn làm dự án đọc và hiển thị nhiệt độ & độ ẩm trên LOTODA Mobile app
Bạn dễ dàng kết nối đến LOTODA platform bằng Board ESP8266 để tạo một dự án đọc và hiển thị nhiệt độ & độ ẩm trên app Lotoda! Hãy download code tại đây và sử dụng Arduno IDE để nạp code!
Bước 1: Chuẩn bị phần cứng và phần mềm:
Hardware: 1 Node MCU (ESP8266) và 1 cáp USB, 1 Cảm biến nhiệt độ & độ ẩm AM2301 (DHT21)

Thông số kỹ thuật của cảm biến AM2301:
- Điện áp hoạt động: 3.3-5V.
- Dòng tiêu thụ: 300uA
- Dải đo độ ẩm: 0-99.9%.
- Dải đo nhiệt độ: -40 ~ 80 độ C.
- Sai số độ ẩm: +-3%.
- Sai số nhiệt độ: +-0.5 độ C
Software: Arduino IDE và LOTODA app
Sơ đồ kết nối từ NodeMCU ESP8266 như sau:
- Chân GND -> GND AM2301 (đen)
- Chân 3V3 -> VCC AM2301 (đỏ)
- Chân D1 (GPIO 05) -> Signal AM2301 (vàng)
Bước 2: Dùng Arduino IDE để nạp code cho NodeMCU
Hướng dẫn nạp code sau đây trên NodeMCU và kết nối với LOTODA platform. Code như sau:
Chú ý như sau đây trước khi nạp code:
- Bạn cần thay đổi SSID và Password của WiFi mà bạn muốn thiết bị kết nối đến.
- Bạn cần thay đổi DeviceID của thiết bị
- Bạn cần nhập các <User ID Key> và <Password ID Key> của riêng bạn (đã được gởi qua email lúc đăng ký)
-------------------------------------Code start here------------------------------------------------------------
// This example shows how to connect to LOTODA using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <DHT.h>
#include <Wire.h>
#include <ArduinoJson.h>
#define DHTPIN 5
#define DHTTYPE DHT21
DHT dht(DHTPIN, DHTTYPE);
#define WIFI_SSID "..." //Enter your WIFI SSID
#define WIFI_PASSWORD "..." //Enter your WiFi password
#define BUILTIN_LED 2
char LotodaServer[] = "app.lotoda.vn"; //app.lotoda.vn is the default servername
int LotodaPort = 1883; //1883 is the default port
char deviceId[] = "LotodaEsp8266-0001"; //every device should have a different name
char topic[] = "<User_ID_Key>/DHT21"; //Enter <User_ID_Key> from LOTODA system, the topic should be different for each device as well
//Please create this new topic on LOTODA mobile app, in this case is "lamp/power"
char User_ID_Key[] = "<User_ID_Key>"; //Enter <User_ID_Key> from LOTODA system
char Password_ID_Key[] = "<Password_ID_Key>"; //Enter <Password_ID_Key> from LOTODA system
WiFiClient wifiClient;
PubSubClient client(wifiClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output led
Serial.begin(115200);
delay(20);
dht.begin();
Serial.println("Let' start now");
}
void loop() {
if ( !client.connected() ) {
connect();
}
client.loop();
long now = millis();
if (now - lastMsg > 1000) {
lastMsg = now;
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Convert the value to a char array
char tempString[8];
// float to string
dtostrf(t, 1, 2, tempString);
Serial.print("Temperature: ");
Serial.println(tempString);
char humString[8];
dtostrf(h, 1, 2, humString);
Serial.print("Humidity: ");
Serial.println(humString);
String key1 = "{\"temperature\":";
String key2 = ",\"humidity\":";
String endkey = "}";
String str = key1 + tempString + key2 + humString + endkey; // json message
char data[80];
str.toCharArray(data, (str.length() + 1));
Serial.println(str);
client.publish(topic, data); // publish message to server
}
}
void callback(char* topic, byte* payload, unsigned int length) {
// handle message arrived
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void connect() {
while (!client.connected()) {
if ( WiFi.status() != WL_CONNECTED) {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("Connected to ");
Serial.println(WIFI_SSID);
}
client.setServer(LotodaServer, LotodaPort);
client.setCallback(callback);
if (client.connect(deviceId, User_ID_Key, Password_ID_Key)) {
client.subscribe(topic);
Serial.println("Connected to LOTODA's Server");
} else {
Serial.print("[FAILED] [ rc = ");
Serial.print(client.state() );
Serial.println(" : retrying in 5 seconds]");
delay(5000);
}
}
}
--------------------------------------------Code end here-------------------------------------------------------
Bước 3: Mở LOTODA Mobile app và thiết lập thông số kết nối điều khiển NodeMCU
- Vào menu "SETTINGS", và thêm topic như sau "DHT21" (chú ý mặc định ứng dụng LOTODA mobile app đã chèn thêm <User ID key> vào phía trước topic bạn tạo)


- Sau đó click "Connect ..." để kết nối ứng dụng với IoT LOTODA. Hiển thị trạng thái "Connected" khi bạn đã kết nối thành công.
Bước 4: Tạo khối điều khiển (Widget) trên LOTODA mobile app để hiển thị nhiệt độ và độ ẩm
- Vào menu "DASHBOARD" như hình sau.
- Sau đó click lên "+ADD" chọn khối hiển thị "GaugeChart JSON" như hình bên dưới.


- Tiếp theo click lên icon edit của khối Widget vừa thêm, chọn Topic bạn vừa tạo và nhập các thông tin khai báo như hình sau đây.



- Sau đó Submit để kết thúc thiết lập.
Đến đây bạn có thể dùng LOTODA mobile app trên điện thoại smartphone để xem thông tin hiển thị nhiệt độ và độ ẩm realtime trên Dashboard.
Kết quả như sau:


Bạn xem thêm chi tiết tại LOTODA Github như sau:
https://github.com/lotoda/DIY-Mqtt-Esp8266-Am2301
Chúc bạn thành công!
Danh sách tin tức
Không có tin tức thuộc chủ đề này
MENU SẢN PHẨM
SẢN PHẨM ĐẶC BIỆT
Không có sản phẩm thuộc chủ đề này.