Pybleno + iPhone(LightBlue Explorer)でLチカ(Raspberry Pi Zero W)
- 2018/02/28
- 23:24
Pybleno + iPhone(LightBlue Explorer)でLチカ(Raspberry Pi Zero W)
RoboTakaoです。ようこそ「極力ローコスト ロボット製作 ブログ」にお越しくださいました。
前回はpython版のbleno(BLEモジュール)であるpyblenoをテストしてみました。今回はとりあえずこれでLチカしたいと思います。
前回の投稿はこちらを参照してください。

構成
Raspberry Pi Zero W
iPhone SE + LightBlue Explorer (次のステップで自作アプリ予定)
ブレッドボードとLED
結線


pythonのコード
pyblenoのサンプルコードのechoを改造しました。
まずはEchoCharacteristic.pyとmain.pyを合体させて、GPIO部分を追加
from pybleno import *
import sys
import signal
import array
import struct
import traceback
from builtins import str
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.OUT) #GPIO4
print('bleno - echo');
bleno = Bleno()
class EchoCharacteristic(Characteristic):
def __init__(self, uuid):
Characteristic.__init__(self, {
'uuid': uuid,
'properties': ['read', 'write', 'notify'],
'value': None
})
self._value = array.array('B', [0] * 0)
self._updateValueCallback = None
def onReadRequest(self, offset, callback):
print('EchoCharacteristic - %s - onReadRequest: value = %s' % (self['uuid'], [hex(c) for c in self._value]))
callback(Characteristic.RESULT_SUCCESS, self._value)
def onWriteRequest(self, data, offset, withoutResponse, callback):
self._value = data
print('EchoCharacteristic - %s - onWriteRequest: value = %s' % (self['uuid'], [hex(c) for c in self._value]))
if (self._value == b'\x01'):
GPIO.output(4, True) # 0x01が入力されたらLEDを点灯
print('True')
else:
GPIO.output(4, False) # 0x01以外ならLEDを消灯
print('False')
if self._updateValueCallback:
print('EchoCharacteristic - onWriteRequest: notifying');
self._updateValueCallback(self._value)
callback(Characteristic.RESULT_SUCCESS)
def onSubscribe(self, maxValueSize, updateValueCallback):
print('EchoCharacteristic - onSubscribe')
self._updateValueCallback = updateValueCallback
def onUnsubscribe(self):
print('EchoCharacteristic - onUnsubscribe');
self._updateValueCallback = None
def onStateChange(state):
print('on -> stateChange: ' + state);
if (state == 'poweredOn'):
bleno.startAdvertising('echo', ['ff00'])
else:
bleno.stopAdvertising();
bleno.on('stateChange', onStateChange)
def onAdvertisingStart(error):
print('on -> advertisingStart: ' + ('error ' + error if error else 'success'));
if not error:
bleno.setServices([
BlenoPrimaryService({
'uuid': 'fff0',
'characteristics': [
EchoCharacteristic('3012')
]
})
])
bleno.on('advertisingStart', onAdvertisingStart)
GPIO.output(4, False)
print ('Hitto disconnect')
if (sys.version_info > (3, 0)):
input()
else:
raw_input()
bleno.stopAdvertising()
bleno.disconnect()
GPIO.cleanup()
print ('terminated.')
sys.exit(1)
動作テスト
iPhoneにインストールしたLightBlue Explorerでraspberrypiに接続後
UUID 3012の値に0x01を入力→LED点灯
UUID 3012の値に0x01を意外を入力→LED消灯
うまいこと動きました。
さて現在iPhoneアプリを作るためにCore Bluetoothを研究中
それでは今回はこの辺で失礼します!ありがとうございます。
スポンサードリンク