Design of smart home system based on Arduino

Design of smart home system based on Arduino

1. Design requirements
1. Use DHT11 temperature and humidity detection module and photoresistor to collect environmental temperature, humidity and light intensity information;
2. Initially set the DS1302 clock module through the Arduino development board, and read the date and time information at regular intervals ;
3. Use the LCD1602 display module to display information such as temperature, humidity, light intensity, date, time and control mode;
4. According to the time information read from the DS1302 clock module, automatically feed and water pets at a fixed time every day 5.
Can switch between automatic control mode and voice control mode;
6. In automatic control mode, lights, air conditioners, curtains, etc. are automatically controlled according to environmental parameters such as temperature, humidity, light intensity, etc. 7. In the voice control mode, the voice control of household appliances is
realized according to the control instructions received by the LD3320 voice recognition module.
insert image description here

2. System hardware design
1. Arduino development board
insert image description here
2. DHT11 temperature and humidity detection module
DHT11 is a digital sensor integrating humidity and temperature. The sensor includes a resistive humidity measuring element and an NTC temperature measuring element, and is connected with a high-performance 8-bit microcontroller. The local humidity and temperature can be collected in real time through a simple circuit connection of a microprocessor such as a single chip microcomputer. DHT11 can use simple single bus to communicate with single-chip microcomputer, and only needs one I/O port. The 40Bit data of the internal humidity and temperature data of the sensor is transmitted to the single-chip microcomputer at one time, and the data is verified by the checksum method to effectively ensure the accuracy of data transmission. The power consumption of DHT11 is very low. Under the power supply voltage of 5V, the average maximum working current is 0.5mA. The technical parameters of DHT11 are as follows: Operating voltage range: 3.3V-5.5V, operating current: average 0.5mA, output: single bus digital signal, measuring range: humidity 20~90%RH, temperature 0~50℃, accuracy: humidity ± 5%, temperature ±2°C, resolution: humidity 1%, temperature 1°C, the pin arrangement of DHT11 is shown in the figure below: DHT11
insert image description here
digital humidity temperature sensor adopts single bus data format. That is, a single data pin port completes input and output bidirectional transmission. Its data packet consists of 5Byte (40Bit). The data is divided into a fractional part and an integer part, and a complete data transmission is 40bit, and the high bit is first out. The data format of DHT11 is: 8bit humidity integer data + 8bit humidity decimal data + 8bit temperature integer data + 8bit temperature decimal data + 8bit checksum. The checksum data is the addition of the first four bytes. Sensor data output is unencoded binary data. Data (humidity, temperature, integer, decimal) should be processed separately.
3. LCD1602 display module
insert image description here
4. Potentiometer
The potentiometer is a resistive element with three terminals and the resistance value can be adjusted according to a certain change rule. A potentiometer usually consists of a resistor body and a movable brush. When the brush moves along the resistor body, a resistance value or voltage that has a certain relationship with the displacement is obtained at the output end. The potentiometer can be used as a three-terminal element or as a two-terminal element. The latter can be regarded as a variable resistor. Because its function in the circuit is to obtain an output voltage that has a certain relationship with the input voltage (applied voltage), it is called a potentiometer.
5. Relay A
relay is an electrical appliance that turns on or off a circuit according to changes in physical quantities such as voltage, current, temperature, speed or time. It is a switch that can turn off or turn on a circuit under certain conditions, but The power of the switch is not human hands, but electricity, magnetism, sound, light, heat, etc. It is often used to isolate control and controlled circuits, control high-voltage systems with low voltage, and control high-current systems with small currents to realize logic functions. The main function of relays is protection and control, so although there are many types of relays, they can usually be divided into two categories: protection relays and control relays. Common relays include thermal relays, electromagnetic relays, time relays, temperature relays and speed relays.
6. DS1302 clock module
DS1302 is a high-performance, low-power, real-time clock circuit with RAM launched by DALLAS Company of the United States. It can time the year, month, day, Sunday, hour, minute, and second, and has the function of leap year compensation. And it can automatically adjust the date of the month with less than 31 days, and supports 12-hour and 24-hour format. The operating voltage is 2.5V ~ 5.5V. It contains a real-time clock/calendar and a 31-byte RAM, and uses a three-wire interface to communicate synchronously with the CPU, and can transmit multiple bytes of clock signals or RAM data at a time in burst mode. By using serial communication, the connection between DS1302 and microcontroller can be simplified. Only three lines are needed to connect with DS1302: RST (reset); I/O (data line); SCLK (serial clock signal); data can be transferred between RAMs 1 byte at a time or up to 31 bytes. Description of other pins: VCC2: working power supply. GND: Power ground. VCC1: back power supply, which requires a 3V button battery, which can continue to supply power when VCC2 is disconnected, so as to maintain the clock data. The DS1302 requires very low power consumption to operate, requiring less than 1 microwatt to maintain data and clock information. There are multiple registers inside it, and we can read and write these registers by accessing specific addresses.
7. Photoresistor
Photocell, also known as photoresistor (abbreviated as ldr) or light guide, is often represented by the letters "R" or "RL" or "RG" in circuits. It is a resistance that changes with the intensity of external light. When the incident light is strong, the resistance decreases, and when the incident light is weak, the resistance increases. The commonly used material is cadmium sulfide, and there are also semiconductor materials such as selenium, aluminum sulfide, lead sulfide and bismuth sulfide. Generally used for light measurement, light control and photoelectric conversion (convert light changes into electrical changes); such as street lights, corridor light control lights, smoke alarms, etc. Photoresistors are special resistors made of semiconductor materials such as vulcanization or selenization, and their working principle is based on the internal photoelectric effect. These materials generate carriers under the irradiation of light of a specific wavelength to participate in conduction, and drift movement under the action of an external electric field. The electrons rush to the positive pole of the power supply, and the holes rush to the negative pole of the power supply, so that the resistance of the photoresistor rapidly increases. decline.
insert image description here
8. LD3320 speech recognition module
The LD332X chip is a special chip for "speech recognition" designed and produced by ICRoute. The chip integrates a speech recognition processor and some external circuits, including AD, DA converter, microphone interface, audio output interface, etc. It does not need any external auxiliary chips such as Flash, RAM, etc., and can be directly integrated into existing products to realize voice recognition/voice control/man-machine dialogue functions. Really provides a single identification solution. Inside LD332X, there is an efficient non-specific speech recognition search engine module and a complete non-specific speech recognition feature library, as well as hardware optimization and acceleration design specially for speech recognition.
insert image description here
3. System software design
insert image description here
4. System testing **
Open the serial monitor and observe the MOC3 port recognized by the machine (different USB ports are different), and the lCD1602 display will display the current light intensity, humidity temperature information and clock date. The default control mode of the system is automatic control. In automatic control, cover the photoresistor with your hand, the system will judge that the current light intensity is weak, and trigger the command to turn on the light, and the LED light will be lit. After removing your hand, Use the flashlight of the mobile phone to give a light, the system will trigger the command to turn off the light, and the LED will go out. The DHT11 temperature and humidity sensor module will collect the temperature and humidity of the surrounding environment. When the temperature in the environment is too high, it will trigger the command to turn on the air conditioner in the system. Here, the serial port shows that the air conditioner is turned on. When the humidity in the environment is too high, the serial port will display open curtains. When the voice module receives the voice signal, it will convert the current control mode to the voice mode. In the program, multiple times can be customized to set the feeding time and drinking time for the pet. The DS1302 module will always read the current time , Use the conditional judgment statement to check whether the current time is equal to the time set by yourself. If they are equal, the system will execute the corresponding instructions such as feeding and drinking. When the voice module receives the voice, it will switch from the automatic mode to the voice mode, and at the same time define the voice content in advance and bind the voice content. When the system receives the command "kai deng" and "guan deng", The LED light will respond accordingly. When receiving the commands of "kai kong tiao" and "guan kong tiao", the serial port will display the belief. When receiving the "kai jia shi" and "guan jia shi" commands, the serial port will display accordingly. The entire simple furniture system is also considered a successful design and there is no problem with the test.
appendix

