Showing posts with label raspberry pi. Show all posts
Showing posts with label raspberry pi. Show all posts

Sunday, 31 May 2015

Handheld Humidity Measuring with Astro-Pi

The main theme of our winning entry for the Astro-Pi completion was detecting the 'sweaty astronaut' using the HAT's humidity sensor.


To test how this might work,  and to build a simple demonstration for the CodeClub team,  we wrote some Python to use the Astro-Pi's awesome LED matrix to display the latest readings from the humidity sensor.

Adding a battery pack turns this into a useful hand-held humidity sensor (you could easily use it as a thermometer instead with a minor code change).

The code changes the colour of the displayed numbers based on the previous reading. If the humidity has increased then the digits are green, if it has decreased, they're red, and (you guessed it) orange if there has been no change.


Of course the screen only displays two digits so when it gets really hot and steamy...



The code also logs the humidity out to a file, so you can plot humidity against time. Here is 24 hours in our house. The big spike near the start was due to another 'breath test'.


The graph above came from data that was saved every time the display was updated - every half a second - which resulted in a lot of rows! I've now modified the code so that it only writes a result to file once every 30 seconds (the display continues to update every 0.5 seconds).  Much more manageable.

Here is the code:

from astro_pi import AstroPi
import time
import logging
from datetime import datetime

ap = AstroPi()

r = [255, 0, 0]
e = [0, 0, 0]

space = [
e,e,e,e,e,e,e,e,
e,e,e,e,e,e,e,e,
]

tmstmp = time.strftime("%Y%m%d-%H%M%S")

logging.basicConfig(format='%(asctime)s %(message)s',filename='humidity'+str(tmstmp)
+'.log',level=logging.DEBUG)

def numToMatrix(num):

# define 1x8 columns that make  single digits when combined

num_1_2 = [e,e,r,e,e,e,r,e]
num_1_1 = [e,r,r,r,r,r,r,e]
num_1_0 = [e,e,e,e,e,e,r,e]

num_2_2 = [e,r,e,e,e,r,r,e]
num_2_1 = [e,r,e,e,r,e,r,e]
num_2_0 = [e,r,r,r,e,e,r,e]

num_3_2 = [e,r,e,e,r,e,r,e]
num_3_1 = [e,r,e,e,r,e,r,e]
num_3_0 = [e,r,r,r,r,r,r,e]

num_4_2 = [e,r,r,r,r,e,e,e]
num_4_1 = [e,e,e,e,r,e,e,e]
num_4_0 = [e,r,r,r,r,r,r,e]

num_5_2 = [e,r,r,r,r,e,r,e]
num_5_1 = [e,r,e,e,r,e,r,e]
num_5_0 = [e,r,e,e,r,r,e,e]

num_6_2 = [e,r,r,r,r,r,r,e]
num_6_1 = [e,r,e,e,r,e,r,e]
num_6_0 = [e,r,e,e,r,r,r,e]

num_7_2 = [e,r,e,e,e,e,e,e]
num_7_1 = [e,r,e,e,e,e,e,e]
num_7_0 = [e,r,r,r,r,r,r,e]

num_8_2 = [e,r,r,r,r,r,r,e]
num_8_1 = [e,r,e,e,r,e,r,e]
num_8_0 = [e,r,r,r,r,r,r,e]

num_9_2 = [e,r,r,r,r,e,r,e]
num_9_1 = [e,r,e,e,r,e,r,e]
num_9_0 = [e,r,r,r,r,r,r,e]

num_0_2 = [e,r,r,r,r,r,r,e]
num_0_1 = [e,r,e,e,e,e,r,e]
num_0_0 = [e,r,r,r,r,r,r,e]

# combine columns to make digits

sing_0 = num_0_0 + num_0_1 + num_0_2
sing_1 = num_1_0 + num_1_1 + num_1_2
sing_2 = num_2_0 + num_2_1 + num_2_2
sing_3 = num_3_0 + num_3_1 + num_3_2
sing_4 = num_4_0 + num_4_1 + num_4_2
sing_5 = num_5_0 + num_5_1 + num_5_2
sing_6 = num_6_0 + num_6_1 + num_6_2
sing_7 = num_7_0 + num_7_1 + num_7_2
sing_8 = num_8_0 + num_8_1 + num_8_2
sing_9 = num_9_0 + num_9_1 + num_9_2

# map digits onto appropriate strings
if num == '1':
return sing_1
if num == '2':
return sing_2
if num == '3':
return sing_3
if num == '4':
return sing_4
if num == '5':
return sing_5
if num == '6':
return sing_6
if num == '7':
return sing_7
if num == '8':
return sing_8
if num == '9':
return sing_9
if num == '0':
return sing_0

# set previous humidity value to zero

hum_prev = 0
sec_count = 0

# Main program loop

while True:

# Get humidity from astro-pi

hum_f = ap.get_humidity()
hum_int = int(hum_f)

# test to see if value is higher or lower than previous, and then set led colour appropriately 

if sec_count == 60: # only write data every 30 seconds
logging.info(hum_f)
sec_count = 0
if hum_int > hum_prev:
r = [0,255,0] # green if higher
elif hum_int == hum_prev:
r = [255,127,0] # orange if the same
else:
r = [255,0,0] # red if lower

hum_prev = hum_int
hum =  str(hum_int) # convert reading to string

image = numToMatrix(hum[1]) + space + numToMatrix(hum[0]) # build image from two digits plus spacer
ap.set_pixels(image)

time.sleep(0.5)
sec_count+=1

Tuesday, 18 March 2014

Spud Game

My School was lucky enough to win 5 Raspberry Pis in last Autumn's Hour of Code promotion and I thought it'd be a good idea to introduce them to my CodeClub posse.

I'm a big fan of Raspberry Pis - I've got several doing various jobs at home - but I've always wondered just how well they'd fit into a Primary School environment. The biggest draw for many youngsters is the ability to interact with the Minecraft Pi Edition through code. But my Codeclubbers are still using Scratch and so for their first taste of Raspberry Pi I wanted to stick with something familiar.

So there is the dilemma: running Scratch on the Pi by itself doesn't really offer any advantages - in fact if they connect one up to the school's ICT suite PCs, the only difference they'll probably notice is that Scratch runs more ponderously. Will that get them excited about Raspberry Pis?

I needed a project to really demonstrate what (for me) is the big plus point of Pis: the ease with which you can connect and interact with external devices.  A simple idea would be to connect the GPIO to some breadboarded LEDs and make them flash with Scratch code, but that seemed a little tame.  So in the end I decided to make use of an Adafruit capacitive touch sensor which I'd got from Pimoroni and hadn't really put to good use yet.

Fortunately ARGHBOX have already produced a great tutorial for using the sensor to interact with a set of potatoes! So using that as a starting point I created a simple "Simon says" type game for the Pi using Scratch.

What you'll need
  • A Raspberry Pi (obviously)
  • An Adafruit touch sensor
  • A breadboard
  • 5 x 10K resistors
  • Some wires
  • Some potatoes
  • A Pi Cobbler 
Software
  • The latest and updated version of Raspbian (this will probably work on other distros but I did not test any others). 
  • ScratchGPIO5 from cymplecy (the default version of Scratch that comes with Raspbian doesn't know how to talk to the GPIO pins). 
Assembly

The setup is almost identical to ARGHBOX's but I used different GPIO pins so that it would work more easily with the sensor inputs made available by default in ScratchGPIO5.

Scratch code

The code for my game is available on Github.

The cymplecy site has some great documentation for using Scratch with GPIO inputs and outputs, however the key elements of my Scratch game are quite straightforward.

First of all you need to set the relevant GPIO pins to act as inputs. This is achieved by sending a broadcast message of the format shown below.


Then the main code that interacts with the spuds simply checks for a change in the input value and then acts accordingly.



The game


The player presses the S key and the potatoes on screen flash in a random sequence.



The player then presses the space bar and gently taps the real spuds in the same order as they flashed on the screen.  The spuds say "Oi!" when they're tapped and the entered sequence is displayed in the 'answer' variable on the screen.



If the player remembers correctly then they see the WIN screen, if not, the big fat LOSE screen awaits them!