【ラズパイ】pythonからscratchへデータ送信方法(pythonからの送信した情報をscratchの変数に代入)
スポンサーリンク

前のエントリで、pythonからメッセージをscratchへ送る方法を記載しましたが、今回は、受け取った値をscratchの変数に入れる方法です。
こここでは py-scratch モジュールを使います。(このモジュールを入れなくもできるんだろうけど方法が見つからず。。。)
py-scratchのインストール
http://code.google.com/p/py-scratch/downloads/listから圧縮ファイルをダウンロードして解凍後、解凍されたフォルダに移動し以下を実行
python setup.py install
pythonスクリプト作成
test.py
import scratch
import time
s = scratch.Scratch()
# to make a broadcast to scratch
s.broadcast("from python")
# to receive an update from scratch
message = s.receive()
# blocks until an update is received
# message returned as {'broadcast': [], 'sensor-update': {'scratchvar': '64'}}
# or {'broadcast': ['from scratch'], 'sensor-update': {}}
# where scratchvar is the name of a variable in scratch
# and 'from scratch' is the name of a scratch broadcast
# send sensor updates to scratch
data = {}
data['pyvar'] = 123
for data['pycounter'] in range(60):
time.sleep(5) #5秒毎にdata['pyvar']の情報をscratchへ送信
s.sensorupdate(data)
