蓝牙控制带密码门锁
一款基于 Arduino 的密码保护门锁,
引言
这款智能门锁安全、简单,易于管理家中的锁。这款锁无需钥匙,锁安装在门内,您可以通过蓝牙从门外控制它。由于锁在门内,窃贼无法破门而入。需要一个Android应用程序来打开和关闭锁,我将在教程的后面部分详细介绍如何开发一个Android应用程序。通过Android应用程序向锁发送密码,如果密码与您预设的锁密码匹配,则锁将打开并向您的手机发送反馈,例如锁已打开。在继续阅读之前,您可以观看演示视频
材料清单
- Arduino UNO (从 gearbest.com 购买): 这是锁的主要控制板。
- 蓝牙模块 HC-05 (从 gearbest.com 购买): 这是一个低成本、最受欢迎的蓝牙模块,常用于Arduino项目。使用此模块,您可以将数据从智能手机传输到Arduino微控制器板,通过蓝牙控制任何设备。
- 一个9g微型伺服电机 (从 gearbest.com 购买): 这是一个微型伺服电机。该电机可以使用微控制器非常精确地从0度旋转到180度。这里使用该电机来移动锁。
- 3D打印门锁 (.stl 文件已附带): 锁的原始版本由Thingiverse用户 Michal Altair Valasek 设计。感谢Valasek先生的创意设计。我对他的设计做了一些小的修改。
要打印锁,您需要一台3D打印机。如果您有3D打印机,那就太好了。如果您没有3D打印机,您可以在 这里 以 145 美元的价格购买。实际上,gerabest.com 以套件的形式出售。所以,您也可以享受自己动手制作3D打印机的乐趣。制作它并不难。
原理图
组件之间的连接非常简单。HC-05模块有四个引脚需要与Arduino连接,伺服电机有三个引脚需要与Arduino连接。原理图清楚地显示了连接。如需进一步说明,请遵循下表
Arduino & HC-05 模块之间的连接
连接 # | Arduino 引脚 | HC-05 引脚 |
1 | 5V | VCC |
2 | GND | GND |
3 | TX | RX |
4 | RX | TX |
Arduino & 伺服电机之间的连接
连接 # | Arduino 引脚 | 伺服电机引脚 |
1 | 5V | VCC |
2 | GND | GND |
3 | 数字引脚 9 | 信号 |
请相应地连接HC-05蓝牙模块。TX、RX引脚之间的连接有点误导。但其逻辑是,蓝牙模块发送的内容由Arduino接收。因此,蓝牙模块的TX引脚连接到Arduino的RX引脚,反之亦然。
您应该记住的另一件事是,当设备连接到RX、TX引脚时,Arduino无法上传任何代码。因此,在向板上载草图之前,必须移除TX和RX引脚的连接。
Using the Code
用于蓝牙控制智能门锁的代码非常简单。我设计了一个Android应用程序,它发送带有密码的命令。实际上,我设置了两个命令,一个是“OPEN=
”,另一个是“CLOSE=
”,密码可以包含任何数字、字母或符号或它们的组合。在应用程序中,我使用了两个按钮——一个用于打开门,另一个用于关闭门。当在密码框中输入密码并单击打开按钮时,应用程序会将“OPEN=
”命令与密码连接起来并发送到Arduino。我在命令后面添加了“=
”符号,它用作分隔符。从手机接收到字符串后,Arduino程序将命令和密码从接收到的字符串中分离出来,并分别保存到两个变量中。然后首先检查密码,如果密码与保存的密码匹配,则为“OPEN
”命令打开门,为“CLOSE
”命令关闭门。“=
”有助于分隔命令和密码。
以下代码段用于从蓝牙模块接收数据。Arduino使用串行协议与蓝牙模块通信。当串行端口接收到任何数据时,就会发生串行中断,并且以下函数会自动调用。
void serialEvent()
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
//Serial.write(inChar);
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline or a carriage return, set a flag
// so the main loop can do something about it:
if (inChar == '\n' || inChar == '\r') {
stringComplete = true;
}
}
}
从蓝牙模块接收数据后,以下代码段帮助Arduino从接收到的数据中分离命令和密码。
// identified the position of '=' in string and set its index to pos variable
int pos = inputString.indexOf('=');
// value of pos variable > or = 0 means '=' present in received string.
if (pos > -1) {
// substring(start, stop) function cut a specific portion of string
// from start to stop
// here command will be the portion of received string till '='
// let received string is open=test123
// then command is 'open'
command = inputString.substring(0, pos);
// value will be from after = to newline command
// for the above example value is test123
// we just ignore the '=' taking first parameter of substring as 'pos+1'
// we are using '=' as a separator between command and value
// without '=' any other character can be used
// we are using = menas our command or password
// must not contains any '=', otherwise it will cause error
value = inputString.substring(pos+1,
inputString.length()-1); // extract command up to \n excluded
//Serial.println(command);
//Serial.println(value);
然后使用一个if else
块来比较接收到的密码与预设密码,并根据此驱动伺服电机打开或关闭锁。
// password.compareTo(value) compare between password tring and value string,
// if match return 0
if(!password.compareTo(value) && (command == "OPEN")){
// if password matched and command is 'OPEN' than door should open
openDoor(); // call openDoor() function
Serial.println(" OPEN"); // sent open feedback to phone
delay(100);
}
else if(!password.compareTo(value) && (command == "CLOSE")){
// if password matched and command is 'CLOSE', then door should close
closeDoor();
Serial.println(" CLOSE"); // sent " CLOSE" string to the phone
delay(100);
}
else if(password.compareTo(value)){
// if password not matched, then sent wrong feedback to phone
Serial.println(" WRONG");
delay(100);
}
如果Arduino收到错误的密码,它会向Android手机发送一条消息,表明密码错误。实际上,Arduino发送“ WRONG
”字符串。Android程序然后显示密码错误的消息。完整的草图包含在zip文件中。
门锁3D设计
锁的所有部件都经过3D打印。我想感谢Thingiverse用户 ridercz 的精美设计。他设计并发布了这个基于伺服电机的门锁在thingiverse.com上。除Rack_Holder.stl外的所有文件均取自他的设计。您可以从下方或从 Thingiverse.com 下载文件。
一些文件有LT版本。轻量版比普通版需要更少的PLA,强度也更低。我打印了LT版本,工作良好。我使用Anet A8打印了所有部件。打印部件的照片已附上。
组装3D打印部件
要组装所有打印部件,您可以参考下面的视频说明。组装后,使用四个螺钉将所有部件紧密固定。
开发Android应用程序
我使用MIT App Inventor为我的Android手机开发应用程序。我使用App Inventor是因为它不需要任何编码,也不需要安装任何软件。您只需要一个Google帐户。访问 http://ai2.appinventor.mit.edu/,它会要求您使用Google帐户登录。
完整的App Inventor源文件 (BTcontrol.aia) 已随附。如果您不想自己制作应用程序或修改它,只需下载BTcontrol.apk并将其安装到您的Android手机上。
您也可以通过以下链接从Google Play商店下载
要修改源代码,请点击“Projects”菜单,然后选择“Import project (.aia) from my computer”,如图24所示,然后从您的计算机浏览BTcontrol.aia文件。导入后,您可以轻松地修改它。
如果您想了解更多关于使用App Inventor开发Android应用程序的信息,请访问 http://appinventor.mit.edu/explore/ai2/tutorials.html。
您也可以尝试 http://meta-guide.com/videography/100-best-appinventor-videos/。