Foginator-Banner-003.jpg

 

Welcome to installment #003 of Project: Foginator 2000 part of the 2015 Raspberry Pi Halloween Project series here at Element14. In this week's episode I am going to cover the basics of getting the Raspberry Pi Sense HatRaspberry Pi Sense Hat up and running, and a very light tutorial on how to push this data to the cloud in order to record and analyze the data. The cool thing about this is that we can simply save all of our acquired data to the cloud, and access it from anywhere!

 

The Hardware

 

Below is a table containing the parts you will need for this segment of the project. In addition to these parts you will need to connect the Raspberry Pi to the internet either via a wifi dongle, or a wired Ethernet connection.

 

Newark Part No.

Notes

Qty

Manufacturer / Description

38Y646738Y6467

RPi

1

RASPBERRY PI 2, MODEL B

38Y647038Y6470

SD Card

1

RASPBERRY PI 8GB NOOBS MICRO SD CARD

44W493244W4932

PSU

1

USB PORT POWER SUPPLY 5V, 1A

06W104906W1049

USB Cable

1

USB A PLUG TO MICRO USB B PLUG

53W628553W6285

WiFi Dongle

1

USB WIFI MODULE

49Y756949Y7569RPi Sense Hat1Raspberry Pi Sense HAT

 

 

The Theory

 

 

The idea behind this part of the project is to log environmental data from the immediate area surrounding the fog machine. Using the Raspberry Pi Sense Hat we will measure and log temperature, humidity, and barometric pressure, and then push it up to the cloud. We will trigger this data logging even every time the fog machine trips, and will record the entire event as a “Trick-Or-Treat Event.” Then we will be able to export the data as a CSV file and analyze what temperatures, humidity levels, and barometric pressure levels correlated with spikes in Trick-Or-Treat events.

 

For the purpose of this blog post, we will be simply figuring out how to read the data from the Sense Hat with our Raspberry Pi. Once we have that figured out we can move on to learning how to push that data to the cloud.

 

 

The Sense Hat

rpisensehat.jpg

 

The new Raspberry Pi Sense Hat was released a few weeks ago, and to be quite honest, I had planned this project out before it was released, and I was quite confused as to what this Hat actually did. The Sense HAT is an add-on board for Raspberry Pi, made especially for the Astro Pi mission that will be performed on the International Space Station in December 2015.

 

 

The Sense HAT has an 8×8 RGB LED matrix, a five-button joystick and includes the following sensors:

  • Gyroscope
  • Accelerometer
  • Magnetometer
  • Temperature
  • Barometric pressure
  • Humidity

To make things simple, the Raspberry Pi Foundation has created Python library providing easy access to everything on the board. You can find that library here.

 

 

Installing Astro Pi / Sense Hat

 

 

Raspberry-Pi-Sense-HAT.jpg

To get started you need to connect the Sense Hat to a Raspberry Pi 2 by placing it on the GPIO Pins. Note the orientation of the board in the image above.

 

Now we need to install the Raspberry PI Sense Hat library package into Raspbian. So SSH into the Raspberry Pi, or open the terminal if you are using your Pi with a Monitor such as the new Raspberry Pi 7-Inch Touch Screen. If you are following along at home, and building your own Foginator, your Raspbian install should already be updated, but just incase run the following commands in the terminal.

 

sudo apt-get update

sudo apt-get upgrade -y

 

 

Then run the following command which will download the necessary package to get the Sense Hat up and running. This process should take less than five minutes on a Raspberry Pi 2, but it could take longer. Do unplug your Raspberry Pi during this process.

 

sudo apt-get install sense-hat

 

Then to finish up the process you need to restart the Raspberry Pi. To do this, run the following command.

 

sudo reboot

 

Once the Pi has had time to reboot, reconnect via SSH or re-open the terminal.

 

 

Testing AstroPi and The Sense Hat

 

2015-09-23-23_48_42-Start.jpg

 

Let’s create a quick Python script to test that everything was installed and connected correctly. Using the Nano text editor, create a new file named sense_test.py. You can do this with the command below.

 

sudo nano sense_test.py

 

Now copy and paste the script below. This script basically tells the sense hat to scroll the words “Hello World” across the Hat’s LED matrix. When you have the code pasted, exit out of nano while keeping the same file name.

 

from sense_hat import SenseHat
sense = SenseHat()
sense.show_message("Hello World")
































 

Now enter the command below to run the script we just created.

 

sudo nano sense_test.py

 

Now you should see Hello World scroll across the LED matrix. If this works, you are ready to move onto the next step. If not, something went wrong with your install of Astro Pi. Go back over the steps to make sure everything is installed and written correctly.

 

 

Acquiring Data From The Sense Hat

 

temp-test-Start.jpg

 

Following some excellent tutorials on the RaspberryPi.org website, I was able to quickly get data from the temperature, air pressure, and humidity sensors. Surprisingly, this is possible with only a handful of lines of Python code. Below is a breakdown of the code and what each line / section does.

 

First need to import the sense_hat library, the time library, and the system library.

 

from sense_hat import SenseHat
import time
import sys





























 

