Acquire Sensor data and email

Dependencies:   C12832_lcd EthernetInterface LM75B NTPClient SimpleSMTPClient TextLCD USBHost mbed

Fork of USBHost-MSD_HelloWorld by avnish aggarwal

Revision:
10:0902b3dc4d96
Parent:
9:3ca15106a7d1
--- a/main.cpp	Tue Aug 13 06:11:51 2013 +0000
+++ b/main.cpp	Fri Sep 20 15:42:44 2013 +0000
@@ -1,13 +1,50 @@
 #include "mbed.h"
 #include "USBHostMSD.h"
+#include "LM75B.h"
+#include "EthernetInterface.h"
+#include "NTPClient.h"
+#include "SimpleSMTPClient.h"
+#include "TextLCD.h"
+//#include "C12832_lcd.h"
+
+#define DOMAIN "comcast.net"
+#define SERVER "smtp.comcast.net"
+#define PORT "587" //25 or 587,465(OutBound Port25 Blocking )
+#define USER "ramyanagaraj"
+#define PWD "Abhi#123"
+#define FROM_ADDRESS "ramyanagaraj@comcast.net"
 
 DigitalOut led(LED1);
+LM75B tmp(p28,p27);
+//DigitalIn center(p14);
+//C12832_LCD screen;
+Ticker sense;
+int grab = 0;
 
+InterruptIn center_button(p14);
+
+// TO_ADDRESS (Of some address is possible.)
+// to-user1@domain, to-user2@domain, to-user3@domain ....
+// The TO_ADDRESS are less than 128 characters.
+#define TO_ADDRESS "ramyanagaraj@comcast.net" 
+
+#define SUBJECT "Sensor Data in USB"
+
+TextLCD lcd(p24, p26, p27, p28, p29, p30);
+
+   
+            
+/*void CENTER_ISR(){                  //resets the metronome at beat rate 60
+        grab = 1;
+} */  
 void msd_task(void const *) {
-    
+    //if (grab) {
     USBHostMSD msd("usb");
     int i = 0;
-    
+    static long filesize;
+    static long last_filesize = 0;
+    #define MAX_FILESIZE 1000000L
+   //float temp;
     while(1) {
         
         // try to connect a MSD device
@@ -21,10 +58,28 @@
         while(1) {
             
             // append a file
-            FILE * fp = fopen("/usb/test1.txt", "a");
+            // Check Last file size before appending?
+            if( last_filesize >= MAX_FILESIZE )
+            {
+                FILE * fp = fopen("/usb/text1.txt", "w"); // Create of make sure it is new. 
+            }
+            else
+            {
+                FILE * fp = fopen("/usb/text1.txt", "a");
+            }
+            // Get current file size
+            FILE * fp = fopen("/usb/text1.txt", "a");
+            fseek(fp,0,SEEK_END);
+            filesize=ftell(fp);
+            printf("the file's length is %1dB\n",filesize);
         
             if (fp != NULL) {
                 fprintf(fp, "Hello fun SD Card World: %d!\r\n", i++);
+                fprintf(fp, "C=%.2f  \tF=%.2f\n",tmp.read(),(1.8)*tmp.read()+32);
+                //temp = tmp.read();
+                //fprintf(fp, %d, i++,);
+                 //fprintf(fp,"\r\n%f,", temp);
+                //fprintf(fp, "Goodbye World!\r\n");
                 printf("Goodbye World!\r\n");
                 fclose(fp);
             } else {
@@ -36,16 +91,92 @@
             // if device disconnected, try to connect again
             if (!msd.connected())
                 break;
-        }
-            
+        }    
     }
 }
 
 
 int main() {
-    Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);
-    while(1) {
+    
+    /*screen.cls();
+    screen.locate(0,0);
+    screen.printf("Press Center Button to Acq");*/
+    //sense.attach(&temp, 1);
+    //center_button.rise(&CENTER_ISR); 
+    int t;
+    unsigned char data[120*160];
+    unsigned char c;
+    long length;
+    EthernetInterface eth;
+    char strTimeMsg[16];
+    lcd.cls();
+    printf("\n\n/* SimpleMTPClient library demonstration */\n");
+
+    printf("Setting up ...\n");
+    eth.init();
+    eth.connect();
+    printf("Connected OK\r\n");
+    
+    // IP Address 
+    printf("IP Address is %s\r\n", eth.getIPAddress());
+    lcd.locate(0,1);
+    lcd.printf("%s", eth.getIPAddress());
+    
+    // NTP Client
+    printf("NTP setTime...\r\n");
+    NTPClient ntp;
+    ntp.setTime("pool.ntp.org");
+    
+    time_t ctTime = time(NULL)- 25200; // PST
+    printf("\nTime is now (PST): %d %s\r\n", ctTime, ctime(&ctTime));
+    strftime(strTimeMsg,16,"%y/%m/%d %H:%M",localtime(&ctTime));
+    
+    lcd.locate(0,0);
+    lcd.printf("[%s]",strTimeMsg);
+    
+    SimpleSMTPClient smtp;
+    int ret;
+    char msg[]="USB Sensor Data";
+    //t = tmp.read();
+    smtp.setFromAddress(FROM_ADDRESS);
+    smtp.setToAddress(TO_ADDRESS);
+    smtp.setMessage(SUBJECT,msg);
+    smtp.addMessage("TEST TEST TEST\r\n");
+    FILE * fp = fopen("/usb/text1.txt", "r");
+    if(fp==NULL) {
+      printf("file not found!\n");
+    }
+    else 
+    {
+      fseek(fp,0,SEEK_END);
+      length=ftell(fp);
+      printf("the file's length is %1dB\n",length);
+    }
+    fseek(fp,0,SEEK_SET); // Seek back to beginning of ile in case ftell moved the file pointer to the end?
+    char *emailtextbuffer = (char*) malloc(sizeof(char)*(length+1)); // Adding one to include the NULL character at the end.
+    while (!feof(fp)){                        // while not end of file
+           c=fgetc(fp);
+           *emailtextbuffer++ = c;      // save char into buffer
+           //emailtextbuffer++;
+      }
+    *emailtextbuffer = NULL;
+    fclose(fp);
+    smtp.addMessage(emailtextbuffer);
+    //smtp.addMessage("C=%.2f  \tF=%.2f\n",tmp.read(),(1.8)*tmp.read()+32);
+  
+    ret = smtp.sendmail(SERVER, USER, PWD, DOMAIN,PORT,SMTP_AUTH_PLAIN);
+ 
+    if (ret) {
+        printf("E-mail Transmission Error\r\n");
+    } else {
+        printf("E-mail Transmission OK\r\n");
+    }
+   
+   Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);
+   
+   while(1) {
         led=!led;
         Thread::wait(500);
     }
+    free(emailtextbuffer);
 }
\ No newline at end of file