2015年3月30日月曜日

Raspberry piのGPIOを動かしてみる

Raspberry piのGPIOを動かしてみる

Python用のGPIOライブラリを更新

# sudo python
# import RPi.GPIO as GPIO
# GPIO.VERSION
 # sudo apt-get update
 # sudo apt-get upgrade
 # sudo rpi-update

enter image description here
from Meltwater’s Raspberry Pi Hardware

2015年3月29日日曜日

RaspberryPIでgitを使ってみる。

RaspberryPIでgitを使ってみる。

1.ユーザー名、メールアドレスの設定

 $ git config --global user.name "user"  
 $ git config --global user.email mail.com

2.SSHの鍵の設定

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/pi/.ssh/id_rsa):← Enter
Created directory '/home/pi/.ssh'. ← ~/.ssh がない場合、ディレクトリを作成します
Enter passphrase (empty for no passphrase): ← パスフレーズを入力します
Enter same passphrase again: ← もう一度パスフレーズを入力します
Your identification has been saved in /home/pi/.ssh/id_rsa.
Your public key has been saved in /home/pi/.ssh/id_rsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:pi@raspberrypi
The key's randomart image is:

「./ssh/id_rsa.pub」の内容を
githubのpersonal settingの「SSH Public Keys」に登録する

3.SSHで接続してみる。

$ ssh -l git -i ~/.ssh/github_id_rsa github.com
:
:
Enter passphrase for key '/home/pi/.ssh/id_rsa : さっきのパスフレーズ
:
Connection to github.com closed.
$ vi ~/.ssh/config
Host github.com
 HostName      github.com
 IdentityFile  ~/.ssh/github_id_rsa
 User          git
$ ssh github.com

4.リポジトリからクローンする。
 すでにホームページでリポジトリは作ってあったので、クローンをしてファイルをダウンロードする。

$ git clone リポジトリのurl 

githubと同じフォルダができ、ファイルがダウンロードされる。別のフォルダ(AAA)にダウンロードしたい場合は以下とする。

$ git clone リポジトリのurl AAA

5.新しいファイルをリポジトリに追加

$ git add filename
$ git add directoryname

6.変更したファイルをローカルリポジトリにコミット

$ git commit -m 'something changed'

$ git commit -am “-aオプションを付けるとaddとcommitを一緒にできる”

7.ローカルリポジトリの変更をリモートリポジトリに反映

$ git push
Username for 'https://github.com' : xxx
Password for 'https://xxxxx : yyy

まだよくgitの使い方を理解できていないですが、変更内容をgithubに登録できました。

参考
Ubuntuでgithubを使う
ターミナルからgithubのリポジトリにアクセスする設定(sshの設定)
いまさら聞けないgitの使い方
githubに既にあるプロジェクトからcloneして編集してpushする手順

Raspberry Piでシリアル通信2

Raspberry Piでシリアル通信2

シリアル通信での送信ができるようになったので、受信の実験をしてみました。
(Python 2.7.3)

#! /usr/bin/python
import serial
import time
try:
    port = "/dev/rfcomm0"
    baudrate = 115200
    ser = serial.Serial(port, baudrate)
except  serial.SerialException as e:
    print("could not open serial port '{}':{}".format(port,e))

while 1:
    time.sleep(1)
    if ser.inWriting() > 0:
        print(ser.inWaiting())
        for i in range(ser.inWaiting()):
            print(i,ser.read(1))    
ser.close()

スマホからBluetooth Terminalで”あ”と送ると
3
(0, ‘\xe3’)
(1, ‘\x81’)
(2, ‘\x82’)
と表示された。改行コードなしでUTF-8で送られていることが分かりました。

だからreadline()でリードできないと思い、以下のように修正したら”あ”と表示されました。

while 1:
    time.sleep(1)
    if ser.inWriting() > 0:
        print ser.read(ser.inWaiting())
#       for i in range(ser.inWaiting()):
#       print(i,ser.read(1))    
ser.close()

2015年3月28日土曜日

Raspberry PiでBluetoothを使う

Raspberry PiでBluetoothを使う

Raspberry PiにBluetoothドングルをつけて接続しました。

~ペアリング~

1.Bluetoothのパッケージの追加

$ sudo apt-get install bluetooth bluez-utils blueman

2.bluetooth-agentを自動起動する。

$sudo vim /etc/init.d/bluetooth-agent
$sudo chmod 755 /etc/init.d/bluetooth-agent
$sudo update-rc.d bluetooth-agent defaults

bluetooth-agentに以下を記載。

### BEGIN INIT INFO
# Provides: bluetooth-agent
# Required-Start: $remote_fs $syslog bluetooth pulseaudio
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Makes Bluetooth discoverable and connectable to 0000
# Description: Start Bluetooth-Agent at boot time.
### END INIT INFO
#! /bin/sh
# /etc/init.d/bluetooth-agent
USER=root
HOME=/root
export USER HOME
case "$1" in
        start)
                #echo "setting bluetooth discoverable"
                #sudo hciconfig hci0 piscan
                sudo hciconfig hci0 pscan
                start-stop-daemon -S -x /usr/bin/bluetooth-agent -b -- 0000
                echo "bluetooth-agent startet pw: 0000"
                ;;
        stop)
                echo "Stopping bluetooth-agent"
                start-stop-daemon -K -x /usr/bin/bluetooth-agent
                ;;
        *)
                echo "Usage: /etc/init.d/bluetooth-agent {start|stop}"
                exit 1
                ;;
esac
exit 0

3.PINコードではなくYes/Noで接続できるようにする。

$ sudo perl -i -pe 's/KeyboardDisplay/DisplayYesNo/' /usr/bin/bluez-simple-agent

4.スマホ(Xperia)で設定→Bluetoothで”周辺のすべてのBluetooth危機で検出可能”の状態にする。(ここではまりました。)

5.以下でRaspberry Piをスキャン状態にする

$ hcitool scan
  xx:xx:xx:xx:xx:xx   Xperia AX

6.接続

$ sudo bluez-simple-agent hci0 xx:xx:xx:xx:xx:xx

RequestConfirmation
Confirm passkey (yes/No):yes

ここでスマホ側にペアリングするかのメニューがでてくるのでペアリングする。

Creating device failed: org.bluez.Error.AlreadyExists: AlreadyExistsというエラーがでた場合は、以下で1回消す。

$ sudo bluez-test-device remove XX:XX:XX:XX:XX:XX

ペアリングできると以下がでる。
New device・・・

5.自動接続
次回から自動接続する場合は以下となります。

$ sudo bluez-test-device trusted XX:XX:XX:XX:XX:XX yes
$ sudo bluez-test-input connect XX:XX:XX:XX:XX:XX

参考
Raspberry PiでBluetoothを扱う
Raspberry PiからBluetoohを使ってiPhoneでテザリング

~シリアル通信してみる~

1.SDP(Service Directory Protocol)にSPP(Serial Port Profile)を追加する。

まず登録されているサービスを確認する。

$sdptool browse local

Service NameにSerial Portがないので、以下でSPPを登録する。

$sdptool add --channel=22 SP
Serial Port service registered

もう一回以下で確認。

$sdptool browse local

Service Name: Serial Port
Service Description: COM Port
が追加されている。

2.RFCOMMサーバーを起動

$sudo rfcomm listen /dev/rfcomm0 22

Waiting for connection on channel 22とでて待機状態になる。

3.Android側からBluetooth Terminalで接続
接続されるとRaspberry Pi側で、以下が表示され、/dev/rfcomm0が作成される。

Connection from XX:XX:XX:XX:XX:XX to /dev/rfcomm0
Press CTRL-C for hangup

受信データの確認

$ cat /dev/rfcomm0

送信

$ echo "hello" > /dev/rfcomm0

以下のように修正すると自動化できると書いてあったが、逆にうまくいっていない。。

sudo vim /etc/bluetooth/rfcomm.conf
rfcomm0 {
        # Automatically bind the device at startup
        bind yes;

        # Bluetooth address of the device
        device XX:XX:XX:XX:XX:XX;

        # RFCOMM channel for the connection
        channel 22;

        # Description of the connection
        comment "a comment";
}

起動後自動でSerial Portができるようにする

rc.localに以下を修正することで改善

sudo vim /etc/rc.local
sdptool add --channel=22 SP
hciconfig hci0 piscan
hciconfig hci0 namepi’
rfcomm listen /dev/rfcomm0 22 &
exit 0
$sudo chmod +x  /etc/rc.local

参考
Linux PCにUSB Bluetoothを付けてNexus7と通信
[GPSRCC] RaspberryPiでBluetooth QZPOD編
Raspberry Pi をA2DPのsinkにして携帯やタブレットから音楽を再生する
Bluetooth/rfcomm
Camp Hack Day 2014 喋るテントTipee ソース

2015年3月25日水曜日

パソコンとRaspberryPIがVNCで繋がらなくなった

パソコンとRaspberryPIがVNCで繋がらなくなった

パソコンにUltraVNC Viewerを入れてRaspberry PI側にtightVNCserverを入れてVNC接続ができていたのですが、急にできなくなりました。

調べてみるとWifi AdapterのIPv4アドレスが192.xx.xx.19となっていた。
19個も接続するものもないのに。。。

Wifi Adapterを電源を入れなおしたら
IPv4アドレスが192.xx.xx.4となり、無事VNC接続できるようになりました。

2015年3月22日日曜日

MinecraftのデータはDropboxで管理し、複数のパソコンでやりたい場合

MinecraftのデータはDropboxで管理し、複数のパソコンでやりたい場合

MInecraftをWindowsとUbuntuでそれぞれで実行したいので、Dropboxでデータを共有しているのですが、Edit Profileでそれぞれのパソコン用にProfileを用意した。しかし、それぞれのパソコンで実行するとGameDirectoryの名前が勝手に変わってしまいます。

例えばWindows側で実行してからUbuntuで実行するとGameDirectoryに
C/がついてしまう。。。

どうもlancher_profile.jsonが勝手に上書きされてしまうらしい。
それならlancher_profile.jsonだけを共有しなければいいと思い、以下のようにlauncher_profiles.json以外
は、mklinkで以下のようにしました。今のところはうまくいっているようです。

enter image description here

Google+フォトとの自動同期

Google+フォトとの自動同期 Bloggerに画像を載せるときには毎回Google+にアップロードしていたのですが、調べているとGoogle+ Auto Backupを使うと自動で同期をとってくれることが分かりました。
Picasaのソフトの一部だそうです。
とりあえずインストールしてみようと思います。