Raspberry PI

The raspberry pi is a low-cost credit card sized computer that is capable of doing all sorts of things. Here I present to you a list of commands applicable to the PI.

Here are a bunch a useful commands:

# Display commands
vcgencmd display_power 0                # turn off the video output
vcgencmd display_power 1                # turn on the video output
tvservice -n | awk -F'=' '{print $2}'   # get the name of the display
tvservice -s                            # get hdmi status
tvservice -a                            # get supported audio information

# Wakeup display from screensaver
DISPLAY=:0 xset s reset

# Network Information
ip route                                # get network metric
arp -an                                 # get arp entries
ifconfig                                # display and/or configure network interfaces
netstat -rn                             # get the Kernel IP routing table

ip addr | grep -Po '(?!(inet 127.\d.\d.1))(inet \K(\d{1,3}\.){3}\d{1,3})'         # get the ip address

# Disk stats
df -h                                   # get all the disk information in human readable format
vmstat                                  # report virtual memory statistics

# Memory Information
vcgencmd get_mem arm && vcgencmd get_mem gpu                      # get memory split information [arm, gpu]
vcgencmd get_mem arm | awk -F'=' '{print $2}' | sed s'/.$//'      # get just arm portion of memory split
vcgencmd get_mem gpu | awk -F'=' '{print $2}' | sed s'/.$//'      # get just the gpu portion of memory split

cat /proc/meminfo | grep MemFree | awk '{print $2/1024}'          # get free memory in Mb
cat /proc/meminfo | grep MemTotal | awk '{print $2/1024}'         # get total memory in Mb
cat /proc/meminfo | grep MemAvailable | awk '{print $2/1024}'     # get availabe memory in Mb
cat /proc/meminfo | grep Buffers | awk '{print $2/1024}'          # get Buffers info in Mb
cat /proc/meminfo | grep Cached | awk '{print $2/1024}'           # get Cached memory info in Mb
cat /proc/meminfo | grep Dirty | awk '{print $2/1024}'            # get Dirty memory info in Mb
cat /proc/meminfo | grep Active | awk '{print $2/1024}'           # get Active memory info in Mb
cat /proc/meminfo | grep Inactive | awk '{print $2/1024}'         # get Inactive memory info in Mb

# System Information
cat /proc/cpuinfo | grep Hardware | awk '{print $3}'              # Hardware
cat /proc/cpuinfo | grep Revision | awk '{print $3}'              # Revision
hostname                                                          # get the hostname
uptime -p                                                         # get system uptime
cat /proc/cpuinfo | grep Serial | awk '{print $3}'                # get serial number of the pi
cat /sys/class/net/eth0/address                                   # get hardware mac address [eth0]
uname -mrs                                                        # kernel information

cat /proc/cpuinfo | grep 'model name' | cut -d' ' -f 3- | awk 'NR==1'                 # get processor model information

# Voltages and Clocks
# voltages
vcgencmd measure_volts core | awk -F'=' '{print $2 }'             # Measure core voltage
vcgencmd measure_volts sdram_c | awk -F'=' '{print $2 }'          # measure sdram_c voltage
vcgencmd measure_volts sdram_i | awk -F'=' '{print $2 }'          # measure sdram_i voltage
vcgencmd measure_volts sdram_p | awk -F'=' '{print $2 }'          # measure sdram_p voltage

# clock
vcgencmd measure_clock arm
vcgencmd measure_clock core
vcgencmd measure_clock h264
vcgencmd measure_clock isp
vcgencmd measure_clock v3d
vcgencmd measure_clock uart
vcgencmd measure_clock pwm
vcgencmd measure_clock emmc
vcgencmd measure_clock pixel
vcgencmd measure_clock vec
vcgencmd measure_clock hdmi
vcgencmd measure_clock dpi

cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq         # cpu frequency

