Merge pull request #465 from adafruit/Drive_a_16x2_LCD_with_the_Raspberry_Pi

Drive a 16x2 lcd with the raspberry pi
This commit is contained in:
Limor "Ladyada" Fried 2019-01-09 13:10:20 -05:00 committed by GitHub
commit b2bb765ea7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 0 deletions

View file

@ -0,0 +1,71 @@
from subprocess import Popen, PIPE
from time import sleep
from datetime import datetime
import board
import digitalio
import adafruit_character_lcd.character_lcd as characterlcd
# Modify this if you have a different sized character LCD
lcd_columns = 16
lcd_rows = 2
# compatible with all versions of RPI as of Jan. 2019
# v1 - v3B+
lcd_rs = digitalio.DigitalInOut(board.D22)
lcd_en = digitalio.DigitalInOut(board.D17)
lcd_d4 = digitalio.DigitalInOut(board.D25)
lcd_d5 = digitalio.DigitalInOut(board.D24)
lcd_d6 = digitalio.DigitalInOut(board.D23)
lcd_d7 = digitalio.DigitalInOut(board.D18)
# Initialise the lcd class
lcd = characterlcd.Character_LCD_Mono(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6,
lcd_d7, lcd_columns, lcd_rows)
# looking for an active Ethernet or WiFi device
def find_interface():
find_device = "ip addr show"
interface_parse = run_cmd(find_device)
for line in interface_parse.splitlines():
if "state UP" in line:
dev_name = line.split(':')[1]
return dev_name
# find an active IP on the first LIVE network device
def parse_ip():
find_ip = "ip addr show %s" % interface
find_ip = "ip addr show %s" % interface
ip_parse = run_cmd(find_ip)
for line in ip_parse.splitlines():
if "inet " in line:
ip = line.split(' ')[5]
ip = ip.split('/')[0]
return ip
# run unix shell command, return as ASCII
def run_cmd(cmd):
p = Popen(cmd, shell=True, stdout=PIPE)
output = p.communicate()[0]
return output.decode('ascii')
# wipe LCD screen before we start
lcd.clear()
# before we start the main loop - detect active network device and ip address
sleep(2)
interface = find_interface()
ip_address = parse_ip()
while True:
# date and time
lcd_line_1 = datetime.now().strftime('%b %d %H:%M:%S\n')
# current ip address
lcd_line_2 = "IP " + ip_address
# combine both lines into one update to the display
lcd.message = lcd_line_1 + lcd_line_2
sleep(2)

View file

@ -0,0 +1,4 @@
# Drive_a_16x2_LCD_with_the_Raspberry_Pi
Code to accompany this tutorial:
https://learn.adafruit.com/drive-a-16x2-lcd-directly-with-a-raspberry-pi

View file

@ -0,0 +1,15 @@
[Unit]
Description=LCD date|time|ip
Requires=network-online.target
After=network-online.target
[Service]
ExecStart=/usr/bin/python3 Drive_a_16x2_LCD_with_the_Raspberry_Pi.py
WorkingDirectory=/home/pi
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=network-online.target