Skip to content

Instantly share code, notes, and snippets.

@topshed
Last active November 1, 2018 14:18
Show Gist options
  • Save topshed/bba8fc250ab38f5a4f6ce80d372c7555 to your computer and use it in GitHub Desktop.
Save topshed/bba8fc250ab38f5a4f6ce80d372c7555 to your computer and use it in GitHub Desktop.
Environmental sensing with SenseHAT

Environmental sensing with SenseHAT

What's needed?

A Pi and SenseHAT

Two options. Both really rely on the SenseHAT being somewhat accessible so that participants can actually change the environment. Ideally the PI and SenseHAT would be on the table (perhaps in a 3d printed flight case with with cables glued in to secure it?). Alternatively, there could be a hole cut in perspex screen so that participants can blow on the SenseHAT (to change humidity and temperature).

Option 1: simple bar graph displayed on SenseHAT LED matrix.

Imgur

Here's the code at the starting point. Reset would return things to this point.

from sense_hat import SenseHat

sense = SenseHat()

green = (0,255,0)
white = (255,255,255)

while True:
    humidity = sense.humidity
    humidity_value = 64 * humidity /100
    print(humidity)
    pixels = [green if i < humidity_value else white for i in range(64)]
    sense.set_pixels(pixels)

How would the participants interract?

Basic: change the colour definitions to alter the colour of the LEDs used in the graph. So instructions would be how to choose new RGB colour values.

red = (255,0,0)
blue = (0,0,255)
yellow = (255,255,0)

e.g

from sense_hat import SenseHat

sense = SenseHat()

red = (255,0,0)
blue = (0,0,255)

while True:
    humidity = sense.humidity
    humidity_value = 64 * humidity /100
    print(humidity)
    pixels = [green if i < humidity_value else white for i in range(64)]
    sense.set_pixels(pixels)

Harder: modify the code to display temperature rather than humidity. The instructions would cover how to read from different sensors in Python.

for temperature: sense.get_temperature()
for pressure: sense.get_pressure()

So the modified code would become:

from sense_hat import SenseHat

sense = SenseHat()

red = (255,0,0)
blue = (0,0,255)

while True:
    temp = sense.temperature
    print(temo)
    pixels = [green if i < temp else white for i in range(64)]
    sense.set_pixels(pixels)

Option 2: If we want to showcase more advanced things that are possible with the Pi.

This will plot a graph that updates in real-time (shown on the monitor).

imgur

Here's the starting code. reset will return things to this point:

import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from time import sleep
from sense_hat import SenseHat
import os


# Create figure for plotting
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = []
ys = []


# Initialize communication with Sense HAT
sense = SenseHat()

# This function is called periodically from FuncAnimation
def animate(i, xs, ys):

    # Read humidity
    humidity = sense.get_humidity()

    # Add x and y to lists
    xs.append(dt.datetime.now().strftime('%H:%M:%S'))
    ys.append(humidity)


    # Limit x and y lists to 50 items
    xs = xs[-50:]
    ys = ys[-50:]


    # Draw x and y lists
    ax.clear()
    ax.plot(xs, ys)
    ax.xaxis.set_major_locator(plt.MaxNLocator(20))

    # Format plot
    plt.xticks(rotation=45, ha='right')
    plt.subplots_adjust(bottom=0.30)
    plt.title('Local Environment')
    plt.ylabel('%')

# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=1000)
plt.show()

How would the participants interract?

Change the code to display temperature rather than humidity

import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from time import sleep
from sense_hat import SenseHat
import os


# Create figure for plotting
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
xs = []
ys = []


# Initialize communication with Sense HAT
sense = SenseHat()

# This function is called periodically from FuncAnimation
def animate(i, xs, ys):

    # Read temperature 
   
    temp = sense.get_temperature()

    # Add x and y to lists
    xs.append(dt.datetime.now().strftime('%H:%M:%S'))
    ys.append(temp)


    # Limit x and y lists to 50 items
    xs = xs[-50:]
    ys = ys[-50:]


    # Draw x and y lists
    ax.clear()
    ax.plot(xs, ys)
    ax.xaxis.set_major_locator(plt.MaxNLocator(20))

    # Format plot
    plt.xticks(rotation=45, ha='right')
    plt.subplots_adjust(bottom=0.30)
    plt.title('Local Environment')
    plt.ylabel('%')

# Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=1000)
plt.show()

I guess there's no reason why both basic and advanced options couldn't be offered (although they couldn't be run at the same time). The instructions could just prompt the particiapnt to open one of two Python files depending on which activity they wanted to try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment