电子门铃-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3050|回复: 0

电子门铃

[复制链接]
发表于 2020-8-30 14:20 | 显示全部楼层 |阅读模式
本帖最后由 vany5921 于 2020-8-30 14:24 编辑

一个利用BLE通讯的门铃应用。
M5Atom Lite/Matrix发送BLE广播,在M5Atom echo中检测到相应的广播包后,播放声音。
m5atom_lite_bcast.ino在按下按钮后在BLE上发送广播
m5atom_echo_chime进行扫描,当检测到匹配设备名的广播包时,从扬声器发出铃音。

声音文件 chime.h.zip (57.59 KB, 下载次数: 0)
  1. #include <M5Atom.h>
  2. #include <BLEDevice.h>
  3. #include <BLEUtils.h>
  4. #include <BLEScan.h>
  5. #include <BLEAdvertisedDevice.h>
  6. #include <driver/i2s.h>
  7. #include "chime.h"  //wav file

  8. /* LED */
  9. #define LED_COLOR_INACTIVE  0xF00000
  10. #define LED_COLOR_ACTIVE    0x00F000

  11. /* BLE */
  12. BLEScan* pBLEScan;
  13. #define TARGET_DEVNAME  "M5Atom-chime"
  14. #define SCAN_TIME 1 //In seconds
  15. #define DELAY_AFTERCHIME 2000 //In miliseconds

  16. /* I2S */
  17. #define CONFIG_I2S_BCK_PIN      19
  18. #define CONFIG_I2S_LRCK_PIN     33
  19. #define CONFIG_I2S_DATA_PIN     22
  20. #define CONFIG_I2S_DATA_IN_PIN  23
  21. #define SPEAKER_I2S_NUMBER      I2S_NUM_0
  22. #define MODE_MIC                0
  23. #define MODE_SPK                1

  24. bool scanChimeDevice()
  25. {
  26.   bool found = false;
  27.   int devcount;

  28.   BLEScanResults foundDevices = pBLEScan->start(SCAN_TIME, false);
  29.   devcount = foundDevices.getCount();
  30.   for(int i=0; i<devcount; i++)
  31.   {
  32.       BLEAdvertisedDevice d = foundDevices.getDevice(i);
  33.       if(d.haveName())
  34.       {
  35.         if(d.getName() == TARGET_DEVNAME)
  36.         {
  37.           found = true;
  38.           break;
  39.         }
  40.       }
  41.   }

  42.   // delete results fromBLEScan buffer to release memory
  43.   pBLEScan->clearResults();

  44.   return found;
  45. }

  46. void InitI2SSpeakerOrMic(int mode)
  47. {
  48.   esp_err_t err = ESP_OK;

  49.   i2s_driver_uninstall(SPEAKER_I2S_NUMBER);
  50.   i2s_config_t i2s_config = {
  51.     .mode                 = (i2s_mode_t)(I2S_MODE_MASTER),
  52.     .sample_rate          = 16000,
  53.     .bits_per_sample      = I2S_BITS_PER_SAMPLE_16BIT,
  54.     .channel_format       = I2S_CHANNEL_FMT_ALL_RIGHT,
  55.     .communication_format = I2S_COMM_FORMAT_I2S,
  56.     .intr_alloc_flags     = ESP_INTR_FLAG_LEVEL1,
  57.     .dma_buf_count        = 6,
  58.     .dma_buf_len          = 60,
  59.     .use_apll             = false,
  60.     .tx_desc_auto_clear   = true,
  61.     .fixed_mclk           = 0
  62.   };
  63.   if (mode == MODE_MIC)
  64.   {
  65.     i2s_config.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM);
  66.   }
  67.   else
  68.   {
  69.     i2s_config.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX);
  70.   }

  71.   err += i2s_driver_install(SPEAKER_I2S_NUMBER, &i2s_config, 0, NULL);

  72.   i2s_pin_config_t tx_pin_config = {
  73.     .bck_io_num           = CONFIG_I2S_BCK_PIN,
  74.     .ws_io_num            = CONFIG_I2S_LRCK_PIN,
  75.     .data_out_num         = CONFIG_I2S_DATA_PIN,
  76.     .data_in_num          = CONFIG_I2S_DATA_IN_PIN,
  77.   };
  78.   err += i2s_set_pin(SPEAKER_I2S_NUMBER, &tx_pin_config);

  79.   if (mode != MODE_MIC) {
  80.     err += i2s_set_clk(SPEAKER_I2S_NUMBER, 16000, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO);
  81.   }

  82.   i2s_zero_dma_buffer(SPEAKER_I2S_NUMBER);
  83. }

  84. void setup() {
  85.   M5.begin(true, false, true);
  86.   delay(50);
  87.   M5.dis.drawpix(0,LED_COLOR_INACTIVE);

  88.   //Speaker,Mic Initialize(Mic Mode)
  89.   InitI2SSpeakerOrMic(MODE_MIC);

  90.   //Create BLE Device
  91.   BLEDevice::init("");

  92.   //BLE Scan Initialize
  93.   pBLEScan = BLEDevice::getScan();
  94.   pBLEScan->setActiveScan(false); //Passive Scan
  95.   
  96.   delay(100);
  97. }

  98. void loop() {
  99.   if(scanChimeDevice())
  100.   {
  101.     size_t bytes_written;

  102.     M5.dis.drawpix(0,LED_COLOR_ACTIVE);
  103.    
  104.     //Set Speaker Mode
  105.     InitI2SSpeakerOrMic(MODE_SPK);  
  106.     //Write Speaker
  107.     i2s_write(SPEAKER_I2S_NUMBER, chime_wav, sizeof(chime_wav), &bytes_written, portMAX_DELAY);
  108.     i2s_zero_dma_buffer(SPEAKER_I2S_NUMBER);
  109.     //Set Mic Mode
  110.     InitI2SSpeakerOrMic(MODE_MIC);
  111.    
  112.     delay(DELAY_AFTERCHIME);
  113.     M5.dis.drawpix(0,LED_COLOR_INACTIVE);
  114.   }

  115.   delay(100);
  116.   M5.update();
  117. }
