Created
November 21, 2011 15:32
-
-
Save maebert/1382962 to your computer and use it in GitHub Desktop.
Dancing Mouse Mail Notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Arduino Mail Notification | |
* http://www.portwempreludium.de | |
* | |
* See http://portwempreludium.tumblr.com/post/13068255903/dancing-mouse for details. | |
* Based on the XMAS hitcounter by tinkerlog.com | |
*/ | |
int motorPin = 13; // control pin for servo motor | |
int mails = 0; // number of new mails | |
long danceDuration = 1500; // how long to dance | |
long danceBreak = 1000; // time between to dances | |
void setup() { | |
pinMode(motorPin, OUTPUT); // Set motor pin as an output pin | |
Serial.begin(9600); | |
} | |
void loop() { | |
if (Serial.available() > 0) { | |
// check for mails | |
mails = Serial.read() | |
// If we have mails, perform a dance! | |
for (i = 0; i<mails; i++) { | |
digitalWrite(motorPin, HIGH); | |
delay(danceDuration); | |
digitalWrite(motorPin, LOW); | |
delay(danceBreak); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
Arduino Mail indicator | |
http://portwempreludium.de | |
See http://portwempreludium.tumblr.com/post/13068255903/dancing-mouse for details. | |
""" | |
import getpass | |
import imaplib | |
import argparse | |
import serial | |
import time | |
class DancingMouse: | |
def __init__(self, port='/dev/ttyUSB0', imap='imap.googlemail.com', interval=1): | |
# usb serial connection to arduino | |
self.arduino = serial.Serial(port, 9600) | |
# Connect to IMAP server | |
self.mail = imaplib.IMAP4(imap) | |
self.mail.login(getpass.getuser(), getpass.getpass()) | |
self.last_count = 0 | |
self.last_count = self.count() | |
self.interval = interval | |
def run(self): | |
# Loop and check again | |
while (True): | |
new_mail = self.count() | |
if new_mail: | |
self.notify(new_mail) | |
time.sleep(self.interval) | |
def count(self): | |
type, num = self.mail.select() | |
result = int(num[0]) - self.last_count | |
self.last_count = int(num[0]) | |
return result | |
def notify(self, number): | |
print "You've got mail!" | |
self.arduino.write(chr(ord(chr(number)))) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description=u'Set up Dancing Mouse') | |
parser.add_argument('-p', '--port', dest='port', action='store', default='/dev/tty.usbserial-A6004nY5', help='Serial port of the Arduino') | |
parser.add_argument('-m', '--imap', dest='imap', action='store', default='imap.serv.uos.de', help='IMAP server') | |
parser.add_argument('-i', '--interval', dest='interval', action='store', type=int,default=30,help='Interval between checks in seconds') | |
args = parser.parse_args() | |
mouse = DancingMouse(interval = args.interval, port = args.port, imap = args.imap) | |
mouse.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment