📅  最后修改于: 2021-01-04 00:57:58             🧑  作者: Mango
在这个项目中,我们将使用蓝牙网络构建一个家庭照明控制系统。在此项目中,我们使用蓝牙模块HC-05,Arduino设备,4通道中继模块等。
在该项目中,使用了四个主要组件:Android智能手机蓝牙应用程序,蓝牙收发器,Arduino设备和4通道中继模块。
Android应用程序通过单击“打开”按钮将串行数据发送到连接的蓝牙模块HC-05。蓝牙设备从应用程序接收数据,并通过蓝牙模块的TX引脚将其发送到Arduino的RX引脚。 Arduino设备读取输入数据并根据其中上传的程序对其进行处理,并将输出生成到4个Chanel继电器模块。
蓝牙应用程序的按钮打开时,它将主页灯设置为打开,蓝牙应用程序的按钮关闭时,它将主页灯设置为关闭。
编写一个Arduino程序以使用Bluetooth模块,带有Android应用程序的4 Chanel Relay Module来控制家庭照明。
String inputs;
#define relay1 2 //connect relay1 to pin 9
#define relay2 3 //connect relay2 to pin 8
#define relay3 4 //connect relay3 to pin 7
#define relay4 5 //connect relay4 to pin 6
#define relay5 6 //connect relay5 to pin 5
#define relay6 7 //connect relay6 to pin 4
#define relay7 8 //connect relay7 to pin 3
#define relay8 9 //connect relay8 to pin 2
void setup(){
Serial.begin(9600); //set rate for communicating with phone
pinMode(relay1, OUTPUT); //set relay1 as an output
pinMode(relay2, OUTPUT); //set relay2 as an output
pinMode(relay3, OUTPUT); //set relay1 as an output
pinMode(relay4, OUTPUT); //set relay2 as an output
pinMode(relay5, OUTPUT); //set relay1 as an output
pinMode(relay6, OUTPUT); //set relay2 as an output
pinMode(relay7, OUTPUT); //set relay1 as an output
pinMode(relay8, OUTPUT); //set relay2 as an output
digitalWrite(relay1, LOW); //switch relay1 off
digitalWrite(relay2, LOW); //switch relay2 off
digitalWrite(relay3, LOW); //switch relay1 off
digitalWrite(relay4, LOW); //switch relay2 off
digitalWrite(relay5, LOW); //switch relay1 off
digitalWrite(relay6, LOW); //switch relay2 off
digitalWrite(relay7, LOW); //switch relay1 off
digitalWrite(relay8, LOW); //switch relay2 off
}
void loop(){
while(Serial.available()){ //check if there are available bytes to read
delay(10); //delay to make it stable
char c = Serial.read(); //conduct a serial read
if (c == '#'){
break; //stop the loop once # is detected after a word
}
inputs += c; //means inputs = inputs + c
}
if (inputs.length() >0){
Serial.println(inputs);
if(inputs == "A"){
digitalWrite(relay1, LOW);
}
else if(inputs == "a"){
digitalWrite(relay1, HIGH);
}
else if(inputs == "B"){
digitalWrite(relay2, LOW);
}
else if(inputs == "b"){
digitalWrite(relay2, HIGH);
}
else if(inputs == "C"){
digitalWrite(relay3, LOW);
}
else if(inputs == "c"){
digitalWrite(relay3, HIGH);
}
else if(inputs == "D"){
digitalWrite(relay4, LOW);
}
else if(inputs == "d"){
digitalWrite(relay4, HIGH);
}
else if(inputs == "E"){
digitalWrite(relay5, LOW);
}
else if(inputs == "e"){
digitalWrite(relay5, HIGH);
}
else if(inputs == "F"){
digitalWrite(relay6, LOW);
}
else if(inputs == "f"){
digitalWrite(relay6, HIGH);
}
else if(inputs == "G"){
digitalWrite(relay7, LOW);
}
else if(inputs == "g"){
digitalWrite(relay7, HIGH);
}
else if(inputs == "H"){
digitalWrite(relay8, LOW);
}
else if(inputs == "h"){
digitalWrite(relay8, HIGH);
}
inputs="";
}
}
使用Arduino USB电缆将代码编译并上传到Arduino设备。在上传代码时,不会将其他设备与Arduino连接。
蓝牙模块HC-05 Arduino UNO
RX ——————————–> TX
TX ——————————–> RX
GND —————————–> GND
5V ——————————–> 3.3V
4通道中继模块Arduino UNO
GND ——————————> GND
IN1 ——————————–>针脚9
IN2 ——————————–>针脚8
IN3 ——————————–>针脚7
IN4 ——————————–>针脚6
VCC ——————————–> 5V
继电器模块,灯泡和输入电源之间的连接:
下载并安装Arduino Bluetooth Controller.apk文件的Android应用程序。点击这里下载
项目成果: