Skip to content

Instantly share code, notes, and snippets.

@kgantsov
Created August 24, 2014 12:56
Show Gist options
  • Save kgantsov/cbdfabf992cf4ee12841 to your computer and use it in GitHub Desktop.
Save kgantsov/cbdfabf992cf4ee12841 to your computer and use it in GitHub Desktop.
Gets temperature from USB Temper, show it by LED lighting of BerryClip on Raspberry Pi and log it into a file.
#!/usr/bin/python
# coding: utf-8
import RPi.GPIO as GPIO
import datetime
import logging
import usb
from temperusb import TemperDevice
GREEN_LED_1 = 4
GREEN_LED_2 = 17
YELLOW_LED_1 = 22
YELLOW_LED_2 = 10
RED_LED_1 = 9
RED_LED_2 = 11
MIN_TEMP = 16
MAX_TEMP = 28
LEDS = [
GREEN_LED_1, GREEN_LED_2, YELLOW_LED_1, YELLOW_LED_2, RED_LED_1, RED_LED_2
]
VENDOR_ID = 0x0c45
PRODUCT_ID = 0x7401
logging.basicConfig(
format='%(message)s',
filename='temper.log',
level=logging.INFO,
)
logger = logging.getLogger('temperature')
dev = usb.core.find(idVendor=VENDOR_ID, idProduct=PRODUCT_ID)
TDev = TemperDevice(dev)
def init():
GPIO.setmode(GPIO.BCM)
for led in LEDS:
GPIO.setup(led, GPIO.OUT)
GPIO.output(led, False)
def show_temperature():
old_active_leds = 0
old_temp = 0
while True:
temp = round(TDev.get_temperature(), 1)
active_leds = int(
(temp - MIN_TEMP) / ((MAX_TEMP - MIN_TEMP) / len(LEDS))
)
if active_leds != old_active_leds:
for led in LEDS:
GPIO.output(led, False)
for led in LEDS[:active_leds]:
GPIO.output(led, True)
if temp != old_temp:
time_stop = datetime.datetime.now()
print '%s: %s ºC (Active LEDS: %s)' % (
time_stop.strftime('%Y-%m-%d %H:%M:%S'), temp, active_leds
)
logger.info(
'%s;%s;%s' % (
time_stop.strftime('%Y-%m-%d %H:%M:%S'), temp, active_leds
)
)
old_active_leds = active_leds
old_temp = temp
if __name__ == '__main__':
try:
init()
show_temperature()
except KeyboardInterrupt:
GPIO.cleanup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment