Skip to content

Useful (or stuff i've found useful) snippets of code bash/python/etc basically a dumping ground for info that was initially concentrated on the raspberry pi

Notifications You must be signed in to change notification settings

drakxtwo/pi_snippets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 

Repository files navigation

Useful snippets for working with the Raspberry Pi

Table of contents

File

read csv and create an array variable

# 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)

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;

# 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'
}

now call this file as a module in the main code and assign to your chosen variable

import credentials

empassword = credentials.em_login['password']
inpassword = credentials.in_login['password']

print("email password:", empassword)
print("initial password:", inpassword)

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

sed -i 's/\r//' filename

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

#!/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)

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

@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

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;

@chromium-browser --noerrdialogs --kiosk http://127.0.0.1:5000 --incognito --disable-translate

so my file reads;

@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

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;

# bind_to_address "localhost"
bind_to_address "127.0.0.1"
port "6600"

shutdown/reboot your Raspberry PI after it’s up and running again add an Internet radio URL, for instance:

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

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')

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

alias enw='emacs -nw'

to the end of your .profile or .bashrc file located at /home or /home/usr

misc

Display system’s serial support

dmesg | grep tty

resulting output resembles;

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 $

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;

deb-src http://nginx.org/packages/mainline/debian/ wheezy nginx 

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;

curl -O https://nginx.org/keys/nginx_signing.key && apt-key add ./nginx_signing.key

now run

sudo apt-key update
sudo apt-get update

and you should get no error messages meaning you can now run;

apt-get source nginx

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

sudo dpkg -i nginx_1.9.4-1~squeeze_armhf.deb

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;

export VISUAL=nano; crontab -e

alternatively you can edit the ~/.selected.editor file and change to your chosen editor;

# Generated by /usr/bin/select-editor
SELECTED_EDITOR="/usr/bin/nano"

network

Static or fixed IP address

add to /etc/dhcpcd.conf

#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

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:

sudo tail -f /var/mail/<user>

alternatively you can set up a more capable system and allow your Pi to send mail using your google account

sudo apt-get install ssmtp mailutils mpack
sudo nano /etc/ssmtp/ssmtp.conf
# 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

Example python script for using sSMTP

#!/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)

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);

//192.168.1.1/Drive /media/nas_documents cifs credentials=/home/drakx/.nas_credentials,sec=ntlmv2,uid=1000,gid=1000,iocharset=utf8 0 0

Now create a file called .nas_credentials in your home directory

username=YOUR_ROUTER_LOGIN
password=YOUR_ROUTER_LOGIN_PASSWORD

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

alias emacs="emacs -nw"

camera

Disable red LED

Edit /boot/config and add the line

disable_camera_led=1

About

Useful (or stuff i've found useful) snippets of code bash/python/etc basically a dumping ground for info that was initially concentrated on the raspberry pi

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published