Infrared Image IMG_5758 img_1773 img_15730 IMG_5617 img_15684 Hursley Fireworks img_18199 img_14084 img_15652 img_15822 Hursley Model Railway img_10880 img_18146 Hursley Model Railway img_15862 Hursley Model Railway img_17021 img_19575 img_1630

@antonpiatek on twitter

Posting tweet...

Powered by Twitter Tools

process forking in perl

This is a very geeky topic, but I was having a conversation at work today with someone who was trying to do several really simple things in parallel in perl because they didn’t want to wait several minutes for each one to finish. They had knocked up a change to the existing perl, but had tried to do it using perl threads. Normally, that would be fine for most boxes, but when you are running rather old perl on various unixes, including under USS on the IBM z/OS Mainframe, you tend to find out that perl was not compiled with thread support (and recompiling perl for the mainframe is not the easiest thing in the world).

I suggested that they simply use fork() to do the same thing with multiple perl processes, and having not done this in quite a while I tried to search for a good example but couldn’t find anything that looked both complete and simple enough to explain quickly – Hence this post…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/perl 
use strict;
use warnings;
 
sub main
{
  my @children;
 
  foreach my $i ( reverse(1..3) )
  {
    my $pid = fork();
 
    if( $pid )
    {
      #If $pid is non zero, then the parent is running
      print "PID $pid forked ($i)\n";
      push(@children, $pid);
    }
    else
    {
      # Else we are a child process ($pid == 0)
      my $rc = child($i);
      exit($rc);
    }
  }
 
  foreach my $n (@children)
  {
    my $pid = waitpid($n,0); # waitpid returns the pid that finished, see perldoc -f waitpid
    my $rc = $? >> 8; # remove signal / dump bits from rc
    print "PID $pid finished with rc $rc\n";
  }
}
 
sub child
{
  my ($arg) = @_;
  print "$arg: start\n";
  sleep $arg;
  print "$arg: end\n";
  return $arg*2;
}
 
main();
exit;

And the output of running the above code is as follows:

PID 16767 forked (3)
PID 16768 forked (2)
PID 16769 forked (1)
3: start
2: start
1: start
1: end
2: end
3: end
PID 16767 finished with rc 6
PID 16768 finished with rc 4
PID 16769 finished with rc 2

It is worth remembering that because each process gets an entire copy of the memory, you have to do more clever things to actually get the processes to talk to each other – but if all you want to do is run something and get a return code back, the above example should get you going pretty quickly.

For what it is worth, the example at https://wiki.bc.net/atl-conf/pages/viewpage.action?pageId=20548191 isn’t too bad actually, and compares perl threads with process forking, and a php example.

Fix your Arduino Uno for Linux

Are you using an Arduino Uno on Linux? If so, you may have noticed that writing to the serial port in a loop can cause the Arduino Editor/Programmer software to appear to lock up, or even Linux having trouble using the serial port for your Uno.

It turns out there was a bug in the USB->Serial firmware on the chip which caused this [1], and it is quite easy to update your arduino to fix it. According to the author of the USB->Serial firmware, it is not possible to break your Arduino permenantly doing this [1], but I can’t promise anything (though it worked fine for me and others)

From https://github.com/arduino/Arduino/tree/master/hardware/arduino/firmwares/arduino-usbserial/ download your hex file for your board, i.e. either Arduino-usbserial-mega.hex or Arduino-usbserial-uno.hex. As it is github, click on the file in the list then right click on “raw” and click “Save as“.

The md5sum of the Arduino Uno file from the 4th December is 8e01ee236e70bbea43f7eb4e11c9688a

You will need the dfu programmer tools, which are conveniently in Lucid/Universe, so just run

sudo aptitude install dfu-programmer

You need to do a hardware-reset to put the chip into the correct DFU programming mode, which is simply best explained with the picture at http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1285962838/10#10 (you will need to register on the arduino site to login, but a picture speaks a thousand words)

The Uno’s use a AT90USB82 serial-usb chip, so we then reflash the firmware with the following commands

