スマホの写真をディジタルフォトフレームに表示する


デジタルフォトフレームとRaspberryを組み合わせることで、Bluetooth対応のディジタルフォトフレームを作ることができます。
こ ちらにBluetooth経由で、スマホから写真を受け取る方法が公開されています。
スマホの共有機能を使って、Bluetooth経由でRaspberry側に写真を転送することができます。
あとは、こちらの方法で、Raspberry側に保存している写真を、定期的にUSB経 由で ディジタルフォトフレームに転送してやるだけです。

RaspberryでBluetooth経由で、スマホから写真を受け取る手順を簡単に紹介します。
Raspberry以外にUSBのBluetoothドングルが必要になります。

bluzのインストール

$ sudo apt-get update

$ sudo apt-get install bluez

定義ファイルを変更

こ ちらに説明が有りますが、デフォルトではSDP(Bluetoothプロトコルの一つ)が無効になっているので、SDPを有効化し、
さらにSAP(Sim Access Profile)を無効にします。
$ sudo service bluetooth stop

$ sudo vi /etc/systemd/system/dbus-org.bluez.service

ExecStart=/usr/lib/bluetooth/bluetoothd

ExecStart=/usr/lib/bluetooth/bluetoothd --compat --noplugin=sap

$ sudo systemctl daemon-reload

bluetoothサービスを開始

「Failed to obtain handles for "Service Changed" characteristic」が出ますが、気にしなくてもいいみたいです。
$ sudo service bluetooth start

$ sudo service bluetooth status
● bluetooth.service - Bluetooth service
   Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2020-11-15 17:59:11 JST; 8s ago
     Docs: man:bluetoothd(8)
 Main PID: 1334 (bluetoothd)
   Status: "Running"
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/bluetooth.service
           mq1334 /usr/lib/bluetooth/bluetoothd --compat --noplugin=sap

Nov 15 17:59:11 raspberrypi systemd[1]: Starting Bluetooth service...
Nov 15 17:59:11 raspberrypi bluetoothd[1334]: Bluetooth daemon 5.43
Nov 15 17:59:11 raspberrypi systemd[1]: Started Bluetooth service.
Nov 15 17:59:11 raspberrypi bluetoothd[1334]: Starting SDP server
Nov 15 17:59:11 raspberrypi bluetoothd[1334]: Excluding (cli) sap
Nov 15 17:59:11 raspberrypi bluetoothd[1334]: Bluetooth management interface 1.14 initialized
Nov 15 17:59:11 raspberrypi bluetoothd[1334]: Failed to obtain handles for "Service Changed" characteristic

$ sudo hciconfig -a hci0
hci0:   Type: Primary  Bus: USB
        BD Address: 00:1B:DC:03:D3:E5  ACL MTU: 310:10  SCO MTU: 64:8
        UP RUNNING
        RX bytes:1100 acl:0 sco:0 events:55 errors:0
        TX bytes:1916 acl:0 sco:0 commands:52 errors:0
        Features: 0xff 0xff 0x8f 0xfe 0x9b 0xff 0x59 0x83
        Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3
        Link policy: RSWITCH HOLD SNIFF PARK
        Link mode: SLAVE ACCEPT
        Name: 'raspberrypi' -----> これがデバイス名
        Class: 0x000000
        Service Classes: Unspecified
        Device Class: Miscellaneous,
        HCI Version: 2.1 (0x4)  Revision: 0x149c
        LMP Version: 2.1 (0x4)  Subversion: 0x149c
        Manufacturer: Cambridge Silicon Radio (10)

Bluetoothデバイス名の変更

デフォルトのデバイス名はraspberrypiとなっています。
デバイス名を変えたいときは以下のコマンドで変更します。
$ sudo hciconfig hci0 name 'hoge'

デバイス名を検出可能に設定

以下のコマンドを実行すると、デバイス名がスマホから検出可能になります。
スマホでbluetoothデバイスを検索するとraspberrypiが現れるのでペアリングを確立します。
一度ペアリングを確立すると、スマホ側でペアリングを解除しない限り、同じペアリングが使われます。
raspberryを再起動すると、ペアリングが可能なったタイミングで常に同じペアリングが実行されます。
$ sudo hciconfig -a hci0 | grep -i 'PSCAN *ISCAN'

$ sudo hciconfig hci0 piscan

$ sudo hciconfig -a hci0 | grep -i 'PSCAN *ISCAN'
        UP RUNNING PSCAN ISCAN

obexpushdのインストールと起動

これが今回の最大の肝です。
obexpushdはBluetooth 又は IRDA 経由でのファイル受信プログラムです。
obexpushdについてはこ ちらにも解説が有ります。
以下のコマンドを実行するとBluetooth経由で受信したファイルを「/home/pi/photos_from_phone」に保存しま す。
$ sudo apt-get install obexpushd

$ mkdir /home/pi/photos_from_phone

$ sudo obexpushd -B -n -o '/home/pi/photos_from_phone'
obexpushd 0.11.2 Copyright (C) 2006-2010 Hendrik Sattler
This software comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
Listening on bluetooth/[00:00:00:00:00:00]:9

スマホ側の操作

