Saturday, January 4, 2014

Arduino Yun: reading mails with Python: the simple way

All the previous stuff works, but was unnecessarily complicated. It can be done much simpler with Python. The following code will check all unseen messages and print the main 'body' of the mail.


#!/usr/bin/python
import sys
import imaplib
import private.pw
import email

USERNAME = private.pw.myMailUser
PASSWORD = private.pw.myMailPass
MAILTO  = private.pw.myMailTo

# first check if there is new mail
PROTO="https://"
SERVER="mail.google.com"
PATH="/gmail/feed/atom"

n_email = int(feedparser.parse(PROTO + USERNAME + ":" + PASSWORD + "@" + SERVER + PATH)["feed"]["fullcount"])
if n_email > 0:
    print "New mail!"
else:
    print "No new mail!"
    exit(0)

imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
imap_server.login(USERNAME, PASSWORD)
imap_server.select('INBOX')

result, data = imap_server.uid('search', None, "UNSEEN") # get unseen message UIDs
for my_uid in data[0].split():
   my_msg=''
   #result, data = imap_server.uid('fetch',my_uid, '(BODY.PEEK[HEADER.FIELDS (FROM)])')   # spaces are important here !!
   #result, data = imap_server.uid('fetch',my_uid, '(BODY.PEEK[])')   # use this if dont want to flag mail as seen
   result, data = imap_server.uid('fetch',my_uid, '(BODY[])')
   raw_email=data[0][1]
   email_msg = email.message_from_string(raw_email)
   maintype = email_msg.get_content_maintype()
   if maintype == 'multipart':
      for part in email_msg.get_payload():
         if part.get_content_maintype() == 'text':
            my_msg = part.get_payload()
   elif maintype == 'text':
     my_msg = email_msg.get_payload()
   print my_msg
imap_server.close()
imap_server.logout()



Note: feedparser should be installed on the Arduino Yun first. I did this as follows:
  • download it from https://code.google.com/p/feedparser/downloads/list (tar.gz) 
  • copy it to somewhere on the Yun SD card 
  • unzip and untar: 
    • gunzip feedp...
    • tar -xvf feedpa...
  • opkg update 
  • opkg install setuptools 
  • cd to the uncompressed folder 
  • python setup.py install
Note2: you need to have openssl stuff installed for this:
  • opkg update
  • opkg install python-openssl

Thursday, January 2, 2014

Sending email with the Arduino Yun

See thread http://forum.arduino.cc/index.php?topic=187319.msg1414629#msg1414629

In summary:

install the ssl stuff for python:

opkg update
opkg install python-openssl

next you can send mails as follows (using gmail smtp)


#!/usr/bin/python
import smtplib
from email.mime.text import MIMEText
import private.pw

USERNAME = private.pw.myMailUser
PASSWORD = private.pw.myMailPass
MAILTO  = "something@somewhat.com"

msg = MIMEText('Hello,\nMy name is Yun, \nhow are you')
msg['Subject'] = 'Mail from Yun'
msg['From'] = USERNAME
msg['To'] = MAILTO

server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo_or_helo_if_needed()
server.starttls()
server.ehlo_or_helo_if_needed()
server.login(USERNAME,PASSWORD)
server.sendmail(USERNAME, MAILTO, msg.as_string())
server.quit()

The username and passwords are stored in the file pw.py in subdir private

Wednesday, January 1, 2014

ramdisk /tmp

The /tmp directory is a RAM based directory. Means that it doesn't wear out like an SD card, or so...
Obviously when rebooting, you lose all the content. It still is the most obvious place to put temporary files, etc...

To make it look like a seperate drive you can do:

mkdir /tmp/ramdisk

in rc.local.
If you want it to look like a mounted drive you can also

ln -s /tmp/ramdisk /mnt/ramdisk