Raspberry Pi Handy Notes

Raspberry Pi Handy Notes



########################################################################

                                Raspberry Pi Pre-requisites

########################################################################


sudo mount -o remount,rw /media/boot

Note: This will give access to the "boot" partition of raspberry pi's SD card. 

How to flash raspberry pi image into SD card from ubuntu OS Terminal's  command line? 
 
1. First get the device in /dev corresponding to SD inserted into the laptop.
$ sudo fdisk -l | grep "/dev/sd"

Disk /dev/sda: 29.74 GiB, 31914983424 bytes, 62333952 sectors
/dev/sda1        2048 62333951 62331904 29.7G  b W95 FAT32
 
 2. Now, use dd command to copy the image into the SD card.
# sudo dd bs=1M if=/home/<user>/raspberry_pi.img of=/dev/sda1 status=progress conv=fsync

2891972608 bytes (2.9 GB, 2.7 GiB) copied, 95 s, 30.4 MB/s
 
 
How to change the metric of ethernet port so that routing over Wi-Fi to internet is preferred over ethernet port

1. install 'ifmetric' in linux machine
#sudo apt-get install ifmetric

2. Do "route -n"

3.  ifmetric eth0 <any number greater than me>

once set, this will persist after reboot.

4. do "route -n" and check whether 'ethernet interface' metric is greater than 'wlan interface metric'.

To change the resolution of Raspberry pi, 

run the command sudo raspi-config, navigate to Advanced Options > Resolution, and choose an option.

Add route in linux to the default gateway so that the port is reachable externally

route add default gw 10.10.1.1 eth0


Steps to ensure that Wi-Fi scan connects to pre-defined SSID in RASPBERRY pi


1. In /etc/dhcpcd.conf   (at this place we can assign static ip address to raspberry pi)
#interface wlan0
#static ip_address=192.168.1.201/24
#static routers=192.168.1.1

#static domain_name_servers=192.168.1.1

2. In the below file add the changes

Neelkanth $:cat /etc/network/interfaces

# interfaces(5) file used by ifup(8) and ifdown(8)


# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

3.  Now add the ssid and password to the below file. On reboot, Raspberry pi will automatically connect to this.
Neelkanth $:cat /etc/wpa_supplicant/wpa_supplicant.conf 
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=IN

network={
ssid="sparrow"
psk="whyshoulditell"
key_mgmt=WPA-PSK
}

How to use minicom alternative in Raspberry pi as computer to which a device's serial console is connected to Raspberry pi via USB  ?
# apt-get install picocom
# picocom /dev/ttyUSB0 -b 115200 -l
(Exit picocom: Ctrl+a, then Ctrl+x)


Do this to ensure that the keyboard settings are changed to US format.


sudo vi /etc/default/keyboard 


XKBLAYOUT=”gb”

Change the gb to us 
(This assumes you want a us mapping, if not replace the gb with the two letter code for your country)



##############################################################
Install "terminator" terminal software in raspberry pi.
##############################################################
sudo apt-get install terminator


############################################################
                               Install webcam in raspberry pi
############################################################
sudo apt-get install fswebcam

############################################################
                               Install text editors:
############################################################
install vi editor and gedit

make changes in vimrc.

To be added to vimrc
set cindent
set expandtab
set shiftwidth=4
set tabstop=4
set hlsearch


############################################################
Live video feed using raspberry pi camera or normal USB webcam. 
Complete working steps:

Note: With this approach, there is 5 secs delay in video reception.
############################################################
1. sudo apt-get update
2. sudo apt-get upgrade
3. sudo apt-get install motion -y    [install motion capture software]
4. ls /dev/video*       [check if any video device is created]

5. sudo modprobe bcm2835-v4l2        
[if cam is raspi camera, use this. this has to be done in 
  .bashrc or startup scripts   ---> write into this ==> /etc/rc.local]

6. v4l2-ctl -V                       [check camera details]  
7. sudo vi /etc/motion/motion.conf   [do the following]
    a. daemon on
    b. stream_localhost off
    c. target_dir /home/pi/Monitor
    d. v4l2_palette 15
    e. width 640 
    f. height 480 
    g. framerate 10 
8. sudo vi /etc/default/motion
    start_motion_daemon=yes

9. mkdir /home/pi/Monitor
   sudo chgrp motion /home/pi/Monitor
   chmod g+rwx /home/pi/Monitor

10. sudo service motion start       [do this in /etc/rc.local]

############################################################
Steps to install apache 2 server in Raspberry Pi and access camera 
connected to Rapsberry pi remotely via WebUI/Browser
############################################################
sudo apt-get install apache2 -y

This will also create a new directory in /var/ called /www/ which we shall use to serve our pages.

Open the text editor on your Raspberry Pi. We will now write a few lines of HTML to build a simple web page.

src="http://192.168.0.3:8081/">
script>


To make sure the IP address matches that of your Pi we add :8081 at the end. 
We then instruct the browser to load a JavaScript file containing the strapdown 
functionality. Save your file as index.html to your home directory. 
Open a terminal and type the following to copy the file to our web server:

$ sudo cp /home/pi/index.html /var/www/html/


Finally, we need to start our web server and restart the motion service.


$ sudo service apache2 start

$ sudo service motion restart

pi@raspberrypi:~ $ sudo cp ~/Desktop/index.html /var/www/html/
pi@raspberrypi:~ $ sudo service apache2 restart

#######################################################################
Steps to Connect Raspberry Pi to internet via Wi-Fi interface. At the same time, 
connect PI to local network for ssh. And wifi shouldn't get disconnected.
#######################################################################
1.  raspi-config t  [enable ssh and configure password]
2.  delete default route to the gateway device connected to raspberry Pi ethernet port.

pi@raspberrypi:~ $ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    202    0        0 eth0   <------------
0.0.0.0         192.168.1.1     0.0.0.0         UG    303    0        0 wlan0
192.168.0.0     0.0.0.0         255.255.255.0   U     202    0        0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     303    0        0 wlan0

pi@raspberrypi:~ $ route del default gw 192.168.0.1   <-------------------------------
[Procedure incomplete.. to be updated later]


##################################################################
To open a video from terminal
##################################################################
First install vlc player by running the below commands on terminal
sudo add-apt-repository ppa:videolan/stable-daily
sudo apt-get update
sudo apt-get install vlc

Then go into the directory which contains videos you want to play,
cd /path/to/the/directory/which/contains/videos

Play the video from terminal using vlc player,
vlc "videofilename.fileformat"

##################################################################
Install shotwell to open a picture via terminal,
##################################################################
sudo add-apt-repository ppa:yorba/ppa
sudo apt-get update
sudo apt-get install shotwell

Then go into the directory which contain picture you want to open,
cd /path/to/the/directory/which/contains/picture

Open the picture via terminal using shotwell,
shotwell "picturefilename.fileformat"


############################################################
Install Tinker in python   [Develop GUI using python]
############################################################
Finally, Install Tkinter
sudo apt-get install python-tk

for python 3
sudo apt-get install python3-tk

Tutorial to learn Tinker:
https://www.python-course.eu/tkinter_labels.php


######################################################
gpio bulb on/off testing: 
######################################################
echo "17" > /sys/class/gpio/export 
echo "out" > /sys/class/gpio/gpio17/direction
echo 1 > /sys/class/gpio/gpio2/value
echo 0 > /sys/class/gpio/gpio2/value

###############################################################################

Install Driver for HDMI
###############################################################################

Open terminal and Download the driver on RPI

Run:

git clone https://github.com/Elecrow-keen/Elecrow-LCD35.git


Install driver
=====================================================================
Run:
cd Elecrow-LCD35
sudo ./Elecrow-LCD35


Switch to HDMI:

Run:
cd Elecrow-LCD35
sudo ./Elecrow-LCD35 hdmi


Transmit Live Video Feed from Raspberry Pi camera to vlc media player in same network:

Give the below command in Raspberry Pi linux terminal:

raspivid -o - -t 0 -n -w 600 -h 400 -fps 12 | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/}' :demux=h264


give the below in vlc media player in laptop or some other device

rtsp://192.168.1.6:8554/

i.e. rtsp://<ip address of raspberry pi>:8554/



#########################################################
How to configure static ip address in raspberry pi
#########################################################
in file /etc/dhcpcd.conf,

paste the below:

interface wlan0
static ip_address=192.168.1.200/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1

and reboot.....
after this, the ip address assigned would be static.

############################################################

How to view current username in linux terminal
############################################################
whoami

###########################################################

Disable vim automatic visual mode on mouse select
###########################################################
copy vimrc contents from /etc/vimrc to ~/.vimrc

set mouse-=a

add to ~/.vimrc: set mouse-=a 


######################################################

Steps to run Python Program after Raspberry pi boots
up:
Add the ones that are highlighted in yellow
######################################################
Neelkanth $:cat ~/.config/lxsession/LXDE-pi/autostart 
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@point-rpi
@midori
@python /home/pi/python_gui_automate_remote_camera.py

########################################################

Python program using pyautogui library to start
raspberry pi camera so that it can be accessed by
vlc player application running on any device in the
network
########################################################
@python /home/pi/python_gui_automate_remote_camera.py
Neelkanth $:cat python_gui_automate_remote_camera.py 
import pyautogui
import time

time.sleep(15)

###############################################################
# open Terminal window
###############################################################
pyautogui.PAUSE=1
pyautogui.click(x=142, y=19, clicks=1, interval=1, button='left')
################################################################
# Now, double left click on terminal tab so that i would auto
# adjuct PI 3.5 Touch Screen.
################################################################
pyautogui.PAUSE=1
pyautogui.click(x=476, y=54, clicks = 1, interval = 1, button='left')

#################################################################
# Now, put the mouse pointer onto the screen and start typing
#################################################################
pyautogui.PAUSE=1
pyautogui.click(x=52, y=149, clicks = 2, interval = 2)

##################################################################
# Now, start typing on the terminal
##################################################################
pyautogui.typewrite("raspivid -o - -t 0 -n -w 640 -h 480 -fps 12 | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/}' :demux=h264\n", interval=0.1)


########################################################################
How to enable monitor mode in ubuntu, such that the wifi processes don't change the 
WLAN interface back to Monitor Mode:

Note: airmon-ng ensures that all the processes responsible for starting WLAN interface in 
managed mode are killed. 
########################################################################
vvdn@11684--SW--C:~$ cat monitor_mode.py 
import os

os.system("sudo airmon-ng start wlp9s0")

Output:
sudo python3 monitor_mode.py 

Found 4 processes that could cause trouble.
Kill them using 'airmon-ng check kill' before putting
the card in monitor mode, they will interfere by changing channels
and sometimes putting the interface back in managed mode

    PID Name
    991 avahi-daemon
    998 NetworkManager
   1033 wpa_supplicant
   1037 avahi-daemon

PHY Interface Driver Chipset

phy0 wlp9s0 iwlwifi Intel Corporation Wi-Fi 6 AX200 (rev 1a)

(mac80211 monitor mode vif enabled for [phy0]wlp9s0 on [phy0]wlp9s0mon)
(mac80211 station mode vif disabled for [phy0]wlp9s0)


#####################################################################
Code to configure ip address on ethernel port on laptop running Ubuntu and
this is the interface which is connected to the ethernet interface of Raspberry pi:
#####################################################################
import os
import time

os.system('sudo ifmetric enp0s31f6 1000')
os.system('sudo ifconfig enp0s31f6 down')
os.system('sudo ifconfig enp0s31f6 up')
os.system('sudo ifconfig enp0s31f6 10.10.10.2/24 up')
os.system('sudo route add default gw 10.10.10.2 enp0s31f6')

time.sleep(2)
os.system('sudo ifmetric enp0s31f6 1000')