#include <dht11.h>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    那你  
#include <ld3320.h>
#include <PinMap.h>
#include <LiquidCrystal.h>
#include "DS1302.h"//时钟控制
#include <PinMap.h>
#define AD5 A5   //定义模拟口A5   光敏电阻使用
#define LED A0   //定义模拟口A5    led灯的 控制
#define DHT11PIN A4   //  温湿度数据
const int rs = 6, en = 10, d4 = 5, d5 = 8, d6 = 3, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int Intensity = 0;//光照度数值
dht11 DHT11;// 定义一个温湿度模块的对象
unsigned char Water_Hour=10;          // 喂水小时设置
unsigned char Water_Minute=38;        // 喂水分钟设置
unsigned char Food_Hour=10;          // 喂食小时设置
unsigned char Food_Minute=45;        // 喂食分钟设置
int HumBorder=60;  //湿度上限
int TemBorder=30;// 温度上限
int lightBorder=990;  //光强上限
String mode="Auto";   // 自动情况下
VoiceRecognition Voice;  //声明一个语音识别对象
bool Always=false;  //是否不间断地输出时间到串口(每隔 1 秒)
bool IsOpenHum=true;  //是否不间断地输出时间到串口(每隔 1 秒)
bool IsOpenTem=true;  //是否不间断地输出时间到串口(每隔 1 秒)

