0 Replies Latest reply on Aug 31, 2014 3:39 AM by TOMMY_Z

    Galileo Send Real Time test result VIA email

    TOMMY_Z

      This is a simple example to write python script and send any real time data VIA email. Might can save you some

      time when you need send the real time data. you can play aroud with it.

      Maybe can used at the access control system send the sensor status every half hour when you outside for weekend,

      so you get quick response when some animal breaked in.

      or you can monitor the temperator of your lovly Garden.

      Don't use this as a spam generator!

       

       

      
      
      #include <SPI.h>
      #include <SD.h>
      File myFile;
      void setup()
      {
       // Open serial communications and wait for port to open:
        Serial.begin(9600);
         while (!Serial) {
          ; // wait for serial port to connect. Needed for Leonardo only
        }
      
        Serial.print("Initializing SD card...");
        // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
        // Note that even if it's not used as the CS pin, the hardware SS pin 
        // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
        // or the SD library functions will not work. 
         pinMode(10, OUTPUT);
         
        if (!SD.begin(4)) {
          Serial.println("initialization failed!");
          return;
        }
        Serial.println("initialization done.");
        
         // Check to see if the file exists: 
        if (SD.exists("test.py")) {
          Serial.println("test.py exists.");
              // delete the file:
          Serial.println("Removing example.py...");
          SD.remove("test.py");
        }
        else {
          Serial.println("test.py doesn't exist.");  
        }
      
        
      }
      String readTemperature(){
        return "hello world";
        //Put execution code here
      }
      
      void loop()
      {
         // readTemperature();
         
        // open the file. note that only one file can be open at a time,
        // so you have to close this one before opening another.
        myFile = SD.open("test.py", FILE_WRITE);
        
        // if the file opened okay, write to it:
        if (myFile) {
          Serial.print("Writing to test.py...");
          myFile.println("import smtplib");
          myFile.println("try:");
          myFile.println("    fromaddr = 'youremail@gmail.com'");
          myFile.println("    toaddrs  = 'youremail@gmail.com'");
          myFile.println("    msg = \"\\r\\n\".join([");
          myFile.println("    \"From: youremail@gmail.com\",");
          myFile.println("    \"To: youremail@gmail.com\",");
          myFile.println("    \" Subject: Current Temperature Reading \",");
          myFile.println("    \"\",");
          myFile.println("    \"" + readTemperature() +"\",");
          myFile.println("    ]\)");
          myFile.println("    username = 'youremail@gmail.com'");
          myFile.println("    password = 'password'");
          myFile.println("    server = smtplib.SMTP\('smtp.gmail.com:587'\)");  //myFile.println("    server = smtplib.SMTP\('smtp.163.com:25'\)");
          myFile.println("    server.ehlo()");
          myFile.println("    server.starttls\(\)");
          myFile.println("    server.login\(username,password\)");
          myFile.println("    server.sendmail\(fromaddr, toaddrs, msg\)");
          myFile.println("    server.quit\(\)");
          myFile.println("    print 'send success'");
          myFile.println("except Exception,e:");
          myFile.println("    print Exception,'error: ',e ");
          //myFile.println("print '123'"); //for test
       // close the file:
          myFile.close();
          Serial.println("done.");
        } else {
          // if the file didn't open, print an error:
          Serial.println("error opening test.py");
        }
        
        delay(500); //wait just for sure.
      system("python /media/realroot/test.py > /media/realroot/emails.txt");
        delay(5000);//wait for email get connected and script got executed.
         // nothing happens after setup
        // re-open the file for reading:
        myFile = SD.open("emails.txt");
        if (myFile) {
          Serial.println("emails.txt");
          
          // read from the file until there's nothing else in it:
          while (myFile.available()) {
           Serial.write(myFile.read());
          }
          // close the file:
          myFile.close();
        } else {
         // if the file didn't open, print an error:
          Serial.println("error opening emails.txt");
        }
        delay(500);
      }