Datalogger that reads temperature, humidity and light level every couple of seconds, and store the data in a microSD card (.CSV file).

Dependencies:   DS1302 SDFileSystem mbed

Fork of temp_humid_time_DS1302_LM35_DHT11 by Clovis Fritzen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystem.h"
00003 #include "DHT.h"
00004 #include "DS1302.h"
00005 
00006 // Defines for the DS1302 timer module
00007 #define SCLK    PTC1
00008 #define IO      PTB19
00009 #define CE      PTB18
00010 
00011 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); //MOSI, MISO, SCLK, SSEL. (SD Card) Tested on K64F, correct pins.
00012 AnalogIn LM35(PTB2);
00013 AnalogIn LDR(PTB3);
00014 AnalogIn SolarPanel(PTB10);
00015 int count=0;
00016 float ldrcalc= 0.00;
00017 DigitalOut RedLed(LED1); // Error indication
00018 DigitalOut GreenLed(LED2); // LM35 read indication
00019 DigitalOut Blueled(LED3); // DHT11 read indication
00020 DHT sensor(PTB20,DHT11); // Use the DHT11 sensor, on pin 
00021 DS1302 clk(SCLK, IO, PTB18); // ports for the DS1302 time keeper
00022  
00023 int main() {
00024     // the 6 lines below are for the time keeper chip
00025     #ifdef INITIAL_RUN
00026     clk.set_time(1406017928);
00027     #endif
00028     
00029     char storedByte = clk.recallByte(0);
00030     //printf("\r\nStored byte was %d, now increasing by one\r\n", storedByte);
00031     clk.storeByte(0, storedByte + 1);
00032     //
00033     
00034     int err;
00035     wait(1); // wait 1 second for DHT11 to stabilyze
00036     
00037     //printf("Hello World!\n");   
00038  
00039     mkdir("/sd/dados", 0777);
00040     
00041     FILE *fp = fopen("/sd/dados/data004.csv", "a");
00042     if(fp == NULL) {
00043         error("Could not open file for write\n");
00044         RedLed= 0;
00045         
00046         }
00047         fprintf(fp, "%s\r,", "--------------"); 
00048     fclose(fp);  
00049     
00050     while (1) {
00051     
00052     Blueled= 1;
00053     RedLed= 1;
00054     GreenLed= 1;
00055     
00056    if (count < 5000000){ //around 10 seconds before it reaches the count of 12000000
00057         count++;
00058         } else{  
00059         // --------------------------
00060        
00061         err = sensor.readData();
00062         if (err == 0) {
00063             GreenLed= 0;
00064             printf("Temperature is %4.2f \r\n",sensor.ReadTemperature(CELCIUS));
00065             //printf("Temperature is %4.2f F \r\n",sensor.ReadTemperature(FARENHEIT));
00066             //printf("Temperature is %4.2f K \r\n",sensor.ReadTemperature(KELVIN));
00067             printf("Humidity is %4.2f \r\n",sensor.ReadHumidity());
00068             //printf("Dew point is %4.2f  \r\n",sensor.CalcdewPoint(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity()));
00069             //printf("Dew point (fast) is %4.2f  \r\n",sensor.CalcdewPointFast(sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity()));
00070             
00071         } else
00072             printf("\r\nErr %i \n",err);
00073             
00074         // ----------------------------  
00075     FILE *fp = fopen("/sd/dados/data004.csv", "a");
00076     if(fp == NULL) {
00077         error("Could not open file for write\n");
00078         
00079        }
00080     
00081     ldrcalc= LDR.read();
00082     ldrcalc= (1/ldrcalc)-1; //Transforms the LDR value into a 0-5 signal (integer)
00083     time_t seconds = clk.time(NULL);
00084     //fprintf(fp, "%s\r,%f,%f,%f\n", "LM35", "DHT11", "Humid", "Month", "Day", "Hour", "Year");
00085     fprintf(fp, "%s\r,%f,%f,%f,%f,%f", ctime(&seconds), 333.333*LM35.read(), SolarPanel.read(), sensor.ReadTemperature(CELCIUS), sensor.ReadHumidity(), ldrcalc); 
00086     fclose(fp);  
00087     //printf("Goodbye World!\n");
00088     count=0;
00089     Blueled= 0;
00090     }
00091     }
00092     }