Jim's Jottings

Thursday, 24 May 2018

I recently bought my first Raspberry Pi Zero W, a tiny little computer just 3 inches long and 1½ inches wide (in its case). I intend eventually to get it working with pywws (some weather station software I wrote a while ago) and I'll be adding to this blog post as I go along, to keep it all in one place.

Getting started

As I'll be using the Pi in "headless" mode (no monitor or keyboard in normal operation) I decided to start as I mean to go on. This also means I don't yet need adapters for the mini HDMI and OTG USB ports on the Pi Zero.

I downloaded and copied the current version of Raspbian, called "stretch", and copied it to a 16 GB micro SD card. An 8 GB card would have been sufficient but the larger card only cost a few pence extra. Note that this is the full version of Raspbian (i.e. not "lite") and is not the NOOBS version.

Booting the Pi directly into headless mode turned out to be surprisingly easy. The instructions in this forum post were all I needed, and worked perfectly despite the negative comments on the post. I inserted the micro SD card, in an adapter, into my openSUSE Linux PC and used the "open with file manager" prompt to mount both partitions. The df command showed them to be mounted as /run/media/jim/boot and /run/media/jim/rootfs.

Having changed directory to the /run/media/jim/boot partition all I had to do was create an empty file called ssh (with the command sudo touch ssh) then use a text editor (run via sudo) to create a file wpa_supplicant.conf with the following contents:

After safely unmounting the SD card partitions I removed it from the PC, put it in the Pi, and turned the power on. About a minute later the hostname raspberrypi showed up in the DHCP server's list of hosts and I was able to ssh into it (the initial password is raspberry):

jim@brains:~$ ssh pi@raspberrypi
The authenticity of host 'raspberrypi (192.168.1.176)' can't be established.
ECDSA key fingerprint is SHA256:bw10eHPZ3debE04S+5a8xEYSDHT9AmZk8MJWxfNTapQ.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'raspberrypi,192.168.1.176' (ECDSA) to the list of known hosts.
pi@raspberrypi's password:
Linux raspberrypi 4.14.34+ #1110 Mon Apr 16 14:51:42 BST 2018 armv6l
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Apr 18 01:25:27 2018
SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.
pi@raspberrypi:~ $ passwd
Changing password for pi.
(current) UNIX password:
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
pi@raspberrypi:~ $

Creating a user

I could continue to use the default pi user name, but I find it more convenient to have the same user name on all my computers. As I'll be using NFS to share storage with some of those computers it's also useful if the user has the same UID number:

The final visudo command is to add the new user to the list of users permitted to do things as root.

Logging in without a password

It may sound strange but this is a way to improve security when using ssh. Passwords can perhaps be guessed, but a 2048 bit RSA key is harder to crack. I already have a key, so I just needed to copy the public key to my new account on the Pi:

jim@brains:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub raspberrypi
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/jim/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
jim@raspberrypi's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'raspberrypi'"
and check to make sure that only the key(s) you wanted were added.
jim@brains:~$ ssh raspberrypi
Linux raspberrypi 4.14.34+ #1110 Mon Apr 16 14:51:42 BST 2018 armv6l
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Fri May 25 07:13:41 2018 from 192.168.1.162
jim@raspberrypi:~ $

No password was needed to login to the Pi, as expected.

The next step is to disable ssh with passwords entirely. Edit the sshd_config file and restart the ssh daemon:

Completing initial setup

I used sudo raspi-config to set the host name (under network options), set the locale (en_GB.UTF-8) and timezone (Europe/London) under localisation options, and set it to boot to console rather than desktop (under boot options).

To minimise SD card "wear" I moved some directories to RAM by adding the following to /etc/fstab and then rebooting:

I added a few of my favourite command aliases by creating a file /etc/profile.d/aliases.sh containing the following:

alias df='df -h'
alias la='ls -ao'
alias ll='ls -l'

To save a small amount of power consumption I turned off video output by adding the following to /etc/rc.local, before the exit 0 line:

# turn off video output signal
/usr/bin/tvservice -off

File sharing with NFS

It's very convenient to be able to copy files from one machine to another with NFS. The autofs service can be used to mount NFS shares when needed. I started by exporting the Pi's /home file system so it can be mounted on other machines. This requires installing the NFS server package:

Scheduling backup

Experience has taught me the value of backing up computers. The Raspberry Pi is no exception, and as I have a NAS that's always on backing up couldn't be easier. I created a "scripts" directory and added two files. /home/jim/scripts/backup_to_zero-x.sh does the actual backup:

The install process was hanging whilst fetching dependencies from https://pypi.python.org/ until I disabled IPv6 entirely by adding net.ipv6.conf.all.disable_ipv6 = 1 to /etc/sysctl.conf and rebooting.

Connecting the weather station

Given the limitations of the "OTG" USB port on the Pi Zero I was pleasantly surprised that when I connected my test weather station console (an old one whose outside sensors have failed) via a standard USB A to micro USB lead (as used to connect a phone to a computer) it powered up normally. It also works with pywws:

As soon as I tried "live" logging, with the command pywws-testweatherstation -l -vv, I got USB timeout errors as reported by other pywws users on the pywws mail list.

A bit of experimenting with longer timeouts and measuring times to read data from the station showed the problem only occurs after pywws has paused USB activity for 5 seconds or more. I had a hunch the the Pi might be switching USB modes during this apparent inactivity, so I added the line dtoverlay=dwc2,dr_mode=host to /boot/config.txt to fix the USB port in "host" mode. This appears to have cured the timeout problem.

Final steps

To finish off I installed all the pywws dependencies I need, then copied the pywws data directory from the Pi 2 I was using. That machine has a disc drive so I haven't had to worry about SD card wear. I made a few changes to weather.ini to minimise writes to the card:

I'm also using /tmp/pywws for the logfile. This avoids writing to the SD card, but does mean that the log is cleared every time the Pi is rebooted. I don't expect to have to do this too often.

The Raspberry Pi Zero is so small I'm tempted to stick it on the back of the weather station console, with a short USB lead (with right angle plugs?) to make a neat installation. However I suspect the wi-fi may interfere with the station's reception of data from the outside sensors.

There has been some talk recently about apparent bias in the way Oxford and Cambridge universities select undergraduate students, disproportionately favouring (white) students from fee-paying schools. The universities respond, with some justification, that they accept anyone who gets top grades at A level and performs well at interview, and it's not their fault if state schools are failing to educate pupils to the required standard. (This is a gross oversimplification of the argument, but you get the idea.)

When I went to Cambridge (in the late 1970s) about half the undergraduates were, like me, from state schools. The good news is that this has improved to around 62%. The bad news is that this is still not enough. To be at the forefront of academic research the leading universities need to attract the best minds from whatever background. It is unlikely that they're to be found in such high proportions in fee-paying schools, which only educate about 6.5% of Britain's children.

The big question is how to remedy this imbalance. Almost any proposed change to the admission system still allows fee-paying schools to gain an advantage. Their higher number of teachers per pupil means they can provide intensive coaching that helps students get better A level results than they would in a state school. The supreme self-confidence they instil in many pupils is a great advantage at interview. The extra-curricular activities, from music to sport to mountain climbing, look good on the UCAS form and are in decline in many cash-strapped state schools. I think it's time for something more radical.

There are roughly 4 million people aged 15-19 in the UK at present, so there are about 800,000 reaching university age every year. Oxford University has around 3,200 undergraduate places each year, so one might expect one in 250 people to go to Oxford, or say 1 in 300 to allow a margin for foreign students. A 1000 pupil secondary school has around 150 students in each year, so might expect someone to get into Oxford every two years or so.

