树莓派 报IP 和 lcd显示IP
接 16x2 LCD
准备
由于我使用的是pidora, 所以很多库默认都没装(貌似是没wheezy方便,不过习惯了centos)
播放声音本想用mpc的,但安装完总有问题,于是就编译了一个 madplay
yum -y install alsa-utils alsa-lib-devel
至于TTS嘛,木有,就自己从网上下载了几段,然后拼起来播放。
for i in {0..255}; do wget -O $i.mp3 http://tts-api.com/tts.mp3?q=$i; done
实现代码
#!/usr/bin/env python
# encoding: utf-8
import Adafruit_CharLCD
import subprocess, time, datetime, math, signal, sys, os
def get_ip(device):
ip = subprocess.check_output("ip -4 addr show " + device + " | grep inet | awk '{print $2}' | cut -d/ -f1", shell = True).strip()
return ip
def main():
last_ip = ip = ''
maxLen = 16
while True:
ip = get_ip('eth0')
if ip == last_ip:
time.sleep(10)
continue
last_ip = ip
sounds = ' /root/Music/tts/dot.mp3 '.join(['/root/Music/tts/' + i + '.mp3' for i in last_ip.split('.')]).split(' ')
spaceLen = maxLen - len(ip)
ip = int(math.floor(spaceLen / 2)) * ' ' + ip
lcd.clear()
lcd.message(' Raspberry Pi \n')
lcd.message(ip)
subprocess.call(['madplay', '/root/Music/tts/your_ip_address_is.mp3'] + sounds)
time.sleep(10)
# + chr(0xdf) + 'C'
# while True:
# time.sleep(1)
# lcd.clear()
# lcd.message(datetime.datetime.now().strftime(' %I : %M : %S \n%a %b %d %Y'))
def onShutdown(sig, id):
lcd.clear()
time.sleep(.1)
sys.exit(0)
if __name__ == '__main__':
pid = os.fork()
if pid == 0:
signal.signal(signal.SIGTERM, onShutdown)
lcd = Adafruit_CharLCD.Adafruit_CharLCD()
main()
else:
sys.exit(0)
兄弟,我的LCD1602是有I2C的板子的,这个怎么接啊?我查了半天资料,都没有写,我找到树莓派的GPIO接口,也接入了我的LCD,就是不知道该怎么驱动SDL和SDA显示我想要的内容。。。能否提供我点儿帮助呀?