Tutorials

GIT

Git Cheat Sheet #


🔧 Configuration (One-time setup) #

git config user.name “Your Name”
git config user.email “you@example.com

Set username and email for commits.


📁 Repository Setup #

git init
git init -b main
git remote add origin

Initialise a repository and connect it to a remote.


🔍 Status & Inspection #

git status
git log
git show
git diff

Check repository state, history, commit details, and differences.

Home Cloud

Banana Pi - Home Cloud (Part - 2) #


Refresh MiniDLNA #

MiniDLNA couldn’t find any new files in my media_dir even though I was adding new files. I think the problem was that my media_dir was set to an external hard drive.

(media_dir=v,/media/ExtHD/x/x). I got it working, here are the steps I used to fix.

sudo service minidlna stop sudo rm -rf /var/cache/minidlna/* sudo nano /etc/minidlna.conf

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

GitHub

Adding a SSH key to GitHub account #

ls -al ~/.ssh

Generate a key

ssh-keygen -t rsa -b 4096 -C <user mail id>
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa

Clip or copy manually

clip < ~/.ssh/id_rsa.pub
cat ~/.ssh/id_rsa.pub

Go to GitHub, add the public key to settings -

https://github.com/settings/keys

Home | Technology

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:

Home Cloud

Banana Pi - Home Cloud #


SSH #

(Enable SSH)

$ sudo apt-get install openssh-server

$ sudo nano /etc/rc.local
/etc/init.d ssh
exit 0

Resize #

(Card on a ubuntu box)

$ sudo fdisk -l
Disk /dev/mmcblk0: 7.4 GiB, 7948206080 bytes, 15523840 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x59d0bd13

Device         Boot     Start       End  Blocks  Id System
/dev/mmcblk0p1           2048    104447   51200  83 Linux
/dev/mmcblk0p2         104448  15523839 7709696  83 Linux

Disk /dev/sda: 7.5 GiB, 7998537728 bytes, 15622144 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x59d0bd13

Device    Boot     Start       End  Blocks  Id System
/dev/sda1           2048    104447   51200  83 Linux
/dev/sda2         104448   7167999 3531776  83 Linux

umount /dev/sda2

[bananapi@lemaker ~]$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.24.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): n

Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (104448-15622143, default 104448): 335872
Last sector, +sectors or +size{K,M,G,T,P} (104448-15622143, default 15622143): 

Created a new partition 2 of type 'Linux' and of size 7.4 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

pi@bananapi:~$ sudo resize2fs /dev/mmcblk0p2

SAMBA #

(For accessing SSD from Windows PC inside home network on DLNA)

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))