iceycode
10/19/2017 - 6:20 AM

Useful RPi Snippets

Various Raspberry Pi snippets/programs for use with Raspian, NOOBS, Nix-based ARM operating systems like Archlinux, Snaps for Ubuntu, Kali, and other linux distros.

# exapdn file system first
#  NOTE: some systems will expand disk on initial boot, like Ubuntu Mate
#   and some will install different tools based on space offered.
sudo raspi-config # expands filesystem on init
sudo reboot
    
# 2. Install new kernel
# NOTE: it is normal for pi to stall/freeze during process
curl -SLs https://apt.adafruit.com/add-pin | sudo bash
sudo apt-get install -y raspberrypi-bootloader adafruit-pitft-helper raspberrypi-kernel

# 3. COnfigure the PiTFT
# 35r = 3.5 in responsive 
sudo adafruit-pitft-helper -t 35r


# final step - reboot
sudo reboot
# original instructions call for using raspi-config after updating
sudo apt-get update
sudo apt-get upgrade
sudo raspi-config 

# this shoudl show an option with camera

# also can check using lusb to see if camera attached
lusb


#First, connect the Raspberry Pi with the camera and boot it. The camera module needs these two packages:


sudo apt-get install libraspberrypi-dev
sudo pip install picamera
 
# Then, open config.txt with sudo nano /boot/firmware/config.txt 
#  and add the lines towards end of file
# If the firmware folder does not exist
cd ./boot/


# start_x=1 
# gpu_mem=128

# then reboot
sudo reboot
# Get current session type - text-mode (cli) or lightdm+xfce (desktop)

systemctl get-default


# set current session to text-mode, disabling lightdm autostart
systemctl set-default multi-user:target


# Enabling lightdm+xfce again
systemctl set-default graphical.target

About

Various useful snippets to be used with RPi. Includes various configuration files, bash scripts and python programs that use camera.

References

A list of references where scripts where either obtained or adapted from:

############
## Installing BlackArch on top of Arch Linux ARM
############

#!bash
sudo bash
# Run https://blackarch.org/strap.sh as root and follow the instructions.
curl -O https://blackarch.org/strap.sh
# The SHA1 sum should match: 6f152b79419491db92c1fdde3fad2d445f09aae3
sha1sum strap.sh
# Set execute bit
chmod +x strap.sh
# Run strap.sh
sudo ./strap.sh

#############
## Install Tools
#############

# To list all of the available tools, run
sudo pacman -Sgg | grep blackarch | cut -d' ' -f2 | sort -u

# To install all of the tools, run
sudo pacman -S blackarch

# To install a category of tools, run
sudo pacman -S blackarch-{{category}}

# To see the blackarch categories, run
sudo pacman -Sg | grep blackarch


###########
## Alt Installation - Build from source
###########
# First, you must install blackman. If the BlackArch package repository is setup on your machine,
# you can install blackman like:
sudo pacman -S blackman
# Download, compile and install package:
sudo blackman -i {{package}}
# Download, compile and install whole category
sudo blackman -g <group>
# Download, compile and install all BlackArch tools
sudo blackman -a
# To list blackarch categories
blackman -l
# To list category tools
blackman -p <category>
### by file size, 1 MB
import io
import itertools
import picamera


class RecordVideoMod_RpiPy:
    
    # class variables can be set during instatiation
    int _filesize = 1 # filesize in MB
    
    # resolution x, y axis
    int _resolX = 640
    int _resolY = 360
    
    int _framerate = 24 # framerate default is 24
    int _timeTillStop = 44444 # time given in milliseconds
    int _quality = 20 # quality of recording default
    
    # bitrate default
    float _bitrate = 750000
    float _maxSize = 1048576 # max size of output before stops recording 
    
    # the file output as raw h264 data
    str _fileout = 'file%02d.h264'
    
    
    def __init__(self, str fileout):
        self._filesize = 1
        if (fileout != null){
            self._fileout = fileout
        }

    def outputs():
        for i in itertools.count(1):
            yield io.open('file%02d.h264' % i, 'wb')
    
    
    def with_resolution(self, int x, int y, int framerate):
    
        with picamera.PiCamera() as camera:
            camera.resolution = (x, y)
            camera.framerate = framerate
            for output in camera.record_sequence(
                    outputs(), quality=20, bitrate=750000):
                while output.tell() < _maxSize:
                    camera.wait_recording(0.1)
                if output.name == 'file99.h264':
                    break
    
    def timed_video(int time):
        with picamera.PiCamera() as camera:
            camera.resolution = (640, 360)
            camera.framerate = 24
            for filename in camera.record_sequence([
                    'hour%02d.h264' % (h + 1)
                    for h in range(24)
                    ], quality=20, bitrate=750000):
                camera.wait_recording(60 * 60)
#!bash 
sudo bash
mount /dev/mmcblk0p1 /boot
cd /usr/local/src
## For current stable
wget  -O re4son-kernel_current.tar.xz https://whitedome.com.au/re4son/downloads/11299/
tar -xJf re4son-kernel_current.tar.xz
# currently version of kernel is 4

cd re4son-kernel_4*
./install.sh



## For old stable
wget -O re4son-kernel_old.tar.xz https://whitedome.com.au/re4son/downloads/12009/
tar -xJf re4son-kernel_old.tar.xz
cd re4son-kernel_4*
./install.sh

# reboot after hitting Y 
reboot


#### Kernel Headers included after install
# install after reboot
cd /usr/local/src/re4son-kernel_4*
./install.sh -e



########## Enhancements of Wifi ##############
# nexmon drivers enablew monitor mode and injection fraames for built-in wifi
#  on Pi2, Pi3, Pi0W
cd /usr/local/src/re4son-kernel_4*
./install.sh -x


cd /usr/local/src/re4son-kernel_4*
./install.sh -o




########## OPTIONAL ##############
# In case issues with apt-update cause bluetooth dependencies to fail install
## Remove Bluetooth:
cd /usr/local/src/re4son-kernel_4*
./install.sh -r
## Upgrade system
## Re-install Bluetooth:
./install.sh -b
##### To view all options, either just
# NOTE: Pi captures video as a raw H264 video stream

raspivid 

# OR, scroll thru, pressing q to quit
raspivid 2>&1 | less


# record video normally
# Records to path given to vid.h264 - this can be wrapped in MP4 container
raspivid -o vid.h264

# record horizontally and vertically, respectively
raspivid -o vid.h264 -hf # horizantal 
raspivid -o vid.h264 -vf # vertical



##### Specificy length of time, in milliseconds
raspivid -o vid.h264 -t 10000


## changing so that output file will not be that large
raspivid --width 640 --height 360 --framerate 24 --bitrate 17000000 --qp 20 \
--timeout 30000 --output test.h264

# split video into multiple parts
raspivid --width 640 --height 360 --framerate 24 --bitrate 750000 --qp 20 --timeout $((24*60*60*1000)) --segment $((1*60*60*1000)) --output hour%02d.h264




########################################################
#### Capture raw and wrap into an MP4 using MP4Box
############################################
sudo apt-get install -y gpac


# capture raw video and wrap in MP4 container:

# Capture 30 seconds of raw video at 640x480 and 150kB/s bit rate into a pivideo.h264 file:
raspivid -t 30000 -w 640 -h 480 -fps 25 -b 1200000 -p 0,0,640,480 -o pivideo.h264 
# Wrap the raw video with an MP4 container: 
MP4Box -add pivideo.h264 pivideo.mp4
# Remove the source raw file, leaving the remaining pivideo.mp4 file to play
rm pivideo.h264


### to wrap exisitng h264 raw pi video, go to path & run:
MP4Box -add video.h264 video.mp4


sudo xset s off # disable screen saver
sudo xset -dpms # disables DPMS (Display Power Management Signal)
sudo xset s noblank # tells X server to not blank the video device

# add the following to config file in 
# Add to file, in SeatDefaults section, /etc/lightdm/lightdm.conf
sudo nano /etc/lightdm/lightdm.conf # for root rights to alter file


# add following to section [SeatDefaults]
#       [SeatDefaults]
#       xserver-command=X -s 0 -dpms