RaspberryPi

Bluetooth Speaker

Raspberry Pi : Connect Bluetooth Speaker #

Connecting Raspberry Pi to Mi Pocket Speaker 2

[bluetooth]# power on
Changing power on succeeded
[bluetooth]# agent on
Agent registered
[bluetooth]# scan on
Discovery started
[CHG] Controller B8:27:EB:79:F6:B7 Discovering: yes
[bluetooth]# default-agent
Default agent request successful
[CHG] Device 00:EC:0A:57:F2:65 LegacyPairing: yes
[bluetooth]# list
Controller B8:27:EB:79:F6:B7 raspberrypi [default]
[bluetooth]# devices
Device 5C:72:FE:B7:35:FD 5C-72-FE-B7-35-FD
Device 28:F0:76:0A:B9:7B 28-F0-76-0A-B9-7B
Device 00:EC:0A:57:F2:65 Redmi
[NEW] Device 74:A3:4A:15:DD:71 Mi Pocket Speaker 2
[bluetooth]# pair 74:A3:4A:15:DD:71
Attempting to pair with 74:A3:4A:15:DD:71
[CHG] Device 74:A3:4A:15:DD:71 Connected: yes
[CHG] Device 74:A3:4A:15:DD:71 UUIDs:
        00001108-0000-1000-8000-00805f9b34fb
        0000110b-0000-1000-8000-00805f9b34fb
        0000110c-0000-1000-8000-00805f9b34fb
        0000110e-0000-1000-8000-00805f9b34fb
        0000111e-0000-1000-8000-00805f9b34fb
[CHG] Device 74:A3:4A:15:DD:71 Paired: yes
Pairing successful
[CHG] Device 74:A3:4A:15:DD:71 Connected: no
[bluetooth]# info 74:A3:4A:15:DD:71
Device 74:A3:4A:15:DD:71
        Name: Mi Pocket Speaker 2
        Alias: Mi Pocket Speaker 2
        Class: 0x240404
        Icon: audio-card
        Paired: yes
        Trusted: no
        Blocked: no
        Connected: no
        LegacyPairing: yes
        UUID: Headset                   (00001108-0000-1000-8000-00805f9b34fb)
        UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)
        UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
        UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
        UUID: Handsfree                 (0000111e-0000-1000-8000-00805f9b34fb)
[bluetooth]# trust 74:A3:4A:15:DD:71
[CHG] Device 74:A3:4A:15:DD:71 Trusted: yes
Changing 74:A3:4A:15:DD:71 trust succeeded
[bluetooth]# info 74:A3:4A:15:DD:71
Device 74:A3:4A:15:DD:71
        Name: Mi Pocket Speaker 2
        Alias: Mi Pocket Speaker 2
        Class: 0x240404
        Icon: audio-card
        Paired: yes
        Trusted: yes
        Blocked: no
        Connected: no
        LegacyPairing: yes
        UUID: Headset                   (00001108-0000-1000-8000-00805f9b34fb)
        UUID: Audio Sink                (0000110b-0000-1000-8000-00805f9b34fb)
        UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
        UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
        UUID: Handsfree                 (0000111e-0000-1000-8000-00805f9b34fb)
[bluetooth]# quit

pi@raspberrypi:~ $ sudo apt-get update && sudo apt-get install pulseaudio -y
pi@raspberrypi:~ $ mkdir -p scripts
pi@raspberrypi:~ $ vim scripts/autopair
#!/bin/bash
bluetoothctl << EOF
connect [enter your MAC add]
EOF
pi@raspberrypi:~ $ chmod +x !$
chmod +x scripts/autopair
pi@raspberrypi:~ $ sudo vim /boot/config.txt
#dtparam=audio=on
pi@raspberrypi:~ $ vim ~/scripts/on.py
#!/usr/bin/python

Monitor removal of bluetooth reciever #

import sys
import subprocess
import time
def blue_it():
    status = subprocess.call('ls /dev/input/event0 2>/dev/null', shell=True)
    while status == 0:
        print("Bluetooth UP")
        print(status)
        time.sleep(15)
        status = subprocess.call('ls /dev/input/event0 2>/dev/null', shell=True)
    else:
        waiting()
def waiting():
    subprocess.call('killall -9 pulseaudio', shell=True)
    time.sleep(3)
    subprocess.call('pulseaudio --start', shell=True)
    time.sleep(2)
    status = subprocess.call('ls /dev/input/event0 2>/dev/null', shell=True)
    while status == 2:
        print("Bluetooth DOWN")
        print(status)
        subprocess.call('~/scripts/autopair', shell=True)
        time.sleep(15)
        status = subprocess.call('ls /dev/input/event0 2>/dev/null', shell=True)
    else:
        blue_it()
