Arduino Yun project 'Email via Python Script'

Arduino

Scetch: TestSendingFileContentlViaPythonEmail

Projekt sendet Email an vorkonfigurierten Empfänger mittels Python script.
HTML text script hinterlegt. VRC Datagram in vrc_status.txt von SD-Karte

Using Arduino Yun REST-API interface

Sending VRC status Email via REST-API request (http://<server>/arduino/vrc)

TestSendingFileContentlViaPythonEmail.ino

/*
 Project: TestSendingFileContentlViaPythonEmail.ino
 Author: Michael Gries
 Creation: 2014-08-01
 Modified: 2014-08-02
*/

#define PROGRAM "TestSendingFileContentlViaPythonEmail" 
#define VERSION "14.8.2" 

/*
Sending emails via Python script

ssh root@yourYunsName.local 'telnet localhost 6571' 

NB: If you are using Windows, you must install a terminal emulator. 
PuTTY is a reasonable choice, but you will have to enter the two commands above separately.
*/

/*
 used Libraries:          http://arduino.cc/en/Guide/Libraries
 Bridge-Library           http://arduino.cc/en/Reference/YunBridgeLibrary
 SMTP-Reference           https://docs.python.org/2/library/smtplib.html
 Email examples           https://docs.python.org/2/library/email-examples.html
*/

/*
 required files on 'www' folder of this sketch directory:
   pushmail.py
   vaillant_logo.gif
   vrc_status.txt (containing some ASCII characters only)
*/

#include 
#include 
#include 
#include "Process.h"

YunServer server;
String sConsoleRead;
String sConsoleWrite;

void setup() {
  Bridge.begin();
  Console.begin(); 
  // Wait for Console port to connect
  while (!Console); 
  Console.println(">>>: Console active ...");
  Console.print(PROGRAM); Console.print(" ");  Console.println(VERSION);
  Console.println(" ");
  Console.println("Open ssh session and run script");
  Console.println("python /mnt/sd/arduino/www/TestSendingFileContentlViaPythonEmail/pushemail.py");
  Console.println(" ");
  Console.println("or use REST-API interface to request email by using following command:");
  Console.println("http://gries.spdns.de/arduino/vrc");
  Console.println(">>>");

  server.listenOnLocalhost();
  server.begin();

}

void loop() {

  YunClient client = server.accept();
  if (client) {
    process(client);
    client.stop();
  }

} 

void process(YunClient client) {
  // RESP-API URL's are terminated with '\r\n' (i.e. two characters)
  String command = client.readStringUntil('\r');
  sConsoleRead = command;
  Console.print("<<<: ");
  Console.println(sConsoleRead);

  // is "vrc" command?
  if (command == "vrc") {
    vrcCommand(client);
  } else {
    vrcError(client);
  }
}

void vrcCommand(YunClient client) {
  // Send feedback to client
  sConsoleWrite = "VRC status requested - sending email to michael@gries.name";
  sendEmail(sConsoleWrite);
  client.print(sConsoleWrite);
  Console.print(">>>: ");
  Console.println(sConsoleWrite);
}

void vrcError(YunClient client) {
  // Send feedback to client
  sConsoleWrite = "no valid request";
  client.print(sConsoleWrite);
  Console.print(">>>: ");
  Console.println(sConsoleWrite);
}

void sendEmail(String subject) {
  Process runSendEmail;
  // Linino script
  String scriptType = "python ";
  String scriptPath = "/mnt/sda1/arduino/www/TestSendingFileContentlViaPythonEmail/";
  String scriptName = "pushemail.py";
  // Linino shell command running by Process
  String shellCommand = scriptType + scriptPath + scriptName;
  runSendEmail.runShellCommand(shellCommand);
  while(runSendEmail.running());
  Console.println(" ... runSendEmail script ended");
}


    

www/pushemail.py

# /mnt/sd/arduino/www/TestSendingFileContentlViaPythonEmail/pushemail.py
# cd /mnt/sd/arduino/www/TestSendingFileContentlViaPythonEmail
# ls-l 
# python /mnt/sd/arduino/www/TestSendingFileContentlViaPythonEmail/pushemail.py
# Michael Gries
# 2014-08-02
# Hinweis: Dateiname email.py kollidiert mit Import function 'from email.mime.text' 

# Import smtplib for sending an email
import smtplib

# Import the required email modules 
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

path = "/mnt/sd/arduino/www/TestSendingFileContentlViaPythonEmail/"
data = "vrc_status.txt"
html = "index.htm"
logo = "logo_vaillant.gif"
textfile  = path + data
imagefile = path + logo
htmlfile  = path + html

# Create Html style as first email part
html = """\

  
  
    

VRC Monitor data
Showing status of last stored datagram

Use homepage VRC Monitor for decoding.

""" part1 = MIMEText(html, 'html') #Open image as second email part fi = open(imagefile) part2 = MIMEImage(fi.read()) fi.close() # Create common ASCII text as third email part plaintext = "\n\nDatagram: \n" part3 = MIMEText(plaintext, 'plain') # Open a plain text file as fourth email part # Make sure the text file contains only ASCII characters. fp = open(textfile, 'rb') # Create a text/plain message part4 = MIMEText(fp.read(), 'plain') fp.close() # Open a html file as fifth email part fh = open(htmlfile) part5 = MIMEText(fh.read(), 'html') fh.close() # sendFrom == email address by sending entity # sendTo == recipient's email address sendFrom = "iPhone@gries.name" sendTo = "michael@gries.name" msg = MIMEMultipart() msg['Subject'] = 'The contents of %s' % data msg['From'] = sendFrom msg['To'] = sendTo msg.attach(part1) msg.attach(part2) msg.attach(part3) msg.attach(part4) msg.attach(part5) # Send the message via our own SMTP server, but don't include the # envelope header. try: # server = smtplib.SMTP('localhost') server = smtplib.SMTP('smtp.1und1.de', 587) server.set_debuglevel(0) #server.set_debuglevel(1) server.login('michael@gries.name', 'nopasswd1') server.sendmail(sendFrom, sendTo , msg.as_string()) print "Sucessfully sent email" except SMTPException: print "SMTP error: email not sent" finally: server.quit() print "SMTP service finalized"

www/logo_vaillant.gif

Vaillant Logo

www/vrc_status.txt

06 02 00 03 EE 98 84 00 02 20 06 02 00 03 F2 84 9A 00 00 00 00 03 0C 02 00 04 01 08 0E 00 92 AA 00 E8 00 02 65 00 02 92 00 A8 0C 02 00 03 F1 00 A0 01 00 00 00 E3 83 00 1F 06 00 00 00 08 68 84 00 00 22