//显示时间
void Display_RTCC()
{
    
    

  if(DS1302Buffer.Year < 10)
  {
    
    
    Serial.print("200");
  }
  else
  {
    
    
    Serial.print("20");
  }
  Serial.print(DS1302Buffer.Year);
  Serial.print('-');
  Serial.print(DS1302Buffer.Month);
  Serial.print('-');
  Serial.print(DS1302Buffer.Day);
  Serial.print("   ");
  switch(DS1302Buffer.Week)
  {
    
    
  case 1:
    Serial.println("Mon");         //显示星期一
    break;
  case 2:
    Serial.println("Tue");         //显示星期二
    break;
  case 3:
    Serial.println("Wed");         //显示星期三
    break;
  case 4:
    Serial.println("Thu");         //显示星期四
    break;
  case 5:
    Serial.println("Fri");         //显示星期五
    break;
  case 6:
    Serial.println("Sat");         //显示星期六
    break;
  case 7:
    Serial.println("Sun");         //显示星期日
    break;
  default : break;            
  }
  Serial.print(DS1302Buffer.Hour);
  Serial.print(':');
  if(DS1302Buffer.Minute<10){
    
    
    Serial.print("0");
  }
  Serial.print(DS1302Buffer.Minute);
  Serial.print(':');
  Serial.println(DS1302Buffer.Second);
}
//串口输入
void Scan_Input()
{
    
    
  String comdata=""; 
  while (Serial.available() > 0)  
  {
    
    
    comdata += char(Serial.read());
    delay(2);
  }
  const char*timechar=comdata.c_str();
  if(strncmp(timechar,"set",3)==0)
  {
    
    
    Set_Time(comdata);
    Display_RTCC();
  }
  if(strncmp(timechar,"waterset",8)==0)
  {
    
    
    Water_Hour=comdata.substring(9,11).toInt();
    Water_Minute=comdata.substring(12,14).toInt();
    lcd.clear();
    lcd.print("Waterset:");
    lcd.print((String)Water_Hour+":"+(String)Water_Minute);
    lcd.setCursor(0,1);
    lcd.print("Set done!");
    delay(2000);
    lcd.clear();
  }
  if(strncmp(timechar,"foodset",7)==0)
  {
    
    
    Food_Hour=comdata.substring(8,10).toInt();
    Food_Minute=comdata.substring(11,13).toInt();
    lcd.clear();
    lcd.print("Foodset:");
    lcd.print((String)Food_Hour+":"+(String)Food_Minute);
    lcd.setCursor(0,1);
    lcd.print("Set done!");
    delay(2000);
    lcd.clear();
  }

  if(strncmp(timechar,"lightset",8)==0)
  {
    
    
    lightBorder=comdata.substring(8,12).toInt();
    lcd.clear();
    lcd.print("Lightset:");
    lcd.print((String)lightBorder);
    lcd.setCursor(0,1);
    lcd.print("Set done!");
    delay(2000);
    lcd.clear();
  }

  if(strncmp(timechar,"show",4)==0)
  {
    
    
    Serial.println("The Time Now Is:");
    Always=false;
    Display_RTCC();
  }

  //是否不间断地将时间输出到串口
  if(strncmp(timechar,"always",6)==0)
  {
    
    
    Always=!Always;
  }

  //显示闹钟
  //if( strncmp(timechar,"alarm",5)==0)
  //{
    
    
  //  Serial.print("Water Set to ");
  //  Serial.print(Water_Hour); 
  //  Serial.print(":");
  //  Serial.println(Water_Minute);   
  //}
  //帮助v
  if(strncmp(timechar,"help",4)==0)
  {
    
    
    help();
  }
  //切换自动
  if(strncmp(timechar,"auto",4)==0 )
  {
    
    
    mode="Auto";
    Serial.println("Mode: Auto");
    Serial.println("Set done");
  }
  //切换语音
  if(strncmp(timechar,"voice",5)==0){
    
    
    mode="Voice";
    Serial.println("Mode: Voice");
    Serial.println("Set done");
  }
}

