Raspberry Pi – camera box

This weekend, I was finally happy that I’d managed to implement a reasonable temperature and humidity project as well as a motion detecting camera for my Raspberry Pi. I decided to invest £19 in a ModMyPi camera box to consolidate them onto my Pi 3. It arrived today, and after an evening’s fun this is the result.

Pi Camera BoxLid open for testing. The DHT22 temperature and humidity sensor is at the front, with the motion sensor and camera mounted in the lid.

Pi Camera Box - installed in the garageIn situ.

I think it looks much better than my original attempt, even if the rather fiddly assembly took a couple of hours (with testing) rather than the 10 minutes claimed by the manufacturer! It also means that as my camera is now mounted the correct way up, I no longer need to rotate the image by 180 degrees in my code …

Update: After I’d installed this in the garage, I started to get a large number of false positives. A change back to my Pi2 made little difference (although the original version I’d put together but without the DHT22 had worked well). Finally, soldering a 10k resistor between the data and ground wires of the PIR detector seems to have resolved the issue of the data pin going high without it sensing movement.

Humidity and temperature monitoring with a Raspberry Pi

Continuing my quest to protect my Caterham 7 using my Raspberry Pi, I bought a bargain box of 37 assorted sensors for £21. One of the devices supplied was a DHT11, capable of monitoring temperature and humidity. A quick internet search led me to discover the very useful pigpio libraries and daemon. Wiring up the DHT11 to the Pi’s GPIO pins was simple – 3 wires, 1 to the 3V3 supply, 1 to a ground pin and the data pin of the device to GPIO pin 17. The author of the pigpio library also provides some example code for the sensor.

This was enough to demonstrate that I could get it to work, but the accuracy of the DHT11 was woeful. It’s advertised as having an accuracy of +/- 2 Celsius, but my experiments suggested that the one I had acquired for the princely sum of 57p had an accuracy of around +/- 5 Celsius. As I also wanted the sensor to work in an unheated garage, another limitation was that the DHT11 is unable to read values below freezing.

Encouraged, I decided to invest a further £6 in a plug compatible DHT22 sensor. This has an operating range of -40 Celsius to 125 Celsius, with an accuracy of +/- 0.5 degrees. Initial tests suggested that it was far more accurate than the DHT11. While the sensor produces a slightly different data stream to the DHT11, it was close enough for the example code to have been written in a way that it would still work.

As part of my experiments, I made a small modification to the example code (testDHT.c) to calculate the dew point from the temperature and relative humidity readings. I’ve also changed the way it outputs data, to write it to a file rather than the screen.

void cbf(DHTXXD_data_t r)
{
   FILE* fp;
   char buff[100];
   time_t now;
   float rdwpt,rtemp,rhum;
   extern float rdewpt_();
 
   if (r.status == 0) {
       now = time (0);
       strftime (buff, 100, "%Y%m%d-%H%M", localtime (&now));
       rtemp=r.temperature;
       rhum=r.humidity;
       rdwpt=rdewpt_(&rtemp,&rhum);
       fp=fopen("readings.txt", "a");
       fprintf(fp,"%s ",buff);
       fprintf(fp,"%.1f ", rtemp);
       fprintf(fp,"%.1f ", rhum);
       fprintf(fp,"%.1f", rdwpt);
       fprintf(fp,"\n");
       fclose(fp);
   }
}

While it would have been simple enough to write a dew point calculation in C, I decided to write it in FORTRAN instead. Hey, the more people who get to love FORTRAN the better. Here’s the function I wrote, which uses the Magnus formula. It’s declared in the C fragment above as an external function that returns a float. Parameters are passed to FORTRAN by address, rather than by value.

C
C     DEWPOINT CALCULATION FROM TEMPERATURE IN CELSIUS - RTEMP
C                               RELATIVE HUMIDITY - RHUM
C     USING THE MAGNUS FORMULA WITH CONSTANTS SUITABLE FOR A
C     TEMPERATURE RANGE OF -45 TO 60 CELSIUS
C
C     AUTHOR: TJH 28-01-2017
C
      REAL FUNCTION RDEWPT(RTEMP,RHUM)
      REAL RH
      RH = (LOG10(RHUM)-2)/0.4343+(17.62*RTEMP)/(243.12+RTEMP)
      RDEWPT = 243.12*RH/(17.62-RH)
      RETURN
      END

Compiling and linking the C and FORTRAN code using gfortran:

gfortran -Wall -pthread -o DHTXXD *.c *.f -lpigpiod_if2

produces an executable that can create a data file. This example was created using the command:

./DHTXXD -g17 -i600

which reads the data from GPIO pin 17 every 10 minutes.

20170128-2009 6.9 72.0 2.2
20170128-2019 6.9 71.7 2.1
20170128-2029 6.8 71.3 2.0
20170128-2039 6.8 71.4 2.0
20170128-2049 6.8 71.7 2.0
20170128-2059 6.8 71.4 2.0
20170128-2109 6.7 71.1 1.8

This provides data in a suitable format to use some relative simple gnuplot commands to create a charts. The first example plots the relative humidity readings over time, with the larger spikes in the relative humidity data correlating to the garage door being opened while it was raining.

set xdata time
set xlabel "Time"
set ylabel "Relative Humidity %"
set timefmt '%Y%m%d-%H%M'
set format x '%H%M'
plot 'readings.txt' using 1:3 title 'Relative Humidity' with linespoints

HumidityThe second example graphs the air temperature reading directly from the DHT22 with the dew point temperature calculated from the Magnus formula.

set xdata time
set xlabel "Time"
set ylabel "Temperature in degrees Celsius"
set timefmt '%Y%m%d-%H%M'
set format x '%H%M'
plot 'readings.txt' using 1:2 title 'Air Temperature' with linespoints, \
     'readings.txt' using 1:4 title 'Dew Point' with linespoints

Air temperature and dew pointI’m going to be keeping an eye on the data, to understand if it might be beneficial to seal the garage door more effectively than at present and invest in a dehumidifier.

Raspberry Pi motion sensitive camera

Other than messing around with a few FORTRAN benchmarks and learning how to code using Python, I haven’t really used my Raspberry Pi computers for very much that’s been practical. However, having bought a Raspberry Pi camera to play with over Christmas, I decided to have a go at building a motion sensitive camera for the garage. It’s cheap and easy to find passive infrared detectors these days, so I acquired three for the princely sum of £5.

The passive infrared detector
PIR detector

The first challenge was working out the function of the three pins in the foreground. A little bit of searching led me to the conclusion that the top pin is the ground, the bottom pin the 5v supply, with the middle being the status pin. If the middle pin goes high, it means that motion has been detected. The sensitivity of the device, and the length of time the status pin stays high for, can be adjusted using the two potentiometers.

I connected the power pins to a couple of the available 5v supply and ground pins on a Raspberry Pi 2. I used physical pin 26 (GPIO pin 7) to connect up to the status pin.

The code

The next challenge was writing some code to detect changes in the status pin and take a photograph when motion is detected. Fortunately, there are plenty of code snippets available that made this task relatively straightforward. The current version of my code is below.

import RPi.GPIO as GPIO
import time
from picamera import PiCamera
 
# Initialise the camera settings
camera=PiCamera()
camera.resolution=(1024,768)
camera.rotation=(180)
camera.meter_mode=('backlit')
 
# Use GPIO pin 7 (physical pin 26) for the PIR detector
GPIO.setmode(GPIO.BCM)
GPIO_PIR=7
GPIO.setup(GPIO_PIR,GPIO.IN)
 
# Variables used to determine when a picture should be taken.
# GPIO pin 7 => high (ts==1) from low (qs==0) 
# triggers the camera.
ts=0
qs=0
 
try:
 
  # Wait until PIR GPIO pin is low (0)  
  print "Waiting ..."
  while GPIO.input(GPIO_PIR)==1:
    ts=0    
  print "... detector is ready"     
 
  # Loop until quit signal
  while 1:
 
    # Read PIR state
    ts=GPIO.input(GPIO_PIR)
    # DEBUG print ts
    if ts==1 and qs==0:
      # Create unique filename with timestamp and set qs high
      t=time.localtime()
      timestamp=time.strftime('%Y%m%d-%H%M%S',t)
      filename=("img" + timestamp + ".jpg")
      camera.capture(filename)
      print "Movement detected - ",filename," created"
      qs=1
    elif ts==0 and qs==1:
      # GPIO pin 7 has returned to low, therefore set qs low
      qs=0
 
    # Wait for a second
    time.sleep(1)      
 
except KeyboardInterrupt:
 
  # Cleanup GPIO
  GPIO.cleanup()
  print "PIR-PiCamera program terminated"
The results

My Raspberry Pi 2 is now set up in the garage with the motion detector and camera. At the moment it’s simply saving the images onto a drive available to my home network, but I’m probably going to experiment with sending email alerts as well.

Raspberry Pi motion sensitive cameraI’m pleased to report my motion sensitive camera has already caught an intruder …

C7 thief!

Benchmarking the Raspberry Pi 3

I was fortunate enough to be able to order a Raspberry Pi 3 on Monday morning from The Pi Hut. This was because I’d taken the day off work as holiday, because I’d been to see the brilliant Adam Hills in Birmingham the evening before. This meant that the email announcing the Pi 3’s arrival was just about the first thing I read that day – and I ordered it immediately. It arrived today, so as is my tradition, the first thing that I’ve done with it is to run the FORTRAN version of the Whetstone Double Precision benchmark, compiled using gfortran.

Before I get to the results, it’s worthwhile noting how simple the Pi 3 was to set up and attach to my wireless network. I still remember my experience with the original Pi some four years ago, where simply getting an operating system onto an SD card took quite a lot of effort, and persuading it to work over WiFi even more. I think I spent a couple of days getting it to work properly – possibly longer – and got to play around in configuration files that I’d forgotten all about in the 25 or so years that had passed since I used to hack Sun UNIX kernels for a living.

Now, four years on, the first startup process is stupidly easy. Along with the Pi 3 (£30) and a case (£13), I’d bought a micro SD card with Raspbian pre-loaded (£6). This automatically configured itself when I powered up the Pi 3 for the first time and once that had finished (around 10 minutes or so) immediately rebooted into the desktop environment. A simple right-click on the WiFi Networks icon on the task bar allowed me to select my network and enter its password.

Installing a FORTRAN compiler was simple – all I had to do was open a terminal window and type:

sudo apt-get install gfortran

I also installed a copy of scrot (for taking screenshots) and gimp, so that I could edit them. A pre-installed version of Libre Office meant that I’d also got a spreadsheet editor with no effort – and no additional cost. Both gimp and Libre Office Calc seem to run exceptionally well on the Pi 3. I did try gimp on the original Pi, but lost the will to live waiting for it to do anything. As for Libre Office, I wouldn’t have dreamt of trying it.

So, to the results. The graph below compares the same benchmark using identical source code on the original Pi B, the Pi 2, Pi Zero and Pi 3. Over 10 runs, it manages an average of just under 400 MIPS – and like the Pi 2, that’s on one of its four cores (as the benchmark isn’t threaded). So at a cost of £30, that works out an amazing 7.5 pence per MIP – or less than 2 pence per MIP if you’re running something that can utilise all four cores simultaneously. The Raspberry Pi Foundation say that the Pi 3 is 10x faster than the original – this benchmark result therefore more than supports their claim.

Whetstone Double Precision Benchmark

All I need to do now is to find something useful to do with it (I have a couple of Internet of Things projects I’m looking at), although if my Windows 10 desktop ever gives up the ghost I think I might be tempted to simply replace it with a Pi 3, attached to my network storage device. The whole of this article has been prepared using the Pi 3, using the pre-installed Epiphany web browser to access my self-hosted WordPress site.

Raspberry Pi 3 - Raspbian Desktop

If my Pi 3 continues to perform reliably, I’m not sure why I’d ever need to buy a traditional Windows desktop again.

Whetstone FORTRAN benchmark on the Raspberry Pi Zero

I eventually succumbed to my gadget cravings over Christmas and spent the princely sum of £4 on a Raspberry Pi Zero(*). This has enabled me to re-run the Whetstone double precision FORTRAN benchmarks that I’d previously tried out on my original model B and the Pi 2 last year.

Pi Zero and a 1964 penny coinMy Pi Zero with a 1964 penny coin for scale …

My configuration is currently headless as the HDMI output appeared to stop working after around 5 minutes use. At less than the price of a sandwich at an average motorway service station, it’s not worth the hassle to send it back for the refund graciously offered by the supplier to my somewhat tetchy tweet … I’ll simply replace it with another one when they’re a little easier to obtain.

Anyway, taking an average of 10 runs over 100,000 loops, the benchmark indicates a performance of 201,159 double Whetstone KIPS (thousands of instructions per second), which puts it squarely between that of the original model B (150,962) and a single core of the quad-core Pi 2 (276,369).

The Raspberry Pi Zero running the FORTRAN Whetstone double precision benchmarkBenchmark output

Hmm. Now what?!

(*) In reality £18.50, including postage and packaging, an 8Gb micro SD card and the Pi Zero essentials kit from The Pi Hut.

Digital psychology: why the paperless tax disc has failed to deliver benefits

I’m a huge fan of online services (or “digital”, to use the term beloved of marketing departments everywhere). I make a substantial part of my living from evangelising, designing and helping to deliver them. But sometimes it makes little economic sense to do digital for the sake of digital, as the government has found out today.

Here’s the then director of Government Digital Services (GDS), writing in December 2014:

… work goes hand-in-hand with deep business transformation. With the abolition of the paper tax disc, the DVLA have gone from being a department that processes millions of pieces of paper each year to becoming one of our digital flagships.

And here’s the apparent result of that transformation, as reported by the BBC today:

The number of vehicles without road tax – Vehicle Excise Duty (VED) – doubled to 560,000 this summer according to the Department for Transport’s survey, months after the paper tax disc was abolished in October 2014.

 

The Department for Transport admits these changes probably caused the increase in untaxed vehicles.

 

The loss in revenue for the government is “significant”, he [David Bizley of the RAC] said, having risen from £35m in 2013 to an estimated £80m now “and, it has to be pointed out, far exceeds the forecast £10m efficiency saving”.

It’s right that online services should be used to save money and improve convenience, but not at the cost of the taxpayer (or consumer). DVLA had been delivering both of these aspects successfully by allowing people to pay for the tax disc online a long time before this particular transformation.

Why did it go wrong? My analysis is that the impact of displaying a paper tax disc was hugely important psychologically – far more so than the digital transformation evangelists ever acknowledged. The removal of the behavioural “nudge” that the act of displaying the tax disc created meant that the DVLA were gambling on the benefits of removing paper outweighing the costs of removing the nudge.

Instead, it would appear that an annually recurring £35m cost to the taxpayer has resulted directly from this transformation (assuming that the efficiency saving referred to in the BBC article was an annual one resulting largely from eliminating printing and postal costs).

The lesson? Always ensure that your transformation initiatives have a robust business case behind them and that they consider the psychological impact of change as a variable that can materially alter your benefits forecast.

I’m not sure what the DVLA will be able to do to resolve this problem now, as I suspect that they can’t simply roll back this change without incurring even greater costs. To me, it underlines that imposing behavioural changes of this kind on your customers can never be viewed as a “Beta”.

I hope that this experience is giving HMRC food for thought as they strive to become a “digital tax titan” over the next few years. For them to be successful, the psychological aspects of digital service design must be front and centre in their thinking, followed closely by a robust business case analysis.

Otherwise, we may all be even poorer at the end of austerity than we thought we were going to be.

The ethical crisis in software development

I’ve been following the news on the scandal surrounding Volkswagen with keen interest. It appears that at the heart of the matter is a piece of software, written for or by Volkswagen, that forms part of their engine management system. As a software professional of more than 30 years standing, it angers me that an apparently reputable organisation (of which I was a customer of for many years) thinks that it is acceptable to misuse code in this way. While there’s clearly a need for those in charge of VW to take responsibility, there is also a need for the individual software professionals involved to examine their conduct. So I’ve been pleased to see that the British Computer Society CEO, Paul Fletcher, has published a blog article on this topic today.

Software is no longer confined to large computers in purpose-built rooms – it’s everywhere

In it, Paul calls for all technologists to work to a strong professional code of conduct. Naturally, the BCS has a code of conduct that it expects its members to conform to. However, in my opinion, it’s not as strongly worded or as visible as it needs to be, particularly when you compare it to those of other professional bodies, such as the British Psychological Society’s code of ethics and conduct. Professional qualifications and membership really mean something in psychology – but despite rising membership numbers and the BCS’s best efforts, the equivalent professional qualifications and membership for software professionals carry a fraction of the weight that they ought to.

Sadly, even if the code of conduct was stronger and more visible, the BCS would need far more clout than it has today to promote it more widely. Even more importantly, a government-backed regulatory framework, to ensure that the BCS can support its members put under undue pressure to act unethically, is absent.

I believe we should be just as interested in ensuring that people who write and implement software are as well-regulated and ethically aware as professional psychologists. After all, unethical behaviour in software development can have potentially devastating effects on the environment, health, wealth … in fact, on any aspect of society touched by software.

Which, as society is becoming increasingly aware, is all of it.

The best thing that could come out of the VW scandal is that we all start to pay far more attention to ensuring that technologists, especially software developers, understand their ethical duty to society and that they have the necessary professional and regulatory backing to be able to stand up to rogue employers.

The continued resurgence of computer science?

There was some encouraging news for the IT industry in the data released by UCAS today. The number of students accepting places on computer science courses at university has increased by 12% compared to last year’s intake.

In graphical form, the change in the relative numbers of students accepted onto computing courses since 2011 looks like this:

Computer Sciences 2011-2015The figures look encouraging if you only consider the intake over these five years. However, given that there was a decline of 23.3% in the number of computer sciences undergraduates between 2002 and 2012, it’s clear that there’s still a way to go before demand for these skills outstrips supply in what is a growing industry in the UK. Skill shortages are also increased by the limits the current government is imposing on granting visas to non-EU foreign nationals, despite the negative consequences for the economy this policy has.

1 2 3 9