This section is an extension of the digital clock that you have developed in the previous section. In addition to all the components mentioned in the previous section, you will require one green LED and one red LED for displaying the status. In this section, you will develop an e-mail notifier, which will turn red if you have any unread e-mails and it will turn green if you have no unread e-mails. This will also show the count of unread messages on the LCD by using the code that you have developed in the previous section.
As this is an extension of the previous section, first make sure you have connected the LCD pins and Raspberry GPIO pins as mentioned previously. In addition to the connection you have made, you have to connect two LEDs at Raspberry Pi Pin 14 and Pin 15. Connect the anode of the green LED and red LED to Pin 14 and Pin 15 respectively. Similarly, connect the cathode of both LEDs to GND. Complete the connection diagram, as shown here:
Once you have connected the Raspberry Pi pins with LCD pins and LED pins, create a new file as EmailNotifier.py
.
Here is the code that needs to be copied in the EmailNotifier.py
file:
#!/usr/bin/python import threading , feedparser, time import RPi.GPIO as GPIO from time import strftime from datetime import datetime from time import sleep USERNAME = "username" # just the part before the @ sign, add yours here PASSWORD = "password" GREEN_LED = 14 RED_LED = 15 class HD44780: def __init__(self, pin_rs=7, pin_e=8, pins_db=[18,23,24,25]): self.pin_rs=pin_rs self.pin_e=pin_e self.pins_db=pins_db GPIO.setmode(GPIO.BCM) GPIO.setup(self.pin_e, GPIO.OUT) GPIO.setup(self.pin_rs, GPIO.OUT) GPIO.setup(GREEN_LED, GPIO.OUT) GPIO.setup(RED_LED, GPIO.OUT) for pin in self.pins_db: GPIO.setup(pin, GPIO.OUT) self.clear() def clear(self): # Blank / Reset LCD """ self.cmd(0x33) self.cmd(0x32) self.cmd(0x28) self.cmd(0x0C) self.cmd(0x06) self.cmd(0x01) def cmd(self, bits, char_mode=False): # Send command to LCD """ sleep(0.001) bits=bin(bits)[2:].zfill(8) GPIO.output(self.pin_rs, char_mode) for pin in self.pins_db: GPIO.output(pin, False) for i in range(4): if bits[i] == "1": GPIO.output(self.pins_db[i], True) GPIO.output(self.pin_e, True) GPIO.output(self.pin_e, False) for pin in self.pins_db: GPIO.output(pin, False) for i in range(4,8): if bits[i] == "1": GPIO.output(self.pins_db[i-4], True) GPIO.output(self.pin_e, True) GPIO.output(self.pin_e, False) def message(self, text="' '+datetime.now().strftime('%H:%M:%S')"): # Send string to LCD. Newline wraps to second line""" for char in text: if char == ' ': self.cmd(0xC0) # next line else: self.cmd(ord(char),True) if __name__=='__main__': while True: lcd = HD44780() newmails = int(feedparser.parse("https://" + USERNAME + ":" + PASSWORD +"@mail.google.com/gmail/feed/atom")["feed"]["fullcount"]) print "You have", newmails, "new emails!" lcd.message(" You have "+ str(newmails) +" mails .") if newmails> 0: GPIO.output(GREEN_LED, True) GPIO.output(RED_LED, False) print 'Green' else: GPIO.output(GREEN_LED, False) GPIO.output(RED_LED, True) print 'red' time.sleep(60)
Copy the preceding code in the EmailNotifier.py
file and then run it using the following command:
sudo python EmailNotifier.py
As mentioned earlier, this is an extension of the previous project, so you already have the understanding of HD44780
class, which is given as follows:
if __name__=='__main__': while True: lcd = HD44780() newmails = int(feedparser.parse("https://" + USERNAME + ":" + PASSWORD +"@mail.google.com/gmail/feed/atom") ["feed"]["fullcount"]) print "You have", newmails, "new emails!" lcd.message(" You have "+ str(newmails) +" mails .") if newmails> 0: GPIO.output(GREEN_LED, True) GPIO.output(RED_LED, False) print 'Green' else: GPIO.output(GREEN_LED, False) GPIO.output(RED_LED, True) print 'red' time.sleep(60)
There are two additions in this section:
This will also show the count of unread e-mails on the LCD. If the user has 0 unread e-mails, then the green LED will turn ON by sending True
to GREEN_LED
and sending False
to RED_LED
; otherwise, the red LED will remain ON. It will check the status of unread e-mail every single minute. One minute difference between each execution has been introduced using the time.sleep(60)
command.
3.14.247.77