John Leimgruber / Mbed 2 deprecated TempDataLogger

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 /***************************
00004 * John W. Leimgruber III
00005 * Test Application
00006 * 1. Read analog value from LM34 temperature sensor and convert to deg Fahrenheit
00007 * 2. Filter using 3-tap median sampling every 1/3 second
00008 * 3. Append to local flash filesystem log file every 512 seconds
00009 * 4. Externally, use a python program I wrote based on matplotlib to grab and parse updated log file then display graphical plot
00010 *****************************/
00011 
00012 DigitalOut myled(LED1); // sanity test light
00013 Serial pc(USBTX, USBRX); // beautiful debugging tx, rx to Linux and Minicom @ /dev/ttyACM0 9600 8N1
00014 AnalogIn temp(p15);    // analog temperature sensor, LM34, reads +5 to +300 degF @ 10mv per deg and takes 5-30v Vin...
00015 
00016 LocalFileSystem local("local"); // setup local filesystem on the ~2mb internal flash
00017 Timer timer; // timer to generate delays, as I'm newbish and don't know about wait
00018 
00019 int main() 
00020 {
00021     printf("Starting to read and log temperature data to local flash disk.\r\n"); 
00022     char *tempData, filter[3];
00023     unsigned int dataCount, filterCount;
00024     FILE *fp = 0;
00025     dataCount = 0;
00026     filterCount = 0;
00027     memset(filter, 3, 0x00);
00028 
00029     // allocate 512 bytes of memory to store 512 seconds worth of temperature values at one per second stored in deg F [0,255]      
00030     tempData=(char *) malloc(512);
00031     if (!tempData)
00032     {
00033       fprintf(stderr, "Not enough memory to allocate 512 bytes for temperature data\n");
00034       exit(1);
00035     }
00036 
00037     timer.reset();
00038     timer.start();
00039     while(1)
00040     {    
00041       //grab a temp reading ever second and convert to 0-255 in deg F
00042       if(timer.read() > 0.333)
00043       { // implement 3 tap median filter and oversample by 3x
00044         filter[0] = filter[1];
00045         filter[1] = filter[2];
00046         filter[2] = (char) (temp.read() * 330.0 - 5.0);
00047         filterCount++;
00048 
00049         if(filterCount >= 3)
00050         {
00051           filterCount = 0;
00052           if(filter[0] >= filter[1] && filter[0] <= filter[2] ||
00053              filter[0] <= filter[1] && filter[0] >= filter[2])
00054           {
00055             tempData[dataCount] = filter[0];
00056           }
00057           else if(filter[1] >= filter[0] && filter[1] <= filter[2] ||
00058                   filter[1] <= filter[0] && filter[1] >= filter[2])
00059           {
00060             tempData[dataCount] = filter[1];
00061           }
00062           else
00063           {
00064             tempData[dataCount] = filter[2];
00065           }
00066           // in case you want to grab some 'real-time' temp data and debugging...                 
00067           printf("Temperature [%d] = %d deg F\r\n",dataCount, tempData[dataCount]);
00068           dataCount++;
00069         }
00070         timer.reset();
00071       }
00072 
00073       // now that we've grabbed a full page of data, dump it do disk then start over...
00074       if(dataCount >= 512)
00075       {
00076         myled = 1;
00077         fp = fopen("/local/templog.dat", "ab");
00078         if(!fp)
00079         {
00080           printf("File /local/templog.dat could not be opened for append binary writing!\r\n");
00081           exit(1);
00082         }
00083         printf("Writing %d bytes to internal flash!\r\n", dataCount); // should always be 512 bytes? (maybe should keep writing until 512 hit... meh)
00084         printf("Actually wrote %d bytes...\r\n", fwrite(tempData,1,dataCount,fp)); //memory array, sizeof(char), 512 bytes, file pointer
00085         fclose(fp);
00086         dataCount = 0;
00087         myled = 0;
00088       }
00089     }
00090 }