e-Paperにいろいろ表示する。



―――――

Pythonのデモ用スクリプトを使って表示をカスタマイズしてみました。

時間の表示の後に日時を表示するようにしてみました。

time.strftime(‘%H:%M:%S – %Y/%m/%d’)

■[Python] 日時に関するtimeとdatetimeのまとめ
https://qiita.com/flcn-x/items/0c84afb30dc62aa97e3d

―――――

Pythonの標準ライブラリにあったsocketを使ってIPアドレス表示するとlocalの127.0.1.1を表示してしまう。OSや機器によってはちゃんとDNSから割り振られたIPアドレスを表示するらしいのですが。
PCはだいたい、IPアドレスを複数持っています。自分自身を表す固定の127.0.1.1とネットワークに接続された時に割り振られたIPアドレスで、ノートPCだとWifiと物理ケーブルで別のIPアドレスが割り振られたりします。

import socket
print(socket.gethostbyname(socket.gethostname()))

今回使用しているRaspberryPiZeroWも同じような問題を抱えている可能性があるので検索してみました。複数のIPアドレスを取得できるようにと思っていくつか探していたら、netifacesという度良いライブラリが見つかりました。
インストールが必要で、pip install netifacesというコマンドをつかって見たものの、プログラムでエラーとなるので以下のコマンドでインストールしなおしました。

pip3 install netifaces

こちらのコマンドで問題が解決しました。今後はpipのコマンドはpip3と置き換えて使用したほうが良いかもしれません。カッコ内のwlan0はワイアレスのネットワークに割り振られる記号。有線LANの場合はeth0となります。JetsonNanoは有線接続なのでeth0を使います。また、有線LANポートが複数ある場合はeth0、eth1、eth2と割り振られます。

ipif= netifaces.ifaddresses(‘wlan0’)
ip = netifaces.ifaddresses(‘wlan0’)[netifaces.AF_INET][0][‘addr’]

―――――

上記のPythonをRaspberryPiが起動した時に表示されるようにすれば、動的IPのためにアドレスが変更されても、いちいちモニタとマウスとキーボードを繋いでIPを確認しなくても良くなり、実用的になります。と言う事で情報を探していたところCronとrc.localの情報を見つけました。

■Raspberry Piでプログラムを自動起動する5種類の方法を比較・解説
https://qiita.com/karaage0703/items/ed18f318a1775b28eab4

■Raspberry Piで cronを使って起動時にコマンドを自動実行したりn分間隔で繰り返し実行する方法
http://www.neko.ne.jp/~freewing/raspberry_pi/raspberry_pi_cron_crontab_autoexec_script/

コマンドでcrontab -eと入力するとテキストエディタで設定ファイルが開きます。幾つか試したのですが、どうもrebootでは表示できない様子。ログを見ると動作されているようではあるのですが。10分ごとに実行するコマンドは問題なく動作しました。

@reboot
→起動した時に続けて記入したコマンドが実行されます。

*/10 * * * *
→10分ごとに続けて記入したコマンドが実行されます。

起動した時に表示する方法としてrc.localを編集する方法もあったので試してみましたが、こちらも起動の際に画面に表示させることはできませんでした。

sudo nano /etc/rc.local

rc.localに記載する方法ですが、検索していると互換性の為に残してある機能なので積極的に使わない方が良いと言うような書き方もされていて使いどころが難しい機能なのかもしれません。

―――――

■改造したpythonのコード

―白黒e-Paper

#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
import os
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), ‘pic’)
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), ‘lib’)
if os.path.exists(libdir):
sys.path.append(libdir)

import logging
from waveshare_epd import epd2in13_V2
import time
from PIL import Image,ImageDraw,ImageFont
import traceback
import netifaces

logging.basicConfig(level=logging.DEBUG)

try:

logging.info(“epd2in13_V2 Demo”)

epd = epd2in13_V2.EPD()
logging.info(“init and Clear”)
epd.init(epd.FULL_UPDATE)

# Drawing on the image
font15 = ImageFont.truetype(os.path.join(picdir, ‘Font.ttc’), 15)
font24 = ImageFont.truetype(os.path.join(picdir, ‘Font.ttc’), 24)

#logging.info(“1.Drawing on the image…”)
image = Image.new(‘1’, (epd.height, epd.width), 255) # 255: clear the frame
draw = ImageDraw.Draw(image)

#draw
draw.rectangle((0, 0, 250, 122), fill = 255)
ip = netifaces.ifaddresses(‘wlan0’)[netifaces.AF_INET][0][‘addr’]
draw.text((90,95), ip, font = font24, fill = 0)
now_time = time.strftime(‘%H:%M:%S – %Y/%m/%d’)
logging.info(now_time)
draw.text((5,5), now_time, font = font24, fill = 0)
epd.display(epd.getbuffer(image))

except IOError as e:

logging.info(e)

except KeyboardInterrupt:

logging.info(“ctrl + c:”)
epd2in13_V2.epdconfig.module_exit()
exit()

―三色e-Paper

#!/usr/bin/python
# -*- coding:utf-8 -*-
import sys
import os
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), ‘pic’)
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), ‘lib’)
if os.path.exists(libdir):
sys.path.append(libdir)

import logging
from waveshare_epd import epd2in13bc
import time
from PIL import Image,ImageDraw,ImageFont
import traceback
import netifaces

logging.basicConfig(level=logging.DEBUG)

try:

logging.info(“epd2in13bc Demo”)

epd = epd2in13bc.EPD()
logging.info(“init and Clear”)
epd.init()
epd.Clear()

# Drawing on the image
logging.info(“Drawing”)
font20 = ImageFont.truetype(os.path.join(picdir, ‘Font.ttc’), 20)
font18 = ImageFont.truetype(os.path.join(picdir, ‘Font.ttc’), 18)

# Drawing on the Horizontal image
logging.info(“1.Drawing on the Horizontal image…”)
HBlackimage = Image.new(‘1’, (epd.height, epd.width), 255) # 298*126
HRYimage = Image.new(‘1’, (epd.height, epd.width), 255) # 298*126 ryimage: red or yellow image
drawblack = ImageDraw.Draw(HBlackimage)
drawry = ImageDraw.Draw(HRYimage)
#drawblack.text((10, 0), ‘hello world’, font = font20, fill = 0)
drawry.rectangle((0, 25, 212, 80), fill = 0)
ip = netifaces.ifaddresses(‘wlan0’)[netifaces.AF_INET][0][‘addr’]
drawblack.text((70, 80), ip, font = font20, fill = 0)
now_time = time.strftime(‘%H:%M:%S – %Y/%m/%d’)
drawblack.text((5, 0), now_time, font = font20, fill = 0)
epd.display(epd.getbuffer(HBlackimage), epd.getbuffer(HRYimage))

except IOError as e:

logging.info(e)

except KeyboardInterrupt:

logging.info(“ctrl + c:”)
epd2in13bc.epdconfig.module_exit()
exit()

―――――

コメント

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です