# Temperature
vcgencmd measure_temp | awk -F'=' '{print $2}' | sed s'/..$//'                                # GPU temperature
cat /sys/class/thermal/thermal_zone0/temp | awk '{print $1/1000}'                             # thermal CPU temperature

# disabling onboard wifi and bluetooth on RPI3
# modify /boot/config.txt to add the following two lines

# dtoverlay=pi3-disable-wifi
# dtoverlay=pi3-disable-bt

# Scan wireless SSIDS that are around your raspberry pi
sudo iwlist wlan0 scanning | egrep 'Cell |Encryption|Quality|Last beacon|ESSID|Mode|Channel'

# Copy directory contents from remote host to local system
#syntax:: scp user@remote-server:/path/to/dir/* /path/to/local/dir/
scp pi@* /Users/krsna/Desktop/

# Copy directory contents from local system to remote host
#syntax:: scp /path/to/local/dir/ user@remote-server:/path/to/dir/*
scp /Users/krsna/Desktop/ pi@*

# One can also use the rsync utility to do similar things
# To sync a local directory to a remote host
rsync -avzr /path/to/dir user@remote-server:/path/to/dir

# To sync a local directory to a remote host and delete files that are in remote that are not in local
rsync -avzr --delete /path/to/dir user@remote-server:/path/to/dir

Setting up a raspberry pi as an access point in a standalone network (NAT)

# Install dnsmasq and hostapd
sudo apt-get install dnsmasq hostapd -y

# Turn them off since there are no configuration files yet
sudo systemctl stop dnsmasq
sudo systemctl stop hostapd

# Configure a static ip address
sudo bash -c "cat << EOT >> /etc/dhcpcd.conf
interface wlan0
    static ip_address=
    nohook wpa_supplicant

# Restart the dhcpcd daemon and set up the new wlan0 configuration
sudo service dhcpcd restart

# Move /etc/dnsmasq.conf to /etc/dnsmasq.conf.orig for a backup just incase
sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig

# Edit /etc/dnsmasq.conf file like this so that ips of range and is leased for 42 hours
sudo bash -c "cat << EOT >> /etc/dnsmasq.conf
interface=wlan0      # Use the require wireless interface - usually wlan0

# Set up the access point host software (hostapd)
sudo bash -c "cat << EOT >> /etc/hostapd/hostapd.conf

# Tell the system where to find the configuration file above
sudo bash -c "cat << EOT >> /etc/default/hostapd

# Start hostapd and dnsmasq services
sudo systemctl start hostapd
sudo systemctl start dnsmasq

# In the file /etc/sysctl.conf uncomment the line #net.ipv4.ip_forward=1 using sed
sudo sed -i '/#net.ipv4.ip_forward=1/s/^#//g' /etc/sysctl.conf

# Add a masquerade for outbound traffic on eth0
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

# Save the iptables rule
sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

# Insert the line "iptables-restore < /etc/iptables.ipv4.nat" before the last line "exit 0" in file /etc/rc.local
sudo sed -i '$iiptables-restore < /etc/iptables.ipv4.nat' /etc/rc.local # the $i with the extra i is not a typo :)

# Reboot
sudo reboot now

# At this point you should be able to see a wifi network with SSID called TESTSSID being broadcasted by your raspberry pi, you can connect to it using the password SOMEPASSWORD1234

Hosting a webpage using lighttpd on the raspberry pi

# Install lightppd and reload it
$ sudo apt-get -y install lighttpd
$ sudo service lighttpd force-reload

# Set user group permissions for the /var/www/html and /var/www/cgi-bin directory
sudo chown pi:pi /var/www/html /var/www/cgi-bin
sudo chmod 775 /var/www/html /var/www/cgi-bin

# Place your index.html file inside /var/www/html/ and make sure you are on the same network as the raspi or connect to the SSID that the raspi is broadcasting  and you are all done
# Go to [your raspberry pis ip address] in a browser and you should now see your index.html being served