复制代码
  1. #include <M5Atom.h>
  2. #include <BLEDevice.h>
  3. #include <BLEUtils.h>

  4. /* LED */
  5. #define LED_COLOR_INACTIVE  0xF00000
  6. #define LED_COLOR_ACTIVE    0x00F000

  7. /* BLE */
  8. BLEAdvertising *pAdv;
  9. #define DEVICE_NAME "M5Atom-chime"
  10. #define ADVERTISING_TIME 3000 //In miliseconds

  11. void setAdvertisementData(BLEAdvertising *pAdvertising) {
  12.   std::string strData = "";
  13.   BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();

  14.   //Device Name
  15.   oAdvertisementData.setName(DEVICE_NAME);

  16.   //Flags
  17.   oAdvertisementData.setFlags(0x01|0x04); // LE Limited Discoverable Mode | BR_EDR_NOT_SUPPORTED

  18.   //Data
  19.   strData += (char)0xff;                      //Type :Manufacture Specific
  20.   strData += (char)0xff;                      //Company ID(L)
  21.   strData += (char)0xff;                      //Company ID(H) :for Test(0xffff)
  22.   strData += (char)0xff;                      //dmy Data
  23.   strData = (char)strData.length() + strData; //Length
  24.   oAdvertisementData.addData(strData);
  25.   pAdvertising->setAdvertisementData(oAdvertisementData);

  26.   //AdvertisementType
  27.   pAdvertising->setAdvertisementType(ADV_TYPE_NONCONN_IND);
  28. }

  29. void setup() {
  30.   M5.begin(true, false, true);
  31.   delay(50);
  32.   M5.dis.drawpix(0,LED_COLOR_INACTIVE);
  33.   
  34.    //Create BLE Device
  35.   BLEDevice::init("");
  36.    
  37.   //Set AdvertisementData
  38.   pAdv = BLEDevice::getAdvertising();
  39.   setAdvertisementData(pAdv);
  40.   
  41.   delay(100);
  42. }

  43. void loop() {
  44.   if (M5.Btn.isPressed())
  45.   {
  46.     M5.dis.drawpix(0,LED_COLOR_ACTIVE);
  47.     pAdv->start();  //Start advertising
  48.     delay(ADVERTISING_TIME);
  49.     pAdv->stop();   //Stop advertising
  50.     M5.dis.drawpix(0,LED_COLOR_INACTIVE);
  51.   }
  52.   
  53.   delay(100);
  54.   M5.update();
  55. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 02:37 , Processed in 0.107494 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表