スマホ側で写真を選んで
共有→Bluetooth→raspberrypi
で、raspberryの「/home/pi/photos_from_phone」に写真が転送されます。

Raspberry側のobexpushdを起動したコンソールに以下の様に表示されれば転送成功です。
$ sudo obexpushd -B -n -o '/home/pi/photos_from_phone'
obexpushd 0.11.2 Copyright (C) 2006-2010 Hendrik Sattler
This software comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
Listening on bluetooth/[00:00:00:00:00:00]:9
Creating file "/home/pi/photos_from_phone/C3CE8B78-_1.jpg"
Creating file "/home/pi/photos_from_phone/8F5D1ADB-.jpg"

obexpushdのサービス登録

obexpushdの起動オプションはこ ちらに有りますが、デーモン化の起動オプションは無いみたいです。
こ ちらにobexpushdのサービス登録手順が公開されています。
「/etc/systemd/system /bt-obexpushd.service」に以下のファイルを作りま す。
[Unit]
Description=Bluetooth obexpushd
After = bluetooth.service

[Service]
ExecStartPre=/bin/sleep 4
ExecStart=/usr/bin/obexpushd -B -n -o /home/pi/photos_from_phone
Type=simple

[Install]
WantedBy=multi-user.target

以下のコマンドでサービスの有効化、起動を行います。
$ sudo systemctl enable bt-obexpushd

$ sudo systemctl list-unit-files --type=service | grep bt-obexpushd
bt-obexpushd.service                   enabled

$ sudo systemctl start bt-obexpushd

$ sudo service bt-obexpushd status
● bt-obexpushd.service - Bluetooth obexpushd
   Loaded: loaded (/etc/systemd/system/bt-obexpushd.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2020-11-15 06:41:00 GMT; 3min 27s ago
  Process: 2628 ExecStartPre=/bin/sleep 4 (code=exited, status=0/SUCCESS)
 Main PID: 2629 (obexpushd)
   Memory: 8.1M
   CGroup: /system.slice/bt-obexpushd.service
           mq2629 /usr/bin/obexpushd -B -n -o /home/pi/photos_from_phone

Nov 15 06:40:56 raspberrypi systemd[1]: Starting Bluetooth obexpushd...
Nov 15 06:41:00 raspberrypi systemd[1]: Started Bluetooth obexpushd.
Nov 15 06:41:01 raspberrypi obexpushd[2629]: obexpushd 0.11.2 Copyright (C) 2006-2010 Hendrik Sattler
Nov 15 06:41:01 raspberrypi obexpushd[2629]: This software comes with ABSOLUTELY NO WARRANTY.
Nov 15 06:41:01 raspberrypi obexpushd[2629]: This is free software, and you are welcome to redistribute it
Nov 15 06:41:01 raspberrypi obexpushd[2629]: under certain conditions.
Nov 15 06:41:01 raspberrypi obexpushd[2629]: Listening on bluetooth/[00:00:00:00:00:00]:9

ディジタルフォトフレームへのダウンロード

raspberryの「/home/pi/photos_from_phone」に格納されている全ての写真を、以下のスクリプトでディジタル フォトフレームにダウンロードします。
USB-SWボックスはGPIO21でON/OFFしています。
このスクリプトをcronで動かすか、「/home/pi/photos_from_phone」をpython+pyinotifyか python+watchdogで監視してやれば完成です。
python+pyinotifyかpython+watchdogで監視する場合、ファイルが連続して送られてくることを考慮する必要が有りま す。
#!/bin/bash
#set -x

#ソースディレクトリ
source="/home/pi/photos_from_phone/*.jpg"

#デバイスファイルとマウントポイント
#SONY内蔵メモリー
#device="/dev/sda1"
#TrancentSDカード
device="/dev/sdb1"
mpoint="/mnt/sdcard"

#USB-SWのON
GPIO=21

for i in `seq 1 30`
do
    sudo sh -c "echo ${GPIO} > /sys/class/gpio/export"
    sudo sh -c "echo out > /sys/class/gpio/gpio${GPIO}/direction"
    sudo sh -c "echo 1 > /sys/class/gpio/gpio${GPIO}/value"
    sleep 20

#フォトフレームメモリのマウント
    #sudo mount -t vfat /dev/sda1 /mnt/sdcard
    sudo mount -t vfat ${device} ${mpoint}

#マウントチェック
    Check=`df | grep -c ${mpoint}`
    if test $Check -eq 1 ; then
        break;
    fi
    sleep 10
done

#フォトフレームメモリ内のImageファイルを削除
files=`find ${mpoint} -name "*.jpg" -print`
if test -n "$files" ; then
    sudo rm ${mpoint}/*.jpg
fi

#新しいファイルをコピー
#sudo cp /home/pi/photos_from_phone /mnt/sdcard
sudo cp ${source} ${mpoint}
sync
sync
sync

#フォトフレームメモリのアンマウント
#sudo umount -l /dev/sda1
sudo umount -l ${device}

#USB-SWのOFF
sudo sh -c "echo 0 > /sys/class/gpio/gpio${GPIO}/value"
sudo sh -c "echo ${GPIO} > /sys/class/gpio/unexport"