![見出し画像](https://assets.st-note.com/production/uploads/images/54066432/rectangle_large_type_2_a2796da2f0f0fea54f8e7ad3c16fb332.jpg?width=1200)
Petoi Bittle 入門 (18) - RasPiによるBittleの制御
「RasPi」からのシリアル通信で「Bittle」を制御する方法をまとめました。
・Raspberry Pi 3 Model A+
・Raspbian Buster
前回
1. PCのPythonからのシリアル通信
「RasPi」からのシリアル通信を試す前に、PCからのシリアル通信を試します。
(1) お腹のバッテリーをONした後、アップローダーでPCとBittleを繋ぐ。
(2) Pythonの仮想環境でpyserialをインストール。
$ pip install pyserial
(2) hello.pyの作成。
「デバイス名」は「Arduino IDE」のシリアルポートで確認してください。
import serial
import time
# シリアル通信のオープン
ser = serial.Serial(
port='/dev/cu.usbserial-14320',# デバイス名
baudrate=115200, # ポート番号
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
time.sleep(2)
# スキルの実行
ser.write(str.encode('khi\n'))
print('done!!!')
(3) hello.pyの実行
成功すると、挨拶のスキルを発動します。
$ python hello.py
2. RasPiのPythonからのシリアル通信
(1) お腹のバッテリーをONした後、アップローダーでRasPiとBittleを繋ぐ。
(2) pyserialをインストール。
$ pip3 install pyserial
(2) hello.pyの作成。
「デバイス名」は「ls -la /dev/ttyUSB*」で確認してください。USBポートが1つの場合は、「/dev/ttyUSB0」になります。RasPiなので、行頭の「# -*- coding: utf-8 -*-」も必要です。
# -*- coding: utf-8 -*-
import serial
import time
# シリアル通信のオープン
ser = serial.Serial(
port='/dev/ttyUSB0',# デバイス名
baudrate=115200, # ポート番号
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
time.sleep(2)
# スキルの実行
ser.write(str.encode('khi\n'))
print('done!!!')
(3) hello.pyの実行
成功すると、挨拶のスキルを発動します。
$ python3 hello.py
3. BittleのバッテリーからのRasPiへの電源供給
「Bittle」のバッテリーからの「RasPi」への電源供給するには、「NyBoard」に「2x5ソケット」をハンダ付けして、「RasPi」を差し込みます。「Raspberry Pi 3 Model A+」は「Bittle」のサイズに最適です。ソケットをハンダ付けした後は、裏蓋は取り付けできなくなります。
「RasPi」を取り付けると↓のようになります。
ボードのスペーサーと短いmicroUSBを用意したほうが良さそうです。