Hướng dẫn làm dự án đọc khoảng cách từ cảm biến siêu âm SRF05
Hướng dẫn làm dự án đọc khoảng cách từ cảm biến siêu âm SRF05
Bạn dễ dàng kết nối đến LOTODA platform bằng Board ESP8266 để tạo một dự án đọc khoảng cách từ cảm biến siêu âm SRF05 và vẽ đồ thị trên app Lotoda theo thời gian thực! 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 siêu âm SRF05
Thông số kỹ thuật của cảm biến siêu âm SRF05:
- Điện áp hoạt động: 5V
- Dòng cấp: 30mA, 50mA Max.
- Tần số: 40KHz
- Khoảng cách đo được xa nhất: 3 m
- Phát hiện vật cản trong khoảng: 3cm đến 3m
- Kích thước: 43mm x 20mm x 17mm
Software: Arduino IDE và LOTODA app
Sơ đồ kết nối từ NodeMCU ESP8266 như sau:
+ Chân GND -> GND
+ Chân 5V -> VCC
+ Chân D1 (GPIO 05) -> TRIG
+ Chân D2 (GPIO 04) -> ECHO
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------------------------------------------------------------
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
const unsigned int TRIG_PIN=5;
const unsigned int ECHO_PIN=4;
#define WIFI_SSID "xxxx" //Enter your WIFI SSID
#define WIFI_PASSWORD "xxxx" //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-xxxx"; //every device should have a different name
char topic[] = "<User_ID_Key>/SRF05"; //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(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(115200);
}
void loop() {
if ( !client.connected() ) {
connect();
}
client.loop();
long now = millis();
if (now - lastMsg > 1000) {
lastMsg = now;
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
const unsigned long duration= pulseIn(ECHO_PIN, HIGH);
int distance= duration/29/2;
if(duration==0){
Serial.println("Warning: no pulse from sensor");
}
else{
Serial.print("distance to nearest object:");
Serial.println(distance);
Serial.println(" cm");
}
char distanceString[6];
dtostrf(distance, 1, 2,distanceString);
String key = "{\"Distance\":";
String endkey = "}";
String str = key + distanceString + endkey;
char data[20];
str.toCharArray(data, (str.length() + 1));
Serial.println(data);
client.publish(topic, data);
}
}
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 "SRF05" (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ị "LineChart 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ị khoảng cách đo được 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-SRF05
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.