Raspberry PiにふられたIPアドレスをHDMIディスプレイ以外で知る方法 (3) LCD

I2C接続のLCDモジュールAQM1602AXにIPアドレスを表示

 こちらの記事に掲載したように、ラズパイのI2CラインにそのままAQM1602AXをつないでも、認識しませんでした。バス・リピータPCA9515ADを利用します。

 前回の記事でも利用しましたが、IPアドレスはhostname -Iを実行した結果を利用します。

LCDモジュールとの接続

プログラム

import commands

 シェル・コマンドの実行モジュールを利用します。Python2.6で撤廃されたと説明されていますが、まだ利用できます。

commands.getoutput("hostname -I")[0:15]

 IPアドレスとMACアドレスが返ってきます。IPアドレスの全桁は最大15文字なので、[0:15]で先頭から15文字を切り取ります。

#!usr/bin/env python
# -*- coding: utf-8 -*-
import smbus
import time
import commands

i2c = smbus.SMBus(1) # 1 is bus number
addr02=0x3e #lcd
_command=0x00
_data=0x40
_clear=0x01
_home=0x02
display_On=0x0f
LCD_2ndline=0x40+0x80


# 初期化+画面初期化 LCD AQM1602
i2c.write_byte_data(addr02, _command, 0x38)
time.sleep(0.2)
i2c.write_byte_data(addr02, _command, 0x39)
time.sleep(0.1)
i2c.write_byte_data(addr02, _command, 0x14)
time.sleep(0.1)
i2c.write_byte_data(addr02, _command, 0x73)
time.sleep(0.1)
i2c.write_byte_data(addr02, _command, 0x56)
time.sleep(0.2)
i2c.write_byte_data(addr02, _command, 0x6c)
time.sleep(0.2)
i2c.write_byte_data(addr02, _command, _home) # 
time.sleep(0.1)
i2c.write_byte_data(addr02, _command, _clear)
time.sleep(0.1)
i2c.write_byte_data(addr02, _command, display_On)
time.sleep(0.1)

#main
IPaddr = commands.getoutput("hostname -I")[0:15]
print IPaddr
mojilist=[]
for moji in IPaddr:
        mojilist.append(ord(moji))
# print moji,hex(ord(moji)),mojilist
i2c.write_i2c_block_data(addr02, _data, mojilist)

 実行結果です。