sudo dfu-programmer at90usb82 erase
sudo dfu-programmer at90usb82 flash --debug 1 Arduino-usbserial-uno.hex
sudo dfu-programmer at90usb82 reset

Finally, just disconnect the USB lead and reconnect to reset the device back to normal mode.

Done – You no longer have to worry about writing to your serial port on your Arduino.

References:
[1] http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1286088093/38#38 – Author of Arduino USB stack talks about
[2] http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1286088093/34#34 – First person to find fix and author of instructions that these are heavily based on

Arduino 1-wire temperature sensors

So you have played a bit with your Arduino, and have heard about other people using it to monitor the temperature, but weren’t sure how they did it – Here is how I did.

Maxim make a very simple 1-wire temperature sensor – the DS18B20 (the replacement to the DS1820). This 3 pin sensor looks just like a transistor, and can work with only two wires, combined data+power and ground. You can put several on the same wire and address them all separately.

The DS18B20 is less that £4 from RS, so not exactly expensive.

Wiring up the 1-wire sensors is fairly simple:

You can power them with either dedicated power wires (exercise for the reader), or use their “parasitic mode” where they use power from the same wire as data. To wire them this way you need to wire pins 1 and 3 (ground and Vcc) from the DS18B20 both to ground. Pin 2 goes to your Arduino (any digital I/O pin), and also 5v DC through a 4.7kΩ resistor (if you have a lot of 1-wire devices, there are reports that you may need to reduce this down to 2kΩ).
If you have long wires you may need to start looking at dedicated power, but certainly for testing parasitic power will be fine.

Stringing multiple 1-wire sensors together is as simple as connecting all the pins up in parallel.

Wiring DS18B20

To actually query the devices on your Arduino, you will need the 1-wire and Dallas temperature sensor libraries (unzip them to your Arduino editors’ libraries/ folder).

Examples of using the sensors are all over the place (also examples of querying the ID’s of the devices), or you can try mine below which prints to the serial connection the hex ID’s for each 1-wire device found, and the temperature of each every 10 sec.

// OneWire and DallasTemperature libraries from
//   http://milesburton.com/index.php?title=Dallas_Temperature_Control_Library
// Code based on examples from above and at
//   http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
// See also http://www.arduino.cc/playground/Learning/OneWire
 
#include <onewire .h>
#include <dallastemperature .h>
 
// Data wire is plugged into pin 2 on the Arduino (can be any digital I/O pin)
#define ONE_WIRE_BUS 2
 
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
 
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&amp;oneWire);
 
int numberOfSensors;
 
void setup(void)
{
  // start serial port
  Serial.begin(9600);
  Serial.println("1-Wire DS18B20 example code");
 
  // Start up the library
  sensors.begin();
 
  delay(5000);  //important on linux as serial port can lock up otherwise
  numberOfSensors = discoverOneWireDevices();
  Serial.println();
}
 
void loop(void)
{
  printTemperaturesToSerial();
  delay(10000); //wait 10 sec
}
 
void printTemperaturesToSerial(void) {
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
 
  // Read each of our sensors and print the value
  for(int i=0; i < numberOfSensors; i++) {
   Serial.print("Temperature for Device ");
   Serial.print( i );
   Serial.print(" is: ");
   // Why "byIndex"? You can have more than one IC on the same bus. 
   // 0 refers to the first IC on the wire
   Serial.println( sensors.getTempCByIndex(i) );
  }
 
  Serial.println();
}
 
// Based on http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
int discoverOneWireDevices(void) {
  byte i;
  byte present = 0;
  byte data[12];
  byte addr[8];
  int count = 0;
 
  Serial.println("Looking for 1-Wire devices...");
  while(oneWire.search(addr)) {
    Serial.print("Found \'1-Wire\' device with address: ");
    for( i = 0; i < 8; i++) {
      Serial.print("0x");
      if (addr[i] < 16) {
        Serial.print('0');
      }
      Serial.print(addr[i], HEX);
      if (i < 7) {
        Serial.print(", ");
      }
    }
    if ( OneWire::crc8( addr, 7) != addr[7]) {
        Serial.println("CRC is not valid!");
        return 0;
    }
    Serial.println();
    count++;
  }
  Serial.println("That's it.");
  oneWire.reset_search();
  return count;
}

I get the following output on the serial connection

1-Wire DS18B20 example code
Looking for 1-Wire devices...
Found '1-Wire' device with address: 0x28, 0xCE, 0x85, 0xBB, 0x02, 0x00, 0x00, 0xC1
Found '1-Wire' device with address: 0x28, 0xEF, 0x7F, 0xBB, 0x02, 0x00, 0x00, 0x5B
That's it.

Requesting temperatures...DONE
Temperature for Device 0 is: 21.31
Temperature for Device 1 is: 21.37

Requesting temperatures...DONE
Temperature for Device 0 is: 21.37
Temperature for Device 1 is: 21.37
...

There you have it – A very quick Arduino temperature sensor, using pretty cheap 1-wire devices. The only problem you have now is how to get the wires everywhere you might want to read the temperature of.

Who came up with eBook pricing

Seriously… Who decided how to price eBooks?

A friend at work recently recommended I read Richard Morgan’s Altered Carbon. The Kindle app on android allowed me to find it quickly and download the first 3 chapters free to read – I loved it and decided I wanted to read the rest of it… However when checking the prices I was left the feeling that eBooks are just too expensive for what they are

Play.com £5.99
Amazon UK £5.19
Kindle (Amazon UK) £4.88

I went with Play as their free delivery normally takes about 2 days, so that was great. But seriously, nearly the same price for Kindle? For a book I cannot resell, lend or give away?

Digital books should be much, much cheaper – There is no printing cost involved, so the book should be significantly cheaper, but this is not what I am seeing.

If I choose to keep the book and reread it again in a few years, the price may be fine. But I am more likely to read it once, and then give it to a friend to read. Why should i pay nearly the same price for something I cannot lend, sell, or give to a charity shop?

Also, how on earth does a paper book qualify for no VAT, yet as soon as that book has no paper involved I have to pay VAT?!?

There are lots of Kindle books for £3 or less, and for these I will probably just buy it digitally as its incredibly cheap. But if I wanted to buy a recent release, then I really am paying a lot more for a digital book just to have it early? Why?
I can understand pricing getting lower for older books as they have to compete with people lending books, libraries and second-hand sales, however none of that is possible for DRM protected eBooks, so they should all be priced the same as older releases

App Inventor for Android

Saw this recently on the Google blog:

App Inventor is a new tool in Google Labs that makes it easy for anyone—programmers and non-programmers, professionals and students—to create mobile applications for Android-powered devices. And today, we’re extending invitations to the general public.

via Official Google Blog: App Inventor for Android.

It looks really cool, and allows really quick generation of apps via a gui interface which is powered by MIT’s Open Blocks framework which sounds like a really cool way to get children and students into programming

I can’t wait to get my invite to try it out!

eBooks on Android

Since buying an Android phone I have been starting to think about using it to read books. The screen isn’t bad, and it turns out that there is some good software out there

Aldiko is a fantastic program for reading free ePub books, so I am working my way through a few H. G. Wells books as they are now public domain and available directly through the program (along with many other free books). The software has good options for font styling, line spacing, page turning, black on white vs. white on black as well as quick shortcuts for changing the brightness.

One thing I have yet to figure out though, is buying eBooks.

Sure, there are loads of places out there that sell them, many including their own software (available for Android) for reading the books with DRM. However it is almost impossible to work out which ones use which DRM systems, and what the restrictions are on them (some may even be per-book)

Why am I worried about DRM? Well, what if I want to read the book on a train on a laptop, or what if my phone dies and I get a different phone? Can I transfer it to another device? What if I decide to buy a hardware eBook reader? Will I be able to copy my books to it and read them there?

I haven’t even addressed the idea of lending the book to my wife without giving her my phone.

DRM worries me greatly, and so I doubt I will buy an eBook anytime soon unless it comes without DRM (but no sites make it very clear that they are DRM-free). Why can’t they come to the same conclusions as online music distributors and realise that DRM-free means more sales?

I guess it is paper books for the forseeeable future