「1602 キャラクタ LCDモジュール を STM32 と RaspberryPi で動かす」
https://qiita.com/nanbuwks/items/d5f094e54a3f6641c970
で使用したLCDを使って、いつもお悩みの起動時のIPアドレスの表示をしてみます。
環境
- Raspbian GNU/Linux 10 (buster)
- Raspberry Pi 3 Model B
- Adafruit_CharLCD ライブラリ
- Python 3.7.3
配線
先の記事の通り
LCD PIN | SIGNAL NAME | RaspberryPi3 |
---|---|---|
1 | VSS | GND |
2 | VDD | 3.3V |
3 | VO | (Volume) |
4 | RS | GPIO26 |
5 | R/W | GND |
6 | E | GPIO19 |
7 | D0 | (NC) |
8 | D1 | (NC) |
9 | D2 | (NC) |
10 | D3 | (NC) |
11 | D4 | GPIO20 |
12 | D5 | GPIO21 |
13 | D6 | GPIO22 |
14 | D7 | GPIO27 |
15 | LED(+) | 3.3V |
16 | LED(-) | GND |
インストール
adafruit_character_lcd ライブラリを以下のようにしてインストールします
sudo pip3 install adafruit-circuitpython-charlcd
プログラム
引数でメッセージを表示する汎用プログラムです。
#!/usr/bin/env python
import sys
import Adafruit_CharLCD as LCD
lcd_rs = 26
lcd_en = 19
lcd_d4 = 20
lcd_d5 = 21
lcd_d6 = 22
lcd_d7 = 27
#
lcd_columns = 16
lcd_rows = 2
#
#
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
lcd_columns, lcd_rows)
lcd.clear()
if ( 2 > len(sys.argv)):
lcd.message("No IP address.")
if ( 1 < len(sys.argv)):
message = sys.argv[1]
lcd.message(message)
if ( 2 < len(sys.argv)):
message = sys.argv[2]
lcd.message("\n")
lcd.message(message)
# cursol blink
lcd.blink(False)
自動起動に設定
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# ifconfig eth0:1 169.254.12.24
sleep 5
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
python3 /home/pi/LCDmessage.py $_IP
exit 0
途中でコメントアウトしている、
# ifconfig eth0:1 169.254.12.24
は以下の記事で紹介した、リンクローカルアドレスの割当です。
「Raspbian Buster で eth0:1 にスタティックIPを割り当てる」
https://qiita.com/nanbuwks/items/1433e8203dcf03034a1a
また、sleep 5
は、試したDHCP環境だと有線のIPアドレス取得に時間がかかっていたので、そのためのディレイです。
実行結果
先の記事にあった、HATもろともケースに入れてみました。
ちゃんと、有線と無線のIPアドレスが表示されています。