blue_it()
pi@raspberrypi:~ $chmod +x ~/scripts/on.py
pi@raspberrypi:~ $vim ~/.bashrc
pulseaudio --start
wait
~/python/on.py

Reboot #

If the connection fails, remove the device and pair-trust-connect

Dropbox

Dropbox with Raspberry Pi #

Act as root, since I wanted to run the script on startup as a service, so I must setup dropbox for ‘root’ user rather than ‘pi’ -

pi@raspberrypi:~ $ sudo -s
root@raspberrypi:/home/pi# cd /root/
root@raspberrypi:~# git clone http://github.com/andreafabrizi/Dropbox-Uploader.git
Cloning into 'Dropbox-Uploader'...

remote: Counting objects: 785, done.
remote: Total 785 (delta 0), reused 0 (delta 0), pack-reused 785
Receiving objects: 100% (785/785), 237.87 KiB | 0 bytes/s, done.
Resolving deltas: 100% (409/409), done.
Checking connectivity... done.
root@raspberrypi:~# cd Dropbox-Uploader/
root@raspberrypi:~/Dropbox-Uploader# ./dropbox_uploader.sh

This is the first time you run this script, please follow the instructions:

SSH

SSH login to Raspberry Pi without password #

Execute following 3 commands from PC/Laptop from which you want to login to Raspberry Pi without password.

laptop:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/laptop/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/laptop/.ssh/id_rsa.
Your public key has been saved in /home/laptop/.ssh/id_rsa.pub.
The key fingerprint is:
6f:72:4e:e5:f3:81:d4:27:9d:27:06:f0:ce:f1:75:7d laptop
The key's randomart image is:
+--[ RSA 2048]----+
|          .      |
|                .|
|            +   A|
|           o = o+|
|        S   = *.+|
|         o + o +.|
|        . = + .  |
|         *   o . |
|          .   .  |
+-----------------+

NUsing ssh create a directory ~/.ssh as user pi on Raspberry -

laptop:~$ ssh pi@192.168.1.68 mkdir -p .sshpi@192.168.1.68's password:

Append public key to pi@Raspberry:.ssh/authorized_keys and enter pi’s password one last time:

Bootup Script

Bootup script Setup #

Edit Python script, add as first line -

#!/usr/bin/python

Change permissions -

chmod 755 raspiLapseCam.py

That means you can execute it by typing raspiLapseCam.py rather than python raspiLapseCam.py

Then we can use an /etc/init.d script to get it started when the system boots.

Start by copying /etc/init.d/skeleton to /etc/init.d/timelapse -

cd /etc/init.d && sudo cp skeleton timelapse

edit /etc/init.d/timelapse, change lines 23 & 24 to refer to the command ‘/home/pi/raspiLapseCam.py’ -

DESC="Raspberry Pi TimeLapse"
DAEMON=/home/pi/raspiLapseCam.py

Make it executable -

e-mail

E-Mail with Raspberry Pi #

A quick guide to setting up the E-Mail on Raspberry Pi.


Using SSMTP #

pi@raspberrypi ~ $ sudo apt-get install ssmtp mailutils mpack

Now edit the file /etc/ssmtp/ssmtp.conf as root and add the next lines. Please note that some of the lines already exist and may need to be changed. Others don’t exist yet and need to be added to the end of the file.

Surveillance

Raspberry Pi Surveillance #

One of the hobby projects:

  • Keep on capturing pictures in a loop, compare current picture with last picture, if the difference in pixel is more than threshold, capture a high resolution image, save it to RAM disk (so that memory card is safe from frequent read write operations), rename the image using date and time; e-mail it with date in subject field.
  • Run a cron job to delete temporary files older than 20 minutes.
  • Run the script as service and use another to monitor the service, on failure restart the service.

Python script to detect motion #

(To Do: add git link for the code here))

WiFi

Wi-Fi Set-up for Raspberry Pi #

Make sure your interface file and wpa_supplicant.conf look like the ones below -


pi@raspberrypi ~ $ sudo nano /etc/network/interfaces

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp

pi@raspberrypi ~ $ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="YOURSSID"
psk="YOURPASSWORD"

# Protocol type can be: RSN (for WP2) and WPA (for WPA1)
proto=WPA

# Key management type can be: WPA-PSK or WPA-EAP (Pre-Shared or Enterprise)
key_mgmt=WPA-PSK

# Pairwise can be CCMP or TKIP (for WPA2 or WPA1)
pairwise=TKIP

#Authorization option should be OPEN for both WPA1/WPA2 (in less commonly used are SHARED and LEAP)
auth_alg=OPEN
}


pi@raspberrypi ~ $ sudo reboot

To view all WiFi networks -