Last active
May 13, 2017 21:45
-
-
Save stlehmann/9861417 to your computer and use it in GitHub Desktop.
Raspberry Pi - Read temperature, print it to a lcd plate and indicate warnings with backlight color.
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 | |
""" | |
Read the temperature from the connected sensor on the w1 bus and write it | |
to the LCD plate. If the temperature is below TEMP_LIMIT_L the backlight | |
color changes to blue. If the temperature is above TEMP_LIMIT_H the | |
backlight color changes to red. | |
This script uses the Adafruit LCD Plate as output device. Documentation and | |
about infos for Python implementation can be found here: | |
http://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-pi | |
As a temperature sensor a onewire sensor like the DS18B20 can be used. | |
""" | |
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate | |
from time import sleep | |
BACKLIGHT_TIME = 5 | |
TEMP_W1_PATH = "/sys/bus/w1/devices/28-000004ac7761/w1_slave" | |
TEMP_LIMIT_H = 23.0 | |
TEMP_LIMIT_L = 21.5 | |
def read_temp(): | |
tfile = open(TEMP_W1_PATH) | |
text = tfile.read() | |
tfile.close() | |
secondline = text.split("\n")[1] | |
temperaturedata = secondline.split(" ")[9] | |
temperature = float(temperaturedata[2:]) | |
temperature = temperature / 1000 | |
return temperature | |
lcd = Adafruit_CharLCDPlate() | |
ALL_BUTTONS = (lcd.UP, lcd.DOWN, lcd.RIGHT, lcd.LEFT, lcd.SELECT) | |
lcd.numlines=2 | |
lcd.clear() | |
msg = "Temperatur:" | |
lcd.message(msg) | |
c=0 #: backlight counter | |
backlight_color = lcd.ON | |
while True: | |
def change_color(color): | |
global c | |
global backlight_color | |
backlight_color = color | |
c = 0 | |
lcd.backlight(color) | |
#read current temperature | |
temp = read_temp() | |
#switch backlight off after BACKLIGHT_TIME | |
if c >= BACKLIGHT_TIME and not (temp>TEMP_LIMIT_H or temp<TEMP_LIMIT_L): | |
lcd.backlight(lcd.OFF) | |
else: | |
c += 1 | |
#switch backlight on if a button is pressed | |
for b in ALL_BUTTONS: | |
if lcd.buttonPressed(b): | |
change_color(lcd.ON) | |
#choose color acording to temperature | |
if temp < TEMP_LIMIT_L: | |
change_color(lcd.BLUE) | |
elif temp > TEMP_LIMIT_H: | |
change_color(lcd.RED) | |
else: | |
if not backlight_color == lcd.ON: | |
change_color(lcd.ON) | |
lcd.setCursor(0, 1) | |
lcd.message("%.2f C" % temp) | |
sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
anyone having problems with buttons?
i cant wake the lcd with any button
but the buttons work with adafruits lcd test