Online Sales Physical Notification with a Raspberry Pi 
Friday, January 24, 2014, 15:15
Posted by Administrator
Selling no longer wanted things from my apartment online with Amazon and Ebay is one of my new favorite hobbies. I take pride in getting quality items out for sale in a timely manner but it has been a constant drag to be checking my email account hundreds of times a day to see if I have sold anything. I’ve been mentally working on an idea to have my ceiling mounted LEDs turn on every time I have a new sale to ship out. I finally sat down to implement this project a few nights ago and was shocked when it only took me about 20 minutes to get everything working the way I wanted.

The hardware component of this is shown below:

Image Removed

This is the set of LED’s that I have attached to the ceiling with thumbtacks and some cardboard. I’ve lately added the green strand of LED leaves that I got at the dollar store. I’ve been controlling these lights with the same “LazierGeek” app I use to turn my binary clock on and off with. The only difference now is that I also let the following scripts turn on the light if they want to.
curl -u username:password --silent "https://mail.google.com/mail/feed/atom" | tr -d '\n' | awk -F '<entry>' '{for (i=2; i<=NF; i++) {print $i}}' | sed -n "s/<title>\(.*\)<\/title.*name>\(.*\)<\/name>.*/\2 - \1/p

This is a very hand bash script making use of curl that I found online which scrapes Gmail’s atom feed for unread emails and does some sed manipulation to trim down the output to just the senders name and subject. To get this one working just replace the username and password section with your appropriate account information. In conjunction with this script I made the following Python code to run it occasionally.
#!/usr/bin/env python
"""
Unread email notifier - Spike Snell - 2014
Version 1.5
"""
# Import what we need to drive the light
import time, sys, subprocess
import RPi.GPIO as GPIO
from datetime import datetime

# Set Up the GPIO to use our notification light
GPIO.setwarnings(False) # Set warnings to false
GPIO.setmode(GPIO.BOARD) # Use board pin numbering
GPIO.setup(5, GPIO.OUT) # Setup GPIO Pin 8 to OUT

# Loop Forever and Drive the Notification Light
while True:

GPIO.output(5,True) # Turn off the light by default

# Get the current local machine hour in 24 hour format
h = int(datetime.now().strftime('%H'))

# Make sure it is after 0700 and before 2100
if h > 6 and h < 21:
# The time is just right so check for sales with bash script
proc = subprocess.Popen(["bash","unread.sh"], stdout=subprocess.PIPE)
(out, err) = proc.communicate()

# Check if there are recent unread mail subjects that indicate a sale
if out.find("ship now:") > 0 or out.find("item sold!") > 0:
GPIO.output(5,False) # Turn on the light

time.sleep(600) # Wait ten minutes
To run this notifier I use the command " sudo nohup python unread.py & " this lets me run the script in the background as a nohup ( no hang up ) process where the output is logged to nohup.out so that I can monitor the state of it remotely if I wish. Every 600 seconds ( 10 minutes ) this script will wake up from its slumber and fire the Gmail scraping bash script. If any of the subjects match text information indicating that an online sale has occurred it turns the LEDs on. If no sales have occurred lately it sets the LEDs to their off position. One handy feature of this is that after I have checked my email to see what sale occurred the lights automatically turn themselves off after a while since the email is no longer unread at that point. I might increase the delay in checking for sales since I’m a little hesitant to be polling Gmail for information this frequently and don’t want to be a nuisance to a service I rely so heavily on.

The only complaint I have about my notification system so far is that it is annoying when it comes on in the middle of the night when I am trying to sleep and wakes me up. I'm probably going to modify this code tonight so that it doesn't come on at night even if a sale has occurred but then operates normally if it is daytime.

So far this sales notification light has been working splendidly. Future expansions could include making the light blink to indicate how many sales have occurred or to have the script also text my phone to let me know that something sold. I’d love to hear any ideas or comments that the rest of you have about this small project.

-- Edit 2.11.14

I have updated the Python portion of the code to reflect my latest version which doesn't turn on at night. This one is much more straightforward because the indicator light's off position is the default state. Periodically the light will turn off briefly and then back on when going through another cycle if there is an active sale; this operational feature is to make it even more obvious that a sale has occurred.
5 comments ( 73350 views )   |  permalink   |  related link   |  $star_image$star_image$star_image$star_image$star_image ( 3 / 6591 )

<<First <Back | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | Next> Last>>