kuroの覚え書き

96の個人的覚え書き

Raspberry piにキャラクタLCDをつける(2)

とりあえず起動したらDHCPで拾ったアドレスなり、接続したESSIDなりを取得してLCDに表示させるようにしてみた。
f:id:k-kuro:20210601005934j:plain

#!/usr/bin/python3
# coding: utf-8
import subprocess
import Adafruit_CharLCD as LCD
import time

# GPIO
lcd_rs        = 17
lcd_en        = 27
lcd_d4        = 11
lcd_d5        = 22
lcd_d6        = 9
lcd_d7        = 10

lcd_columns = 8
lcd_rows    = 2

# Adafruit_CharLCD
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
                       lcd_columns, lcd_rows)
# reset lcd
lcd.clear()

cmd='ip a show eth0 | grep "inet " |cut -f6,13 -d " " > eth0.txt'
subprocess.call(cmd, shell=True)
with open('eth0.txt') as f:
    ip1=f.read()
cmd='ip a show wlan0 | grep "inet " |cut -f6,12 -d " " > wlan0.txt'
subprocess.call(cmd, shell=True)
with open('wlan0.txt') as f:
    ip2=f.read()
cmd='iwconfig wlan0 | grep "ESSID" |cut -f9 -d " " > essid.txt'
subprocess.call(cmd, shell=True)
with open('essid.txt') as f:
    ip3=f.read()

# Demo scrolling message right/left.
lcd.set_cursor(7,0)
lcd.message(ip1)
for i in range(lcd_columns+len(ip1)):
    time.sleep(0.5)
    lcd.move_left()
time.sleep(1.0)
lcd.clear()
lcd.set_cursor(7,0)
lcd.message(ip3)
for i in range(lcd_columns+len(ip3)):
    time.sleep(0.5)
    lcd.move_left()
time.sleep(1.0)
lcd.clear()
lcd.set_cursor(7,0)
lcd.message(ip2)
for i in range(lcd_columns+len(ip2)):
    time.sleep(0.5)
    lcd.move_left()
lcd.clear()