2 Replies Latest reply on Sep 2, 2014 5:26 PM by TOMMY_Z

    Galileo Write and Upload Pyton script use the Arduino sketch(How to Show email subject by LCD?)

    TOMMY_Z

      This is just a simple way on the following project, to do like this i don't need to Run any Linux command or try to remove my SD card and copy files into it.

      But still this code need Python environment which means you will have to boot the system with SD card linux. I tested with Gen2 but i believe it will also work at Gen1.

      Unread-Email-Checker Example Project >

      This can also be an easier way for beginners like me who want run python script easily.

      one more benifit is this way we can change the python script without go in to the linux command and change it.

       

      so here is the code.  but in this code it just save the email count into a file and then i can read it and show at LCD/LED, actually i want to use display to show the Unread email subject and use button to switch one to the other. Any suggestion on which LCD part number might be the better option? any sample code for me?

       

       

      #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.");  
        }
      
        
        
        
        
        
        
        // 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 imaplib");
          myFile.println("try:");
          myFile.println("    obj = imaplib.IMAP4_SSL('imap.gmail.com', '993')");
          myFile.println("    obj.login('my_email_address@gmail.com','myPassword')");
          myFile.println("    obj.select()");
          myFile.println("    print len(obj.search(None,'UnSeen')[1][0].split())");
          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");
        }
      }
      void loop()
      {
      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);
      }