Getting Yun to Email using Python

Okay, I am a little new to the Yun. My goal is to set up a temperature sensor and if it is too cold, to send me an email. I am trying to do this by following the instructions at Adventures with Arduino Yun: Simple way to email pin state changes from the Yun (and an explanation of how the bridge works).

I have the Yun all set up on my network with the local IP address 192.168.1.x. I used the LUCI software to download the openssl packages and that all seem to go well. The arduino side of the yun seems to be working well and I can use the serial monitor to measure changes on A0 using an RTD thermistor. Where I seem to be stuck is the openWRT side of the yun.

After looking at several tutorials, I can’t seem to figure out how python scripts get executed. From the one tutorial from Adafruit, it says to copy the python script to the SD card Root and insert into the yun. However, there seems to be no further instructions. Does that python script automatically execute or do I need to tell the openWRT to run it? The Arduino sketch seems to do nothing more than send the Key->Value pair to the bridge but it does not have any command to tell the openWRT to start the python script. Accordingly, how does that run? Do I need to use the terminal (PUTTY) or something? When python prints, what does it print to?
Links to respective tutorials would be good.

Here are the two code sets just to test the email portion of python:
Arduino Sketch (upload using the IDE Com port) seems to work fine.

#include <Bridge.h>
int pirPin = 7;
int minSecsBetweenEmails = 60; // 1 min
long lastSend = -minSecsBetweenEmails * 1000l;


void setup() {
  // put your setup code here, to run once:
   pinMode(pirPin, INPUT);
   Bridge.begin();
   Bridge.put("D7","1");
}


void loop() {
  // put your main code here, to run repeatedly:
  Bridge.put("D7","1");
  delay(50000);
}

And here is the python script, saved to the SDcard root directory and put into sd slot of yun.

#!/usr/bin/python
import time
import sys
import smtplib
sys.path.insert(0, '/usr/lib/python2.7/bridge/')

from bridgeclient import BridgeClient as bridgeclient

TO = 'myreceivingmail@gmaildotcom'
GMAIL_USER = 'mygmail@gmaildotcom'
GMAIL_PASS = 'nottellingyoumypassword'
SUBJECT = 'Digital pin changed value!!'
TEXT = 'Your digital pin went HIGH'
value = bridgeclient()

#function to send email
def send_email():
        print("Sending Email")
        smtpserver = smtplib.SMTP("smtp.gmail.com",587)
        smtpserver.ehlo()
        smtpserver.starttls()
        smtpserver.ehlo
        smtpserver.login(GMAIL_USER, GMAIL_PASS)
        header = 'To:' + TO + '\n' + 'From: ' + GMAIL_USER
        header = header + '\n' + 'Subject:' + SUBJECT + '\n'
        print header
        msg = header + '\n' + TEXT + ' \n\n' + TEXT2 + ' \n\n'
        smtpserver.sendmail(GMAIL_USER, TO, msg)
        smtpserver.close()

value = bridgeclient()

while True:
        message = value.get("D7")
        print message
         if message == '1' :
                print "sending email, D7 went high!"
                TEXT2 =  "A0 analog value: "+ value.get("A0")
                send_email()
                print TEXT2
        elif message == "0":
                print "D7 low"
        else:
                print "no message"

        time.sleep(200)

That tutorial some how expects you to run that python script at yun startup or something like that. You should comment that blog post and ask the author

Thanks for the reply. I emailed him so we will see what he says. As a note, I updated the firmware on the Yun as was mentioned on the arduino site.