Turning On/Off Monitor Raspberry Pi Scripts

You can use these scripts to turn on/off monitor of raspberry pi

screen_off.sh:
#!/bin/sh
tvservice --off > /dev/null

screen_on.sh:
#!/bin/sh
export DISPLAY=:0
tvservice -p
fbset -depth 8; fbset -depth 16; xrefresh

Another option (this is failed when tested because apt-get doesnt found cec-util package):

apt-get install cec-util

# Screen on, change to the active input of the Pi

echo "on 0" | cec-client -s > /dev/null
sleep 5
echo "as" | cec-client -s > /dev/null

#Screen off
echo "standby 0" | cec-client -s

Source:
– https://www.danpurdy.co.uk/web-development/raspberry-pi-kiosk-screen-tutorial/

Raspberry Pi Force Boot HDMI Mode and Resolution

This command to store current EDID data to file:

sudo /opt/vc/bin/tvservice -d /boot/edid.dat

And add this line below to /boot/config.txt to load the EDID data above on boot, ignore EDID data from monitor

hdmi_edid_file=1

so RPi will boot with mode and resolution correctly even when monitor is off (or slowly starting on), this is useful when power blackout, the monitor starting slowly and is not detected by RPi correctly and fallback to VGA resolution automatically

Source:
– https://www.raspberrypi.org/forums/viewtopic.php?p=173430#p173430

Raspberry Pi Raspbian SWAP Configuration

Raspbian uses dphys-swapfile, which is a swap-file based solution instead of the “standard” swap-partition based solution. It is much easier to change the size of the swap.

The configuration file is:

/etc/dphys-swapfile

The content is very simple. By default my Raspbian has 100MB of swap:

CONF_SWAPSIZE=100

If you want to change the size, you need to modify the number and restart dphys-swapfile:

/etc/init.d/dphys-swapfile stop
/etc/init.d/dphys-swapfile start

Edit: On Raspbian the default location is /var/swap, which is (of course) located on the SD card. I think it is a bad idea, so I would like to point out, that the /etc/dphys-swapfile can have the following option too:

CONF_SWAPFILE=/media/btsync/swapfile

I only problem with it, the usb storage is automounted, so a potential race here (automount vs. swapon)

Source:
– http://raspberrypi.stackexchange.com/questions/70/how-to-set-up-swap-space

Mounting Windows 7 Share on Raspbian Raspberry Pi

Use this command to access windows shared folder with guest user:

sudo mount -t cifs -o guest "//SERVER/share" /mnt/mountfolder

Or use this command for password protected share:

sudo mount -t cifs -o username=user,password=password //SERVER/share /mnt/mountfolder

You can use this command to mount as a user

sudo mount -t cifs -o username=username,password=password,uid=userid,gid=group //SERVER/share /mnt/mountfolder

If mounting a Windows 7 (SMB) share on Linux gives us a “Remote I/O error” like below:

mount error(121): Remote I/O error
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)

Solution #1:

This is Windows problem and following registry values need to be changed:
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management\LargeSystemCache
set it to 1
HKLM\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\Size
set it to 3

Solution #2:

From the man page:
vers = SMB protocol version. Allowed values are:
· 1.0 – The classic CIFS/SMBv1 protocol. This is the default.
· 2.0 – The SMBv2.002 protocol. This was initially introduced in Windows Vista Service Pack 1, and Windows Server 2008. Note that the initial release version of Windows Vista spoke a slightly different dialect (2.000) that is not supported.
· 2.1 – The SMBv2.1 protocol that was introduced in Microsoft Windows 7 and Windows Server 2008R2.
· 3.0 – The SMBv3.0 protocol that was introduced in Microsoft Windows 8 and Windows Server 2012.

Here is the corrected command which now works for me:

sudo mount -t cifs //192.168.x.x/share ~/share -o username=bob,vers=2.1

Source:
– http://wood1978.dyndns.org/~wood/wordpress/2014/12/17/solve-mount-cifs-remote-io-error-after-data-transfer/

VPN PPTP Client on Raspberry Pi

To start, you will need to install pptpclient, this can be achieved by:

sudo apt-get install pptp-linux

Next, Create a file in /etc/ppp/peers with arbitrary name and the following contents:

pty "pptp $VPNHOSTNAME --nolaunchpppd --debug"
name $USERNAME
password $PASSWORD
remotename PPTP
require-mppe-128
require-mschap-v2
refuse-eap
refuse-pap
refuse-chap
refuse-mschap
noauth
debug
persist
maxfail 0
defaultroute
replacedefaultroute
usepeerdns

Where $VPNHOSTNAME is your VPN host name, $PASSWORD is your VPN password and $USERNAME is your VPN username.

After you have done that, you should do:

sudo pon $FILENAME

where $FILENAME is the name of the file you saved earlier. Or you should do this to see debug messages (this command will display logs until connection off):

pon $FILENAME debug dump logfd 2 nodetach

To start your VPN client on boot, you can follow the instructions on http://pptpclient.sourceforge.net/howto-debian.phtml (point 8 or 9, Hand configuration section)

An alternate method to make your VPN client run on boot is to make a script /etc/init.d/pptp containing these contents:

#! /bin/sh

case "$1" in
  start)
sleep 10
pon $/etc/ppp/peers/FILENAME

    echo "PPTP Started"
    ;;
  stop)
    poff $/etc/ppp/peers/FILENAME
    echo "PPTP Stopped"
    ;;
  *)
    echo "Usage: /etc/init.d/pptp {start|stop}"
    exit 1
    ;;
esac

exit 0

Then run:

update-rc.d [filename of script] defaults

To make it run at startup.

Raspberry Pi (or Ubuntu) IP Address Setting

IP Address Settings Static:

# /etc/network/interfaces
iface eth0 inet static
address a.b.c.d
netmask 255.255.255.0
gateway 192.168.0.1

IP Address Settings DHCP:

# /etc/network/interfaces
iface eth0 inet manual

Or

# /etc/network/interfaces
iface eth0 inet dhcp

Starting Ubuntu 18.04 LTS using netplan:
/etc/netplan/01-network-manager-all.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    enp1s0:
      dhcp4: no
      addresses: [192.168.1.222/24]
      # gateway4: 192.168.1.1 --deprecated
routes:
- to: default
via: 192.168.1.1

      nameservers:
        addresses: [8.8.8.8,8.8.4.4]

apply the configuration:
netplan apply

or using NetworkManager

network:
  version: 2
  renderer: NetworkManager

and use command below to set ip:

nmcli con mod "Wired connection 1" ipv4.address 1.2.3.4/24 ipv4.gateway 1.2.3.4 ipv4.dns 1.2.3.4 ipv4.method manual
# write above command in one single line

Nameserver:
a) Ubuntu versions < 14.04

# /etc/resolv.conf
nameserver 8.8.8.8
nameserver 8.8.4.4

b) Ubuntu versions >= 14.04

# /etc/network/interfaces
dns-nameservers 8.8.8.8

Note: For raspbian, when using static ip, you must disable DHCP service in /etc/dhcpcd.conf, add this line:

denyinterfaces eth0

Update 13 Oct 18, raspbian now use dhcp5 so you need to edit/etc/dhcpcd.conf

# Example static IP configuration:
#interface eth0
#static ip_address=192.168.0.10/24
#static ip6_address=fd51:42f8:caae:d92e::ff/64
#static routers=192.168.0.1
#static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1

Raspberry Pi Kiosk Mode

To make Raspberry Pi automatically run specific app (browser) — kiosk mode, first install:

apt-get install x11-xserver-utils
apt-get install xscreensaver

Then edit this file:

/etc/xdg/lxsession/LXDE/autostart (on newer raspbian this is LXDE changed to LXDE-pi)
@lxpanel --profile LXDE

@pcmanfm --desktop --profile LXDE
#@xscreensaver -no-splash
#run midori
#@midori -e Fullscreen
#or run chromium
@chromium --kiosk http://localhost/dashboard
@xset s noblank
@xset s off
@xset -dpms

Notes:
– Check if the file /home/pi/.config/lxsession/LXDE-pi/autostart file exists. If it exists, it will be used instead of /etc/xdg/lxsession/LXDE-pi/autostart
– View logs (after reboot) in /home/pi/.xsession-errors

You can also change this file to disable screen blank when LXDE not running (terminal mode):

/etc/kbd/config
BLANK_TIME=0
BLANK_DPMS=off
POWERDOWN_TIME=0

And this to disable screen blank in LXDE:

/etc/lightdm/lightdm.conf
[SeatDefaults]
xserver-command=X -s 0 -dpms

Source:
– https://www.danpurdy.co.uk/web-development/raspberry-pi-kiosk-screen-tutorial/
– http://raspberrypi.stackexchange.com/questions/42633/raspberry-pi-autostart-of-lxde-does-not-work