MicroPython Advent Calender 18日目です。
今日はESP32+micropythonでBLEを試してみました。
ESP8266とESP32の大きな違いはBLE機能が搭載していることです。
micropythonでも動かしてみたいものですが、残念ながら公式で配布されている
ファームはBLEに対応していません。
今月号のInterfaceでBLEを動作させる記事が書かれていますがビルドが必要となり
そこそこ大変です。
気軽にできないかと模索してみてPycom(Wipy)のファームを書き込んだESP32-devkit
でBLEの動作が確認できました。
Pycom(Wipy)のファームの書き込み方法は14日目の記事をご確認ください。
ファームを書いたらPycomのドキュメントにあったsampleを動かしてみました。
from network import Bluetooth
bluetooth = Bluetooth()
bluetooth.set_advertisement(name='ESP32', service_uuid=b'1234567890123456')
def conn_cb (bt_o):
events = bt_o.events() # this method returns the flags and clears the internal registry
if events & Bluetooth.CLIENT_CONNECTED:
print("Client connected")
elif events & Bluetooth.CLIENT_DISCONNECTED:
print("Client disconnected")
def main():
bluetooth.callback(trigger=Bluetooth.CLIENT_CONNECTED | Bluetooth.CLIENT_DISCONNECTED, handler=conn_cb)
bluetooth.advertise(True)
このコードを転送して実行すると、、、
iphoneでESP32が出てきました。
![IMG_0801.PNG](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.amazonaws.com%2F0%2F146663%2F7e3e6852-94ac-09c9-e6d8-c2d82b955dba.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=58b3df82708eed35fae78134355ddb81)
ESP32をタッチすると接続しに行くので、CLIENT_CONNECTEDが検出されました。
画面を戻るとCLIENT_DISCONNECTEDになりました。
非常に簡単な動作確認ではありますが、Pycom(Wipy)のファームであればBLEが
動作するようです!
次は文字のやりとりに挑戦です。