Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
#+TITLE: Useful snippets for working with the Raspberry Pi
#+AUTHOR: drakxtwo
#+EMAIL: lhalls72@gmail.com
#+DESCRIPTION: General information on setting up a RaspberryPi, snippets of code for working in python or bash. Mostly things i've learned for my own projects but a lot will be useful for most people starting their journey with the Pi.
#+KEYWORDS: bash python raspberrypi
#+AUTHOR: Lee Halls
#+MAILTO: lhalls72@gmail.com
#+KEYWORDS: raspberrypi notes snippets code bash python
#+LANGUAGE: en
#+DATE: 2017-02-14
* Table of contents :TOC:
- [[#file][File]]
- [[#read-csv-and-create-an-array-variable][read csv and create an array variable]]
- [[#credential-file][credential file]]
- [[#convert-windows-cr-carriage-returns-to-linux][convert windows CR (carriage returns) to linux]]
- [[#pidisplay][PiDisplay]]
- [[#rotate-official-display][rotate official display]]
- [[#get-backlight-status][get backlight status]]
- [[#kiosk-mode][kiosk mode]]
- [[#audio][Audio]]
- [[#enabling-audio-output][Enabling Audio Output]]
- [[#how-to-play-internet-radio-on-your-raspberry-pi-mpdmpc][How to play Internet radio on your Raspberry PI (mpd/mpc)]]
- [[#github][github]]
- [[#bash][bash]]
- [[#alias][alias]]
- [[#misc][misc]]
- [[#display-systems-serial-support][Display system's serial support]]
- [[#nginx-gpg-key][nginx gpg key]]
- [[#crontab][crontab]]
- [[#cron-sleep][cron sleep]]
- [[#specifying-the-crontab-editor][specifying the crontab editor]]
- [[#network][network]]
- [[#static-or-fixed-ip-address][Static or fixed IP address]]
- [[#mail][mail]]
- [[#access-a-network-drive][access a network drive]]
- [[#ssh][ssh]]
- [[#emacs][emacs]]
- [[#camera][camera]]
- [[#disable-red-led][Disable red LED]]
* File
** read csv and create an array variable
#+BEGIN_SRC python
# read in stored values & initialise preset values
import csv
try:
with open('/tmp/tmpvalues.csv', 'r') as csvfile:
fileRead = csv.reader(csvfile, delimiter=',')
for row in fileRead:
SensorValues = [float(x) for x in row if x != '']
except:
SensorValues = (
66.00,
66.00,
66.00,
66.00,
66.00,
66.00,
66.00,
66.00,
66.00,
66.00)
#+END_SRC
** credential file
If you dont want to store your login details in a python script, for example if you want to share that script you dont want to give people you password do you? So you can create a file credentials file eg
~emacs -nw credentials.py~
Then type in the following;
#+BEGIN_SRC python
# email login details
em_login = {
'smtpdata': 'smtp.server.address',
'login': 'login.address',
'password': 'login.password',
'toemail': 'send.to.email.address'
}
# initial state details
in_login = {
'login': 'login.address',
'password': 'login.password',
'accesskey': 'access.key',
'bucketname': 'monitorhome',
'bucketkey': 'bucket.key'
}
#+END_SRC
now call this file as a module in the main code and assign to your chosen variable
#+BEGIN_SRC python
import credentials
empassword = credentials.em_login['password']
inpassword = credentials.in_login['password']
print("email password:", empassword)
print("initial password:", inpassword)
#+END_SRC
and finally a directory;
~/media/nas_documents~
Reboot and you should be good to go however if you still have problems (my pi would not wait for the wireless network so the drive failed to mount) perhaps consider adding the mount -a command to your crontab (see crontab and cron sleep)
** convert windows CR (carriage returns) to linux
#+BEGIN_SRC bash
sed -i 's/\r//' filename
#+END_SRC
Use with care it could destroy your file, it might be better to the dos2unix utility but the sed command worked for me
* PiDisplay
Notes on using the offical pi touch screen
** rotate official display
Useful if the display case you've bought has a fixed orientation;
edit your ~/boot/config.txt~ file, and add the following line:
~lcd_rotate=2~
** get backlight status
Weirdly (/at least for me 1 has always been on/) it's *0* for on and *1* for off and script must be run as root
#+BEGIN_SRC python
#!/usr/bin/env python3
# gets current backlight state
file=open('/sys/devices/platform/rpi_backlight/backlight/rpi_backlight/bl_power','r+')
current_status=int(file.read(1))
print(current_status)
#+END_SRC
** kiosk mode
Kiosk mode runs your internet browser full screen useful for setting up a display system. Modify the */etc/xdg/lxsession/LXDE/autostart* file or if using the NOOBS installation */etc/xdg/lxsession/LXDE-pi/autostart*
#+BEGIN_SRC bash
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@point-rpi
@xset s noblank
@xset s off
@xset -dpms
@chromium-browser --noerrdialogs --kiosk http://127.0.0.1:5000 --incognito --disable-translate
#+END_SRC
*BE AWARE* if the following file exists then it overrides any other files so the @chromium command must go in here.
~nano .config/lxsession/LXDE-pi/autostart~
so edit it to show the line;
#+BEGIN_SRC bash
@chromium-browser --noerrdialogs --kiosk http://127.0.0.1:5000 --incognito --disable-translate
#+END_SRC
so my file reads;
#+BEGIN_SRC bash
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@point-rpi
@chromium-browser --noerrdialogs --kiosk http://127.0.0.1:5000 --incognito --disable-translate
#+END_SRC
* Audio
** Enabling Audio Output
By default, the RPi audio output is set to automatically select the digital HDMI interface if its being used, otherwise the analog audio output. You can force it to use a specific interface via the sound mixer controls. amixer allows command-line control of the mixer for the ALSA driver.
You can force the RPi to use a specific interface using the command amixer cset numid=3 N where the N parameter means the following:
- 0=auto
- 1=analog
- 2=hdmi
Therefore, to force the Raspberry Pi to use the analog output:
~amixer cset numid=3 1~
** How to play Internet radio on your Raspberry PI (mpd/mpc)
first make sure your audio is working, using the ALSA package
install the mpd and mpc packages:
~sudo apt-get install mpd mpc~
change the permissions of mpd (just to make sure):
~sudo service mpd stop~
~sudo chmod -R g+w /var/lib/mpd~
~sudo chmod -R g+w /var/run/mpd~
make a change to the mpd config file /etc/mpd.conf
~sudo nano /etc/mpd.conf~
Note all the sites i visited suggested that it was simply commenting OUT the line:
~bind_to_address "localhost"~
however i had many issues and nothing seemed to work until i had the conf file set as follows;
#+BEGIN_SRC shell
# bind_to_address "localhost"
bind_to_address "127.0.0.1"
port "6600"
#+END_SRC
shutdown/reboot your Raspberry PI after it's up and running again add an Internet radio URL, for instance:
- mpc add http://icecast.omroep.nl/3fm-bb-mp3
to start playing the stream type:
- mpc play
The other thing to be aware of is you may need to download the radio stations m3u playlist file and open it to extract the direct URL of the stream (/i had to/) so the stream i needed was;
http://stream4.nadaje.com:11986/prs
then save that into MY OWN m3u playlist for for the MPD daemon to work.
*** python check if mpc is playing
#+NAME:check if mpc is running with python
#+BEGIN_SRC python
from mpd import MPDClient
client=MPDClient()
client.idletimeout = None # timeout for fetching the result of the idle command is handled seperately, default: None
client.connect("localhost", 6600) # connect to localhost:6600
print(client.mpd_version)
if client.status()['state'] in ('play', 'pause'):
print('Playing')
else:
print('stopped')
#+END_SRC
* github
no data - waiting update
* bash
** alias
for example if you get tired of typing emacs -nw to start emacs without a gui window then add
#+BEGIN_SRC bash
alias enw='emacs -nw'
#+END_SRC
to the end of your .profile or .bashrc file located at /home or /home/usr
* misc
** Display system's serial support
#+BEGIN_SRC bash
dmesg | grep tty
#+END_SRC
resulting output resembles;
#+BEGIN_SRC bash
pi@raspberrypi:~/xbee $ dmesg | grep tty
[ 0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=656 bcm2708_fb.fbheight=416 bcm2708.boardrev=0x3 bcm2708.serial=0xf83a1e37 smsc95xx.macaddr=B8:27:EB:3A:1E:37 bcm2708_fb.fbswap=1 bcm2708.uart_clock=48000000 vc_mem.mem_base=0xec00000 vc_mem.mem_size=0x10000000 dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
[ 0.000718] console [tty1] enabled
[ 0.201139] 20201000.uart: ttyAMA0 at MMIO 0x20201000 (irq = 81, base_baud = 0) is a PL011 rev2
[ 0.201529] console [ttyAMA0] enabled
[ 2.333606] systemd[1]: Expecting device dev-ttyAMA0.device...
[ 2.353451] systemd[1]: Starting system-serial\x2dgetty.slice.
[ 2.354181] systemd[1]: Created slice system-serial\x2dgetty.slice.
[ 9.556243] usb 1-1.2: FTDI USB Serial Device converter now attached to ttyUSB0
pi@raspberrypi:~/xbee $
#+END_SRC
** nginx gpg key
The distributed version of nginx is old, if you want to install the newer versions you have to self compile but first you need to take a couple of steps;
create a file;
- sudo nano /etc/apt/sources.list.d/nginx.list
and add the line;
#+BEGIN_SRC bash
deb-src http://nginx.org/packages/mainline/debian/ wheezy nginx
#+END_SRC
then run apt-get update and you will receive an error stating the signatures cannot be verified, to overcome this you need the nginx keys which you can get via;
#+BEGIN_SRC bash
curl -O https://nginx.org/keys/nginx_signing.key && apt-key add ./nginx_signing.key
#+END_SRC
now run
#+BEGIN_SRC bash
sudo apt-key update
sudo apt-get update
#+END_SRC
and you should get no error messages meaning you can now run;
#+BEGIN_SRC bash
apt-get source nginx
#+END_SRC
to finally get the latest nginx source for building on your pi (/set aside quite some time for it to build/) and to install use
#+BEGIN_SRC bash
sudo dpkg -i nginx_1.9.4-1~squeeze_armhf.deb
#+END_SRC
* crontab
The & at the end of the line makes the script run in the background whilst the pi carries on booting
@reboot sudo python /home/pi/homeApp/ourhome.py &
By default, the logging for the cron daemon is not enabled in Debian (I assume it is the system you are using). To enable it, please open the file /etc/rsyslog.conf via
~sudo nano /etc/rsyslog.conf~
and uncomment the line
~cron.* /var/log/cron.log~
to run a command on boot i use the following in the crontab file
@reboot run_command
** cron sleep
Starting a command on reboot after x seconds
For some reason my pi would not wait for the wifi network to come online (wait for network on boot is enabled) so my NAS wont connect, to be honest i had other things to do than fault find so as i added an @reboot mount command with a sleep period to ensure the drive was accessible;
~@reboot sleep 10;sudo mount -a~
** specifying the crontab editor
if like me you've accidentally set the wrong editor when first using crontab you can specify the editor everytime using the following;
#+BEGIN_SRC bash
export VISUAL=nano; crontab -e
#+END_SRC
alternatively you can edit the ~/.selected.editor file and change to your chosen editor;
#+BEGIN_SRC bash
# Generated by /usr/bin/select-editor
SELECTED_EDITOR="/usr/bin/nano"
#+END_SRC
* network
** Static or fixed IP address
add to /etc/dhcpcd.conf
#+BEGIN_SRC bash
#Custom static IP address
interface eth0
static ip_address=192.168.1.04/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1
#+END_SRC
changing eth0 to wlan0 depending on connection type and also remember to use your own choice of IP address/subnet etc
** mail
Had problems with a cron job and no idea why & at the time had not setup logging and without an MTA [Mail Transport Assistant] i could not see what errors were being given, after reading up i installed postfix
sudo apt install postfix
Choosing "LOCAL" during setup, then after a reboot i could use the following to find out what went wrong:
#+BEGIN_SRC shell
sudo tail -f /var/mail/<user>
#+END_SRC
alternatively you can set up a more capable system and allow your Pi to send mail using your google account
#+BEGIN_SRC shell
sudo apt-get install ssmtp mailutils mpack
sudo nano /etc/ssmtp/ssmtp.conf
#+END_SRC
#+BEGIN_SRC shell
# Config file for sSMTP sendmail
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
# root=localhost
root=your_chosen_address
# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
mailhub=smtp.gmail.com:587
# Where will the mail seem to come from?
# rewriteDomain=gmail.com
# The full hostname
hostname=yourHOSTNAME
# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
UseSTARTTLS=YES
AuthUser=yourEMAIL
AuthPass=either password or two-factor key
#+END_SRC
*** Example python script for using sSMTP
#+BEGIN_SRC python
#!/usr/bin/python
# Taken and modified from an example here:
# http://tombuntu.com/index.php/2008/10/24/server-monitoring-with-python-and-ssmtp/
import subprocess, re, time
import urllib, urllib2
# email address to use in the to field
recipient = 'towhomit@mayconcern'
# email address to use in the from field
sender = 'fromwhom@sentit'
# template for the email message
# first '%s' is recipient, second is sender, third is content
message = '''To: %s
From: %s
Subject: rpiELEC IP: %s
'''
def send_mail(content):
try:
ssmtp=subprocess.Popen(('/usr/sbin/ssmtp', recipient), stdin=subprocess.PIPE)
except OSError:
print('Error sending mail')
# pass mail to sSMTP client
ssmtp.communicate(message % (recipient, sender,content))
ssmtp.wait()
data = re.search('"([0-9.]*)"', urllib.urlopen("http://ip.jsontest.com/").read()).group(1)
send_mail(data)
#+END_SRC
** access a network drive
Edit your /etc/fstab file and add the following line (changing the ip address and location of the credentials file to suit your setup);
#+BEGIN_SRC bash
//192.168.1.1/Drive /media/nas_documents cifs credentials=/home/drakx/.nas_credentials,sec=ntlmv2,uid=1000,gid=1000,iocharset=utf8 0 0
#+END_SRC
Now create a file called .nas_credentials in your home directory
#+BEGIN_SRC bash
username=YOUR_ROUTER_LOGIN
password=YOUR_ROUTER_LOGIN_PASSWORD
#+END_SRC
** ssh
One trick i didnt learn until well into my pi day was *sshfs* this especially on a linux based system is brilliant, essentially it mounts your remote system as a folder on your desktop/laptop so instead of saving files then transfering etc you can open and load the remote files and work on them as if local. Excellent for tweaking your python website.
The line of code you need is really simple;
~sshfs pi@192.168.1.69:/home/pi home/leehalls/pi_elec/~
* emacs
Most of my pi's run headless and when editing i like to use emacs so i need to type emacs -nw filename all the time. Creating an alias eases the typing
#+BEGIN_SRC bash
alias emacs="emacs -nw"
#+END_SRC
* camera
** Disable red LED
Edit /boot/config and add the line
#+BEGIN_SRC bash
disable_camera_led=1
#+END_SRC