I combined the 16x2 LCD tutorial with the email LED tutorial to get the LCD to display the RasPi's IP (I was running headless) and how many unread emails I had in my Gmail inbox. Here's what I came up with:
Code: Select all
#!/usr/bin/python
from Adafruit_CharLCD import Adafruit_CharLCD
from subprocess import *
from time import sleep, strftime
from datetime import datetime
#!usr/bin/env python
import RPi.GPIO as GPIO, feedparser, time
lcd = Adafruit_CharLCD() #instantiate Adafruit_CharLCD
lcd.begin(16,1)
gmail_user = "user" #gmail username without @gmail.com
gmail_pass = "pass" #plaintext gmail password
cmd = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1" #what is the IP address
#run the shell command to do our bidding
def run_cmd(cmd):
p = Popen(cmd, shell=True, stdout=PIPE)
output = p.communicate()[0]
return output
#the main loop
while True:
mail_count = int(feedparser.parse("https://"+gmail_user+":"+gmail_pass +"@mail.google.com/gmail/feed/atom")["feed"]["fullcount"]) #check email
ip_address = run_cmd(cmd) #check IP address
lcd.clear()
#lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n')) #this prints the date and time if you prefer
lcd.message('IP %s'%(ip_address)) #output IP address to LCD
lcd.message('%s unread email'%(mail_count)) #output unread email count to LCD
sleep(60) #refresh once every minute or so
Happy hacking!