benfreudberg:
I'm not sure if this is related, but typing "ardyun.local/" into a browser does not work.
I'm not sure, but I'm guessing it's not related. For ardyun.local to work, you need to have an mDNS client on your computer (mDNS is also called ZeroConf.) Macs and iOS devices have such a client installed by default. Windows does not, but it can be installed by downloading and installing Apple's "Bonjour Print Services" package, or by downloading and installing iTunes (or probably a few other Apple software packages.) Linux needs to have Avahi installed (that's from memory, the name might not be right.) Last I heard, there was no package available for Android devices.
To answer your mail question, I've not tried ssmtp. I've had good luck using smtplib in Python to send mail, and poplib to read it. I'm sorry, but I don't recall if they are part of the default Python installation, or if there was something I had to install. I wrote the attached Python code as a wrapper to those libraries. Near the beginning of the file is a set of configuration statements you will need to update to match your servers and credentials:
POPSERVER = 'pop.some_domain.com' # Update with your POP server's name (incoming mail server
SMTPSERVER = 'smtp.some_domain.com' # Update with your SMTP server's name (outgoing mail server)
USERNAME = 'yun@some_domain.com' # Update with your login name for the above servers
PASSWORD = 'its_a_secret' # Update with the password for the above servers
EMAIL = 'Yun <yun@some_domain.com>' # Update with the sender name and email address
To use this module in your Python code, you need to import yunEmail, and then call the sendEmail function. For example:
# Send a message via email
#
# Parameters:
# msg - body of message
def sendAlert(msg):
if (not yunEmail.sendEmail( sender = 'sender@some_domain.com',
to = ['somebody@somewhere.com'],
subject = 'Alert Message',
body = msg)):
print "Unable to send message: " + msg
Note that the "to" parameter is actually an array, you can send to multiple people using something like:
to = ['somebody@somewhere.com', 'someone_else@somewhere_else.com'],
To receive mail, you simply call receiveEmail, passing a callback function as a parameter. This will read all incoming mail in the mailbox, and call the callback function for each one. For example:
# Callback function to process received emails
# Called for each individual email received
#
# Parameters:
# sender - Sender of received message
# subject - Subject of receved message
# body - Body of received message
def processEmailMessage(sender, subject, body):
db.logActivity('Email received:
From: {0}
Subj: {1}
Body: {2}'.format(sender, subject, body))
resp = []
if ('<HELP>' in body):
resp.append('Available commands:')
resp.append(' <HELP> - This listing')
resp.append(' <STATUS> - Current system status')
resp.append(' <ON> - Turn output on')
resp.append(' <OFF> - Turn output off')
resp.append('')
if ('<STATUS>' in body):
resp.append('Current system status')
status = db.getStatusStrs()
keys = status.keys()
keys.sort()
for key in keys:
resp.append(' {0:>20}: {1}'.format(key, status[key]))
resp.append('')
if ('<ON>' in body):
db.setCommand('On')
resp.append('Light temporarily turned on')
resp.append('')
if ('<OFF>' in body):
db.setCommand('Off')
resp.append('Light temporarily turned off')
resp.append('')
if (len(resp) > 0):
# Send an email back with the response
if (not yunEmail.sendEmail( to = [sender],
subject = 'Re: ' + str(subject),
body = '\n'.join(resp))):
db.logActivity("Unable to send email response")
#return (len(resp) > 0)
return True
# The beginning of the main code
db = sunsetData.sunData()
db.logActivity("Email handler startup")
lastMailCheckTime = 0
mailCheckInterval = 60
# A loop that repeats forever
while (True):
# Check if it's time to process emails
now = time.time()
if ((now - lastMailCheckTime) > mailCheckInterval):
lastMailCheckTime = now
yunEmail.receiveEmail(processEmailMessage)
# Sleep for a while before checking again
time.sleep(5)
In this example, db is a module that contains a small database of the application state, and provides functions to get status, accept commands, and log activity. This is very much application dependent, and you will likely want to do something completely different. This is just an example of how you can receive emails, react to the contents of the email, and send a response. This is a shortened example, I use this to be able to check on the status of the application running on the Yun, and send it commands, when I am not on the local network and can't access the Yun directly. This way, I can send it emails from anywhere in the world to check in on it. This makes use of a mailbox that is dedicated to the Yun, it's the only thing sending/receiving emails through that mailbox.
All of this is in Python, in a set of scripts that are called from the Yun sketch using the Bridge Library's Process class. This is not a finished set of code, but it should give you some other ideas, and maybe give you a head start to a working solution. (I did make some minor edits to the code to simplify it a bit, and remove confidential information. I didn't compile it after the edits, so it's possible I introduced some minor bugs. It may need some minor corrections if I did something stupid in the edits.)
Note, the forum doesn't allow attaching .py files, so I've renamed it with a .txt extension for the sake of attaching it. It should be renamed to yunEmail.py when putting it on a Yun.
yunEmail.py.txt (3.75 KB)