Created
October 7, 2015 14:16
-
-
Save gwabster/084fcc17939d0ebe7785 to your computer and use it in GitHub Desktop.
Gabrielle's code for moving the servo with firmata
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
import os, sys | |
import time, datetime | |
import imaplib | |
import email | |
import logging | |
import pyfirmata | |
import glob | |
try: | |
from pyfirmata import Arduino, util | |
except ImportError as e: | |
print "Seems like the python firmata library is not installed. Just type: " | |
print "$ sudo pip install pyfirmata" | |
print "To install it and then try again." | |
sys.exit(1) | |
# @TODO change this for the bit before the @ in your email address, so if your email | |
# is J.Modaal@artez.nl, just set this variable to J.Modaal | |
EMAIL_USERNAME = "G.Gijsbers" | |
def find_arduinos(): | |
return glob.glob('/dev/cu.wchusb*') + glob.glob('/dev/tty.usb*') + glob.glob('/dev/cu.usb*') + glob.glob('/dev/ttyUSB*') | |
ard = find_arduinos()[0] | |
print "Trying to use arduino in port", ard | |
# don't forget to change the serial port to suit | |
board = pyfirmata.Arduino(ard) | |
iter8 = pyfirmata.util.Iterator(board) | |
iter8.start() | |
# set up pin D9 as Servo Output | |
servo = board.get_pin('d:9:s') | |
MIN = 5 | |
MAX = 175 | |
def move_servo(a): | |
print "moving servo to: ", a | |
servo.write(a) | |
#board.pass_time(0.3) | |
def cb_new_email(subject, timestamp): | |
""" This function gets called every time there's a new unread email detected """ | |
# Hi Gabrielle: this is where you have to put the code | |
# that will react to any incoming email. | |
# In this case I just print the date and the subject, but you | |
# can send a message to your arduino every time. | |
move_servo(5) | |
move_servo(175) | |
move_servo(5) | |
print "Received email:", timestamp, "//", subject | |
def read_passwd(): | |
""" read you email password form a file called email.secret in your home dir """ | |
fname = os.path.join(os.path.expanduser("~"), "email.secret") | |
print "Trying to read your password from", fname | |
with open (fname, "r") as fin: | |
data = fin.read().strip() | |
return data | |
def get_unread(mark_as_read=False): | |
conn = imaplib.IMAP4_SSL("webmail.artez.nl", 993) | |
imap_password = read_passwd() | |
# coonect to IAMP mail server | |
rv, caps = conn.login(EMAIL_USERNAME, imap_password) | |
# list mailboxes | |
rv, mboxes = conn.list() | |
# select inbox | |
rv, mbox = conn.select("Inbox") | |
# list all unread emails | |
rv, messages = conn.search(None, '(UNSEEN)') | |
if rv == 'OK': | |
for num in messages[0].split(): | |
rv, data = conn.fetch(num,'(RFC822)') | |
if rv != 'OK': | |
print "ERROR getting message", num | |
return | |
msg = email.message_from_string(data[0][1]) | |
if not mark_as_read: | |
rv, data = conn.store(num,'-FLAGS','\\Seen') | |
if rv == 'OK': | |
#print data,'\n',30*'-' | |
#print msg | |
#print 'Message %s: %s' % (num, msg['Subject']) | |
#print 'Raw Date:', msg['Date'] | |
date_tuple = email.utils.parsedate_tz(msg['Date']) | |
if date_tuple: | |
local_date = datetime.datetime.fromtimestamp(email.utils.mktime_tz(date_tuple)) | |
#print "Local Date:", local_date.strftime("%a, %d %b %Y %H:%M:%S") | |
cb_new_email(msg['Subject'], local_date.strftime("%a, %d %b %Y %H:%M:%S")) | |
time.sleep(1) | |
conn.close() | |
def main(): | |
try: | |
while True: | |
get_unread() | |
time.sleep(10) | |
except KeyboardInterrupt, e: | |
logging.info("Seems like you want to exit") | |
finally: | |
# report before finishing | |
logging.info("Finished. Goodbye!") | |
if __name__ == '__main__': | |
#logging.basicConfig(filename="importer.bbcfood.log") | |
logging.basicConfig(level=logging.DEBUG, format="%(asctime)-15s %(message)s") | |
console = logging.StreamHandler() | |
console.setLevel(logging.DEBUG) | |
# add the handler to the root logger | |
logging.getLogger('').addHandler(console) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment