This is basically a dumping ground for various things i’ve found useful (emacs, mpd, raspberry & so on)
This for now covers all sorts in a relatively uncurated manner such things as turning the red light off on the pi camera, kiosk mode for Chrome and emacs tips. Plus in updating this for the new site i’ve learnt more about how ox-hugo functions [fn:0]
Everything here can be downloaded in the original org-mode format.
So the following tries to open a csv file and store in the array SensorValues
the contents of each row, if it fails to open the file it simply stores an arbitrary value
# 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)
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)
sed -i 's/\r//' filename
Use with care it could destroy your file, it might be better to the dos2unix utility
Notes on using the offical pi touch screen
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
You can install an onscreen keyboard by;
sudo apt-get install florence
or
sudo apt-get install matchbox-keyboard
then reboot, to access the keyboard click on menu/accessories/keyboard
Weirdly (at least i think so) 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 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
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
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:
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.
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')
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
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 $
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
Below MOTD was found somewhere on the net, when i find from where i’ll add a link (its only here in this file so i remember)
To get something like this every time you log in;
You need a custom message of the day script, copy the code and place it in /home/pi/.bash_profile
let upSeconds="$(/usr/bin/cut -d. -f1 /proc/uptime)"
let secs=$((${upSeconds}%60))
let mins=$((${upSeconds}/60%60))
let hours=$((${upSeconds}/3600%24))
let days=$((${upSeconds}/86400))
UPTIME=`printf "%d days, %02dh%02dm%02ds" "$days" "$hours" "$mins" "$secs"`
# get the load averages
read one five fifteen rest < /proc/loadavg
echo "$(tput setaf 2)
.~~. .~~. `date +"%A, %e %B %Y, %r"`
'. \ ' ' / .' `uname -srmo`$(tput setaf 1)
.~ .~~~..~.
: .~.'~'.~. : Uptime.............: ${UPTIME}
~ ( ) ( ) ~ Memory.............: `cat /proc/meminfo | grep MemFree | awk {'print $2'}`kB (Free) / `cat /proc/meminfo | grep MemTotal | awk {'print $2'}`kB (Total)
( : '~'.~.'~' : ) Load Averages......: ${one}, ${five}, ${fifteen} (1, 5, 15 min)
~ .~ ( ) ~. ~ Running Processes..: `ps ax | wc -l | tr -d " "`
( : '~' : ) IP Addresses.......: `/sbin/ifconfig eth0 | /bin/grep "inet addr" | /usr/bin/cut -d ":" -f 2 | /usr/bin/cut -d " " -f 1` and `wget -q -O - http://icanhazip.com/ | tail`
'~ .~~~. ~'
'~'
$(tput sgr0)"
$(tput sgr0)”
create a script with the following and make it executable
#!/bin/bash
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
(
syspath="${sysdevpath%/dev}"
devname="$(udevadm info -q name -p $syspath)"
[[ "$devname" == "bus/"* ]] && continue
eval "$(udevadm info -q property --export -p $syspath)"
[[ -z "$ID_SERIAL" ]] && continue
echo "/dev/$devname - $ID_SERIAL"
)
done
taken from: https://unix.stackexchange.com/questions/144029/command-to-determine-ports-of-a-device-like-dev-ttyusb0
using mariadb as the main sql server
logon to sql server: mysql -u root -p
create database: CREATE DATABASE home;
now we need at least one table.
CREATE TABLE temps (recnum int NOT NULL AUTO_INCREMENT, recdate DATE DEFAULT NULL, rectime time DEFAULT NULL, external float DEFAULT NULL, frontroom float DEFAULT NULL, bedroom float DEFAULT NULL, kitchen float DEFAULT NULL,PRIMARY KEY( recnum ));
now we can see what we’ve created with;
show columns in temps
+-----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+----------------+
| recnum | int(11) | NO | PRI | NULL | auto_increment |
| recdate | date | YES | | NULL | |
| rectime | time | YES | | NULL | |
| external | float | YES | | NULL | |
| frontroom | float | YES | | NULL | |
| bedroom | float | YES | | NULL | |
| kitchen | float | YES | | NULL | |
+-----------+---------+------+-----+---------+----------------+
7 rows in set (0.04 sec)
now to leave the sql server simply type exit;
when you want python to access the serial port it will if not run as root error out with permission denied. To get round this add your user to the dialout group eg
sudo adduser <username> dialout
then logout and log back in, you should now be able to run your python script with using root. To check what groups your user is a member of simply type groups
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 To enable it, 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 your_run_command
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
before removing the flashed sd card
create a blank file called ssh
then create a file called wpa_supplicant.conf with the following;
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 country=gb
network={ ssid=”your_SSID” psk=”your_password” key_mgmt=WPA-PSK }
sudo iwlist wlan0 scan
Open the wpa-supplicant configuration file in nano:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Go to the bottom of the file and add the following:
country=gb
update_config=1
ctrl_interface=/var/run/wpa_supplicant
network={
scan_ssid=1
ssid="testing"
psk="testingPassword"
}
Then re-configure the interface by wpa_cli -i wlan0 reconfigure
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
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
sudo apt-get install ssmtp sudo apt-get install mailutils
root=postmaster mailhub=smtp.gmail.com:587 hostname=raspberrypi [email protected] AuthPass=TheGmailPassword FromLineOverride=YES UseSTARTTLS=YES
#!/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)
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
One trick i didnt learn until far too late was sshfs essentially it mounts your remote system as a folder on your dekstop 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.
Assuming you have sshfs installed then the line of code you need is really simple;
sshfs [email protected]:/home/pi home/user/folder/
Little code that could be used as heartbeat for a remote headless device
First sudo pip install ping
import ping, socket
try:
ping.verbose_ping('www.google.com', count=3)
except socket.error, e:
print "Ping Error:", e
when a region is highlighted pressing M-;
will comment out the section so much easier than moving line by line
Pressing C-c C-x -
starts a list in emacs with timer, now pressing M-RET will allow notes
- 0:00:00 :: Now I can start taking some notes. - 0:00:02 :: If I hit ~M-RET~, a new list item is created. - 0:00:06 :: And so on
As emacs is always open and i often do cycle time analysis i find this a nice easy way of timing and making notes instead of sitting with the phone in hand hoping no one calls.
OK not a shortcut but really useful for me once toggled it displays links in their unshortened form which means if a link has a %20
in it then the emacs search & replace M-shift-%
will work (/for some reason files i’ve transferred from machine to machine all had link spaces replaced with %20
so this function became very useful
Quite useful pressing M-x
and then typing occur
brings up a search function
which then searches your file with regexp & opens a second buffer with all matches
kept here just for future reference took me a long time to get this working for a lot of reasons, once configured don’t use ctrl c-e P
to get to publish going you using the below example use alt-x org-publish ret org ret
(setq org-publish-project-alist
'(
("org-notes"
:base-directory "C:/Users/drakx/Documents/org_files"
:base-extension "org"
:publishing-directory "C:/Users/drakx/Documents/org_publish"
:recursive -t
:publishing-function org-html-publish-to-html
:headline-levels 4
:auto-preamble t
:auto-sitemap t ; Generate sitemap.org automagically...
:sitemap-filename "sitemap.org" ; ... call it sitemap.org (it's the default)...
:sitemap-title "Sitemap" ; ... with title 'Sitemap'.
:language "en"
:section-numbers nil
:with-toc t
:html-preamble t
)
("org-static"
:base-directory "C:/Users/drakx/Documents/org_files/files/"
:base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf"
:publishing-directory "C:/Users/drakx/Documents/org_publish/files/"
:recursive t
:publishing-function org-publish-attachment
)
("org" :components ("org-notes" "org-static"))
)
)
Edit /boot/config and add the line
disable_camera_led=1
[fn:0] a single page is the preferred ox-hugo way and now i understand why. Every header in the above file has tags which makes pulling all relevant info together easy as shown by the posts section but in a single file these tags and categories per heading are irrelevant/ignored when published via ox-hugo. It is the document level tags defined in the header of the file by #+HUGO_TAGS: linux raspberry orgmode
that are used. So that when you click on for example orgmode
in your site tag list it will not pull up the individual entries from this page with the tag orgmode but this whole document will be shown eg so i’ve removed the document level tags as i feel its not ”nice” and you can still download the orgfile itself anyway.