Last active
August 29, 2015 14:12
-
-
Save alukach/6af0f30c0f60df09f82d to your computer and use it in GitHub Desktop.
BeagleBone LCD Thermometer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
This script assumes that you have a LCD connected to a RaspberryPi or BeagleBone | |
Black as as described in this tutorial: | |
https://learn.adafruit.com/character-lcd-with-raspberry-pi-or-beaglebone-black | |
along with a temperature sensor as described in this tutorial: | |
https://learn.adafruit.com/measuring-temperature-with-a-beaglebone-black | |
""" | |
from collections import deque | |
import datetime | |
import sys | |
import time | |
import Adafruit_BBIO.ADC as ADC | |
import Adafruit_CharLCD as LCD | |
# Raspberry Pi pin configuration: | |
# lcd_rs = 27 # Note this might need to be changed to 21 for older revision Pi's. | |
# lcd_en = 22 | |
# lcd_d4 = 25 | |
# lcd_d5 = 24 | |
# lcd_d6 = 23 | |
# lcd_d7 = 18 | |
# lcd_backlight = 4 | |
# BeagleBone Black configuration: | |
lcd_rs = 'P8_8' | |
lcd_en = 'P8_10' | |
lcd_d4 = 'P8_18' | |
lcd_d5 = 'P8_16' | |
lcd_d6 = 'P8_14' | |
lcd_d7 = 'P8_12' | |
lcd_backlight = 'P8_7' | |
# 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, lcd_backlight) | |
# Initialze the temp sensor | |
sensor_pin = 'P9_40' | |
ADC.setup() | |
class LineScroller(object): | |
""" Scroll text across LCD """ | |
def __init__(self, max_len, pause_len): | |
self.pause_len = pause_len | |
self.max_len = max_len | |
self.offset = 0 | |
self.pause = 0 | |
self.forward = True | |
def output(self, txt): | |
output = txt[self.offset:(self.max_len + self.offset)] | |
self._prep_next_position(txt) | |
return output | |
def _prep_next_position(self, txt): | |
# Pass on pause | |
if self.pause: | |
self.pause -= 1 | |
return | |
# Update offset | |
self.offset += 1 if self.forward else -1 | |
# Reverse direction at string bounds | |
if ((self.offset + self.max_len) == len(txt)) or (self.offset == 0): | |
self.forward = not self.forward | |
self.pause = self.pause_len | |
def get_datetime_str(): | |
date_out = datetime.datetime.now().strftime('%B %d, %Y') | |
time_out = datetime.datetime.now().strftime('%I:%M%p').lstrip('0') | |
return '{time}, {date}'.format( | |
date=date_out, | |
time=time_out, | |
) | |
def get_temp_str(buf=None): | |
""" | |
Get temperature readings from sensor. Optionaly, pass in a deque buffer | |
with a fixed length to base measurements on average of past N readings. | |
""" | |
buf = buf if buf is not None else [] | |
reading = ADC.read(sensor_pin) | |
buf.append(reading * 1800) # 1.8V reference = 1800 mV | |
millivolts = sum(buf)/len(buf) | |
temp_c = (millivolts - 500) / 10 | |
temp_f = (temp_c * 9/5) + 32 | |
temp_out = 'C={c}{deg} F={f}{deg}' | |
return temp_out.format( | |
c=round(temp_c, 1), | |
f=round(temp_f, 1), | |
deg='' | |
).ljust(lcd_columns) | |
line_one = LineScroller(max_len=lcd_columns, pause_len=5) | |
temp_buf = deque(maxlen=5) | |
while True: | |
try: | |
out = [] | |
out.append(line_one.output(get_datetime_str())) | |
out.append(get_temp_str(temp_buf)) | |
# Print a two line message | |
output = '\n'.join(out) | |
lcd.set_cursor(0,0) | |
lcd.message(output) | |
time.sleep(0.5) | |
except KeyboardInterrupt: | |
sys.stdout.write('\nExiting.\n') | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment