Wednesday 25 September 2013

101 uses for an old smartphone, #1

I'm about to install a freezer in the barn, which is half a mile from my home. How am I going to know if the power fails or the internal temperature rises?

Various companies sell ultra-low power temperature sensors which communicate via Bluetooth, for example the TI SensorTag (which actually does far more, but it's temperature I'm interested in just now)

It is said to run for a year on one button cell.

I have an old Android phone - in fact, an original Google G1 - that is sitting doing nothing. An Android app can detect whether the device is connected to AC power by registering a receiver for the intent ACTION_BATTERY_CHANGED.

Texas Instruments offer sample source code for an app which can read data from the bluetooth sensor (although needless to say their Linux installer doesn't bloody well work!)

So, here is the specification for an app:

  • It runs as a service, from startup;
  • It checks for the presence of the bluetooth sensor, and if it doesn't detect it, sends a message;
  • It registers a receiver for power state events, and if the mains power goes off, it sends a message;
  • It periodically (e.g. every ten minutes, but configurable) reads temperature from the sensor. If it can't communicate with the sensor or the temperature is out of the configured range, it sends a message;
  • When it sends a message, it does so by SMS, email or Twitter (configurably), to a configured number/address

All you do then is drop the sensor in the freezer, plug the phone's charger into a wall socket on the same circuit as the freezer, ensure the phone has a valid SIM with enough credit to send texts, and leave the phone switched on.


NOTE: I haven't yet written this app. I may never write this app. But then again, I may...

Friday 20 September 2013

Editing and clojure revisited: this time, with structure!

Yesterday I blogged on editing Clojure, and commented that

"So after all that I'm going to talk about how wonderful it is to be able to do structure editing in Clojure, aren't I? No, sadly, I'm not, because (as yet) you can't."

Well, you can now. About tea-time yesterday, fooling around with Clojure, I worked out how the 'source' macro works - it retrieves the source of the function or macro whose name is its argument, by inspecting the metadata tagged to the symbol. So, you can retrieve function definitions. They're retrieved as strings from the file, but the read-string function parses s-expressions from strings, so that isn't a fatal problem. And having found that, a terminal based structure editor was not far away.

OK, it is, as yet, crude. It needs a lot of polishing. And, a terminal oriented structure editor is not what I really want - I want a display editor which pops up in a window (and, related to that, a display inspector, too). That window is almost certainly a web page. However, writing a terminal oriented structure editor proves that it can be done.

There are significant problems ahead. You cannot, in theory, rebind a Clojure symbol, so you cannot in theory compile a new definition for an existing function symbol. But the package system does this, so it is possible. I need to find out how. Indeed, the whole package system would have to be modified significantly to support in core editing. Just writing an editor is only scratching the surface.

But the surface is scratched. There is now a structure editor for Clojure. You can get it here.

Thursday 19 September 2013

On editing, and Clojure

Back in the days when the world was young and we had proper software development tools, you'd write your top level function and evaluate it. And, of course, it would break, because you hadn't yet written your lower level functions. So you'd get a break inspector window pop up on the screen, and in that you could do one of several things.

Firstly and most obviously, you could supply a value as the return value of the function you hadn't yet written, and continue the computation. Secondly, you could inspect the stack, unwind a few levels of computation, and choose a different point to continue from, supplying a new return value there. But thirdly...

Thirdly, you could write your new lower level function right there on the stack, and continue the computation through your new function. And you could write it in a structure editor.

What is a structure editor?

OK, Lisp is a homoicinic language. Which is to say, a Lisp program is just a Lisp data structure. And the canonical form of the program is not a stream of byres in a file, it's the structure in memory. Now, OK, one can reconstruct the structure in memory by writing it out to a file as a stream of bytes and then reading that stream of bytes back in; and consequently, one could (as one must for more primitive languages) edit that stream of bytes in a text editor in order to modify the program, and then read in the modified stream of bytes to create a modified structure in memory, and consequently a modified program.

That is, of course, how the LMI and the Symbolics boys worked, how Richard Stallman worked. But that's a lot of hassle. It's much simpler to pop the data structure in memory up in a window (automatically formatted, since it's just data and not a stream of bytes so the indenting can't be wrong) and edit it right there. And the joy of that, of course, is just as you can't get white space wrong, you also can't get parenthesis nesting wrong, since all you can do is insert a new well formed symbolic expression, delete an existing well formed symbolic expression, or replace an existing well formed symbolic expression with a new well formed symbolic expression.

People don't understand Lisp syntax, and that's because they think of it as text. It isn't text, it's structure; and the structure is beautifully simple and perfectly regular. It consists of well formed symbolic expressions nested in other well formed symbolic expressions, and nothing else. There are no keywords. There are no operators. There are no control blocks. Nothing is magic, because everything is magic. The Lisp programmer is perfectly at liberty to define T as NIL, or NIL as something else. The world will probably stop working if he does, and not in a good way - but he can, because not even T and NIL are magic.

So after all that I'm going to talk about how wonderful it is to be able to do structure editing in Clojure, aren't I? No, sadly, I'm not, because (as yet) you can't. All you can do is the old-fashioned primitive thing of editing a text file, a stream of bytes, and then reloading it. The world of modern software tools is still years behind where we were thirty years ago.

But some of the modern editors for Clojure are nevertheless pointing the way forward.


There is an editor which these days is Generally Not Used Except by Middle Aged Computer Scientists; back in the day when eight megabytes was a lot of memory, we used to call it Eight Megabytes And Constant Swapping. Its original author called it 'editor macros', but since, in those days, on most computer file systems, file names were constrained to be short, it was more generally abbreviated to Emacs.

Emacs, of course, is written in Lisp and for Lisp. And consequently, there has long been a Superior Lisp Interaction Mode for Emacs, better known as Slime. Slime establishes a communication between the Emacs editing buffer and a separate Lisp process, and sends chosen symbolic expressions from the buffer to the Lisp process for evaluation. And the beauty of that, of course, is that it works not only with Emacs Lisp but, in principal, with any Lisp, from Portable Standard Lisp through Symbolics Lisp and Franz Lisp to the execrable Common Lisp (now) to Clojure.

The problem with this is that Emacs, although enormously powerful and in its way elegant, is now long in the tooth. It always was user hostile to an extreme degree, and has a very steep learning curve. Thirty years ago when it was designed, modern user interface design hadn't evolved. Now, not only are all the Emacs key commands incompatible with modern conventions, you can't even copy and paste simply between an Emacs buffer and any other window. Don't get me wrong, Emacs was my text editor of choice for fifteen years from 1988 until the early 2000s, and it still is my editor of choice for XML and SGML, since it parses DTDs properly and does helpful auto-completion on that basis (to be fair Oxygen XML does this equally well, but it's expensive), but otherwise in truth it's a pig to work with these days, and Slime, while elegant, doesn't marry up to modern tools.

Clojure Mode

Separately from Slime there's now a 'clojure mode' for Emacs, which (allegedly) works similarly to but better than Slime. The truth is I've no idea whether this is true, because it's built for the Emacs 24 package system, and while I spent an hour this morning trying to back-port it to the Emacs 23 which is supported by Debian, I eventually gave up. Life is too short. It would have to be utterly amazing to be better than the more modern Clojure editing tools.


Counterclockwise is a Clojure mode for my current editor of choice, Eclipse. Not only does it understand Leiningen projects, do syntax highlighting and intelligent auto-complete, structure analysis, linking, all the things one expects of a modern IDE, it also has an interactive Clojure process in which, just as with Slime, you can evaluate either the currently highlighted symbolic expression in a file, or the whole file. This is simple, powerful, easy to use, and, of course, integrates with all the other good Eclipse tools like Mylyn, and version control systems.

Counterclockwise is a joy to use. In fact, one might almost describe it as the perfect development environment for Clojure, were it not for one other competitor...

Light Table

Light Table is something new; something which, although it is still a text editor, has something of the power of the structure editors I still yearn for. It's live. Which means, you don't just send things from the buffer you're typing into to a separate Clojure process to evaluate them; they are continually evaluated as you type. It's remarkable, revolutionary, and, I think, potentially points to a future for programming editors.

Unfortunately, in its present state, it's alpha code, and it's fragile. While it works well for simple exploratory code, trying to load add a connection to a Leiningen project - something which Light Table allegedly supports, and must do if it is to be used for serious work, then, providing the project depends only on Clojure, everything works well. But if, as most projects do, the project depends on other projects, it frequently results in
Exception in thread "main" java.lang.RuntimeException: java.lang.NoSuchMethodError: clojure.lang.RT.mapUniqueKeys([Ljava/lang/Object;)Lclojure/lang/IPersistentMap;
 at clojure.lang.Util.runtimeException(
 at clojure.lang.Compiler.eval(

That's really not helpful. Googling for it suggests that the problem is probably that Light Table is (on my machine) using Clojure 1.3, when it needs at least Clojure 1.5.1; however, while I have both versions on my machine, the path is set up to prefer Clojure 1.5.1, and the Lieningen project.clj file I'm using also specifies Clojure 1.51, so if it's using the wrong version I don't understand why it is.

In short, I'm really excited by Light Table, but I'm afraid it isn't yet ready for prime time.


Of course, there is not (yet) a structure editor for Clojure. It wouldn't at all be impossible to build one, and in fact that is the sort of tool-building project which is likely to pay off well in productivity in the long run; but it's also potentially tricky and I think I need to become more generally fluent in the language first. For the meantime, Counterclockwise is a tool which will serve me well.

Sunday 15 September 2013

A staggered cantilever house

 I'm back to worrying again at the structural design of a house to fit into the natural hollow in the north-east corner of my croft, a house to fit organically into the landscape. It isn't that I've fallen out of love with the Winter Palace, I haven't. I like it very much. So long as I remain single, and remain fit enough to climb its stairs, it suits me very well. But it is my ambition some day to cease to be single, and within the next twenty years the stairs will probably become beyond me. So in the long term another house is necessary. And the view from this house is very restricted; from the hollow in the north east corner I could see out to the Isle of Man.

So let's go over the options for that hollow. The first option I designed was the design I called Sousterran: four tessellated concrete domes, supported by beautifully sculptural flying buttresses. The merits of that design remain that its irregular, sculptural shape would fit very well into the landscape, that it is iconic and would be beautiful; and, in so much as the design is modular, extending it would actually be easy. The demerit is that it uses a lot of concrete, a lot of embodied energy. An eco-house it is not. A further technical problem is that if the waterproofing of the back wall were to fail, it would be extremely expensive and difficult to fix.

So the next design was the design I called Singlespace: a mostly-timber conical roof over a single large, circular room, later (in the Longeaves variant) with some sheltered external storage and the possibility at a later stage of adding an earth closet attached to the building, which one could use without going out of doors. Again, the design is elegant. But for me the major demerit is that the regular cone, even if turf-roofed, would look significantly unnatural. It would draw the eye in the landscape, immediately announcing something artificial. Also, if the back wall were not to suffer the same waterproofing issues as with Sousterran, there would have to be a significant walkway round the back, which firstly wastes space and secondly interrupts the continuity of the walking surface between the natural hillside and the roof. There would be, in effect, a chasm to be stepped over, or fallen into.

It was in thinking about that gap that I came up with the new design. The issue about the gap is that, if the earth of the hillside presses directly against the back wall of the house, that wall has to be very efficiently waterproofed - 'tanked' - to prevent damp. That's complex in itself, but it it fails, a great deal of earth has to be very carefully removed to expose the wall for repair, which would be expensive. Also, the wall needs to be inherently strong enough to resist the thrust of the hillside - not a problem with the hexagonal concrete cells of Sousterran, but much more problematic with a lighter weight wooden or straw structure.

The thing is, soil affected by heavy rainfall will not stand in a vertical cliff without substantial, expensive support. There is a natural angle of repose - the angle of a natural talus slope - at which it will safely lie. The use of modern fabrics, well pegged, or a dry-stane retaining wall, can steepen that slope a bit, at cost, but fundamentally the gap between the wall and the slope will be much wider at the top than at the bottom...

If the wall is vertical.

If the wall is vertical? Of course a wall is vertical.

Yes, but what if it isn't?

If it isn't, if it lies back parallel to the slope, you still get an air gap behind the house, eliminating the penetrating damp problem. And you can still - if rather less comfortably - get in to do any necessary maintenance. But you save that wasted space, you get more volume inside, and you close up the chasm between the hillside and the roof. Good!

Of course, you could have an angled back wall with the conical Singlespace roof, using a yurt like structure. But in thinking about how you support an angled-out back wall, I thought of a W shaped cantilever truss. However, if you have a triangular truss supporting a principal rafter from a point which is fairly central under that rafter, there should be no significant thrust on the roof tree. So the trusses supporting principal rafters on the back and front sheds of the roof don't need to line up with one another. So I came up with a sketch in which the trusses are staggered, and that's actually rather interesting in terms of the interior space. The space is continuous, and you can walk through it, but the trusses naturally break the space up into smaller spaces with a degree of visual privacy.

Of course, the staggered trusses could be built symmetrically to a straight rooftree supporting rectangular plane roof sheds. But actually they need not. A curved roof tree supporting sheds with simple, not compound, curvature would fit much more naturally into the landscape. I think this could be reasonably simple and easy to build - but of course the roof tree and purlins would need to be laminated, which is a little more complex than simply straight beams.

This design is at a fairly early stage. It has three times the space of the Winter Palace upstairs, but only about twice the space downstairs (and the need for a staircase limits the amount of usable room upstairs). Obviously, of course the basic design could be significantly bigger, either by increasing the span of the trusses or by adding an extra bay (or two bays) to the right hand end. There are some details which are not yet worked out. I've shown storage space (for e.g. firewood, or sheddy things) at the left-hand end of the building, but I have not yet thought through the right hand end.

I also haven't decided what to do about a staircase. In order to get two reasonably private bedrooms upstairs, the staircase needs to go up the middle of the building. It could go up the sloping back wall, at the left hand end of the sink unit in these drawings; or it could go across the middle of the space, dividing off the kitchen; although that would be a bit wasteful.

Sunday 8 September 2013

A farewell to pigs

Tonight I have bagged up 12Kg of sausages, 9Kg of chops, 4Kg of spare ribs. I have salted one 7Kg ham, and I have another one waiting in the cool box. In refrigerators and freezers up in the void there is a veritable mountain of pork...

But I get ahead of myself. This week is the first time we've had pigs commercially slaughtered. Previously, we've slaughtered pigs here on the farm, but if you do that firstly need cool weather, and secondly you can't sell the meat, or even give it away. Two pigs, each of them substantially bigger than me, are far more than I can eat; and processing them would have needed me to call on a lot of support from friends.

So after a lot of swithering I decided to get them slaughtered commercially. I organised for them to go to Lockerbie slaughterhouse, and organised for them to be delivered from there to my favourite butcher, Henderson's in Castle Douglas. Again, if you slaughter commercially, you have to have them butchered in a commercial standard, health approved butchery, or you can't sell meat. Henderson's, apart from being my butcher of choice, also quoted a very favourable price - £50 per pig.

Legally, the pigs had to be ear tagged to go to slaughter. We didn't have the requisite tags, but the Tarff Valley farmer's co-op was able to have them made up for us at twenty-four hours notice, which I thought was pretty good. There was some debate about what trailer to use. Finn has an animal trailer, but it's seen better days. After looking it over carefully, James, who was going to be driving (my car doesn't have a tow hitch), decided to borrow a trailer from a friend of his.

Finn decided to send one of his pigs with mine, so on Wednesday evening we loaded his pig into the borrowed trailer, and I towed it over the hill to my croft. I dismantled the electric fence in the gloaming, and then had a long fight with the ScotEID website to register the transfer of the pigs (in these food-safety conscious days, every animal movement on or off the farm has to be recorded), went to bed tired, and slept remarkably well. Thursday morning I was up at six, getting the pig trailer into my yard, seeing that Finn's pig was fed and watered, and organising barriers to help direct the pigs into the trailer. At seven, Finn and James came over to help load.

We were a little concerned that my pigs might fight with Finn's. They are from the same litter but hadn't seen one another for six months. So we divided the trailer into two with a hurdle. I then filled a bucket of pig food and went to call my pigs. They came willingly enough, but were amazingly reluctant to cross the line where the electric fence had been. However, I coaxed first one, then the other, over the line and round into the trailer. In the trailer they greeted their brother with no sign of hostility, and munched their breakfast contentedly. We closed the ramp and headed off to the slaughterhouse.

Coming round Dumfries bypass we were pulled over by police, doing a routine vehicle check. James was doubly glad he had brought the better trailer! But the police, after ten minutes, let us proceed with no problems, and shortly we arrived at Lockerbie. My phone was playing up, and wouldn't get Google Maps to direct us to the slaughterhouse, but I knew roughly where it was, I thought. Eventually after getting lost twice and having to ask for directions (also twice), we found we'd driven straight past it three times - it's a remarkably small, anonymous building, unsignposted. But the people there were friendly and efficient, and handled the pigs calmly and with remarkable gentleness. It was good to see such obvious concern for their welfare. And so I said goodbye to my pigs.

After slaughter the pigs were transported by border meats to Henderson's, on Friday. I hadn't discussed with Henderson's in advance how long they would take to turn them round and was expecting them sometime next week, but when I dropped in on them on Thursday lunchtime they said they would cut them up on Sunday morning and have them ready for half past twelve. They would have done me 'cured' and sliced bacon, but obviously you can't do a traditional cure in that time, so in fact the bacon would simply have been injected and then sliced, as much modern bacon is. Instead I asked them just to give me the bacon in flitches which I shall cure myself.

The pigs weighed around 90Kg each going to slaughter; Henderson's reported 70Kg each deadweight. By coincidence, that's what I weigh.

My friend Jude and I duly collected the meat at lunchtime; it's an extraordinary amount. I had ordered a freezer in good time, but the one I'd ordered was out of stock and couldn't be delivered. So I've ordered another, but it hasn't yet arrived. Fortunately Finn had a large second hand fridge-freezer in the void. I've filled that, and the remainder has gone into James and Vicky's freezer...

Except for the hams. I have two boned out hams, each 7Kg. I brought them down to the winter palace in the cool box. There are various web pages about onto how to make air dried ham; I read several and then decided to experiment. I wanted to do a honey cure, so I mixed up

  • A kilogram of salt
  • Half a jar of honey
  • A desert spoon of crushed pepper corns
  • A desert spoon of mustard seeds
  • A desert spoon of saltpetre
  • A couple of tea spoons of cloves

This made a very sticky paste, and I thought it looked fine - until I tried to apply it to the ham. But it doesn't adhere well, and it's important to get salt into every cut surface of the meat. So I mixed up a mixture of

  • 1.5Kg salt
  • 0.5Kg demarara sugar
  • 1 desert spoon of saltpetre

and I've rubbed that carefully into all the surfaces of the meat. I've then stuffed my original honey paste mostly into the bone cavity, but also sort of spread it on the outer surfaces.

I've taken a plastic container which once held 25Kg of mineral lick for the cows, drilled some holes in the bottom, lined it with kitchen paper and poured in a couple of centimetres of salt. Into this container I've placed my ham. Tomorrow I'll cut boards to loosely fit into the top, and pile about 15Kg of stones onto them. The ham will stay in that, under the house, for a month, and then be hung up in a wire mesh cage in the roof of the woodshed through the winter. If all goes well, I'll then have air-dried ham. If all goes badly, of course, I'll just have a lump of rotten meat, but we'll see.

I'm not yet sure whether I will dry-cure and air-dry my other ham. I'd like to, but I'm aware I'd be taking a substantial risk. It would be much less risky to brine-cure the other and then freeze it. I'll think on that overnight.

But I ended the day with a celebratory meal of sausages, and they were good!

Creative Commons Licence
The fool on the hill by Simon Brooke is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License