【CurieNano试用】 智能锁

2017-05-189918
AI 快速预览详细 收起
本文介绍了如何使用CurieNano控制板制作智能锁。项目所需材料包括TF卡读写模块、热释电红外传感器、9g舵机等。通过蓝牙连接手机,发送特定密码来控制舵机开门,并记录时间和蓝牙地址。项目中使用了CurieNano的低功耗模式和外部中断引脚,实现了智能锁的低功耗唤醒。
【CurieNano试用】 智能锁图1
首先,感谢大爱的DFROBOT给还是新手的我这次机会。有了这块控制板,我相信自己能够学到更多东西,产生更多好点子。 说实话CurieNano是一块功能非常强大的控制板。

它除了拥有内部时钟、BLE、电子罗盘、加速度计、陀螺仪等功能外,
还有传说中的IUM。
{:5_136:}

不过可惜我这个项目里面只用到了BLE、内部时钟这两个功能。

材料:一个TF卡读写模块(外加一张128MB的TF卡);
          一个热释电红外传感器;                                                                           【CurieNano试用】 智能锁图2
          一个9g舵机;

          杜邦线若干 细钢丝一根 泡沫双面胶一卷
          电池

                           
  由于需要用到蓝牙功能,所以要在手机上安装nRF Connect
  那么我要着的事情简单的说就是:
  红外模块检测到人靠近后,控制板由低功耗(sleep)被唤醒(wakeup);
  手机连接Cruienano后发送特定的若干个16进制数(密码),促发舵机开门,门开状态维持3秒钟;
  在开门的同时,将时间和蓝牙地址记录下来。  首先我找了很就,终于在国外的贴吧上找到了可以用在curie上的lowpower库文件
  然后我将热释放电传感器digital引脚于5好引脚相连,准备将5号用作中断引脚。(这里补充一下,cruienano的所有数字引脚都可以用作外部中断。)当有人靠近,传感器输出底变高(RISING)。因此,我将中断模式设置为RISING。然后就根据周围有无人,来决定系统的状态,从而节电。然后是蓝牙部分。这里总结打开BLE里面的LED例子                                                                                            
   控制LED和控制servo就差了一个servo库。然后把SD里面的ReadWrite例子融合到自己的程序里面来,就可以了。
看代码:
  1. #include <SD.h>//SD库需要自己下载
  2. #include <Servo.h>
  3. #include <CurieTime.h>
  4. #include <CurieBLE.h>
  5. #include <Power.h>//这个库直接通过添加一个zip库来添加
  6. int time1=0;//用time1来记录工作时间
  7. BLEService ServoService("19B10000-E8F2-537E-4F6C-D104768A1214");
  8. Servo myservo;
  9. BLEUnsignedCharCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
  10. File myFile;
  11. void setup() {
  12.   
  13.   Serial.begin(9600);
  14.   
  15.   attachInterrupt(5, wakeup, RISING);//中断开
  16.   
  17.   myservo.attach(9);//舵机开
  18.   
  19.   myservo.write(90);//设置初始位置
  20.   
  21.   setTime(00, 00, 00, 15, 5, 2017);//设置时间
  22.   
  23.   BLE.begin();//蓝牙开
  24.   BLE.setLocalName("Servo");//蓝牙名
  25.   BLE.setAdvertisedService(ServoService);
  26.   ServoService.addCharacteristic(switchCharacteristic);
  27.   BLE.addService(ServoService);
  28.   switchCharacteristic.setValue(0);
  29.   BLE.advertise();
  30.   Serial.print("Initializing SD card...");//判断SD卡是否正常
  31.   if (!SD.begin(4)) {
  32.     Serial.println("initialization failed!");
  33.     return;
  34.   }
  35.   Serial.println("initialization done.");
  36. }
  37. void loop()
  38. {
  39.   if(millis()-time1>=20000)//判断工作时间
  40.   {
  41.     time1=0;
  42.     PM.sleep();//低功耗
  43.   }
  44.   
  45.   BLEDevice central = BLE.central();
  46.   
  47.   if (central)
  48.   {
  49.    
  50.       Serial.print("Connected to central: ");
  51.       Serial.println(central.address());
  52.       
  53.       while (central.connected())
  54.       {
  55.           if (switchCharacteristic.written())
  56.           {
  57.             if (switchCharacteristic.value()==0x02) //依次接收两次蓝牙信号,并检测是否是02、06
  58.             {  
  59.               while (1)
  60.               {
  61.                 if (switchCharacteristic.written())
  62.                 {
  63.                   if (switchCharacteristic.value()==0x06)
  64.                   {  
  65.                      myservo.write(0);//开门
  66.                      myFile = SD.open("tp.txt", FILE_WRITE);//记录时间,开门蓝牙地址
  67.                        // if the file opened okay, write to it:
  68.                       if (myFile) {
  69.                             Serial.print("Writing to test.txt...");
  70.                             myFile.print("Who:");
  71.                             myFile.println(central.address());
  72.                            
  73.                             myFile.print("Open Time is: ");
  74.                             print2digits(hour());
  75.                             myFile.print(":");
  76.                             print2digits(minute());
  77.                             myFile.print(":");
  78.                             print2digits(second());
  79.                             myFile.print("   ");
  80.                             myFile.print(year());
  81.                             myFile.print("/");
  82.                             myFile.print(month());
  83.                             myFile.print("/");
  84.                             myFile.print(day());
  85.                             myFile.println();
  86.                            
  87.                             myFile.close();
  88.                             delay(3000);
  89.                             myservo.write(90);
  90.                             Serial.println("done.");
  91.                         }
  92.                         else {
  93.                         // if the file didn't open, print an error:
  94.                         Serial.println("error opening test.txt");
  95.                         }
  96.                      break;
  97.                   }
  98.                   else
  99.                      break;
  100.                      
  101.                 }
  102.                
  103.               }
  104.               
  105.             }
  106.             
  107.            else                           
  108.             myservo.write(90);        
  109.           }
  110.          
  111.         }
  112.       
  113.   }
  114. }
  115. //中断函数,唤醒
  116. void wakeup()
  117. {
  118.   PM.wakeFromDoze();
  119.   time1=millis();
  120. }
  121. //使数据记录整齐的补零函数
  122. void print2digits(int number) {
  123.   if (number >= 0 && number < 10) {
  124.     myFile.print('0');
  125.   }
  126.   myFile.print(number);
  127. }
复制代码

                     【CurieNano试用】 智能锁图4【CurieNano试用】 智能锁图3



[media=x,500,375][/media]视频不知道为什么不能看,等我再上传一次吧。{:5_151:}








T{9IYVWZGLW4(ZZKC7E$VJY.png (53.31 KB, 下载次数: 7031)

智能锁项目 · CurieNano控制板_image_3.webp

创作许可协议

本项目采用 None(不开放任何权利,保留所有权利) 进行许可。

评论(3)
吹口琴的钢铁侠
吹口琴的钢铁侠
最好自己折腾个安卓app出来,不然还是很不方便阿
hnyzcj
hnyzcj
你的秀发好飘逸
durobot
durobot
作者
为什么我写的Power,P变成了笑脸,改不回来
durobot
durobot
作者
回复luna
好的,谢谢
luna
luna回复durobot

已经帮改掉了,需要勾选一下“禁用表情”(被logo当掉的部分)

- 没有更多了 -