Raspberry Pi and Official NFL Score Board API

Now that I’ve got my hands on a Raspberry Pi 3 Model B Motherboard, I decided nows a good time to play around with the 16×2 LCD Module Controller HD44780 I had laying around (I’ve had this thing since December 2015).

A live NFL (National Football League) score board seemed fitting as the season just started.

I found a really good write up on raspberrypi-spy.co.uk about wiring up the controller and Pi, here is the diagram I used:

raspberry_pi_circuit

The code to power this controller is simple Python (pushed to Github):

#!/usr/bin/env python

import time

import Adafruit_CharLCD as LCD
import requests

# Raspberry Pi pin configuration:
lcd_rs = 7
lcd_en = 8
lcd_d4 = 25
lcd_d5 = 24
lcd_d6 = 23
lcd_d7 = 18

# Define LCD column and row size for 16x2 LCD.
lcd_columns = 16
lcd_rows = 2

# Initialize the LCD using the pins above.
lcd = LCD.Adafruit_CharLCD(
  lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
  lcd_columns, lcd_rows
)

# Start fetch / display loop
while True:

  # fetch current score from NFL api
  res = requests.get('http://www.nfl.com/liveupdate/scorestrip/ss.json')

  if res:

    for game in res.json().get('gms', []):

      # parse our teams and scores
      home_team = '%s %s' % (game['h'], game['hnn'])
      home_team = home_team[:13]
      home_team_score = game['hs']

      away_team = '%s %s' % (game['v'], game['vnn'])
      away_team = away_team[:13]
      away_team_score = game['vs']

      # clear lcd screen
      lcd.clear()

      # write results to lcd screen
      lcd.message('%s %s\n%s %s' % (
          home_team, home_team_score, away_team, away_team_score
        )
      )

      time.sleep(5)

 

SensorTag metrics with Grafana

If you haven’t been following, I’ve done a little research into the Texas Instruments SensorTag starting with post RaspberryPi 3 and SensorTag.

Just the other day I stumbled upon the official Texas Instruments wiki for the SensorTag CC2650, the wiki does a good job outlining the formulas to calculate each sensors value.

This lead me to write a minimal Python class (see on Github) using some of the formulas found in the wiki. Beyond just the Python classes, I wrote in a periodic process; this process will save the sensor data to screen, and write to /root/sensor.json every five seconds.

pi@raspberrypi:~# cat /sensor.json
{
 "pressure": 987.2,
 "humidity": 58.44,
 "temperature": 75.65,
 "light": 19.43
}

To better visualize the gathered data I decided to store all metrics in Telegraf, all I needed was a simple execution plugin:

pi@raspberrypi:~# cat /etc/telegraf/telegraf.d/sensor_tag.conf
[[inputs.exec]]
 command = "cat /sensor.json"
 data_format = "json"
 name_suffix = "_sensor_tag"
 interval = "5s"

Then on my workstation I installed Grafana, and connected Telegraf as a remote data source.

This is the type of data I’ve been gathering and storing from the SensorTag, seems like we could make a weather station pretty easy.

screen-shot-2016-09-17-at-3-42-53-pm

screen-shot-2016-09-17-at-3-43-13-pm

SensorTag Temperature Readings in Python

I wanted to wrap up my previous post (TI SensorTag Temperature Readings) with a little python example, thus this write is going to be short and sweet.

Using the bluepy python library (written by Ian Harvey) I’ve been able to to capture temperature readings, then covert them to Fahrenheit.

To demonstrate I captured a couple temperature samples, a few while sitting on my desk, and a few held up to my air condition vent:

root@raspberrypi:~# python temp.py
79.31 degrees fahrenheit
79.31 degrees fahrenheit
79.31 degrees fahrenheit
78.46 degrees fahrenheit
78.01 degrees fahrenheit
76.1 degrees fahrenheit
76.49 degrees fahrenheit
76.27 degrees fahrenheit
75.99 degrees fahrenheit
76.16 degrees fahrenheit

The Python is pretty straight forward:

#!/usr/bin/env python

from bluepy import btle
from time import sleep

sensor = btle.Peripheral('A0:xx:xx:xx:xx:xx')

sensor.writeCharacteristic(0x24, '\x01', withResponse=True)

sleep(1) # warm up

for i in range(10):
    t_data = sensor.readCharacteristic(0x21)
    msb = ord(t_data[3])
    lsb = ord(t_data[2])
    c = ((msb * 256 + lsb) / 4) * 0.03125
    f = c * 9/5.0 + 32
    print '%s degrees fahrenheit' % round(f, 2)
    sleep(2)

sensor.writeCharacteristic(0x24, '\x00', withResponse=True)

Happy hacking!