2021年1月26日火曜日

micro:bitのBluetoothアドバタイズを使う

micro:bitで音を検出して通知するプログラム

 BBCのmicro:bit v2を入手したので、ちょっとためしてみた。作成したのは、Bluetoothをつかっで通知を送るもの。micro:bit v2にあるマイクで音を拾い、閾値以上であれば、「Bluetooth UIDをアドバタイズ」を実行する。これは、実は、EddyStone Beaconになっている。Windows10には、EddyStone Beaconを受信する機能がないので、別途作成した。「Windows10でEddyStone Beaconを受信する その1」を参照してほしい。

 List01が、上記のBeaconを送信するmicro:bit v2のプログラムのJavaScriptソースである。

List01
  1. input.onButtonPressed(Button.A, function () {
  2. 閾値 += 8
  3. if (閾値 >= 255) {
  4. 閾値 = 255
  5. }
  6. input.setSoundThreshold(SoundThreshold.Loud, 閾値)
  7. bluetooth.advertiseUid(
  8. name_space,
  9. 閾値,
  10. 7,
  11. false
  12. )
  13. basic.showArrow(ArrowNames.North)
  14. basic.showNumber(Math.round(Math.map(閾値, 0, 255, 0, 9)))
  15. basic.pause(BeaconCycle)
  16. basic.clearScreen()
  17. bluetooth.stopAdvertising()
  18. })
  19. input.onLogoEvent(TouchButtonEvent.Pressed, function () {
  20. 閾値 = 128
  21. input.setSoundThreshold(SoundThreshold.Loud, 閾値)
  22. bluetooth.advertiseUid(
  23. name_space,
  24. 閾値,
  25. 7,
  26. false
  27. )
  28. basic.showArrow(ArrowNames.East)
  29. basic.showNumber(Math.round(Math.map(閾値, 0, 255, 0, 9)))
  30. basic.pause(BeaconCycle)
  31. basic.clearScreen()
  32. bluetooth.stopAdvertising()
  33. })
  34. input.onSound(DetectedSound.Loud, function () {
  35. = input.soundLevel()
  36. Event_Time = input.runningTime()
  37. basic.showNumber(Math.round(Math.map(音, 0, 255, 0, 9)))
  38. })
  39. input.onButtonPressed(Button.B, function () {
  40. 閾値 += -8
  41. if (閾値 <= 0) {
  42. 閾値 = 0
  43. }
  44. input.setSoundThreshold(SoundThreshold.Loud, 閾値)
  45. bluetooth.advertiseUid(
  46. name_space,
  47. 閾値,
  48. 7,
  49. false
  50. )
  51. basic.showArrow(ArrowNames.South)
  52. basic.showNumber(Math.round(Math.map(閾値, 0, 255, 0, 9)))
  53. basic.pause(BeaconCycle)
  54. basic.clearScreen()
  55. bluetooth.stopAdvertising()
  56. })
  57. let = 0
  58. let Event_Time = 0
  59. let BeaconCycle = 0
  60. let name_space = 0
  61. let 閾値 = 0
  62. bluetooth.setTransmitPower(7)
  63. 閾値 = 128
  64. let Instance = 0
  65. name_space = 2155971203
  66. BeaconCycle = 2000
  67. Event_Time = input.runningTime()
  68. input.setSoundThreshold(SoundThreshold.Loud, 閾値)
  69. basic.showIcon(IconNames.Yes)
  70. bluetooth.advertiseUid(
  71. name_space,
  72. Event_Time * 65536 + 65535,
  73. 7,
  74. false
  75. )
  76. basic.pause(BeaconCycle)
  77. bluetooth.stopAdvertising()
  78. basic.forever(function () {
  79. if (音 > 0) {
  80. bluetooth.advertiseUid(
  81. name_space,
  82. Event_Time * 65536 + 音,
  83. 7,
  84. false
  85. )
  86. if (Event_Time + BeaconCycle < input.runningTime()) {
  87. = 0
  88. bluetooth.stopAdvertising()
  89. basic.clearScreen()
  90. }
  91. }
  92. })

 上記プログラムをmicro:bit v2で動作させると、音が鳴ったときに、Bluetooth UIDをアドバタイズが実行されるが、これは、EddyStoneというBeaconそのものだ。micro:bitの左右のボタンで音の閾値を上下でき、中央上のタッチボタンで閾値をデフォルト値(128)に戻す。音を検出したり、閾値を変更したときには、LEDで表示を行うようになっているほか、閾値の変更もBluetooth UIDアドバタイズで通知するようになっている。

0 件のコメント:

コメントを投稿