Now we need to initialize the sense hat, and clear its matrix.

 

sense = SenseHat()
sense.clear()





























 

Now we need to set a variable named var, and give it a value of 30. This is used to close the program after it loops for 30 cycles.

 

var = 30





























 

Now we need to create our while loop, and set it to run if var is greater than 0.

 

while var > 0:





























 

The first data we want to import is the ambient temperature. The Sense Hat library makes this super simple, and all we have to do is tell the program to get the temperature. The default output is in degrees celsius, and is extended to several places after the decimal.

 

  temp = sense.get_temperature()





























 

To round that number to a more friendly tenth of a degree, we can simply tell the program to round the temperature output to the first decimal place.

 

  temp = round(temp, 1)





























 

Now we can print our temperature to the terminal. To do this we simply write a print command, and write some text to explain what this output is so that other people can understand what they are reading.

 

  print("Teperature C",temp)





























 

The same methods apply to the humidity and pressure readings as well, so I won’t list each of them out line by line.

 

  humidity = sense.get_humidity()
  humidity = round(humidity, 1)
  print("Humidity :",humidity)





























 

  pressure = sense.get_pressure()
  pressure = round(pressure, 1)
  print("Pressure:",pressure)





























 

Now we need to tell the program to wait for one second before continuing on. This slows down the rate at which the data is read and output. You can slow this rate down by increasing the number after the time.sleep command, or you can speed it up by decreasing the number.

 

  time.sleep(1)





























 

Now we need to tell the program to decrease the var variable by one.

 

  var = var -1





























 

With our var variable decreased by one, we need to check to see if it has reached 0 yet. If it has, we can tell the program to exit. This allows us to poll data for as long as we want, and not have the loop constantly running. Basically the while loop will run over and over until the var variable has decreased to 0.

 

  if var == 0:
    sys.exit()





























 

Putting It All Together

 

Below is the full code we just wrote, without any comments. Nano into the sense_test.py file and delete all of the code that is in it. Then copy and paste the code below, and save the file.

 

from sense_hat import SenseHat
import time
import sys

sense = SenseHat()
sense.clear()

var = 30

while var > 0:
  temp = sense.get_temperature()
  temp = round(temp, 1)
  print("Teperature C",temp)
  humidity = sense.get_humidity()
  humidity = round(humidity, 1)
  print("Humidity :",humidity)
  pressure = sense.get_pressure()
  pressure = round(pressure, 1)
  print("Pressure:",pressure)
  time.sleep(1)
  var = var -1
  if var == 0:
    sys.exit()





























 

Now run the program we just wrote with the following command:

 

sudo python sense_test.py

 

You should see each data set we are looking for, temperature, humidity, and pressure being displayed in the terminal. The Raspberry Pi will poll and display the data for 30-seconds / loops if you stuck with the 1-second delay we wrote into the while statement. With that working, lets move on to getting this data into the cloud.

 

 

Pushing The Data To The Cloud / Internet Of Things

 

 

initial_state_data_foginator.jpg

A while back I received an email from a company called Initial State who wanted me to try out their new cloud-based data visualization solution that is geared towards makers, and programmers alike. Unfortunately I forgot all about Initial State due to some life events taking me away from making things for a few months. I had not thought about Initial State for months until I was writing up one of my Design Challenge Summaries, and wrote about Rick Reynolds’ (RWReynolds) project, Vertically Oriented Modular System. In a comment, Rick mentioned that he  used Initial State to record and log the data from his project, and I decided to use it for mine as well.

 

You will need to go to InitialState.com and signup for a free account. I have a pro-account, but that is because I plan on using it in a lot of my future projects, but the free account should suffice for most.

 

2015-09-24 00_46_17-Start.jpg

I won’t go into the whole process of installing Initial State’s logger onto your Raspberry Pi, as Initial State has an excellent video tutorial on how to do exactly that. They also have a very comprehensive written tutorial on the subject as well. You can find the video below, it’s about an hour long, for the full tutorial, but the first 20 minutes should give you a good understanding of how to make this work.

 

 

Now let’s modify our code to enable it to begin sending data to the InitialState Cloud. So this by using the Nano text editor to edit the sense_test.py script.

 

This time around we need to import the InitialState Streamer library as well.

 

from sense_hat import SenseHat
import time
import sys
from ISStreamer.Streamer import Streamer




















 

Now we need to set up the InitialState Logger, name the bucket for this project, and then enter your access key. Chage the code below to include your key.

 

logger = Streamer(bucket_name="Sense Hat Environment Stream", access_key="YOUR_KEY_HERE")




















 

Our Setup stays the same

 

sense = SenseHat()
sense.clear()
var = 30




















 

Now everywhere we told the program to print the output of a sensor, we need to change that to tell the program to log and send that data to the InitialState bucket we created earlier in the code. Everywhere you wrote “print” before, change it to logger.log.

 

while var > 0:
  temp = sense.get_temperature()
  temp = round(temp, 1)
  logger.log("Teperature C",temp)
  humidity = sense.get_humidity()
  humidity = round(humidity, 1)
  logger.log("Humidity :",humidity)
  pressure = sense.get_pressure()
  pressure = round(pressure, 1)
  logger.log("Pressure:",pressure)
  var = var -1
  time.sleep(30)
  if var == 0:
    sys.exit()




















 

Bringing It All Together

 

The full code is below.

 

from sense_hat import SenseHat
import time
import sys
from ISStreamer.Streamer import Streamer


logger = Streamer(bucket_name="Sense Hat Environment Stream", access_key="zLahwAUqKbNKv6YvuT5JuO58EiUOavDa")


sense = SenseHat()
sense.clear()
var = 30


while var > 0:
  temp = sense.get_temperature()
  temp = round(temp, 1)
  logger.log("Teperature C",temp)
  humidity = sense.get_humidity()
  humidity = round(humidity, 1)
  logger.log("Humidity :",humidity)
  pressure = sense.get_pressure()
  pressure = round(pressure, 1)
  logger.log("Pressure:",pressure)
  var = var -1
  time.sleep(30)
  if var == 0:
    sys.exit()




















 

With the script edited and saved, run it using the command below.

 

sudo python sense_test.py

 

Now you can go to your InitialState account and click on the bucket you just created to see the data. Remember that IntialState free accounts have a very limited amount of events you can stream each month. Each data point is one event. So running this script in its current configuration would create three events per second for thirty seconds, totaling out at 90 events each time its ran.

 

2015-09-24 01_01_32-Start.jpg

I wanted to set this up to read data for several days, and I am one of those kinds of people who need a visual indicator to tell me that things are working as they should. So I once again modified the code to not only run the loop for several days, but also tossed in a few lines that would display a creeper head from MineCraft on the LED matrix at the end of every data collection cycle. If you are interested, that code is below. I won’t go into how I did it, but you can learn more at this link from the Raspberry Pi Foundation.

 

Code With Creeper Head Appearing On LED Matrix

from sense_hat import SenseHat
import time
import sys
from ISStreamer.Streamer import Streamer


logger = Streamer(bucket_name="Sense Hat Environment Stream", access_key="zLahwAUqKbNKv6YvuT5JuO58EiUOavDa")


sense = SenseHat()
sense.clear()
var = 14400


O = (0, 255, 0) # Green
X = (0, 0, 0) # Black


creeper_pixels = [
    O, O, O, O, O, O, O, O,
    O, O, O, O, O, O, O, O,
    O, X, X, O, O, X, X, O,
    O, X, X, O, O, X, X, O,
    O, O, O, X, X, O, O, O,
    O, O, X, X, X, X, O, O,
    O, O, X, X, X, X, O, O,
    O, O, X, O, O, X, O, O
]


black_pixels = [
    X, X, X, X, X, X, X, X,
    X, X, X, X, X, X, X, X,
    X, X, X, X, X, X, X, X,
    X, X, X, X, X, X, X, X,
    X, X, X, X, X, X, X, X,
    X, X, X, X, X, X, X, X,
    X, X, X, X, X, X, X, X,
    X, X, X, X, X, X, X, X
]


while var > 0:
  temp = sense.get_temperature()
  temp = round(temp, 1)
  logger.log("Teperature C",temp)
  humidity = sense.get_humidity()
  humidity = round(humidity, 1)
  logger.log("Humidity :",humidity)
  pressure = sense.get_pressure()
  pressure = round(pressure, 1)
  logger.log("Pressure:",pressure)
  var = var -1
  logger.log("Seconds Until Script Exit",var)
  sense.set_pixels(creeper_pixels)
  time.sleep(5)
  sense.set_pixels(black_pixels)
  time.sleep(25)
  sense.clear()
  if var == 0:
    sys.exit()

















 

Run this code by using the command below. Note the “&” on the end. This tells the Raspberry Pi to run the script in the background and return the command prompt in the terminal. This allows you to continue developing on your Pi while the script runs. Also note that this means that if the Pi loses power, or you reboot it, the script will stop and you will need to re-run it. You can get around this by setting the script to run on startup. A good tutorial on how to do this can be found at the following instructable. Additionally, you can download all of the code used in this project from it's Github repo.

 

initial_state_data_foginator3.jpg

 

If you would like to see the data in real time, that my Raspberry Pi / Sense Hat combo is generating, visit the following link or click the image above.

 

 

So that wraps up part 3 of the Foginator2000 project. This was a really fun portion of the project for me as I got to learn how easy it is to push data to InitialState, as well as how easy it is to use the Raspberry Pi Sense Hat. Hats off to the AstroPi team, and the Raspberry Pi Foundation for creating such a feature-rich and easy to use Pi Hat. Tune in in just a few days for my next installment on the Foginator2000 project. Until then remember to Hack The World and Make Awesome!

 

Win this Kit and Build-A-Long


  1. Project Introduction
  2. Fog Controller Hardware and Test
  3. Environment Sensing Coding & Testing
  4. Ambient Audio Hardware and Coding
  5. Lighting Coding and Testing
  6. October 16th -  Final Assembly and Testing
  7. October 23th - Project Wrap-up