Acquire Sensor data and email
Dependencies: C12832_lcd EthernetInterface LM75B NTPClient SimpleSMTPClient TextLCD USBHost mbed
Fork of USBHost-MSD_HelloWorld by
main.cpp
00001 #include "mbed.h" 00002 #include "USBHostMSD.h" 00003 #include "LM75B.h" 00004 #include "EthernetInterface.h" 00005 #include "NTPClient.h" 00006 #include "SimpleSMTPClient.h" 00007 #include "TextLCD.h" 00008 //#include "C12832_lcd.h" 00009 00010 #define DOMAIN "comcast.net" 00011 #define SERVER "smtp.comcast.net" 00012 #define PORT "587" //25 or 587,465(OutBound Port25 Blocking ) 00013 #define USER "ramyanagaraj" 00014 #define PWD "Abhi#123" 00015 #define FROM_ADDRESS "ramyanagaraj@comcast.net" 00016 00017 DigitalOut led(LED1); 00018 LM75B tmp(p28,p27); 00019 //DigitalIn center(p14); 00020 //C12832_LCD screen; 00021 Ticker sense; 00022 int grab = 0; 00023 00024 InterruptIn center_button(p14); 00025 00026 // TO_ADDRESS (Of some address is possible.) 00027 // to-user1@domain, to-user2@domain, to-user3@domain .... 00028 // The TO_ADDRESS are less than 128 characters. 00029 #define TO_ADDRESS "ramyanagaraj@comcast.net" 00030 00031 #define SUBJECT "Sensor Data in USB" 00032 00033 TextLCD lcd(p24, p26, p27, p28, p29, p30); 00034 00035 00036 00037 /*void CENTER_ISR(){ //resets the metronome at beat rate 60 00038 grab = 1; 00039 } */ 00040 void msd_task(void const *) { 00041 //if (grab) { 00042 USBHostMSD msd("usb"); 00043 int i = 0; 00044 static long filesize; 00045 static long last_filesize = 0; 00046 #define MAX_FILESIZE 1000000L 00047 //float temp; 00048 while(1) { 00049 00050 // try to connect a MSD device 00051 while(!msd.connect()) { 00052 Thread::wait(500); 00053 printf("waiting\n"); 00054 } 00055 00056 // in a loop, append a file 00057 // if the device is disconnected, we try to connect it again 00058 while(1) { 00059 00060 // append a file 00061 // Check Last file size before appending? 00062 if( last_filesize >= MAX_FILESIZE ) 00063 { 00064 FILE * fp = fopen("/usb/text1.txt", "w"); // Create of make sure it is new. 00065 } 00066 else 00067 { 00068 FILE * fp = fopen("/usb/text1.txt", "a"); 00069 } 00070 // Get current file size 00071 FILE * fp = fopen("/usb/text1.txt", "a"); 00072 fseek(fp,0,SEEK_END); 00073 filesize=ftell(fp); 00074 printf("the file's length is %1dB\n",filesize); 00075 00076 if (fp != NULL) { 00077 fprintf(fp, "Hello fun SD Card World: %d!\r\n", i++); 00078 fprintf(fp, "C=%.2f \tF=%.2f\n",tmp.read(),(1.8)*tmp.read()+32); 00079 //temp = tmp.read(); 00080 //fprintf(fp, %d, i++,); 00081 //fprintf(fp,"\r\n%f,", temp); 00082 //fprintf(fp, "Goodbye World!\r\n"); 00083 printf("Goodbye World!\r\n"); 00084 fclose(fp); 00085 } else { 00086 printf("FILE == NULL\r\n"); 00087 } 00088 00089 Thread::wait(500); 00090 00091 // if device disconnected, try to connect again 00092 if (!msd.connected()) 00093 break; 00094 } 00095 } 00096 } 00097 00098 00099 int main() { 00100 00101 /*screen.cls(); 00102 screen.locate(0,0); 00103 screen.printf("Press Center Button to Acq");*/ 00104 //sense.attach(&temp, 1); 00105 //center_button.rise(&CENTER_ISR); 00106 int t; 00107 unsigned char data[120*160]; 00108 unsigned char c; 00109 long length; 00110 EthernetInterface eth; 00111 char strTimeMsg[16]; 00112 lcd.cls(); 00113 printf("\n\n/* SimpleMTPClient library demonstration */\n"); 00114 00115 printf("Setting up ...\n"); 00116 eth.init(); 00117 eth.connect(); 00118 printf("Connected OK\r\n"); 00119 00120 // IP Address 00121 printf("IP Address is %s\r\n", eth.getIPAddress()); 00122 lcd.locate(0,1); 00123 lcd.printf("%s", eth.getIPAddress()); 00124 00125 // NTP Client 00126 printf("NTP setTime...\r\n"); 00127 NTPClient ntp; 00128 ntp.setTime("pool.ntp.org"); 00129 00130 time_t ctTime = time(NULL)- 25200; // PST 00131 printf("\nTime is now (PST): %d %s\r\n", ctTime, ctime(&ctTime)); 00132 strftime(strTimeMsg,16,"%y/%m/%d %H:%M",localtime(&ctTime)); 00133 00134 lcd.locate(0,0); 00135 lcd.printf("[%s]",strTimeMsg); 00136 00137 SimpleSMTPClient smtp; 00138 int ret; 00139 char msg[]="USB Sensor Data"; 00140 //t = tmp.read(); 00141 smtp.setFromAddress(FROM_ADDRESS); 00142 smtp.setToAddress(TO_ADDRESS); 00143 smtp.setMessage(SUBJECT,msg); 00144 smtp.addMessage("TEST TEST TEST\r\n"); 00145 FILE * fp = fopen("/usb/text1.txt", "r"); 00146 if(fp==NULL) { 00147 printf("file not found!\n"); 00148 } 00149 else 00150 { 00151 fseek(fp,0,SEEK_END); 00152 length=ftell(fp); 00153 printf("the file's length is %1dB\n",length); 00154 } 00155 fseek(fp,0,SEEK_SET); // Seek back to beginning of ile in case ftell moved the file pointer to the end? 00156 char *emailtextbuffer = (char*) malloc(sizeof(char)*(length+1)); // Adding one to include the NULL character at the end. 00157 while (!feof(fp)){ // while not end of file 00158 c=fgetc(fp); 00159 *emailtextbuffer++ = c; // save char into buffer 00160 //emailtextbuffer++; 00161 } 00162 *emailtextbuffer = NULL; 00163 fclose(fp); 00164 smtp.addMessage(emailtextbuffer); 00165 //smtp.addMessage("C=%.2f \tF=%.2f\n",tmp.read(),(1.8)*tmp.read()+32); 00166 00167 ret = smtp.sendmail(SERVER, USER, PWD, DOMAIN,PORT,SMTP_AUTH_PLAIN); 00168 00169 if (ret) { 00170 printf("E-mail Transmission Error\r\n"); 00171 } else { 00172 printf("E-mail Transmission OK\r\n"); 00173 } 00174 00175 Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4); 00176 00177 while(1) { 00178 led=!led; 00179 Thread::wait(500); 00180 } 00181 free(emailtextbuffer); 00182 }
Generated on Mon Jul 18 2022 20:06:48 by 1.7.2