I propose that Oxford and/or Cambridge should allocate undergraduate places to schools based purely on the number of pupils aged 11-16 years at that school. For most schools the allocation would be a number less than one. The school would "bank" each year's allocation until they have more than 0.5 places at one university in the bank, and have a pupil who the school believes would benefit from going to that university. The bank account would then be debited one place, taking the balance below zero.

It would be up to the school to choose which pupil(s) to send, using any criteria they think appropriate. I'm sure most would take this responsibility seriously, but if St. Cakes (motto Quis paget entrat) decides to send the boy whose parents make the largest donation to the headmaster's drinks cabinet that's their business. I'm sure they'd welcome the ensuing publicity.

The side effects of this policy might be interesting. Would parents move to be near a "failing" school that has lots of places in the bank but no pupils capable of benefiting from a university education? The influx of children from homes with high expectations might do a lot to revive such "failing" schools. Would Eton form a multi-academy trust with lots of inner-city schools to boost its pupil numbers and so be allocated enough places for its fee-paying pupils? Probably, so the allocation rules will have to define a "school" as occupying buildings in close proximity.

Footnote: the state school I went to had 180 pupils in each year. I was the only one of my year group to go to Cambridge, and another of my year got in to Oxford. This was better than average for a state school and reflects the privileged upbringing of many of its pupils. I was lucky enough to be raised in a home full of books, where it was expected that I and my three siblings would go to university, with parents who willingly paid our maintenance costs, and who moved house to be in the catchment area of a good school. I benefited hugely from all these advantages, and it's wrong that people without such advantages are denied the benefits.

Saturday, 3 February 2018

Long focal length lenses for SLR cameras are mostly one of two types - refractors (true lenses) or "mirror" lenses (actually Cassegrain telescopes). Large aperture long lenses are big, heavy, and expensive, but they can perform really well. Mirror lenses are much smaller, lighter, and usually a lot cheaper. However they are fixed aperture, usually manual focus, and have notable optical limitations.

Three years ago I bought a Samyang 500mm f/6.3 mirror lens. (What appears to be the same lens is sold under several other brand names. Mine is clearly labelled "made in Korea" though.) It was sold as "ex demo" but I can't see any sign it wasn't brand new. It comes with a screw-on lens hood not shown in the above photo. The lens has a T-thread (M42x0.75) mount, so I've used a T-thread to M42x1 adapter and an M42x1 to EOS adapter (which I already had) to attach it to my Canon 100D. The EOS adapter has a "focus chip" - this tells the camera it's got a Canon 50mm f/1.8 lens attached and allows the focus confirm system to work on well lit subjects. f/6.3 is about the smallest aperture the EOS 100D's focus system can cope with.

In another blog post I described how I've mounted the camera plus lens on a shoulder brace. I've recently been having some fun using this set up to photograph some birds in my garden. All the following pictures are taken through a double glazed window, which is sub optimal, so better pictures should be possible.

This first picture is of a fieldfare (Turdus pilaris). This unprocessed picture is a bit dull, and if you zoom in (click on the picture to see it on Flickr, then click on it again to zoom in) you can see it's really not very sharp.

I'm no expert in processing photographs but I thought I'd have a go using GIMP, an open source programme available on many operating systems. All I've done here is to boost the contrast a little, adjust the brightness, and use an "unsharp mask" filter to make it a bit less soft. I think it's a significant improvement.

On this more recent picture (a green woodpecker (Picus viridis)) I actually managed to get the bird in focus. I've done similar processing and am pleased with the glint in the eye and the detail in the feathers.

This picture also shows one of the optical defects of mirror lenses, the doughnut shapes formed around out of focus highlights. The ethereal ring shape in front of the bird's shoulder is caused by a berry on a plant stem in the foreground. Some people find this sort of aberration very annoying. I don't think I mind it too much.

The most severe problem with this particular lens design is the difficulty in focusing. Its focal distance goes from 2m to infinity (and beyond) in a quarter turn of the lens body. If they'd only made this nearer a full turn it would be much easier to use. Some people have remedied this by attaching long levers or gearing systems. I've yet to try anything like this.