The Internet of Trees
Building a tweetable, colour-changing Christmas tree
The phrase “Internet of Things” gets thrown around rather a lot at the moment. It’s the idea that – in the very near future – it’ll be standard practice for physical objects to be network-connected. Everything from toasters to fridges to medical devices to… er… Christmas trees.
Yes, at Think Physics we thought we’d join the trend (and also help with reforestation of the Internet) by hooking our festive office foliage up to a Twitter account, so anyone anywhere in the world could change the colour it glowed. Not so much an ‘Internet of Things’ as an ‘Internet of Trees.’ Yes? No? Mildly amusing? Just a little bit? Just us? Drat.
Here’s the story, anyway:
We could, I suppose, have bought a tree. But nooooo, that would have been far too obvious. Instead, we followed these marvellous instructions from the Makedo folks. Makedo is the cardboard rapid-fixing system we featured in our Christmas gift guide.
Our plan was to illuminate the inside of the tree, which meant we had to hack holes in the thing to let the light out. If we’d planned ahead a little more (so… “at all” would have been a start) we might have noticed one of the comments on the Makedo tree Instructables page which shows a lovely tree made from coroplast translucent plastic sheet. Bothers. That would have been a really good idea. As it was, we decided to cut festive-themed shapes, which went OK until one of us insisted any snowflake patterns have the correct symmetry.
That took a while.
Our code is on GitHub so you can view, reuse or amend it as you wish. It’s probably not a great example of high-quality Python, to be honest – this was the first time I’ve used Python in anger. And I do mean ‘anger,’ at times. Much to learn, I have.
The hardest bit to wrap your head around is probably the Twitter authentication stuff. Or at least, that’s the bit I least understand myself. I’ve somehow ended up authenticating two almost-identical applications with Twitter, one registered to my own twitter username, and one to @thinkphysicsne. That probably wasn’t what I meant to do.
There are all sorts of gnarly problems trying to connect bits of physical computing to internet services. Hanging devices off our university network is merely the first challenge. However, after a little poking around I found some Python sample code which did most of what I needed. Adopting the first rule of programming:
Write less code.
…I hauled out one of our Raspberry Pis and got to work.
Hour 1: Wifi! Try to get the Pi on the university WiFi network.
Result (final): Connected the Pi to the university’s guest network, which is a bit flaky but hey, it’s a tree. Reliability means ‘not dropping needles everywhere.’
Hour 2: Blinky lights! We already had a Pimoroni Unicorn HAT, which is an 8×8 array of RGB LEDs. I spent an hour playing with that, and preparing some colour conversion functions.
Result: Success! Except I was now suffering huge after-images from looking too directly at the stupidly bright lights.
Hour 3: Twitter! I used an off-the-shelf Python twitter library, the examples for which already did most of what I needed.
Result: Success! Incoming tweet selection, etc.
Hour 4: Glue! Plugging all the bits of code together so they worked more-or-less in concert, and as expected. Involved working out how to extract things like hex colour codes from Twitter text.
Result: Abject failure! Nothing worked, everything that used to work was broken, I had no idea what any of the error messages mean.
Hours 5 & 6: Debugging! I spent a chunk of time googling and reading StackExchange, trying to figure out how Python does things. Things like: allow me to pass a number into a function, when what I have initially is a list of lists.
Result: Success! A working bot which sets the panel of LEDs to a colour determined from incoming tweets. Hurray!
Hours 7 & 8: Publish! Cleaning up the code, commenting it, and posting it to GitHub so other people can use it. Inevitably, I broke it really badly in the middle of all this, but in the process of fixing it I managed to clear out a few nasty bugs I’d backed myself into. The result is much more reliable than it was initially.
Result: Success! My first Python code in Github. I’m so proud, even if the code is fairly horrid.
Insert Pi in tree. Take to party:
OK, so we have a tree which changes colour. Here’s how you can operate it:
Firstly, check our Twitter stream to see if it’s currently plugged in and turned on. We’ll post when it’s working. If it is, send us a tweet (@thinkphysicsne) with one of two things:
Send the tweet, and if it’s online the tree will turn the colour you specify.
Of course, you’ll have to take our word for it, at least until we manage to integrate a webcam into the system so the tree replies with selfies.
Think Physics are @thinkphysicsne on Twitter.
We’re used to equating colours to temperatures in false-colour thermal images like this. We’re less familiar with thinking about the colour of light given off by objects at specific temperatures, which is what black body radiation spectra are all about.
The sun is usually represented like this – as an orange ball. We see it looking yellow/orange, but we’re always looking through the Earth’s atmosphere, which scatters a load of blue light out sideways.
So what colour is the sun? Yellow? White? Slightly pink?
There’s more physics involved in this tree than you might expect. But hey, did you not notice the name of the project? Did you seriously think our boss let us play with this stuff without it having any relevance to our work? Pfshaw!
If you take a lump of something – like a chunk of iron, maybe, or a big ball of gas like, er, a star – and heat it up, it radiates light. Physicists talk about ‘black body radiation’, which is the distribution of wavelengths – colours – emitted by the theoretically perfect chunk of stuff. It turns out, quite a lot of things (lumps of ion, balls of gas we like to call ‘stars’) fit quite closely to the ideal black body radiation emission spectrum, and we end up using that theoretical basis to describe shades of white.
Ah. I mentioned ‘shades of white’, didn’t I? Well, yes. OK, back up a bit: you know sometimes you take a photograph indoors, and everything looks yellow? Or you take a photograph outdoors and everything looks blue? That’s because your camera has guessed a colour temperature – a colour for ‘white’ – which is way off. It’s assumed the scene is being lit by an ideal black body which is glowing as if heated to a temperature that doesn’t match the light in the room. Your camera has to pick something to represent white, and every so often it simply guesses wrong.
Colour, it turns out, is as much about your perception as it is about the world itself. Your eye and brain are ridiculously good at guessing what colour is neutral – white – at any given, and they interpret other colours by referring to that. This works really well for us, unless we’re trying to decide if a dress is blue or white.
OK, back to ideal black body radiators: by convention, black body colours are described by the temperature of the chunk of stuff, expressed in scientists’ favourite temperature unit, Kelvin. One Kelvin is the same change as one degree Celsius, but with Kelvin you start counting from 0K = -273.15°C. You can’t have negative Kelvin, but that’s another story for another day. For now, roll with it.
Heat a lump of stuff to a mere 1500K (~1225°C) and it’ll glow a nice warming red. Heat it more, to about 6300K (6026°C) and it looks white, in most circumstances. Heat it way way more, to 12000K (11726°C) and it’ll blaze with an icy-looking blue-white heat. So here’s the really weird part: we call the redder-looking colours ‘warm’ and the more blue colours ‘cold,’ when the temperatures which generate them are completely the opposite. ‘Red hot’ is cooler than ‘white hot’ which turns out to be cooler in turn than ‘blue hot.’
In practice, not all that many things stick too closely to the ‘ideal black body’ colour temperature curves. Lumps of iron are pretty close, at least until they start to boil at around 3000K. But boil a big enough lump of iron, wrap it in rather a lot of hydrogen, and you’ve got yourself a star – and it turns out many stars are a reasonable fit to the colour profile above.
Our tree, then, can approximate the colour of a range of stars. Tweet it the surface temperature of a star, and it’ll come pretty close to glowing that colour. So tweet it 5280K, the surface temperature of our Sun, and it glows…
…ah, but then you have to know what colour the Sun is.