Hướng dẫn sử dụng Module thu phát RF Zigbee CC2530 Uart 2.4g
Module thu phát RF Zigbee CC2530 Uart 2.4GHz là giải pháp lý tưởng để giám sát nhiệt độ và độ ẩm từ xa một cách hiệu quả. Với khả năng truyền dữ liệu ổn định, tiết kiệm năng lượng và phạm vi hoạt động rộng, module này phù hợp cho các ứng dụng IoT như nhà thông minh, nông nghiệp thông minh, và hệ thống giám sát môi trường. Bài viết này sẽ hướng dẫn cách cấu hình và sử dụng module CC2530 để thu thập dữ liệu từ cảm biến nhiệt độ, độ ẩm và truyền về trung tâm giám sát thông qua giao tiếp UART, đảm bảo sự tiện lợi và chính xác.
Thông số kỹ thuật của Module thu phát RF Zigbee CC2530 Uart 2.4g
- IC: CC2530
- Điện áp làm việc: 3 ~ 5.5 VDC
- Dòng hiện tại: < 30mA
- Tốc độ truyền điểm-điểm: lên đến 3300Bps (đo ở khoảng cách 1m)
- Khoảng cách truyền: 250m (ở điều kiện lý tưởng)
- Chuẩn truyền sóng: Zigbee 2.4G
- Giao thức: UART TTL
- Baudrate có thể cài đặt: 2400, 4800,… 115200
- Kích thước: 15.5 x 31.5mm
Thành phần cần thiết
Sơ đồ kết nối
| ARDUINO R3 | Zigbee CC2530 | LCD | DHT11 |
| 5V | 5V | 5V | |
| GND | GND | GND | |
| 2 | Data | ||
| 3 | RX | ||
| 11 | TX | ||
| 4 | D4 | ||
| 5 | D5 | ||
| 6 | D6 | ||
| 7 | D7 | ||
| 8 | RS | ||
| 9 | EN |
Code Test
Code Transmiter
// Cac thu vien
#include <LiquidCrystal.h>
#include <DHT.h>
#include <EEPROM.h>
#include <SoftwareSerial.h>
#include <stdlib.h> // Them thu vien nay cho dtostrf
// Dinh nghia cac chan
const int DHT_PIN = 2;
const int LCD_RS = 8, LCD_EN = 9, LCD_D4 = 4, LCD_D5 = 5, LCD_D6 = 6, LCD_D7 = 7;
const int KEYPAD_PIN = A0;
const int SOFT_RX = 3, SOFT_TX = 11;
// Cac hang so
const int DHT_TYPE = DHT11;
#define KEY_RIGHT 0
#define KEY_UP 1
#define KEY_DOWN 2
#define KEY_LEFT 3
#define KEY_SELECT 4
#define KEY_NONE 5
// Bo dem thoi gian cho code khong chan
unsigned long lastSensorRead = 0;
unsigned long lastSerialSend = 0;
const long sensorInterval = 2000; // Doc cam bien moi 2 giay
const long serialInterval = 1000; // Gui du lieu moi 1 giay
// ===== BIEN MOI CHO CHONG DOI PHIM =====
unsigned long lastKeypressTime = 0;
const long keypressInterval = 200; // Thoi gian cho giua 2 lan nhan (ms)
// Cac bien toan cuc
int tempThreshold = 20;
int humiThreshold = 50;
int currentPage = 0;
int settingMode = 0; // 0: Binh thuong, 1: Cai dat Nhiet do, 2: Cai dat Do am
// Khoi tao doi tuong
DHT dht(DHT_PIN, DHT_TYPE);
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
SoftwareSerial softSerial(SOFT_RX, SOFT_TX);
byte degreeSymbol[8] = {0b00110, 0b01001, 0b01001, 0b00110, 0b00000, 0b00000, 0b00000, 0b00000};
void setup() {
Serial.begin(9600);
softSerial.begin(9600);
dht.begin();
lcd.begin(16, 2);
lcd.createChar(0, degreeSymbol);
// Tai nguong da luu tu EEPROM
tempThreshold = EEPROM.read(0);
humiThreshold = EEPROM.read(1);
// Kiem tra co ban cho lan su dung dau tien
if (tempThreshold > 100 || tempThreshold < 0) tempThreshold = 20;
if (humiThreshold > 100 || humiThreshold < 0) humiThreshold = 50;
lcd.clear();
lcd.print("He Thong San Sang!");
delay(1000);
lcd.clear();
}
void loop() {
unsigned long currentMillis = millis();
handleKeypad();
// Tac vu 1: Doc du lieu cam bien dinh ky
if (currentMillis - lastSensorRead >= sensorInterval) {
lastSensorRead = currentMillis;
float h = dht.readHumidity();
float t = dht.readTemperature();
if (!isnan(h) && !isnan(t)) {
// Tac vu 2: Cap nhat LCD theo trang hien tai
updateDisplay(t, h);
// Tac vu 3: Gui du lieu qua serial dinh ky
if (currentMillis - lastSerialSend >= serialInterval) {
lastSerialSend = currentMillis;
sendData(t, h);
}
} else {
lcd.setCursor(0,0);
lcd.print("Loi Cam Bien!");
}
}
}
void updateDisplay(float t, float h) {
lcd.clear();
switch (currentPage) {
case 0: // Man hinh thong tin chinh
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(t, 1);
lcd.write((byte)0);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("H:");
lcd.print(h, 1);
lcd.print("%");
break;
case 1: // Man hinh cai dat
lcd.setCursor(0, 0);
lcd.print("Dat NhietDo: ");
lcd.print(tempThreshold);
lcd.print((char)223); // Ky hieu do
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Dat DoAm: ");
lcd.print(humiThreshold);
lcd.print("%");
// Chi bao truc quan cho cai dat dang kich hoat
if(settingMode == 1) lcd.setCursor(15, 0);
if(settingMode == 2) lcd.setCursor(15, 1);
if(settingMode != 0) lcd.print("<");
break;
}
}
void sendData(float t, float h) {
char tempString[6];
char humiString[6];
char buffer[20];
dtostrf(t, 4, 1, tempString);
dtostrf(h, 4, 1, humiString);
sprintf(buffer, "T:%s|H:%s", tempString, humiString);
softSerial.print(buffer);
softSerial.print('\n');
}
int readKeypad() {
int adc_key_in = analogRead(KEYPAD_PIN);
if (adc_key_in > 1000) return KEY_NONE;
if (adc_key_in < 50) return KEY_RIGHT;
if (adc_key_in < 250) return KEY_UP;
if (adc_key_in < 450) return KEY_DOWN;
if (adc_key_in < 650) return KEY_LEFT;
if (adc_key_in < 850) return KEY_SELECT;
return KEY_NONE;
}
// ===== HAM XU LY PHIM DA DUOC NANG CAP =====
void handleKeypad() {
// Chi kiem tra phim neu da du thoi gian gian cach
if (millis() - lastKeypressTime < keypressInterval) {
return; // Chua du thoi gian, thoat
}
int key = readKeypad();
if (key == KEY_NONE) return; // Khong co phim nao duoc nhan
// Neu co phim duoc nhan, cap nhat thoi gian va xu ly
lastKeypressTime = millis();
if (currentPage == 0) { // Tren man hinh chinh
if (key == KEY_RIGHT || key == KEY_LEFT) {
currentPage = 1; // Di den cai dat
}
} else if (currentPage == 1) { // Tren man hinh cai dat
switch (key) {
case KEY_RIGHT:
case KEY_LEFT:
currentPage = 0; // Quay lai man hinh chinh
settingMode = 0; // Thoat che do cai dat
break;
case KEY_SELECT:
settingMode++;
if (settingMode > 2) {
settingMode = 0; // Thoat che do cai dat
// Chi luu vao EEPROM khi thoat che do cai dat
EEPROM.write(0, tempThreshold);
EEPROM.write(1, humiThreshold);
}
break;
case KEY_UP:
if (settingMode == 1) tempThreshold++;
if (settingMode == 2) humiThreshold++;
break;
case KEY_DOWN:
if (settingMode == 1) tempThreshold--;
if (settingMode == 2) humiThreshold--;
break;
}
// Gioi han gia tri
if (tempThreshold > 50) tempThreshold = 50;
if (tempThreshold < 0) tempThreshold = 0;
if (humiThreshold > 99) humiThreshold = 99;
if (humiThreshold < 20) humiThreshold = 20;
}
}
Code Receiver
// Cac thu vien
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <stdlib.h> // Can thiet cho atof()
// Dinh nghia cac chan
const int LCD_RS = 8, LCD_EN = 9, LCD_D4 = 4, LCD_D5 = 5, LCD_D6 = 6, LCD_D7 = 7;
const int SOFT_RX = 3, SOFT_TX = 11;
// Cac hang so
const byte BUFFER_SIZE = 32;
// Cac bien toan cuc
char serialBuffer[BUFFER_SIZE];
byte bufferIndex = 0;
bool newData = false;
// Khoi tao doi tuong
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
SoftwareSerial softSerial(SOFT_RX, SOFT_TX);
byte degreeSymbol[8] = {0b00110, 0b01001, 0b01001, 0b00110, 0b00000, 0b00000, 0b00000, 0b00000};
void setup() {
Serial.begin(9600);
softSerial.begin(9600);
lcd.begin(16, 2);
lcd.createChar(0, degreeSymbol);
lcd.print("Bo Nhan San Sang");
lcd.setCursor(0, 1);
lcd.print("Dang cho du lieu...");
}
void loop() {
receiveData();
if (newData) {
parseAndDisplayData();
newData = false;
}
}
// Ham nhan du lieu serial khong chan (phien ban sach)
void receiveData() {
while (softSerial.available() > 0 && !newData) {
char receivedChar = softSerial.read();
if (receivedChar != '\n' && bufferIndex < BUFFER_SIZE - 1) {
serialBuffer[bufferIndex] = receivedChar;
bufferIndex++;
} else {
serialBuffer[bufferIndex] = '\0'; // Them ky tu null vao cuoi chuoi
bufferIndex = 0;
newData = true;
}
}
}
// Phan tich chuoi nhan duoc va cap nhat LCD
void parseAndDisplayData() {
// De go loi, in bo dem tho ra Serial Monitor
Serial.print("Bo dem tho: [");
Serial.print(serialBuffer);
Serial.println("]");
// Dinh dang mong doi: "T:25.5|H:60.1"
// Tim dau phan cach '|'
char* separator = strchr(serialBuffer, '|');
if (separator == NULL) { // Neu khong tim thay '|', du lieu bi loi
Serial.println("Loi phan tich: Khong tim thay dau phan cach '|'.");
return;
}
// Tim dau hai cham ':' dau tien cho nhiet do
char* tempColon = strchr(serialBuffer, ':');
if (tempColon == NULL) { // Neu khong tim thay ':', du lieu bi loi
Serial.println("Loi phan tich: Khong tim thay ':' cho nhiet do.");
return;
}
// Tim dau hai cham ':' thu hai cho do am (tim sau dau phan cach)
char* humiColon = strchr(separator, ':');
if (humiColon == NULL) { // Neu khong tim thay ':', du lieu bi loi
Serial.println("Loi phan tich: Khong tim thay ':' cho do am.");
return;
}
// Chuyen doi phan gia tri (sau dau hai cham) sang so thuc
float temp = atof(tempColon + 1); // +1 de bo qua dau ':'
float humi = atof(humiColon + 1); // +1 de bo qua dau ':'
// Cap nhat LCD
lcd.clear();
// Hien thi Nhiet do
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(temp, 1);
lcd.write((byte)0);
lcd.print("C");
// Hien thi Do am
lcd.setCursor(0, 1);
lcd.print("H:");
lcd.print(humi, 1);
lcd.print("%");
}

Nhận xét