//时间设置函数
void Set_Time(String &command)
{
    
    
  DS1302_ON_OFF(0);     //关闭振荡
  DS1302Buffer.Year=command.substring(4,6).toInt();
  DS1302Buffer.Month=command.substring(7,9).toInt();
  DS1302Buffer.Day=command.substring(10,12).toInt();
  DS1302Buffer.Week=command.substring(13,14).toInt();
  DS1302Buffer.Hour=command.substring(15,17).toInt();
  DS1302Buffer.Minute=command.substring(18).toInt();
  DS1302_SetTime( DS1302_YEAR , DS1302Buffer.Year );
  DS1302_SetTime( DS1302_MONTH , DS1302Buffer.Month );
  DS1302_SetTime( DS1302_DAY , DS1302Buffer.Day );
  DS1302_SetTime( DS1302_WEEK , DS1302Buffer.Week );
  DS1302_SetTime( DS1302_HOUR , DS1302Buffer.Hour );
  DS1302_SetTime( DS1302_MINUTE , DS1302Buffer.Minute );
  DS1302_ON_OFF(1);
  Always=false;
  Serial.println("Set Done");
  Display_RTCC();   
}


//帮助内容
void help()
{
    
    
  Serial.println("************************************* help *****************************************");
  Serial.println("Edited By Starky");
  Serial.println("Type \"set YY MM DD WEEK HH MM\" To Set The Time!");
  Serial.println("Such As Type \"set 08 08 08 1 12 00\" To Set The Time As 2008-8-8 Monday 12:00");
  Serial.println("Type \"waterset HH MM\" to set water time");
  Serial.println("Type \"foodset HH MM\" to set food time");
  Serial.println("Type \"lightset ###\" such as \"lightset 987\" to set lightborder");
  Serial.println("Type \"show\" To Display The Time Now!");
  Serial.println("Type \"always\" To Print The Time Every Second!");
  Serial.println("Welcome To Use!"); 
  Serial.println("************************************* help *****************************************");
}

// 自动控制系统
void autoControl(int Intensity,int Hum,int Tem){
    
    
  if(Intensity>=lightBorder)
  {
    
    
    digitalWrite(LED, HIGH);
  }
  else if(Intensity<=900)
  {
    
    
    digitalWrite(LED, LOW);
  }
  if(Hum>=HumBorder)
  {
    
    
    if(IsOpenHum)
    {
    
    
    Serial.println("关闭加湿器");
    IsOpenHum=false;
    }
  }

  if(Tem>=TemBorder)
  {
    
    
    if(IsOpenTem)
    {
    
    
    Serial.println("打开空调");
    IsOpenTem=false;
    }
  }
 
}
void setup()  //程序初始化
{
    
           
  Voice.init();                               //初始化VoiceRecognition模块   
  Voice.addCommand("zi dong",0);
  Voice.addCommand("sheng yin",1);
  Voice.addCommand("kai deng",2);
  Voice.addCommand("guan deng",3);
  Voice.addCommand("kai jia shi",4);
  Voice.addCommand("guan jia shi",5);
  Voice.addCommand("kai kong tiao",6);
  Voice.addCommand("guan kong tiao",7);
  Voice.start();//开始识别
  pinMode(LED,OUTPUT);//设置LED为输出模式
  Serial.begin(9600);//设置波特率9600
  lcd.begin(16, 2);
  DS1302_Init();
  DS1302_GetTime(&DS1302Buffer);
}

void loop()//程序主体循环
{
    
    
  Scan_Input();
  Intensity = analogRead(AD5);  //读取模拟口AD5的值,存入Intensity变量  光敏电阻
  int chk = DHT11.read(DHT11PIN); 
  switch(Voice.read()){
    
    
  case 0:
    mode="Auto";
    Serial.println("自动");
    break;
  case 1:
    mode="Voicqe";
    Serial.println("声音");
    break;
  case 2:
  if(mode=="Voicqe"){
    
    
    Serial.println("开灯===");
    digitalWrite(LED, HIGH);
  }
    break;
  case 3:
  if(mode=="Voicqe"){
    
    
    Serial.println("关灯===");  
    digitalWrite(LED, LOW);
  }
    break;
  case 4:
    Serial.println("开加湿===");  
    break;
  case 5:
    Serial.println("关加湿===");  
    break;
  case 6:
    Serial.println("开空调===");  
    break;
  case 7:
    Serial.println("关空调===");  
    break;
  default:
    break;
  }
  int Hum=(int)DHT11.humidity;
  int Tem=(int)DHT11.temperature;
  if(mode=="Auto"){
    
    
    autoControl(Intensity,Hum,Tem);
  } 
  ///显示在lcd上的 温度 湿度  光强
  lcd.setCursor(0, 0);
  lcd.print("g");
  lcd.setCursor(1, 0);
  lcd.print(Intensity);
  lcd.setCursor(5, 0);
  lcd.print("s");
  lcd.setCursor(6, 0);
  lcd.print(Hum);
  lcd.setCursor(9, 0);
  lcd.print("t");
  lcd.setCursor(10, 0);
  lcd.print(Tem);
  ///显示时间
  lcd.setCursor(0, 1);
  lcd.print(DS1302Buffer.Year);
  lcd.print("-");
  lcd.print(DS1302Buffer.Month);
  lcd.print("-");
  lcd.print(DS1302Buffer.Day);
  lcd.print(" ");
  lcd.print(DS1302Buffer.Hour);
  lcd.print(":");
  if(DS1302Buffer.Minute<10){
    
    
    lcd.print("0");
  }
  lcd.print(DS1302Buffer.Minute);
  lcd.print(":");
  lcd.print(DS1302Buffer.Second);
  delay(500);
  lcd.clear();
  DS1302_GetTime(&DS1302Buffer);       //获取当前RTCC值
  if(Always)
  {
    
    
    if(Flag_Time_Refresh == 1)
    {
    
    
      Flag_Time_Refresh = 0;
      Display_RTCC();
    }
  }
  if(DS1302Buffer.Hour==Water_Hour && DS1302Buffer.Minute==Water_Minute && DS1302Buffer.Second==0)
  {
    
    
    Display_RTCC();
    Serial.println("喂水时间到======");
    lcd.clear();
    for(int times=1;times<4;times++){
    
    
      lcd.setCursor(0, 0);
      lcd.print("Water time!");
      delay(1000);
      lcd.clear();
    }
  }
  if(DS1302Buffer.Hour==Food_Hour && DS1302Buffer.Minute==Food_Minute && DS1302Buffer.Second==0)
  {
    
    
    Display_RTCC();
    Serial.println("喂食时间到=====");
    lcd.clear();
    for(int times=1;times<4;times++){
    
    
      lcd.setCursor(0, 0);
      lcd.print("Food time!");
      delay(1000);
      lcd.clear();
    } 
  }
}

Guess you like

Origin blog.csdn.net/m0_46717588/article/details/122213301