-
Notifications
You must be signed in to change notification settings - Fork 0
/
I2CLCD1602.py
56 lines (48 loc) · 1.75 KB
/
I2CLCD1602.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
########################################################################
# Filename : I2CLCD1602.py
# Description : Use the LCD display data
# Author : freenove
# modification: 2018/08/03
########################################################################
from PCF8574 import PCF8574_GPIO
from Adafruit_LCD1602 import Adafruit_CharLCD
from time import sleep, strftime
from datetime import datetime
def get_cpu_temp(): # get CPU temperature and store it into file "/sys/class/thermal/thermal_zone0/temp"
tmp = open('/sys/class/thermal/thermal_zone0/temp')
cpu = tmp.read()
tmp.close()
return '{:.2f}'.format( float(cpu)/1000 ) + ' C'
def get_time_now(): # get system time
return datetime.now().strftime(' %H:%M:%S')
def loop():
mcp.output(3,1) # turn on LCD backlight
lcd.begin(16,2) # set number of LCD lines and columns
while(True):
#lcd.clear()
lcd.setCursor(0,0) # set cursor position
lcd.message( 'CPU: ' + get_cpu_temp()+'\n' )# display CPU temperature
lcd.message( get_time_now() ) # display the time
sleep(1)
def destroy():
lcd.clear()
PCF8574_address = 0x27 # I2C address of the PCF8574 chip.
PCF8574A_address = 0x3F # I2C address of the PCF8574A chip.
# Create PCF8574 GPIO adapter.
try:
mcp = PCF8574_GPIO(PCF8574_address)
except:
try:
mcp = PCF8574_GPIO(PCF8574A_address)
except:
print ('I2C Address Error !')
exit(1)
# Create LCD, passing in MCP GPIO adapter.
lcd = Adafruit_CharLCD(pin_rs=0, pin_e=2, pins_db=[4,5,6,7], GPIO=mcp)
if __name__ == '__main__':
print ('Program is starting ... ')
try:
loop()
except KeyboardInterrupt:
destroy()