Emailing Sensor Data acquired in USB MSD

Introduction

Application board on the MBED is rich with various sensors. This project is to acquire these sensors and store them in the USB stick. Also, it will email the data to your email.

Program

Tried my level best to email me the text file which is saved in the USB Mass Storage Device. This is the example code I have tried (this is not a working code) -

#include "mbed.h"
#include "USBHostMSD.h"
#include "LM75B.h"
#include "EthernetInterface.h"
#include "NTPClient.h"
#include "SimpleSMTPClient.h"
#include "TextLCD.h"

#define DOMAIN "comcast.net"
#define SERVER "smtp.comcast.net"
#define PORT "587" //25 or 587,465(OutBound Port25 Blocking )
#define USER "username"
#define PWD "password"
#define FROM_ADDRESS "username@comcast.net"

DigitalOut led(LED1);
LM75B tmp(p28,p27);

// 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 "username@comcast.net" 

#define SUBJECT "Sensor Data in USB"

TextLCD lcd(p24, p26, p27, p28, p29, p30);

void msd_task(void const *) {
    USBHostMSD msd("usb");
    int i = 0;
    static long filesize;
    static long last_filesize = 0;
    #define MAX_FILESIZE 1000000L
   while(1) {
        
        // try to connect a MSD device
        while(!msd.connect()) {
            Thread::wait(500);
            printf("waiting\n");
        }
        
        // in a loop, append a file
        // if the device is disconnected, we try to connect it again
        while(1) {
            
            // append a file
            // 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);
                printf("Goodbye World!\r\n");
                fseek(fp,0,SEEK_END);
                filesize=ftell(fp);
                printf("the file's length is %1dB\n",filesize);
                last_filesize = filesize;
                fclose(fp);
            } else {
                printf("FILE == NULL\r\n");
            }
            
            Thread::wait(500);
        
            // if device disconnected, try to connect again
            if (!msd.connected())
                break;
        }
     // }      
    }
}


int main() {
    Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4);
    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";
    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++;
           //printf("Read from file %02x\n\r",c);
    }
    *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");
    }
   
   
   while(1) {
        led=!led;
        Thread::wait(500);
    }
    free(emailtextbuffer);
}


Please log in to post comments.