ZigBee CC2530 UART 2.4G Giám sát nhiệt độ, độ ẩm từ xa

Hướng dẫn sử dụng Module thu phát RF Zigbee CC2530 Uart 2.4g

Hướng dẫn sử dụng Module thu phát RF Zigbee CC2530 Uart 2.4g

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

#include <LiquidCrystal.h>
#include <DHT.h>
#include <EEPROM.h>
#include <SoftwareSerial.h>
#include <stdio.h>
#include <stdlib.h>

SoftwareSerial SoftSerial(3, 11); // RX | TX pins.
int tempT = 0, tempH = 20;

const int DHTPIN = 2;
const int DHTTYPE = DHT11;
const int Q1 = 12;
const int Q2 = 13;

const int pin_RS = 8;
const int pin_EN = 9;
const int pin_d4 = 4;
const int pin_d5 = 5;
const int pin_d6 = 6;
const int pin_d7 = 7;

//
String content = "";
char character;
//
byte degree[8] = {
  0B01110,
  0B01010,
  0B01110,
  0B00000,
  0B00000,
  0B00000,
  0B00000,
  0B00000
};

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(pin_RS, pin_EN, pin_d4, pin_d5, pin_d6, pin_d7);

int page = 0, value;
int cd = 0;

char checkKeyPad()
{
  int x;
  x = analogRead(0);
  if (x < 60)
  {
    return 'r';
  }
  else if (x < 200)
  {
    return 'u';
  }
  else if (x < 400)
  {
    return 'd';
  }
  else if (x < 600)
  {
    return 'l';
  }
  else if (x < 800)
  {
    return 's';
  }
}

void button()
{
  if (checkKeyPad() == 'l')
  {
    page --;
    if (page > 4) page = 0;
    if (page < 0) page = 4;
    lcd.clear();
    while (checkKeyPad() == 'l');
  }
  if (checkKeyPad() == 'r')
  {
    page ++;
    if (page > 3) page = 0;
    if (page < 0) page = 3;
    lcd.clear();
    while (checkKeyPad() == 'r');
  }
}
void setup() {
  lcd.createChar(1, degree);
  pinMode(Q1, OUTPUT);
  pinMode(Q2, OUTPUT);
  lcd.begin(16, 2);
  Serial.begin(9600);
  SoftSerial.begin(9600);
  dht.begin();
  EEPROM.write(1, tempT);
  delay(5);
}
void loop() {
  char string[20];
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  tempT = EEPROM.read(5);
  tempH = EEPROM.read(6);
  if (isnan(t) || isnan(h)) {
    Serial.println("DHT sensor error!");
    lcd.setCursor(0, 0);
    lcd.print("DHT sensor error!");
    lcd.setCursor(0, 1);
    lcd.print("                ");
    lcd.clear();
    return;
  }
  else {
    switch (page)
    {
      case 0:
        {
          lcd.setCursor(0, 0);
          lcd.print("    Thegioiic   ");
          lcd.setCursor(0, 1);
          lcd.print("<");
          lcd.setCursor(3, 1);
          lcd.print("CC22530-TX");
          lcd.setCursor(15, 1);
          lcd.print(">");
          button();
          break;
        }
      case 1:
        {
          lcd.setCursor(0, 1);
          lcd.print("<");
          lcd.setCursor(15, 1);
          lcd.print(">");
          lcd.setCursor(2, 0);
          lcd.print(" T: ");
          lcd.print(t);
          lcd.write(1);
          lcd.print("C");

          lcd.setCursor(2, 1);
          lcd.print(" H: ");
          lcd.print(h);
          lcd.print(" %");
          button();
          SoftSerial.print("t:");
          SoftSerial.print(t);
          SoftSerial.print(" | h:");
          SoftSerial.println(h);
          delay(1000);
          break;
        }
      case 2:
        {
          switch (cd)
          {
            case 0:
              {
                lcd.setCursor(0, 1);
                lcd.print("<");
                lcd.setCursor(15, 1);
                lcd.print(">");
                lcd.setCursor(5, 0);
                lcd.print("Set T,H");
                lcd.setCursor(1, 1);

                lcd.print("T = ");
                lcd.print(tempT);
                lcd.setCursor(9, 1);
                lcd.print("H = ");
                lcd.print(tempH);

                if (checkKeyPad() == 's')
                {
                  cd++;
                  if (cd > 2)cd = 0;
                  while (checkKeyPad() == 's');
                }
                button();
                break;

              }

            case 1:
              {
                setTemp();
                if (checkKeyPad() == 'u')
                {
                  tempT++;
                  if (tempT > 50)tempT = 0;
                  while (checkKeyPad() == 'u');
                }
                if (checkKeyPad() == 'd')
                {
                  tempT--;
                  if (tempT < 0)tempT = 50;
                  while (checkKeyPad() == 'd');
                }
                if (checkKeyPad() == 's')
                {
                  cd++;
                  if (cd > 2)cd = 0;
                  while (checkKeyPad() == 's');
                }
                EEPROM.write(5, tempT);
                delay(5);
                break;
              }
            case 2:
              {
                setHumi();
                if (checkKeyPad() == 'u')
                {
                  tempH++;
                  if (tempH > 95)tempH = 20;
                  while (checkKeyPad() == 'u');
                }
                if (checkKeyPad() == 'd')
                {
                  tempH--;
                  if (tempH < 20)tempH = 95;
                  while (checkKeyPad() == 'd');
                }
                if (checkKeyPad() == 's')
                {
                  cd++;
                  if (cd > 2)cd = 0;
                  while (checkKeyPad() == 's');
                }
                EEPROM.write(6, tempH);
                delay(5);
                break;
              }
          }
          break;
        }
      case 3:
        {
          lcd.setCursor(0, 0);
          lcd.print("T:");
          lcd.print(t);
          lcd.write(1);
          lcd.print("C");
          lcd.setCursor(0, 1);
          lcd.print("H:");
          lcd.print(h);
          lcd.print(" %");

          if (t > tempT) {
            lcd.setCursor(10, 0);
            lcd.print("Q1: CL");
            digitalWrite(Q1, LOW);
          } else {
            lcd.setCursor(10, 0);
            lcd.print("Q1: OP");
            digitalWrite(Q1, HIGH);
          }
          if (h > tempH) {
            lcd.setCursor(10, 1);
            lcd.print("Q2: CL");
            digitalWrite(Q2, LOW);
          } else {
            lcd.setCursor(10, 1);
            lcd.print("Q2: OP");
            digitalWrite(Q2, HIGH);
          }
          button();
          break;
        }
      case 4:
        {
          lcd.setCursor(0, 0);
          lcd.print("Page 4");
          content = SoftSerial.readString(); 
          if (content != "") {
          Serial.println(content);
          }
          button();
          break;
        }
    }
  }
}

void setTemp()
{
  lcd.setCursor(5, 0);
  lcd.print("Set T,H");
  lcd.setCursor(1, 1);
  lcd.print("T = ");
  lcd.print(tempT);
  delay(150);
  lcd.setCursor(5, 1);
  lcd.print("  ");
  lcd.setCursor(9, 1);
  lcd.print("H = ");
  lcd.print(tempH);
  delay(150);
}

void setHumi()
{
  lcd.setCursor(5, 0);
  lcd.print("Set T,H");
  lcd.setCursor(9, 1);
  lcd.print("H = ");
  lcd.print(tempH);
  lcd.setCursor(1, 1);
  lcd.print("T = ");
  lcd.print(tempT);

  delay(150);

  lcd.setCursor(13, 1);
  lcd.print("  ");

  delay(150);
}

Code Receiver

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <stdio.h>
#include <stdlib.h>

const int pin_RS = 8;
const int pin_EN = 9;
const int pin_d4 = 4;
const int pin_d5 = 5;
const int pin_d6 = 6;
const int pin_d7 = 7;

SoftwareSerial SoftSerial(3, 11); // RX | TX pins.
String content = "";
char character;

byte degree[8] = {
  0B01110,
  0B01010,
  0B01110,
  0B00000,
  0B00000,
  0B00000,
  0B00000,
  0B00000
};
LiquidCrystal lcd(pin_RS, pin_EN, pin_d4, pin_d5, pin_d6, pin_d7);
void setup() {
  lcd.createChar(1, degree);
  lcd.begin(16, 2);
  Serial.begin(9600);
  SoftSerial.begin(9600);
  //EEPROM.write(1, tempT);
  delay(5);

}

void loop() {
  // put your main code here, to run repeatedly:
  lcd.setCursor(3, 0);
  lcd.print("CC2530-RX");
  content = SoftSerial.readString();
  if (content != "") {
    lcd.setCursor(0, 1);
    lcd.print(content);
    Serial.println(content);
  }
}

Video

Nhận xét

Mới hơn Cũ hơn