Mad hatter coding

I may have mentioned before that as a librarian I work at Hull Libraries (City of Culture this year don’t you know) and on Saturday this week we held our latest Raspberry Jam session in the library.

Billed as an inventors weekend we had a number of people who had developed their own tools and toys. One gentleman had one of those buzzer toys where you follow the winding wire with a circle (I’m not brave enough to share the video of my other half playing and beating this) and we had the inventor of a wonderful tool that connects a raspberry pi to a Low Power Wide Area Network (LoRaWAN) – a clever wide area network (like wifi) but extremely low power so it’s cheap to run.

This is being used in Hull as part of a project called Connected Hull ( take the time to pop and have a read about the project on their website. It’s really cool and there could be some really interesting uses coming out of the project in the future.

The Connected Hull project people also happen to be helping us with the Jam at the moment and so they had a chat about the project and showed us the envirophat from Pimoroni.

They also showed us the kit that could connect our pies (Pis?) to the LoRaWAN but first up we had to get our sensor working. Then we could share the data.

On Saturday I was being a genial and helpful host and spent my morning running from people playing with the envirophat to people playing with scratch via Minecraft and making music with sonic pi ( Yes, you know who you are “Oh! I can make it make music?!” ) As a result of this, I never got to play with the envirophat on the day although everyone who came had a brilliant time. If you have a raspberry jam bear you I suggest you pop along and have a chat and see what the fuss is about. Be warned though, it might cost you time and money and maybe even more by the time you are done šŸ˜‰

Now, the very kind folks at Connected Hull lent (donated?) a Raspberry Pi 3, an envirophat and the kit to connect to the LoRaWAN to me to play with (maybe the word test might have been used instead of play…) on the proviso I tweet and share my projects with the outside world at large (hence this blog post) and that I help write the instructions for those who follow in our tentative and geeky footsteps…which should be fun.

Earlier this week I decided to have a go with the envirophat and the new pi and see what I could do… with mixed results.

Step 1 – Fire up the pi and update to latest software

This started off well, and in the box was a note on how to

Notes form the previous owner?

output sound through the HDMI port or through the 3.5mm audio jack. “Oh good, it shouldn’t need too many updates then” I thought, rather innocently.


sudo apt-get update
sudo apt-get upgrade

Hmmmm… that upgrade reported an error. Repeat the upgrade command. Same result. Something to do with the perl version having an upgrade issue according to a quick google. Many options for workarounds listed. I cheated.

I powered down the pi and pulled the SD card. I still had the latest Pixel image on my laptop from my misadventure with my own pi a few weeks back and recent experience of something similar (check out my earlier blog post) so I re-imaged the SD card and booted back up with a nice fresh clean Raspberry Pi environment.

A quick password change and I was good to go.

This time the update worked, so I went and made a cuppa while it did it’s thing.

My favourite mug

Step 2 – EnviropHat fun

The next step was to install the envirophat. One of my friendly neighbourhood Raspberry Jammers had soldered this for me already (which is good because my soldering skills are limited at best) so this was simply a matter of fitting the hat to the Raspberry Pi.

Pi with Hat

It lit up and everything was good. With my trusty laptop beside me I visited the website we had used in the jam session and began to follow the instructions.

curl | bash

This command downloads and installs all the software for you and is very helpful thank you much. All the packages required are all done behind the scenes and is as easy as typing the command in.

A quick reboot of the Pi to make sure all the required services are running and things look good. Except that the envirophat LEDs are no longer lit. I pulled the hat and reconnected it. Still nothing. Google resulted in lots of results, none of which appeared particularly helpful so I turned to twitter (hoping to all that is holy I wouldn’t have to remove the solder and redo it).

The strange thing was that after a thorough examination of the hardware (wiggling and the like to check for broken solder – all very scientific I assure you) I reconnected the hat and the LEDs lit up on connection. SUCCESS!

Unsure of what I had done (and assuming I had fixed the slightly dodgy connection with all my prodding and poking) I proceeded undaunted.

Following on with the instructions on the site I proceeded to fire up a scripting (or programming) tool called python “Ah, but hang on” I thought. “In the session on Saturday, they used idle3 not python. I should use that”.

Proud of my usually appalling memory I fired up the idle3 application and was greeted with a bright white screen.

Into the pristine white environment I type my commands

from envirophat import light
print light.light()

and get an error message. I repeat the commands and get the same error message. It’s always good when you can repeat the error.

…idling away

“Right, I’ll follow the instructions on the site then rather than do what we did at the jam”

sudo python

This time when I typed the commands in it returned a number

>>> from envirophat import
>>> print light.light()
>>> 108

Meaningless on it’s own, but a number none the less. Cover the sensor and run the command again.

>>> print light.light()
>>> 23

Result. A lower number. Excellent. This means that the light sensor is working. It reports a lower number the darker it is.

What happens if I ask it to report the temperature?

>>> from envirophat import weather
>>> print weather.temperature
>>> 25.456722309

I’m not sure I need all the decimal places, but the temperature is about right for my living room (and being on top of a passively cooled electronics device). My next plan was to create a small script that would save me having to type all the commands in each time I wanted to know the temp or the light reading.

A simple script file identified as python should do the job. So I copied each of the lines I had typed individually, adding a descriptor so I would know which number was which (I could probably guess unless it was *very* hot and / or dark but I like to do these things properly when I can).

Save the file with a .py extension and make it executable with

sudo chmod +w

and run it. Brilliant. A nicely formatted output to the screen with light levels and temperature in degrees centigrade.

So far so interesting. What I really wanted to do though is to run this program through the internal timer system (I’ve spoken a bit about crontab before with my rf light controls) which means that outputting the data to the console (or screen) is pretty much useless as I won’t be looking at it. Especially if the Pi is running without a screen – more than likely if I’m looking at putting it into a site in the office to gather data.

In this case, I’ll need to log this data into a file. Along with the time the sensors were checked. A bit of research later and we have the following script.

from envirophat import light
from envirophat import weather
import time

f = open("elog.txt","a")
lux = light.light()
temp = weather.temperature()
time = time.strftime("%H:%M:%S")

f.write (%s \n" % time)
f.write ("The light level is... %s \n" % lux)
f.write ("The temperature is... %s \n" % temp)


I run the script and it looks like nothing is happening in the terminal window. The log file however produces a wonderful output.


Brilliant stuff. I should add at this point that this is the first time I’ve used python to output to a file. In fact this is the first time I’ve used python in anger. I bodged my last project with multiple, super complicated batch files.

The next steps…

Speak to the world

Next I need to learn how to work the sensor with the “piface” add on. The “piface” card means I should be able to share the data with the Connected Hull data collection point nearby. Then I need to help others by getting involved in writing the instructions with the rest of the jammers who are testing this all out for the inventor.

More on that in another post when I’ve had chance to have a play and a chat with my collaborators.

See you all soon šŸ™‚

One thought on “Mad hatter coding

Get in touch...